'use client' import { z } from 'zod' import { useForm } from 'react-hook-form' import { zodResolver } from '@hookform/resolvers/zod' import { showSubmittedData } from '@/utils/show-submitted-data' import { Button } from '@/components/ui/button' import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '@/components/ui/dialog' import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from '@/components/ui/form' import { Input } from '@/components/ui/input' import { PasswordInput } from '@/components/password-input' import { SelectDropdown } from '@/components/select-dropdown' import { userTypes } from '../data/data' import { User } from '../data/schema' const formSchema = z .object({ firstName: z.string().min(1, { message: 'First Name is required.' }), lastName: z.string().min(1, { message: 'Last Name is required.' }), username: z.string().min(1, { message: 'Username is required.' }), phoneNumber: z.string().min(1, { message: 'Phone number is required.' }), email: z .string() .min(1, { message: 'Email is required.' }) .email({ message: 'Email is invalid.' }), password: z.string().transform((pwd) => pwd.trim()), role: z.string().min(1, { message: 'Role is required.' }), confirmPassword: z.string().transform((pwd) => pwd.trim()), isEdit: z.boolean(), }) .superRefine(({ isEdit, password, confirmPassword }, ctx) => { if (!isEdit || (isEdit && password !== '')) { if (password === '') { ctx.addIssue({ code: z.ZodIssueCode.custom, message: 'Password is required.', path: ['password'], }) } if (password.length < 8) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: 'Password must be at least 8 characters long.', path: ['password'], }) } if (!password.match(/[a-z]/)) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: 'Password must contain at least one lowercase letter.', path: ['password'], }) } if (!password.match(/\d/)) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: 'Password must contain at least one number.', path: ['password'], }) } if (password !== confirmPassword) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: "Passwords don't match.", path: ['confirmPassword'], }) } } }) type UserForm = z.infer interface Props { currentRow?: User open: boolean onOpenChange: (open: boolean) => void } export function UsersActionDialog({ currentRow, open, onOpenChange }: Props) { const isEdit = !!currentRow const form = useForm({ resolver: zodResolver(formSchema), defaultValues: isEdit ? { ...currentRow, password: '', confirmPassword: '', isEdit, } : { firstName: '', lastName: '', username: '', email: '', role: '', phoneNumber: '', password: '', confirmPassword: '', isEdit, }, }) const onSubmit = (values: UserForm) => { form.reset() showSubmittedData(values) onOpenChange(false) } const isPasswordTouched = !!form.formState.dirtyFields.password return ( { form.reset() onOpenChange(state) }} > {isEdit ? 'Edit User' : 'Add New User'} {isEdit ? 'Update the user here. ' : 'Create new user here. '} Click save when you're done.
( First Name )} /> ( Last Name )} /> ( Username )} /> ( Email )} /> ( Phone Number )} /> ( Role ({ label, value, }))} /> )} /> ( Password )} /> ( Confirm Password )} />
) }