All files / src/components/AuthModal RequestResetPasswordForm.tsx

20% Statements 2/10
0% Branches 0/13
0% Functions 0/2
20% Lines 2/10

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    5x               5x                                                                                                                              
import React, { FC, useState } from 'react';
import { Button, Input } from 'ag-ems-ui-library';
const styles = require('./AuthModal.module.css')
import { useTranslation } from 'react-i18next';
import { useForm } from 'react-hook-form';
import { useUserContext } from '../../contexts';
 
export const RequestResetPasswordForm: FC<{
  closeHandler?: () => void;
  onBackToLogin?: () => void;
}> = ({ closeHandler, onBackToLogin }) => {
  const { t } = useTranslation();
  const { sendResetPasswordEmail, error } = useUserContext();
  const { register, handleSubmit, errors } = useForm({
    reValidateMode: 'onBlur'
  });
  const [sent, setSent] = useState<boolean | null>(null);
 
  const handleResetPassword = async ({ email }) => {
    if (email) {
      setSent(await sendResetPasswordEmail(email));
    }
  }
 
  return (
    <form className={styles.AuthModalForm} onSubmit={handleSubmit(handleResetPassword)}>
      <div className={styles.AuthModalField}>
        <Input
          name="email"
          id="reset-email"
          type="email"
          label={t('email')}
          required={true}
          ref={register({
            required: t('error.required.email') as string,
            pattern: {
              value: /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i,
              message: t('error.pattern.email')
            }
          })}
          classes={errors.email ? 'errorInput' : ''}
        />
        {errors.email && errors.email.message && (
          <span className="errorText">{errors.email.message}</span>
        )}
      </div>
      {onBackToLogin && <div className={styles.AuthModalForgotPassword}>
        <button type="button" onClick={onBackToLogin}>
          {t('backToLogin')}
        </button>
      </div>}
      <div className={styles.AuthModalSubmit}>
        {error.sendResetPasswordEmailError && (
          <div className="mb-1">
            <p className="errorText">{error.sendResetPasswordEmailError}</p>
          </div>
        )}
        {sent && (
          <div className="col">
            <p className="success my-1">{t('resetPassword.emailSent')}</p>
          </div>
        )}
        <Button
          type="submit"
          variant="ks-primary"
          sizing="lg"
          value={t('send_new_password')}
          rounded={true}
          fullWidth={true}
        />
      </div>
    </form>
  );
}