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:
@@ -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<typeof registerSchema>;
|
||||
|
||||
export default function Register() {
|
||||
const navigate = useNavigate();
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [apiError, setApiError] = useState<string | null>(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 (
|
||||
<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">
|
||||
<h2 className="card-title font-display text-2xl font-bold justify-center text-primary tracking-tight">
|
||||
Create a <Logo /> Account
|
||||
</h2>
|
||||
|
||||
<div className="form-control">
|
||||
<label htmlFor="email" className="label font-bold font-display py-1">
|
||||
Email
|
||||
</label>
|
||||
<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>
|
||||
{apiError && (
|
||||
<div className="alert alert-error text-xs py-2 rounded-md">
|
||||
<span>{apiError}</span>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="form-control">
|
||||
<label htmlFor="password" className="label font-bold font-display py-1">
|
||||
Password
|
||||
</label>
|
||||
<input
|
||||
{...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>
|
||||
<FormField
|
||||
label="Full Name"
|
||||
placeholder="Word Smith"
|
||||
registration={register("full_name")}
|
||||
error={errors.full_name?.message}
|
||||
/>
|
||||
|
||||
{/* Confirm Pass */}
|
||||
<div className="form-control">
|
||||
<label htmlFor="confirm_password" className="label font-bold font-display py-1">
|
||||
Confirm Password
|
||||
</label>
|
||||
<input
|
||||
{...register("confirm_password")}
|
||||
type="password"
|
||||
placeholder="••••••••"
|
||||
className={`input input-bordered focus:input-primary ${errors.confirm_password ? "input-error" : ""}`}
|
||||
/>
|
||||
{errors.confirm_password && (
|
||||
<p className="text-error-content text-xs mt-1">
|
||||
{errors.confirm_password.message}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
<FormField
|
||||
label="Email"
|
||||
type="email"
|
||||
placeholder="you@email.com"
|
||||
registration={register("email")}
|
||||
error={errors.email?.message}
|
||||
/>
|
||||
|
||||
<FormField
|
||||
label="Password"
|
||||
type="password"
|
||||
placeholder="••••••••"
|
||||
registration={register("password")}
|
||||
error={errors.password?.message}
|
||||
/>
|
||||
|
||||
<FormField
|
||||
label="Confirm Password"
|
||||
type="password"
|
||||
placeholder="••••••••"
|
||||
registration={register("confirm_password")}
|
||||
error={errors.confirm_password?.message}
|
||||
/>
|
||||
|
||||
{/* Warning */}
|
||||
<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 className="card-actions mt-4">
|
||||
<button type="submit" className="btn btn-primary w-full shadow-lg">
|
||||
Register
|
||||
<button
|
||||
type="submit"
|
||||
disabled={isLoading}
|
||||
className="btn btn-primary w-full shadow-lg"
|
||||
>
|
||||
{isLoading ? (
|
||||
<span className="loading loading-spinner loading-sm" />
|
||||
) : (
|
||||
"Register"
|
||||
)}
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
Reference in New Issue
Block a user