|
| 1 | +"use client"; |
| 2 | +import { useForm } from "react-hook-form"; |
| 3 | +import * as yup from "yup"; |
| 4 | +import { yupResolver } from "@hookform/resolvers/yup"; |
| 5 | +import { ErrorMessage } from "@hookform/error-message"; |
| 6 | +import BaseLayout from "@/components/layout/BaseLayout"; |
| 7 | +import { Alert, Button, Input, Text } from "@mantine/core"; |
| 8 | +import Link from "next/link"; |
| 9 | +import React from "react"; |
| 10 | +import { AuthRepository } from "@/http/repositories/auth.repository"; |
| 11 | +import { useMutation } from "@tanstack/react-query"; |
| 12 | +import { ensureCSRF } from "@/utils/ensureCSRF"; |
| 13 | +import { showNotification } from "@mantine/notifications"; |
| 14 | +import { AxiosError } from "axios"; |
| 15 | + |
| 16 | +import { |
| 17 | + getHookFormErrorMessage, |
| 18 | + getServerErrorMessage, |
| 19 | +} from "@/utils/form-error"; |
| 20 | +import { useTranslation } from "@/i18n/use-translation"; |
| 21 | +import { useDebouncedCallback } from "@mantine/hooks"; |
| 22 | +import { ProfileApiRepository } from "@/http/repositories/profile.repository"; |
| 23 | + |
| 24 | +const RegisterPage = () => { |
| 25 | + const { _t } = useTranslation(); |
| 26 | + const api = new AuthRepository(); |
| 27 | + const api__profile = new ProfileApiRepository(); |
| 28 | + const registrationMutation = useMutation({ |
| 29 | + mutationFn: (payload: IRegistrationFormPayload) => api.register(payload), |
| 30 | + }); |
| 31 | + |
| 32 | + const form = useForm<IRegistrationFormPayload>({ |
| 33 | + resolver: yupResolver(formValidationSchema), |
| 34 | + }); |
| 35 | + |
| 36 | + const handleOnChangeUsernameDebounce = useDebouncedCallback( |
| 37 | + (username: string) => { |
| 38 | + ensureCSRF(async () => { |
| 39 | + await api__profile.getPublicUniqueUsername(username).then((res) => { |
| 40 | + if (res?.data?.username) { |
| 41 | + form.setValue("username", res?.data?.username); |
| 42 | + } |
| 43 | + }); |
| 44 | + }); |
| 45 | + }, |
| 46 | + 500 |
| 47 | + ); |
| 48 | + |
| 49 | + const onSubmit = async (data: IRegistrationFormPayload) => { |
| 50 | + await ensureCSRF(() => { |
| 51 | + registrationMutation.mutate(data, { |
| 52 | + onSuccess: () => { |
| 53 | + window.location.href = "/"; |
| 54 | + }, |
| 55 | + }); |
| 56 | + }); |
| 57 | + }; |
| 58 | + |
| 59 | + return ( |
| 60 | + <BaseLayout> |
| 61 | + <div className="max-w-96 mx-auto my-10 px-2"> |
| 62 | + <form |
| 63 | + className="flex flex-col gap-4" |
| 64 | + onSubmit={form.handleSubmit(onSubmit)} |
| 65 | + method="post" |
| 66 | + > |
| 67 | + {registrationMutation.isError && ( |
| 68 | + <Alert color="red" title="কিছু ভুল হয়েছে"> |
| 69 | + {getServerErrorMessage(registrationMutation.error)} |
| 70 | + </Alert> |
| 71 | + )} |
| 72 | + |
| 73 | + <Input.Wrapper |
| 74 | + label={_t("Name")} |
| 75 | + error={getHookFormErrorMessage("name", form?.formState?.errors)} |
| 76 | + > |
| 77 | + <Input |
| 78 | + placeholder={_t("Your name")} |
| 79 | + size="lg" |
| 80 | + variant="filled" |
| 81 | + {...form.register("name")} |
| 82 | + /> |
| 83 | + </Input.Wrapper> |
| 84 | + |
| 85 | + <Input.Wrapper |
| 86 | + label={_t("Username")} |
| 87 | + error={getHookFormErrorMessage("username", form?.formState?.errors)} |
| 88 | + > |
| 89 | + <Input |
| 90 | + placeholder={_t("Your username")} |
| 91 | + size="lg" |
| 92 | + variant="filled" |
| 93 | + value={form.watch("username") || ""} |
| 94 | + onChange={(e) => { |
| 95 | + form.setValue("username", e.target.value); |
| 96 | + handleOnChangeUsernameDebounce(e.target.value); |
| 97 | + }} |
| 98 | + /> |
| 99 | + </Input.Wrapper> |
| 100 | + |
| 101 | + <Input.Wrapper |
| 102 | + label={_t("Email")} |
| 103 | + error={getHookFormErrorMessage("email", form?.formState?.errors)} |
| 104 | + > |
| 105 | + <Input |
| 106 | + placeholder={_t("Your email")} |
| 107 | + size="lg" |
| 108 | + variant="filled" |
| 109 | + {...form.register("email")} |
| 110 | + /> |
| 111 | + </Input.Wrapper> |
| 112 | + |
| 113 | + <Input.Wrapper |
| 114 | + label={_t("Password")} |
| 115 | + error={getHookFormErrorMessage("password", form?.formState?.errors)} |
| 116 | + > |
| 117 | + <Input |
| 118 | + placeholder="***********" |
| 119 | + type="password" |
| 120 | + size="lg" |
| 121 | + variant="filled" |
| 122 | + {...form.register("password")} |
| 123 | + /> |
| 124 | + </Input.Wrapper> |
| 125 | + |
| 126 | + <Input.Wrapper |
| 127 | + label={_t("Confirm password")} |
| 128 | + error={getHookFormErrorMessage( |
| 129 | + "password_confirmation", |
| 130 | + form?.formState?.errors |
| 131 | + )} |
| 132 | + > |
| 133 | + <Input |
| 134 | + placeholder="***********" |
| 135 | + type="password" |
| 136 | + size="lg" |
| 137 | + variant="filled" |
| 138 | + {...form.register("password_confirmation")} |
| 139 | + /> |
| 140 | + </Input.Wrapper> |
| 141 | + <Button |
| 142 | + type="submit" |
| 143 | + size="lg" |
| 144 | + loading={registrationMutation?.isPending} |
| 145 | + > |
| 146 | + {_t("Register")} |
| 147 | + </Button> |
| 148 | + </form> |
| 149 | + |
| 150 | + <div className="mt-10 flex gap-3"> |
| 151 | + {_t("Already have an account?")}{" "} |
| 152 | + <Link className="text-lg" href={"/auth/login"}> |
| 153 | + {_t("Login")} |
| 154 | + </Link> |
| 155 | + </div> |
| 156 | + </div> |
| 157 | + </BaseLayout> |
| 158 | + ); |
| 159 | +}; |
| 160 | + |
| 161 | +const formValidationSchema = yup.object({ |
| 162 | + name: yup.string().required().label("Name"), |
| 163 | + username: yup.string().required().label("Username"), |
| 164 | + email: yup.string().email().required().label("Email"), |
| 165 | + password: yup.string().min(6).max(32).required().label("Password"), |
| 166 | + password_confirmation: yup |
| 167 | + .string() |
| 168 | + .min(6) |
| 169 | + .max(32) |
| 170 | + .required() |
| 171 | + .label("Password"), |
| 172 | +}); |
| 173 | + |
| 174 | +type IRegistrationFormPayload = yup.InferType<typeof formValidationSchema>; |
| 175 | + |
| 176 | +export default RegisterPage; |
0 commit comments