refactor register page
This commit is contained in:
1 parent
3df85b7522
commit
35f9381573
1 file changed
+18
-50
@@ -1,7 +1,7 @@
|
|||||||
import { BaseAuthPage } from "@/Components/design-elements";
|
import { BaseAuthPage } from "@/Components/design-elements";
|
||||||
import { Button, TextField } from "@/Components/inputs";
|
import { Button } from "@/Components/inputs";
|
||||||
import Alert from "@mui/material/Alert";
|
import Alert from "@mui/material/Alert";
|
||||||
import { useForm, Controller } from "react-hook-form";
|
import { useForm, FormProvider } from "react-hook-form";
|
||||||
import { zodResolver } from "@hookform/resolvers/zod/dist/zod.js";
|
import { zodResolver } from "@hookform/resolvers/zod/dist/zod.js";
|
||||||
import { useRegisterForm } from "@/Hooks/useRegisterForm";
|
import { useRegisterForm } from "@/Hooks/useRegisterForm";
|
||||||
import type { RegisterFormData } from "@/Validation/register";
|
import type { RegisterFormData } from "@/Validation/register";
|
||||||
@@ -12,6 +12,7 @@ import { useDispatch } from "react-redux";
|
|||||||
import { useNavigate, useParams } from "react-router-dom";
|
import { useNavigate, useParams } from "react-router-dom";
|
||||||
import type { AuthResponse } from "@/Types/User";
|
import type { AuthResponse } from "@/Types/User";
|
||||||
import { useEffect, useRef, useState } from "react";
|
import { useEffect, useRef, useState } from "react";
|
||||||
|
import { FormTextField } from "@/Components/inputs/forms/FormTextField";
|
||||||
|
|
||||||
interface RegisterPayload {
|
interface RegisterPayload {
|
||||||
user: Omit<RegisterFormData, "confirm">;
|
user: Omit<RegisterFormData, "confirm">;
|
||||||
@@ -45,10 +46,11 @@ const RegisterPage = () => {
|
|||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const { control, handleSubmit, setError, reset } = useForm<RegisterFormData>({
|
const form = useForm<RegisterFormData>({
|
||||||
resolver: zodResolver(schema),
|
resolver: zodResolver(schema),
|
||||||
defaultValues: defaults,
|
defaultValues: defaults,
|
||||||
});
|
});
|
||||||
|
const { handleSubmit, setError, reset } = form;
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!token || hasVerified.current) return;
|
if (!token || hasVerified.current) return;
|
||||||
@@ -64,14 +66,14 @@ const RegisterPage = () => {
|
|||||||
navigate("/register", { replace: true });
|
navigate("/register", { replace: true });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}, [token]);
|
}, [token, defaults, navigate, reset, verifyToken]);
|
||||||
|
|
||||||
if (isCheckingAdmin) return null;
|
if (isCheckingAdmin) return null;
|
||||||
|
|
||||||
const onSubmit = async (data: RegisterFormData) => {
|
const onSubmit = async (data: RegisterFormData) => {
|
||||||
if (loading) return;
|
if (loading) return;
|
||||||
|
|
||||||
const { confirm, ...userData } = data;
|
const { confirm: _confirm, ...userData } = data;
|
||||||
const payload: RegisterPayload = { user: userData };
|
const payload: RegisterPayload = { user: userData };
|
||||||
if (token) {
|
if (token) {
|
||||||
payload.token = token;
|
payload.token = token;
|
||||||
@@ -89,6 +91,7 @@ const RegisterPage = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
<FormProvider {...form}>
|
||||||
<BaseAuthPage
|
<BaseAuthPage
|
||||||
component="form"
|
component="form"
|
||||||
onSubmit={handleSubmit(onSubmit)}
|
onSubmit={handleSubmit(onSubmit)}
|
||||||
@@ -112,74 +115,38 @@ const RegisterPage = () => {
|
|||||||
{t("pages.auth.register.setupNotice")}
|
{t("pages.auth.register.setupNotice")}
|
||||||
</Alert>
|
</Alert>
|
||||||
)}
|
)}
|
||||||
<Controller
|
<FormTextField
|
||||||
name="firstName"
|
name="firstName"
|
||||||
control={control}
|
|
||||||
render={({ field, fieldState }) => (
|
|
||||||
<TextField
|
|
||||||
{...field}
|
|
||||||
fieldLabel={t("common.form.name.option.firstName.label")}
|
fieldLabel={t("common.form.name.option.firstName.label")}
|
||||||
placeholder={t("common.form.name.option.firstName.placeholder")}
|
placeholder={t("common.form.name.option.firstName.placeholder")}
|
||||||
error={!!fieldState.error}
|
|
||||||
helperText={fieldState.error?.message ?? ""}
|
|
||||||
/>
|
/>
|
||||||
)}
|
<FormTextField
|
||||||
/>
|
|
||||||
<Controller
|
|
||||||
name="lastName"
|
name="lastName"
|
||||||
control={control}
|
|
||||||
render={({ field, fieldState }) => (
|
|
||||||
<TextField
|
|
||||||
{...field}
|
|
||||||
fieldLabel={t("common.form.name.option.lastName.label")}
|
fieldLabel={t("common.form.name.option.lastName.label")}
|
||||||
placeholder={t("common.form.name.option.lastName.placeholder")}
|
placeholder={t("common.form.name.option.lastName.placeholder")}
|
||||||
error={!!fieldState.error}
|
|
||||||
helperText={fieldState.error?.message ?? ""}
|
|
||||||
/>
|
/>
|
||||||
)}
|
<FormTextField
|
||||||
/>
|
|
||||||
<Controller
|
|
||||||
name="email"
|
name="email"
|
||||||
control={control}
|
type="email"
|
||||||
render={({ field, fieldState }) => (
|
|
||||||
<TextField
|
|
||||||
{...field}
|
|
||||||
disabled={!!token}
|
disabled={!!token}
|
||||||
fieldLabel={t("common.form.email.option.email.label")}
|
fieldLabel={t("common.form.email.option.email.label")}
|
||||||
placeholder={t("common.form.email.option.email.placeholder")}
|
placeholder={t("common.form.email.option.email.placeholder")}
|
||||||
error={!!fieldState.error}
|
|
||||||
helperText={fieldState.error?.message ?? ""}
|
|
||||||
/>
|
/>
|
||||||
)}
|
|
||||||
/>
|
<FormTextField
|
||||||
<Controller
|
|
||||||
name="password"
|
name="password"
|
||||||
control={control}
|
|
||||||
render={({ field, fieldState }) => (
|
|
||||||
<TextField
|
|
||||||
{...field}
|
|
||||||
type="password"
|
type="password"
|
||||||
fieldLabel={t("pages.auth.common.form.option.password.label")}
|
fieldLabel={t("pages.auth.common.form.option.password.label")}
|
||||||
placeholder={t("pages.auth.common.form.option.password.placeholder")}
|
placeholder={t("pages.auth.common.form.option.password.placeholder")}
|
||||||
error={!!fieldState.error}
|
|
||||||
helperText={fieldState.error?.message ?? ""}
|
|
||||||
/>
|
/>
|
||||||
)}
|
|
||||||
/>
|
<FormTextField
|
||||||
<Controller
|
|
||||||
name="confirm"
|
name="confirm"
|
||||||
control={control}
|
|
||||||
render={({ field, fieldState }) => (
|
|
||||||
<TextField
|
|
||||||
{...field}
|
|
||||||
type="password"
|
type="password"
|
||||||
fieldLabel={t("pages.auth.common.form.option.confirmPassword.label")}
|
fieldLabel={t("pages.auth.common.form.option.confirmPassword.label")}
|
||||||
placeholder={t("pages.auth.common.form.option.password.placeholder")}
|
placeholder={t("pages.auth.common.form.option.password.placeholder")}
|
||||||
error={!!fieldState.error}
|
|
||||||
helperText={fieldState.error?.message ?? ""}
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
variant="contained"
|
variant="contained"
|
||||||
type="submit"
|
type="submit"
|
||||||
@@ -188,6 +155,7 @@ const RegisterPage = () => {
|
|||||||
{t("pages.auth.register.submit")}
|
{t("pages.auth.register.submit")}
|
||||||
</Button>
|
</Button>
|
||||||
</BaseAuthPage>
|
</BaseAuthPage>
|
||||||
|
</FormProvider>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user