'use client'; import { Button, Input } from '@nextui-org/react'; import { useState } from 'react'; import { z } from 'zod'; type FormData = { username: string; email: string; password: string; confirmPassword: string; }; const customerSchema = z .object({ username: z.string().min(3), email: z.string().email({ message: 'Invalid email' }), password: z.string(), confirmPassword: z.string() }) .refine((data) => data.password === data.confirmPassword, { message: "Passwords don't match", path: ['confirmPassword'] }); export default function SignUpPage() { const initialState = { username: '', email: '', password: '', confirmPassword: '' }; const [formData, setFormData] = useState(initialState); const [error, setError] = useState(initialState); const handleChange = (e: React.ChangeEvent) => { setFormData((prev) => ({ ...prev, [e.target.name]: e.target.value })); }; const handleSignup = async (event: React.FormEvent) => { event.preventDefault(); try { customerSchema.parse(formData); setError(initialState); await fetch('/api/customer', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ username: formData.username, first_name: '', last_name: '', email: formData.email, password: formData.password }) }); } catch (error) { if (error instanceof z.ZodError) { const errorObj: FormData = initialState; error.errors.forEach((err) => { const key = err.path[0] as keyof FormData; errorObj[key] = err.message as string; }); console.log(errorObj); setError(errorObj); } } }; return (

Sign up

); }