mirror of
https://github.com/ramvignesh-b/pi-ku.git
synced 2026-05-04 08:56:52 +00:00
refactor: modularize form fields into a separate component
This commit is contained in:
@@ -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 (
|
||||||
|
<div className="form-control">
|
||||||
|
<label className="field-label font-display text-primary-content">{label}</label>
|
||||||
|
<input
|
||||||
|
{...registration}
|
||||||
|
type={type}
|
||||||
|
placeholder={placeholder}
|
||||||
|
className={`input input-bordered focus:input-primary ${
|
||||||
|
error ? "input-error" : ""
|
||||||
|
}`}
|
||||||
|
/>
|
||||||
|
{error && <p className="field-error">{error}</p>}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,16 +1,19 @@
|
|||||||
import { zodResolver } from "@hookform/resolvers/zod";
|
import { zodResolver } from "@hookform/resolvers/zod";
|
||||||
import { InfoIcon } from "@phosphor-icons/react";
|
import { InfoIcon } from "@phosphor-icons/react";
|
||||||
|
import { useState } from "react";
|
||||||
import { useForm } from "react-hook-form";
|
import { useForm } from "react-hook-form";
|
||||||
|
import { useNavigate } from "react-router-dom";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
import apiClient from "../api/apiClient";
|
||||||
import Logo from "../components/Logo";
|
import Logo from "../components/Logo";
|
||||||
|
import FormField from "../components/ui/FormField";
|
||||||
|
|
||||||
// validation logic
|
// validation logic
|
||||||
const registerSchema = z
|
const registerSchema = z
|
||||||
.object({
|
.object({
|
||||||
|
full_name: z.string().min(2, "Name must be at least 2 characters"),
|
||||||
email: z.email("Please enter a valid email"),
|
email: z.email("Please enter a valid email"),
|
||||||
password: z
|
password: z.string().min(8, "Password must be at least 8 characters"),
|
||||||
.string()
|
|
||||||
.check(z.minLength(8, "Password must be at least 8 characters")),
|
|
||||||
confirm_password: z.string(),
|
confirm_password: z.string(),
|
||||||
})
|
})
|
||||||
.refine((data) => data.password === data.confirm_password, {
|
.refine((data) => data.password === data.confirm_password, {
|
||||||
@@ -21,6 +24,10 @@ const registerSchema = z
|
|||||||
type RegisterInputs = z.infer<typeof registerSchema>;
|
type RegisterInputs = z.infer<typeof registerSchema>;
|
||||||
|
|
||||||
export default function Register() {
|
export default function Register() {
|
||||||
|
const navigate = useNavigate();
|
||||||
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
|
const [apiError, setApiError] = useState<string | null>(null);
|
||||||
|
|
||||||
const {
|
const {
|
||||||
register,
|
register,
|
||||||
handleSubmit,
|
handleSubmit,
|
||||||
@@ -29,64 +36,69 @@ export default function Register() {
|
|||||||
resolver: zodResolver(registerSchema),
|
resolver: zodResolver(registerSchema),
|
||||||
});
|
});
|
||||||
|
|
||||||
const onSubmit = (data: RegisterInputs) => {
|
const onSubmit = async (data: RegisterInputs) => {
|
||||||
console.log("Form Data:", data);
|
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 (
|
return (
|
||||||
<div className="glass-card w-full max-w-sm p-2 transition-all duration-500 hover:shadow-2xl">
|
<div className="glass-card w-full max-w-sm p-2 transition-all duration-500 hover:shadow-2xl fade-zoom">
|
||||||
<form onSubmit={handleSubmit(onSubmit)} className="card-body gap-4">
|
<form onSubmit={handleSubmit(onSubmit)} className="card-body gap-4">
|
||||||
<h2 className="card-title font-display text-2xl font-bold justify-center text-primary tracking-tight">
|
<h2 className="card-title font-display text-2xl font-bold justify-center text-primary tracking-tight">
|
||||||
Create a <Logo /> Account
|
Create a <Logo /> Account
|
||||||
</h2>
|
</h2>
|
||||||
|
|
||||||
<div className="form-control">
|
{apiError && (
|
||||||
<label htmlFor="email" className="label font-bold font-display py-1">
|
<div className="alert alert-error text-xs py-2 rounded-md">
|
||||||
Email
|
<span>{apiError}</span>
|
||||||
</label>
|
</div>
|
||||||
<input
|
)}
|
||||||
{...register("email")}
|
|
||||||
type="email"
|
|
||||||
placeholder="you@email.com"
|
|
||||||
className={`input input-bordered focus:input-primary ${errors.email ? "input-error" : ""}`}
|
|
||||||
/>
|
|
||||||
{errors.email && (
|
|
||||||
<p className="text-error-content text-xs mt-1">{errors.email.message}</p>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="form-control">
|
<FormField
|
||||||
<label htmlFor="password" className="label font-bold font-display py-1">
|
label="Full Name"
|
||||||
Password
|
placeholder="Word Smith"
|
||||||
</label>
|
registration={register("full_name")}
|
||||||
<input
|
error={errors.full_name?.message}
|
||||||
{...register("password")}
|
/>
|
||||||
type="password"
|
|
||||||
placeholder="••••••••"
|
|
||||||
className={`input input-bordered focus:input-primary ${errors.password ? "input-error" : ""}`}
|
|
||||||
/>
|
|
||||||
{errors.password && (
|
|
||||||
<p className="text-error-content text-xs mt-1">{errors.password.message}</p>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{/* Confirm Pass */}
|
<FormField
|
||||||
<div className="form-control">
|
label="Email"
|
||||||
<label htmlFor="confirm_password" className="label font-bold font-display py-1">
|
type="email"
|
||||||
Confirm Password
|
placeholder="you@email.com"
|
||||||
</label>
|
registration={register("email")}
|
||||||
<input
|
error={errors.email?.message}
|
||||||
{...register("confirm_password")}
|
/>
|
||||||
type="password"
|
|
||||||
placeholder="••••••••"
|
<FormField
|
||||||
className={`input input-bordered focus:input-primary ${errors.confirm_password ? "input-error" : ""}`}
|
label="Password"
|
||||||
/>
|
type="password"
|
||||||
{errors.confirm_password && (
|
placeholder="••••••••"
|
||||||
<p className="text-error-content text-xs mt-1">
|
registration={register("password")}
|
||||||
{errors.confirm_password.message}
|
error={errors.password?.message}
|
||||||
</p>
|
/>
|
||||||
)}
|
|
||||||
</div>
|
<FormField
|
||||||
|
label="Confirm Password"
|
||||||
|
type="password"
|
||||||
|
placeholder="••••••••"
|
||||||
|
registration={register("confirm_password")}
|
||||||
|
error={errors.confirm_password?.message}
|
||||||
|
/>
|
||||||
|
|
||||||
{/* Warning */}
|
{/* Warning */}
|
||||||
<div className="bg-warning/10 border-l-2 border-warning p-3 rounded-r-md flex gap-2">
|
<div className="bg-warning/10 border-l-2 border-warning p-3 rounded-r-md flex gap-2">
|
||||||
@@ -102,8 +114,16 @@ export default function Register() {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="card-actions mt-4">
|
<div className="card-actions mt-4">
|
||||||
<button type="submit" className="btn btn-primary w-full shadow-lg">
|
<button
|
||||||
Register
|
type="submit"
|
||||||
|
disabled={isLoading}
|
||||||
|
className="btn btn-primary w-full shadow-lg"
|
||||||
|
>
|
||||||
|
{isLoading ? (
|
||||||
|
<span className="loading loading-spinner loading-sm" />
|
||||||
|
) : (
|
||||||
|
"Register"
|
||||||
|
)}
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
|||||||
Reference in New Issue
Block a user