From 44b1ed51530ea9c1fd5d3e4a268ac2bc1bd3304d Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 10 Apr 2026 15:38:17 +0530 Subject: [PATCH] refactor: modularize form fields into a separate component --- frontend/src/components/ui/FormField.tsx | 32 ++++++ frontend/src/pages/Register.tsx | 126 +++++++++++++---------- 2 files changed, 105 insertions(+), 53 deletions(-) create mode 100644 frontend/src/components/ui/FormField.tsx diff --git a/frontend/src/components/ui/FormField.tsx b/frontend/src/components/ui/FormField.tsx new file mode 100644 index 0000000..c61ef72 --- /dev/null +++ b/frontend/src/components/ui/FormField.tsx @@ -0,0 +1,32 @@ +import type { UseFormRegisterReturn } from "react-hook-form"; + +interface FormFieldProps { + label: string; + type?: string; + placeholder?: string; + registration: UseFormRegisterReturn; + error?: string; +} + +export default function FormField({ + label, + type = "text", + placeholder, + registration, + error, +}: FormFieldProps) { + return ( +
+ + + {error &&

{error}

} +
+ ); +} diff --git a/frontend/src/pages/Register.tsx b/frontend/src/pages/Register.tsx index 80d1410..4e38d01 100644 --- a/frontend/src/pages/Register.tsx +++ b/frontend/src/pages/Register.tsx @@ -1,16 +1,19 @@ import { zodResolver } from "@hookform/resolvers/zod"; import { InfoIcon } from "@phosphor-icons/react"; +import { useState } from "react"; import { useForm } from "react-hook-form"; +import { useNavigate } from "react-router-dom"; import { z } from "zod"; +import apiClient from "../api/apiClient"; import Logo from "../components/Logo"; +import FormField from "../components/ui/FormField"; // validation logic const registerSchema = z .object({ + full_name: z.string().min(2, "Name must be at least 2 characters"), email: z.email("Please enter a valid email"), - password: z - .string() - .check(z.minLength(8, "Password must be at least 8 characters")), + password: z.string().min(8, "Password must be at least 8 characters"), confirm_password: z.string(), }) .refine((data) => data.password === data.confirm_password, { @@ -21,6 +24,10 @@ const registerSchema = z type RegisterInputs = z.infer; export default function Register() { + const navigate = useNavigate(); + const [isLoading, setIsLoading] = useState(false); + const [apiError, setApiError] = useState(null); + const { register, handleSubmit, @@ -29,64 +36,69 @@ export default function Register() { resolver: zodResolver(registerSchema), }); - const onSubmit = (data: RegisterInputs) => { - console.log("Form Data:", data); + const onSubmit = async (data: RegisterInputs) => { + setIsLoading(true); + setApiError(null); + try { + await apiClient.post("/register/", { + full_name: data.full_name, + email: data.email, + password: data.password, + }); + navigate("/verify-email"); + } catch (err: any) { + console.error("Registration error:", err); + setApiError( + err.response?.data?.message || "Registration failed. Please try again." + ); + } finally { + setIsLoading(false); + } }; return ( -
+

Create a Account

-
- - - {errors.email && ( -

{errors.email.message}

- )} -
+ {apiError && ( +
+ {apiError} +
+ )} -
- - - {errors.password && ( -

{errors.password.message}

- )} -
+ - {/* Confirm Pass */} -
- - - {errors.confirm_password && ( -

- {errors.confirm_password.message} -

- )} -
+ + + + + {/* Warning */}
@@ -102,8 +114,16 @@ export default function Register() {
-