1
- "use client" ;
1
+ "use client"
2
2
3
- import React , { useState } from "react" ;
4
- import { useRouter } from "next/navigation" ;
5
- import { Input } from "@/components/ui/input" ;
6
- import { Label } from "@/components/ui/label" ;
7
- import { Alert , AlertDescription } from "@/components/ui/alert" ;
3
+ import React from "react"
4
+ import { useRouter } from "next/navigation"
5
+ import { zodResolver } from "@hookform/resolvers/zod"
6
+ import { useForm } from "react-hook-form"
7
+ import * as z from "zod"
8
+ import {
9
+ Form ,
10
+ FormControl ,
11
+ FormField ,
12
+ FormItem ,
13
+ FormLabel ,
14
+ FormMessage ,
15
+ } from "@/components/ui/form"
16
+ import { Input } from "@/components/ui/input"
17
+ import { Alert , AlertDescription } from "@/components/ui/alert"
8
18
import {
9
19
Card ,
10
20
CardContent ,
11
21
CardDescription ,
12
22
CardFooter ,
13
23
CardHeader ,
14
24
CardTitle ,
15
- } from "@/components/ui/card" ;
16
- import { useAuthStore } from "@/store/AuthStore/useAuthStore" ;
17
- import LoadingButton from "../Buttons/LoadingButton" ;
18
- import AuthBottom from "./AuthBottom" ;
25
+ } from "@/components/ui/card"
26
+ import { useAuthStore } from "@/store/AuthStore/useAuthStore"
27
+ import LoadingButton from "../Buttons/LoadingButton"
28
+ import AuthBottom from "./AuthBottom"
29
+ import { signinSchema } from "@/validations/validation"
19
30
20
- interface FormData {
21
- email : string ;
22
- password : string ;
23
- }
31
+ type SigninFormValues = z . infer < typeof signinSchema >
24
32
25
33
export default function SigninForm ( ) {
26
- const { isSigningIn, signin, signinError } = useAuthStore ( ) ;
27
- const router = useRouter ( ) ;
28
- const [ formData , setFormData ] = useState < FormData > ( {
29
- email : "" ,
30
- password : "" ,
31
- } ) ;
34
+ const { isSigningIn, signin, signinError } = useAuthStore ( )
35
+ const router = useRouter ( )
32
36
33
- const handleChange = ( e : React . ChangeEvent < HTMLInputElement > ) => {
34
- const { name, value } = e . target ;
35
- setFormData ( ( prev ) => ( { ...prev , [ name ] : value } ) ) ;
36
- } ;
37
+ const form = useForm < SigninFormValues > ( {
38
+ resolver : zodResolver ( signinSchema ) ,
39
+ defaultValues : {
40
+ email : "" ,
41
+ password : "" ,
42
+ } ,
43
+ } )
37
44
38
- const handleSubmit = async ( e : React . FormEvent ) => {
39
- e . preventDefault ( ) ;
40
- signin ( formData , router ) ;
41
- } ;
45
+ const onSubmit = ( data : SigninFormValues ) => {
46
+ signin ( data , router )
47
+ }
42
48
43
49
return (
44
50
< main className = "flex min-h-screen items-center justify-center px-4" >
@@ -50,46 +56,47 @@ export default function SigninForm() {
50
56
</ CardDescription >
51
57
</ CardHeader >
52
58
< CardContent >
53
- < form onSubmit = { handleSubmit } className = "space-y-4" >
54
- < div className = "space-y-2" >
55
- < Label htmlFor = "email" > Email</ Label >
56
- < Input
57
- id = "email"
59
+ < Form { ...form } >
60
+ < form onSubmit = { form . handleSubmit ( onSubmit ) } className = "space-y-4" >
61
+ < FormField
62
+ control = { form . control }
58
63
name = "email"
59
- type = "email"
60
- placeholder = "you@example.com"
61
- value = { formData . email }
62
- onChange = { handleChange }
63
- required
64
+ render = { ( { field } ) => (
65
+ < FormItem >
66
+ < FormLabel > Email</ FormLabel >
67
+ < FormControl >
68
+ < Input placeholder = "you@example.com" { ...field } />
69
+ </ FormControl >
70
+ < FormMessage />
71
+ </ FormItem >
72
+ ) }
64
73
/>
65
- </ div >
66
-
67
- < div className = "space-y-2" >
68
- < Label htmlFor = "password" > Password</ Label >
69
- < Input
70
- id = "password"
74
+ < FormField
75
+ control = { form . control }
71
76
name = "password"
72
- type = "password"
73
- placeholder = "••••••••"
74
- value = { formData . password }
75
- onChange = { handleChange }
76
- required
77
+ render = { ( { field } ) => (
78
+ < FormItem >
79
+ < FormLabel > Password</ FormLabel >
80
+ < FormControl >
81
+ < Input type = "password" placeholder = "••••••••" { ...field } />
82
+ </ FormControl >
83
+ < FormMessage />
84
+ </ FormItem >
85
+ ) }
77
86
/>
78
- </ div >
79
-
80
- { signinError && (
81
- < Alert variant = "destructive" >
82
- < AlertDescription > { signinError } </ AlertDescription >
83
- </ Alert >
84
- ) }
85
-
86
- < LoadingButton
87
- loading = { isSigningIn }
88
- loadingTitle = "Signing in"
89
- title = "Signin"
90
- type = "submit"
91
- />
92
- </ form >
87
+ { signinError && (
88
+ < Alert variant = "destructive" >
89
+ < AlertDescription > { signinError } </ AlertDescription >
90
+ </ Alert >
91
+ ) }
92
+ < LoadingButton
93
+ loading = { isSigningIn }
94
+ loadingTitle = "Signing in"
95
+ title = "Sign in"
96
+ type = "submit"
97
+ />
98
+ </ form >
99
+ </ Form >
93
100
</ CardContent >
94
101
< CardFooter className = "flex justify-center" >
95
102
< AuthBottom
@@ -100,5 +107,5 @@ export default function SigninForm() {
100
107
</ CardFooter >
101
108
</ Card >
102
109
</ main >
103
- ) ;
104
- }
110
+ )
111
+ }
0 commit comments