All files / src/components/Profile/Modals EmailEditModal.tsx

22.22% Statements 2/9
0% Branches 0/29
0% Functions 0/3
22.22% Lines 2/9

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      4x                     4x                                                                                                                                                                                                                                              
import React, { FC } from 'react';
import { useTranslation } from 'react-i18next';
import { Modal } from '../../Modal';
const styles = require('./Modal.module.css')
import { Button, Input, Loading } from 'ag-ems-ui-library';
import { useForm } from 'react-hook-form';
 
export const EmailEditModal: FC<{
  isOpen: boolean;
  onClose: () => void;
  email: string;
  onSubmit?: (email: string, password: string) => void;
  errorMessage?: string;
  preloader?: boolean;
}> = ({
  isOpen = false,
  onClose,
  email,
  onSubmit,
  errorMessage,
  preloader
}) => {
  const { t } = useTranslation();
 
  const { register, handleSubmit, getValues, formState: { isDirty, errors } } = useForm({
    reValidateMode: 'onBlur',
    defaultValues: {
      newEmail: '',
      newEmailConfirm: '',
      password: ''
    }
  });
 
  const handleRegister = ({ newEmail, password }) => {
    if (newEmail && password && newEmail !== email) {
      onSubmit && onSubmit(newEmail, password);
    }
  };
 
  return (
    <Modal
      isOpen={isOpen}
      onClose={onClose}
      header={<h2 className={styles.ModalHeader}>{t('profile.updateUserEmail')}</h2>}
    >
      <div className={styles.ModalContent}>
        {preloader && <Loading color="#007c00" />}
 
        <dl>
          <dt>{t('profile.currentEmail')}</dt>
          <dd>{email}</dd>
        </dl>
 
        <form className={styles.ModalForm} onSubmit={handleSubmit(handleRegister)} noValidate={true}>
          <div className={styles.ModalFormField}>
            <Input
              name="newEmail"
              id="user-email"
              type="email"
              label={t('form.newEmail')}
              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.newEmail ? 'errorInput' : ''}
            />
            {errors.newEmail && errors.newEmail.message && (
              <span className="errorText">{errors.newEmail.message}</span>
            )}
          </div>
          <div className={styles.ModalFormField}>
            <Input
              name="newEmailConfirm"
              id="user-email-confirm"
              type="email"
              label={t('form.newEmailConfirm')}
              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')
                },
                validate: value =>
                  value === getValues('newEmail') ||
                  (t('error.confirmed.email') as string)
              })}
              classes={errors.newEmailConfirm ? 'errorInput' : ''}
            />
            {errors.newEmailConfirm && errors.newEmailConfirm.message && (
              <span className="errorText">{errors.newEmailConfirm.message}</span>
            )}
          </div>
          <div className={styles.ModalFormField}>
            <Input
              name="password"
              id="user-password"
              type="password"
              label={t('password')}
              required={true}
              ref={register({ required: t('error.required.password') as string })}
              classes={errors.password ? 'errorInput' : ''}
            />
            {errors.password && errors.password.message && (
              <span className="errorText">{errors.password.message}</span>
            )}
          </div>
 
          <div className={styles.ModalFormButtons}>
            {errorMessage && (
              <div className="mb-1">
                <p className="errorText">{errorMessage}</p>
              </div>
            )}
            <Button
              classes={styles.ModalFormSubmit}
              type="submit"
              variant="ks-primary"
              sizing="lg"
              value={t('save')}
              fullWidth={true}
              disabled={!isDirty}
            />
          </div>
        </form>
      </div>
    </Modal>
  );
};