All files / main/components/Users RoleEmailAddModal.jsx

100% Statements 15/15
100% Branches 9/9
100% Functions 3/3
100% Lines 15/15

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            3x                   196x             196x       196x 44x     196x 11x     196x                                                                                           3x 3x 3x 3x 3x 3x 3x  
import { useEffect } from "react";
import { Modal, Button, Form } from "react-bootstrap";
import { useForm } from "react-hook-form";
import PropTypes from "prop-types";
 
// Stryker disable next-line Regex
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
 
export default function RoleEmailAddModal({
  show,
  onHide,
  onSubmitEmail,
  title = "Add Email",
  buttonLabel = "Add",
  serverError = null,
}) {
  const testIdPrefix = "RoleEmailAddModal";
 
  const {
    register,
    handleSubmit,
    reset,
    formState: { errors },
  } = useForm();
 
  // Clear the form each time the modal opens or closes, so a previous
  // submission's value/errors don't linger.
  useEffect(() => {
    reset();
  }, [show, reset]);
 
  const onSubmit = (data) => {
    onSubmitEmail(data.email);
  };
 
  return (
    <Modal show={show} onHide={onHide} centered data-testid={testIdPrefix}>
      <Form onSubmit={handleSubmit(onSubmit)}>
        <Modal.Header closeButton>
          <Modal.Title>{title}</Modal.Title>
        </Modal.Header>
        <Modal.Body>
          <Form.Group className="mb-3">
            <Form.Label htmlFor="email">Email</Form.Label>
            <Form.Control
              data-testid={testIdPrefix + "-email"}
              id="email"
              type="text"
              isInvalid={Boolean(errors.email) || Boolean(serverError)}
              {...register("email", {
                required: "Email is required.",
                pattern: emailRegex,
              })}
            />
            <Form.Control.Feedback type="invalid">
              {errors.email && "A valid email is required."}
            </Form.Control.Feedback>
          </Form.Group>
          {serverError && (
            <div className="text-danger" data-testid={testIdPrefix + "-error"}>
              {serverError}
            </div>
          )}
        </Modal.Body>
        <Modal.Footer>
          <Button
            variant="secondary"
            onClick={onHide}
            data-testid={testIdPrefix + "-cancel"}
          >
            Cancel
          </Button>
          <Button type="submit" data-testid={testIdPrefix + "-submit"}>
            {buttonLabel}
          </Button>
        </Modal.Footer>
      </Form>
    </Modal>
  );
}
 
RoleEmailAddModal.propTypes = Object.create(null);
RoleEmailAddModal.propTypes.show = PropTypes.bool.isRequired;
RoleEmailAddModal.propTypes.onHide = PropTypes.func.isRequired;
RoleEmailAddModal.propTypes.onSubmitEmail = PropTypes.func.isRequired;
RoleEmailAddModal.propTypes.title = PropTypes.string;
RoleEmailAddModal.propTypes.buttonLabel = PropTypes.string;
RoleEmailAddModal.propTypes.serverError = PropTypes.string;