33import Link from "next/link"
44
55import { zodResolver } from "@hookform/resolvers/zod"
6+ import { Prisma } from "database"
67import { GithubIcon } from "lucide-react"
78import { useTranslations } from "next-intl"
9+ import { useFormState } from "react-dom"
810import { useForm } from "react-hook-form"
11+ import { toast } from "react-toastify"
912import {
1013 Button ,
1114 Card ,
@@ -23,29 +26,50 @@ import {
2326} from "ui"
2427import { z } from "zod"
2528
29+ import { signUp , SignUpDataInput , signUpSchema } from "@/actions/auth"
30+
2631import AuthForm from "../auth-form"
2732
2833export default function SignUp ( ) {
2934 const t = useTranslations ( "auth" )
3035
31- const form = useForm ( {
32- resolver : zodResolver (
33- z . object ( {
34- email : z . string ( ) . email ( t ( "email.invalid" ) ) ,
35- password : z . string ( ) . min ( 8 , t ( "password.min" ) ) ,
36- confirmPassword : z . string ( ) . min ( 8 , t ( "password.min" ) ) ,
37- } )
38- ) ,
36+ const form = useForm < SignUpDataInput > ( {
37+ resolver : zodResolver ( signUpSchema ) ,
38+ mode : "onTouched" ,
3939 } )
4040
4141 const {
4242 formState : { errors } ,
4343 register,
4444 handleSubmit,
45+ setError,
4546 } = form
4647
47- const onSubmit = ( data : any ) => {
48- // TODO: Implement sign up
48+ // TODO:
49+ // const [state, formAction] = useActionState<Pick<Prisma.UserCreateInput, "email" | "password">>(
50+ // signUp,
51+ // null
52+ // )
53+
54+ console . log ( ">>>" , errors )
55+
56+ const formAction = async ( { confirmPassword, ...data } : SignUpDataInput ) => {
57+ const error = await signUp ( data )
58+
59+ console . log ( "formAction>>error" , error )
60+
61+ if ( error . formErrors ) {
62+ toast . error ( error . formErrors ?. at ( 0 ) )
63+ }
64+
65+ if ( error . fieldErrors ) {
66+ Object ?. entries ( error . fieldErrors ) ?. forEach ( ( [ field , message ] ) => {
67+ setError ( field , {
68+ type : "manual" ,
69+ message,
70+ } )
71+ } )
72+ }
4973 }
5074
5175 return (
@@ -55,7 +79,7 @@ export default function SignUp() {
5579 description = "Register to your account to continue."
5680 >
5781 < Form { ...form } >
58- < form onSubmit = { handleSubmit ( onSubmit ) } >
82+ < form onSubmit = { handleSubmit ( formAction ) } >
5983 < div className = "grid w-full gap-4" >
6084 < FormField
6185 name = "email"
@@ -65,7 +89,7 @@ export default function SignUp() {
6589 < FormControl >
6690 < Input
6791 id = "email"
68- placeholder = { t ( "email " ) }
92+ placeholder = { t ( "email_label " ) }
6993 type = "email"
7094 autoCapitalize = "none"
7195 autoComplete = "email"
0 commit comments