initial commit
This commit is contained in:
@@ -0,0 +1,193 @@
|
||||
"use client"
|
||||
|
||||
import { Register } from "@/actions/auth";
|
||||
import Header from "@/components/header";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Field, FieldDescription, FieldError, FieldGroup, FieldLabel } from "@/components/ui/field";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Spinner } from "@/components/ui/spinner";
|
||||
import { useTranslations } from "next-intl";
|
||||
import { useState } from "react";
|
||||
import { useForm, SubmitHandler, Controller } from "react-hook-form";
|
||||
import { useRouter } from "next/navigation";
|
||||
import Link from "next/link";
|
||||
import { Sex } from "@/types";
|
||||
import { Select, SelectContent, SelectGroup, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
|
||||
import { ArrowLeft, ArrowRight, Check } from "lucide-react";
|
||||
import { toast } from "sonner";
|
||||
|
||||
interface FormData {
|
||||
email: string;
|
||||
password: string;
|
||||
first_name: string;
|
||||
second_name: string;
|
||||
sex: Sex;
|
||||
}
|
||||
|
||||
export default function RegisterPage() {
|
||||
const router = useRouter();
|
||||
const t = useTranslations("RegisterPage");
|
||||
|
||||
const [step, setStep] = useState<number>(0);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const { register, handleSubmit, control, formState: { errors, isSubmitting } } = useForm<FormData>();
|
||||
|
||||
const onSubmit: SubmitHandler<FormData> = async (data) => {
|
||||
setError(null);
|
||||
|
||||
const result = await Register(data.email, data.password, data.first_name, data.second_name, data.sex);
|
||||
if (!result.success) {
|
||||
setError(result.message ?? null);
|
||||
return;
|
||||
}
|
||||
|
||||
toast(t("registeredSuccessfully"), { icon: <Check size={16} /> })
|
||||
router.push(`/login`);
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<Header />
|
||||
<div className="bg-background flex min-h-svh flex-col items-center justify-center gap-6 p-6 md:p-10">
|
||||
<div className="w-full max-w-sm flex flex-col gap-6">
|
||||
<form onSubmit={handleSubmit(onSubmit)}>
|
||||
<FieldGroup>
|
||||
<div className="flex flex-col gap-1">
|
||||
<h1 className="text-xl">{t("title")}</h1>
|
||||
<h2 className="text-lg text-muted-foreground leading-6">{t("subtitle")}</h2>
|
||||
</div>
|
||||
{ step == 0 ? (
|
||||
<>
|
||||
<Field data-invalid={!!errors.email}>
|
||||
<FieldLabel htmlFor="email">{t("email.title")}</FieldLabel>
|
||||
<Input
|
||||
id="email"
|
||||
type="email"
|
||||
autoComplete="email"
|
||||
disabled={isSubmitting}
|
||||
aria-invalid={!!errors.email}
|
||||
{...register("email", {
|
||||
required: t("required"),
|
||||
pattern: { value: /^[^\s@]+@[^\s@]+\.[^\s@]+$/, message: t("email.invalid") },
|
||||
})}
|
||||
/>
|
||||
{errors.email && <FieldError>{errors.email.message}</FieldError>}
|
||||
</Field>
|
||||
|
||||
<Field data-invalid={!!errors.password}>
|
||||
<FieldLabel htmlFor="password">{t("password.title")}</FieldLabel>
|
||||
<Input
|
||||
id="password"
|
||||
type="password"
|
||||
autoComplete="current-password"
|
||||
disabled={isSubmitting}
|
||||
aria-invalid={!!errors.password}
|
||||
{...register("password", {
|
||||
required: t("required"),
|
||||
minLength: { value: 8, message: t("password.minLength") },
|
||||
})}
|
||||
/>
|
||||
{errors.password && <FieldError>{errors.password.message}</FieldError>}
|
||||
</Field>
|
||||
|
||||
<Field>
|
||||
<Button onClick={(e) => setStep(1)} variant="default" disabled={isSubmitting}>
|
||||
{t("next")}
|
||||
<ArrowRight />
|
||||
</Button>
|
||||
</Field>
|
||||
</>) : (
|
||||
<>
|
||||
<div className="grid grid-cols-2 gap-4">
|
||||
<Field data-invalid={!!errors.first_name}>
|
||||
<FieldLabel htmlFor="first_name">{t("firstName.title")}</FieldLabel>
|
||||
<Input
|
||||
id="first_name"
|
||||
type="text"
|
||||
disabled={isSubmitting}
|
||||
aria-invalid={!!errors.first_name}
|
||||
{...register("first_name", {
|
||||
required: t("required"),
|
||||
minLength: { value: 2, message: t("firstName.minLength") },
|
||||
maxLength: { value: 64, message: t("firstName.maxLength") },
|
||||
})}
|
||||
/>
|
||||
{errors.first_name && <FieldError>{errors.first_name.message}</FieldError>}
|
||||
</Field>
|
||||
|
||||
<Field data-invalid={!!errors.second_name}>
|
||||
<FieldLabel htmlFor="second_name">{t("secondName.title")}</FieldLabel>
|
||||
<Input
|
||||
id="second_name"
|
||||
type="text"
|
||||
disabled={isSubmitting}
|
||||
aria-invalid={!!errors.second_name}
|
||||
{...register("second_name", {
|
||||
required: t("required"),
|
||||
minLength: { value: 2, message: t("secondName.minLength") },
|
||||
maxLength: { value: 64, message: t("secondName.maxLength") },
|
||||
})}
|
||||
/>
|
||||
{errors.second_name && <FieldError>{errors.second_name.message}</FieldError>}
|
||||
</Field>
|
||||
</div>
|
||||
|
||||
<Field data-invalid={!!errors.sex}>
|
||||
<FieldLabel htmlFor="sex">{t("sex.title")}</FieldLabel>
|
||||
<Controller
|
||||
name="sex"
|
||||
control={control}
|
||||
rules={{
|
||||
required: t("required"),
|
||||
}}
|
||||
render={({ field }) => (
|
||||
<Select
|
||||
disabled={isSubmitting}
|
||||
value={field.value}
|
||||
onValueChange={field.onChange}
|
||||
>
|
||||
<SelectTrigger aria-invalid={!!errors.sex}>
|
||||
<SelectValue placeholder={t("sex.select")} />
|
||||
</SelectTrigger>
|
||||
|
||||
<SelectContent>
|
||||
<SelectGroup>
|
||||
<SelectItem value="MALE">{t("sex.male")}</SelectItem>
|
||||
<SelectItem value="FEMALE">{t("sex.female")}</SelectItem>
|
||||
</SelectGroup>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
)}
|
||||
/>
|
||||
{errors.sex && <FieldError>{errors.sex.message}</FieldError>}
|
||||
</Field>
|
||||
|
||||
<div className="grid grid-cols-3 gap-4">
|
||||
<Field className="col-span-1">
|
||||
<Button onClick={(e) => {e.preventDefault(); setStep(0)}} variant="outline" disabled={isSubmitting}>
|
||||
<ArrowLeft />
|
||||
{t("back")}
|
||||
</Button>
|
||||
</Field>
|
||||
<Field className="col-span-2">
|
||||
<Button type="submit" disabled={isSubmitting}>
|
||||
{isSubmitting && <Spinner />}
|
||||
{t("register")}
|
||||
</Button>
|
||||
</Field>
|
||||
</div>
|
||||
{error && <FieldError className="text-center">{error}</FieldError>}
|
||||
</>)
|
||||
}
|
||||
</FieldGroup>
|
||||
</form>
|
||||
<FieldDescription className="px-6 text-center">
|
||||
{t.rich("hasAccountNote", {
|
||||
login: (chunks) => <Link href="/login">{chunks}</Link>
|
||||
})}
|
||||
</FieldDescription>
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user