diff --git a/components/AuthComponent/SigninForm.tsx b/components/AuthComponent/SigninForm.tsx index edac9c4..c62c9bd 100644 --- a/components/AuthComponent/SigninForm.tsx +++ b/components/AuthComponent/SigninForm.tsx @@ -1,10 +1,20 @@ -"use client"; +"use client" -import React, { useState } from "react"; -import { useRouter } from "next/navigation"; -import { Input } from "@/components/ui/input"; -import { Label } from "@/components/ui/label"; -import { Alert, AlertDescription } from "@/components/ui/alert"; +import React from "react" +import { useRouter } from "next/navigation" +import { zodResolver } from "@hookform/resolvers/zod" +import { useForm } from "react-hook-form" +import * as z from "zod" +import { + Form, + FormControl, + FormField, + FormItem, + FormLabel, + FormMessage, +} from "@/components/ui/form" +import { Input } from "@/components/ui/input" +import { Alert, AlertDescription } from "@/components/ui/alert" import { Card, CardContent, @@ -12,33 +22,29 @@ import { CardFooter, CardHeader, CardTitle, -} from "@/components/ui/card"; -import { useAuthStore } from "@/store/AuthStore/useAuthStore"; -import LoadingButton from "../Buttons/LoadingButton"; -import AuthBottom from "./AuthBottom"; +} from "@/components/ui/card" +import { useAuthStore } from "@/store/AuthStore/useAuthStore" +import LoadingButton from "../Buttons/LoadingButton" +import AuthBottom from "./AuthBottom" +import { signinSchema } from "@/validations/validation" -interface FormData { - email: string; - password: string; -} +type SigninFormValues = z.infer export default function SigninForm() { - const { isSigningIn, signin, signinError } = useAuthStore(); - const router = useRouter(); - const [formData, setFormData] = useState({ - email: "", - password: "", - }); + const { isSigningIn, signin, signinError } = useAuthStore() + const router = useRouter() - const handleChange = (e: React.ChangeEvent) => { - const { name, value } = e.target; - setFormData((prev) => ({ ...prev, [name]: value })); - }; + const form = useForm({ + resolver: zodResolver(signinSchema), + defaultValues: { + email: "", + password: "", + }, + }) - const handleSubmit = async (e: React.FormEvent) => { - e.preventDefault(); - signin(formData, router); - }; + const onSubmit = (data: SigninFormValues) => { + signin(data, router) + } return (
@@ -50,46 +56,47 @@ export default function SigninForm() { -
-
- - + + ( + + Email + + + + + + )} /> -
- -
- - ( + + Password + + + + + + )} /> -
- - {signinError && ( - - {signinError} - - )} - - - + {signinError && ( + + {signinError} + + )} + + +
- ); -} + ) +} \ No newline at end of file diff --git a/components/AuthComponent/SignupForm.tsx b/components/AuthComponent/SignupForm.tsx index e3e8963..8b93fe1 100644 --- a/components/AuthComponent/SignupForm.tsx +++ b/components/AuthComponent/SignupForm.tsx @@ -1,17 +1,23 @@ "use client"; -import React, { useState } from "react"; +import React from "react"; import { useRouter } from "next/navigation"; -import Link from "next/link"; -import { Button } from "@/components/ui/button"; +import { zodResolver } from "@hookform/resolvers/zod"; +import { useForm } from "react-hook-form"; +import * as z from "zod"; +import { + Form, + FormControl, + FormField, + FormItem, + FormLabel, + FormMessage, +} from "@/components/ui/form"; import { Input } from "@/components/ui/input"; -import { Label } from "@/components/ui/label"; import { Select, SelectContent, - SelectGroup, SelectItem, - SelectLabel, SelectTrigger, SelectValue, } from "@/components/ui/select"; @@ -24,33 +30,29 @@ import { CardTitle, } from "@/components/ui/card"; import { Alert, AlertDescription } from "@/components/ui/alert"; -import { UserType } from "@/types/typeInterfaces"; import { useAuthStore } from "@/store/AuthStore/useAuthStore"; import AuthBottom from "./AuthBottom"; import LoadingButton from "../Buttons/LoadingButton"; +import { signupSchema } from "@/validations/validation"; +type SignupFormValues = z.infer; export default function SignupForm() { const { isSigningUp, signup, signupError } = useAuthStore(); const router = useRouter(); - const [formData, setFormData] = useState({ - fullName: "", - email: "", - password: "", - leetcodeUsername: "", - gender: "", - }); - - const handleChange = (e: React.ChangeEvent) => { - setFormData({ ...formData, [e.target.name]: e.target.value }); - }; - const handleSelectChange = (value: string) => { - setFormData({ ...formData, gender: value }); - }; + const form = useForm({ + resolver: zodResolver(signupSchema), + defaultValues: { + fullName: "", + email: "", + password: "", + leetcodeUsername: "", + gender: "", + }, + }); - const handleSubmit = async (e: React.FormEvent) => { - e.preventDefault(); - signup(formData, router); + const onSubmit = (data: SignupFormValues) => { + signup(data, router); }; return ( @@ -65,93 +67,98 @@ export default function SignupForm() { -
-
- - + + ( + + Full Name + + + + + + )} /> -
- -
- - ( + + Email + + + + + + )} /> -
- -
- - ( + + Password + + + + + + )} /> -
- -
- - ( + + LeetCode Username + + + + + + )} /> -
- -
- - -
- - {signupError && ( - - {signupError} - - )} - - - + ( + + Gender + + + + )} + /> + {signupError && ( + + {signupError} + + )} + + +
= FieldPath +> = { + name: TName +} + +const FormFieldContext = React.createContext( + {} as FormFieldContextValue +) + +const FormField = < + TFieldValues extends FieldValues = FieldValues, + TName extends FieldPath = FieldPath +>({ + ...props +}: ControllerProps) => { + return ( + + + + ) +} + +const useFormField = () => { + const fieldContext = React.useContext(FormFieldContext) + const itemContext = React.useContext(FormItemContext) + const { getFieldState, formState } = useFormContext() + + const fieldState = getFieldState(fieldContext.name, formState) + + if (!fieldContext) { + throw new Error("useFormField should be used within ") + } + + const { id } = itemContext + + return { + id, + name: fieldContext.name, + formItemId: `${id}-form-item`, + formDescriptionId: `${id}-form-item-description`, + formMessageId: `${id}-form-item-message`, + ...fieldState, + } +} + +type FormItemContextValue = { + id: string +} + +const FormItemContext = React.createContext( + {} as FormItemContextValue +) + +const FormItem = React.forwardRef< + HTMLDivElement, + React.HTMLAttributes +>(({ className, ...props }, ref) => { + const id = React.useId() + + return ( + +
+ + ) +}) +FormItem.displayName = "FormItem" + +const FormLabel = React.forwardRef< + React.ElementRef, + React.ComponentPropsWithoutRef +>(({ className, ...props }, ref) => { + const { error, formItemId } = useFormField() + + return ( +