refactor: colocate frontend component files
Frontend security / security (push) Successful in 9s
Frontend verify / verify (push) Successful in 5m10s
Conventional commits / commits (pull_request) Successful in 25s
Frontend security / security (pull_request) Successful in 9s
Frontend verify / verify (pull_request) Successful in 5m10s
Frontend security / security (push) Successful in 9s
Frontend verify / verify (push) Successful in 5m10s
Conventional commits / commits (pull_request) Successful in 25s
Frontend security / security (pull_request) Successful in 9s
Frontend verify / verify (pull_request) Successful in 5m10s
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import { Alert, Button, PasswordInput, Stack, TextInput } from "@mantine/core";
|
||||
import type { FC } from "react";
|
||||
import { useState } from "react";
|
||||
import { FormProvider, type Resolver, useForm } from "react-hook-form";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { Link } from "react-router";
|
||||
import {
|
||||
type AuthFormValues,
|
||||
loginSchema,
|
||||
registerSchema,
|
||||
} from "../../model/validation";
|
||||
import classes from "./AuthForm.module.css";
|
||||
|
||||
type AuthFormProps = {
|
||||
register?: true;
|
||||
onSubmit: (email: string, password: string) => Promise<void>;
|
||||
};
|
||||
|
||||
const AuthForm: FC<AuthFormProps> = (props) => {
|
||||
const { register, onSubmit } = props;
|
||||
const { t } = useTranslation();
|
||||
const schema = register ? registerSchema : loginSchema;
|
||||
const methods = useForm<AuthFormValues>({
|
||||
resolver: zodResolver(schema) as Resolver<AuthFormValues>,
|
||||
});
|
||||
const {
|
||||
formState: { errors, isSubmitting },
|
||||
} = methods;
|
||||
const [submitError, setSubmitError] = useState<string | null>(null);
|
||||
return (
|
||||
<FormProvider {...methods}>
|
||||
<form
|
||||
onSubmit={methods.handleSubmit(async (values) => {
|
||||
setSubmitError(null);
|
||||
try {
|
||||
await onSubmit(values.email, values.password);
|
||||
} catch {
|
||||
setSubmitError(t("auth.requestError"));
|
||||
}
|
||||
})}
|
||||
>
|
||||
<fieldset className={classes.fieldset} disabled={isSubmitting}>
|
||||
<Stack>
|
||||
<TextInput
|
||||
label={t("auth.email")}
|
||||
{...methods.register("email")}
|
||||
error={errors.email?.message as string}
|
||||
/>
|
||||
<PasswordInput
|
||||
label={t("auth.password")}
|
||||
{...methods.register("password")}
|
||||
error={errors.password?.message as string}
|
||||
/>
|
||||
{register && (
|
||||
<PasswordInput
|
||||
label={t("auth.passwordConfirmation")}
|
||||
{...methods.register("passwordConfirmation")}
|
||||
error={errors.passwordConfirmation?.message as string}
|
||||
/>
|
||||
)}
|
||||
{submitError && <Alert color="red">{submitError}</Alert>}
|
||||
<Button type="submit" loading={isSubmitting}>
|
||||
{t(register ? "auth.register" : "auth.login")}
|
||||
</Button>
|
||||
<Button
|
||||
component={Link}
|
||||
to={register ? "/login" : "/register"}
|
||||
variant="subtle"
|
||||
>
|
||||
{t(register ? "auth.haveAccount" : "auth.noAccount")}
|
||||
</Button>
|
||||
</Stack>
|
||||
</fieldset>
|
||||
</form>
|
||||
</FormProvider>
|
||||
);
|
||||
};
|
||||
|
||||
export { AuthForm };
|
||||
Reference in New Issue
Block a user