"use client"; import { zodResolver } from "@hookform/resolvers/zod"; import { useForm } from "react-hook-form"; import { z } from "zod"; import { Button } from "@/components/ui/button"; import { Checkbox } from "@/components/ui/checkbox"; import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import { toast } from "@/components/ui/use-toast"; import { Input } from "./ui/input"; const roles = [ { id: "ROLE_SUPER_ADMIN", label: "ROLE_SUPER_ADMIN", }, { id: "ROLE_ADMIN", label: "ROLE_ADMIN", }, { id: "ROLE_SYSADMIN", label: "ROLE_SYSADMIN", }, { id: "ROLE_API", label: "ROLE_API", }, { id: "ROLE_ORGANISATION_ADMIN", label: "ROLE_ORGANISATION_ADMIN", }, { id: "ROLE_ASSISTANT", label: "ROLE_ASSISTANT", }, { id: "ROLE_USER", label: "ROLE_USER", }, ] as const; const displayFormSchema = z.object({ fullName: z.string().trim().min(1, "Full Name is required."), email: z.string().trim().email("Email is required."), newPassword: z.string().trim().min(1, "Password is required."), repeatPassword: z.string().trim().min(1, "Repeat the new password."), roles: z.array(z.string()).refine((value) => value.some((item) => item), { message: "You have to select at least one role.", }), }); type DisplayFormValues = z.infer; // This can come from your database or API. const defaultValues: Partial = { fullName: "", email: "", roles: ["ROLE_USER"], }; export default function AddUserForm() { const form = useForm({ resolver: zodResolver(displayFormSchema), defaultValues, }); function onSubmit(data: DisplayFormValues) { if (data.newPassword !== data.repeatPassword) { toast({ variant: "destructive", title: "Password doesn't match!", }); return; } toast({ title: "You submitted the following values:", description: (
          {JSON.stringify(data, null, 2)}
        
), }); } return (
( Full Name {/* This is your public display name. It can be your real name or a pseudonym. You can only change this once every 30 days. */} )} /> ( Email )} /> ( New Password )} /> ( Repeat Password )} /> (
Roles {/* Select the items you want to display in the sidebar. */}
{roles.map((role) => ( { return ( { return checked ? field.onChange([...field.value, role.id]) : field.onChange( field.value?.filter( (value) => value !== role.id ) ); }} /> {role.label} ); }} /> ))}
)} />
); }