Skip to content

Open-source event management and ticket selling platform — perfect for concerts, conferences, and everything in between 🎟️ If you find this project helpful, please consider giving us a star ⭐️

3.1 KB
Raw Blame
import {Button, TextInput} from "@mantine/core";
import {useForm} from "@mantine/form";
import {useMutation} from "@tanstack/react-query";
import {authClient} from "../../../../api/auth.client.ts";
import {useState} from "react";
import {NavLink} from "react-router";
import {t} from "@lingui/macro";
import classes from "./ForgotPassword.module.scss";
import {IconArrowLeft, IconCheck} from "@tabler/icons-react";
import {Callout} from "../../../common/Callout";

export const ForgotPassword = () => {
    const form = useForm({
        initialValues: {
            email: '',
        },
    });
    const [showSuccessMessage, setShowSuccessMessage] = useState(false);
    const [error, setError] = useState<string | null>(null);

    const mutate = useMutation({
        mutationFn: (email: string) => {
            return authClient.forgotPassword({
                email: email,
            });
        },

        onSuccess: () => {
            setShowSuccessMessage(true);
        },

        onError: (requestError: any) => {
            setError(requestError?.response?.status === 429
                ? t`Too many attempts. Please wait a minute and try again.`
                : t`Something went wrong, please try again, or contact support if the problem persists`);
        }
    });

    if (showSuccessMessage) {
        return (
            <div className={classes.successMessage}>
                <div className={classes.successIcon}>
                    <IconCheck size={24} />
                </div>
                <h3>{t`Check your email`}</h3>
                <p>
                    {t`If you have an account with us, you will receive an email with instructions on how to reset your password.`}
                </p>
                <NavLink to={'/auth/login'}>
                    <IconArrowLeft size={14} />
                    {t`Back to sign in`}
                </NavLink>
            </div>
        );
    }

    return (
        <>
            <header className={classes.header}>
                <h2>{t`Reset password`}</h2>
                <p>{t`Enter your email and we'll send you instructions to reset your password.`}</p>
            </header>
            {error && <Callout variant="danger" onDismiss={() => setError(null)}>{error}</Callout>}
            <div className={classes.forgotPasswordCard}>
                <form onSubmit={form.onSubmit((values) => mutate.mutate(values.email))}>
                    <TextInput
                        type="email"
                        {...form.getInputProps('email')}
                        label={t`Email`}
                        placeholder="[email protected]"
                        required
                    />
                    <Button color="secondary.5" type="submit" fullWidth loading={mutate.isPending} disabled={mutate.isPending}>
                        {mutate.isPending ? t`Sending...` : t`Send reset link`}
                    </Button>
                </form>
                <footer>
                    <NavLink to={'/auth/login'}>{t`Back to sign in`}</NavLink>
                </footer>
            </div>
        </>
    );
}

export default ForgotPassword;