All files / src/app/(auth)/reset-password ResetPasswordForm.tsx

0% Statements 0/17
0% Branches 0/4
0% Functions 0/3
0% Lines 0/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156                                                                                                                                                                                                                                                                                                                       
'use client';
 
import { useMutation } from '@apollo/client/react';
import {
  Alert,
  Button,
  Container,
  Group,
  Paper,
  Stack,
  Text,
  TextInput,
  Title,
} from '@mantine/core';
import { useForm } from '@mantine/form';
import Link from 'next/link';
import { useTranslations } from 'next-intl';
import type { FC } from 'react';
import { useState } from 'react';
import { CiCircleInfo } from 'react-icons/ci';
import { IoArrowBackOutline } from 'react-icons/io5';
import { RESET_PASSWORD } from '@/lib/graphql/mutations';
import {
  isFormSubmitDisabled,
  resetPasswordValidationSchema,
} from '@/lib/validation';
import { zodResolver } from '@/lib/validation/zodResolver';
import { AUTH_ROUTES } from '../../../types/routes';
import {
  showErrorNotification,
  showSuccessNotification,
} from '../../../utils/notifications';
import type { ResetPasswordFormValues, ResetPasswordResponse } from './types';
 
export const ResetPasswordForm: FC = () => {
  const translate = useTranslations();
  const [resetPassword, { loading }] =
    useMutation<ResetPasswordResponse>(RESET_PASSWORD);
  const [isResetPasswordEmailSent, setIsResetPasswordEmailSent] =
    useState(false);
 
  const form = useForm<ResetPasswordFormValues>({
    mode: 'uncontrolled',
    initialValues: {
      email: '',
    },
    validate: zodResolver(resetPasswordValidationSchema),
    validateInputOnBlur: true,
  });
 
  const handleResetPassword = async (values: ResetPasswordFormValues) => {
    try {
      const result = await resetPassword({
        variables: { email: values.email },
      });
 
      if (result.data?.resetPassword?.success) {
        setIsResetPasswordEmailSent(true);
        form.reset();
 
        showSuccessNotification(
          translate('response.success'),
          result.data.resetPassword.message,
        );
      }
    } catch (error: unknown) {
      showErrorNotification(
        translate('response.resetPasswordFailed'),
        translate('response.somethingWentWrong') as string,
        error,
      );
    }
  };
 
  const isSubmitDisabled = isFormSubmitDisabled(form, loading);
 
  return (
    <Container size={460} my={30} id="reset-password-page">
      <Title ta="center" mb="xs">
        {translate('auth.forgotPasswordTitle')}
      </Title>
      <Text c="dimmed" fz="sm" ta="center" mb="xl">
        {translate('auth.forgotPasswordDescription')}
      </Text>
 
      <Paper
        component="form"
        onSubmit={form.onSubmit(handleResetPassword)}
        withBorder
        shadow="md"
        p={30}
        radius="md"
      >
        {isResetPasswordEmailSent ? (
          <Stack gap="md">
            <Alert
              variant="light"
              color="green"
              title={translate('response.emailSent')}
              icon={<CiCircleInfo size={30} />}
            >
              <Text size="sm">
                {translate('response.emailWithResetLinkSent')}
              </Text>
              <Text size="sm" mt="sm" c="dimmed">
                {translate('response.checkSpamFolder')}
              </Text>
            </Alert>
 
            <Button
              variant="light"
              onClick={() => setIsResetPasswordEmailSent(false)}
              fullWidth
            >
              {translate('auth.sendAnotherEmail')}
            </Button>
          </Stack>
        ) : (
          <Stack gap="md">
            <TextInput
              id="email"
              label={translate('user.email')}
              placeholder={translate('auth.emailPlaceholder')}
              required
              key={form.key('email')}
              {...form.getInputProps('email')}
            />
 
            <Button
              type="submit"
              loading={loading}
              loaderProps={{ type: 'dots' }}
              fullWidth
              disabled={isSubmitDisabled}
            >
              {translate('auth.sendResetLink')}
            </Button>
          </Stack>
        )}
 
        <Group justify="center" mt="lg">
          <Button
            component={Link}
            size="sm"
            href={AUTH_ROUTES.LOGIN}
            variant="subtle"
            leftSection={<IoArrowBackOutline />}
          >
            {translate('auth.backToLogin')}
          </Button>
        </Group>
      </Paper>
    </Container>
  );
};