All files / src/pages ForgotPasswordPage.tsx

100% Statements 34/34
100% Branches 27/27
100% Functions 5/5
100% Lines 33/33

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                                            33x 33x 33x   33x 33x 33x 33x   33x   33x 7x 7x 6x 6x 6x   6x 6x         2x 1x 1x   1x     5x 2x 2x 1x   1x     3x 3x   5x   6x       33x                             33x                                                 10x                                                       1x                    
import React, { useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useNavigate } from 'react-router-dom';
import Seo from '../components/Seo';
import { PageWrapper } from '../components/PageWrapper';
import Box from '@mui/material/Box';
import Stack from '@mui/material/Stack';
import Typography from '@mui/material/Typography';
import TextField from '@mui/material/TextField';
import Button from '@mui/material/Button';
import Link from '@mui/material/Link';
import CircularProgress from '@mui/material/CircularProgress';
import AlertMessage from '../components/AlertMessage/AlertMessage';
import { useLanguageStore } from '../stores/useLanguageStore';
import axios from 'axios';
import { passwordForgottenDemand } from '../services/authService';
import { getErrorMessage } from '../utils/errorUtils';
import { logError } from '../utils/logger';
import { isEmailValid } from '../utils/validation';
import type { ApiResponse } from '../types/apiResponse';
 
export default function ForgotPasswordPage() {
  const { t } = useTranslation('forgotPassword');
  const navigate = useNavigate();
  const lang = useLanguageStore.getState().lang;
 
  const [email, setEmail] = useState('');
  const [loading, setLoading] = useState(false);
  const [status, setStatus] = useState<'idle' | 'success' | 'error'>('idle');
  const [message, setMessage] = useState('');
 
  const validEmail = isEmailValid(email.trim());
 
  const handleSubmit = async (e: React.FormEvent) => {
    e.preventDefault();
    if (!validEmail) return;
    setLoading(true);
    setStatus('idle');
    setMessage('');
 
    try {
      const { status } = await passwordForgottenDemand(
        email.trim(),
        lang
      );
 
      if (status >= 200 && status < 300) {
        setStatus('success');
        setMessage(t('forgotPassword.successMessage'));
      } else {
        throw new Error('Unexpected status');
      }
    } catch (err) {
      if (axios.isAxiosError(err)) {
        const data = err.response?.data as ApiResponse | undefined;
        if (data?.code) {
          setMessage(getErrorMessage(t, data?.code));
        } else {
          setMessage(getErrorMessage(t, 'generic_error'));
        }
      } else {
        logError('ForgotPasswordDemand:handleSubmit', err);
        setMessage(getErrorMessage(t, 'network_error'));
      }
      setStatus('error');
    } finally {
      setLoading(false);
    }
  };
 
  return (
    <>
      <Seo title={t('seo.title')} description={t('seo.description')} />
      <PageWrapper disableCard>
        <Box
          component="form"
          onSubmit={handleSubmit}
          sx={{
            maxWidth: 400,
            mx: 'auto',
            mt: { xs: 1, sm: 4, md: 8 },
            p: 4,
            borderRadius: 2,
            boxShadow: 3,
            backgroundColor: 'background.paper',
            border: (theme) => `1px solid ${theme.palette.divider}`,
          }}
        >
          <Typography variant="h4" gutterBottom align="center">
            {t('forgotPassword.title')}
          </Typography>
          <Typography variant="body1" align="center" sx={{ mb: 2 }}>
            {t('forgotPassword.description')}
          </Typography>
 
          {(status === 'error' || status === 'success') && (
            <AlertMessage
              message={message}
              severity={status === 'success' ? 'success' : 'error'}
            />
          )}
 
          <Stack spacing={2} sx={{ mt: 2 }}>
            <TextField
              required
              fullWidth
              id="email"
              label={t('forgotPassword.email')}
              type="email"
              value={email}
              onChange={(e) => setEmail(e.target.value)}
              autoComplete="email"
              error={email.length > 0 && !validEmail}
              helperText={
                email.length > 0 && !validEmail
                  ? t('forgotPassword.invalidEmail') 
                  : ''
              }
            />
 
            <Button
              type="submit"
              variant="contained"
              fullWidth
              disabled={loading || !validEmail}
              startIcon={
                loading ? <CircularProgress color="inherit" size={16} /> : undefined
              }
            >
              {loading
                ? `${t('forgotPassword.buttonLoading')}…`
                : t('forgotPassword.button')}
            </Button>
 
            <Box sx={{ textAlign: 'center', mt: 1 }}>
              <Link
                component="button"
                variant="body2"
                onClick={() => navigate('/login')}
              >
                {t('forgotPassword.backToLogin')}
              </Link>
            </Box>
          </Stack>
        </Box>
      </PageWrapper>
    </>
  );
}