All files / src/components/CreatePassword index.tsx

10% Statements 2/20
0% Branches 0/34
0% Functions 0/5
10% Lines 2/20

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  4x               4x                                                                                                                                                                                                                                      
import React, { FC, useState } from 'react';
const styles = require('./CreatePassword.module.css')
import { Button, Input, Loading } from 'ag-ems-ui-library';
import { validatePassword } from '../../helpers';
import { useForm } from 'react-hook-form';
import { useTranslation } from 'react-i18next';
import { useUserContext } from '../../contexts';
import { navigate } from 'gatsby';
 
export const CreatePasswordForm: FC<{ token: string | null | undefined }> = ({ token }) => {
  const { t } = useTranslation();
  const [errorMessage, setErrorMessage] = useState('');
  const { resetPassword } = useUserContext();
  const [preloader, setPreloader] = useState(false);
 
  const { register, handleSubmit, getValues, formState: { isDirty, errors } } = useForm({
    reValidateMode: 'onBlur',
    defaultValues: {
      email: '',
      newPassword: '',
      newPasswordConfirm: ''
    }
  });
 
  const handleRegister = ({ email, newPassword }) => {
    if (email && newPassword && token) {
      setPreloader(true);
      setErrorMessage('');
      resetPassword(email, token, newPassword).then(({data, errors}) => {
        if (data.resetPassword) {
          navigate('/profile');
        } else if (errors && errors.length) {
          setErrorMessage(errors[0].message);
        }
        setPreloader(false);
      });
    }
  };
 
  return <>
    {preloader && <Loading color="#007c00" />}
 
    <form className={styles.CreatePasswordForm} onSubmit={handleSubmit(handleRegister)} noValidate={true}>
      <div className={styles.CreatePasswordFormField}>
        <Input
          name="email"
          id="user-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>
 
      <div className={styles.CreatePasswordFormField}>
        <Input
          name="newPassword"
          id="user-new-password"
          type="password"
          label={t('form.newPassword')}
          required={true}
          ref={register({
            required: t('error.required.password') as string,
            minLength: {
              value: 8,
              message: t('error.minLength.password')
            },
            validate: value => validatePassword(value) || (t('error.classesOfCharacters.password') as string)
          })}
          classes={errors.newPassword ? 'errorInput' : ''}
        />
        {errors.newPassword && errors.newPassword.message && (
          <span className="errorText">{errors.newPassword.message}</span>
        )}
      </div>
 
      <div className={styles.CreatePasswordFormField}>
        <Input
          name="newPasswordConfirm"
          id="user-new-password-confirm"
          type="password"
          label={t('form.newPasswordConfirm')}
          required={true}
          ref={register({
            required: t('error.required.password') as string,
            validate: value =>
              value === getValues('newPassword') ||
              (t('error.confirmed.password') as string)
          })}
          classes={errors.newPasswordConfirm ? 'errorInput' : ''}
        />
        {errors.newPasswordConfirm && errors.newPasswordConfirm.message && (
          <span className="errorText">{errors.newPasswordConfirm.message}</span>
        )}
      </div>
 
      <div className={styles.CreatePasswordFormButtons}>
        {errorMessage && (
          <div className="mb-1">
            <p className="errorText">{errorMessage}</p>
          </div>
        )}
        <Button
          classes={styles.CreatePasswordFormSubmit}
          type="submit"
          variant="ks-primary"
          sizing="lg"
          value={t('confirm')}
          fullWidth={true}
          disabled={!isDirty}
        />
      </div>
    </form>
  </>;
};