All files / components/Chat ChatCooldownModal.jsx

100% Statements 18/18
100% Branches 6/6
100% Functions 4/4
100% Lines 18/18

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      1x   1x 28x 28x 28x   28x 5x 5x 5x     28x 7x 7x 5x 5x   2x 2x     28x                                 7x                                                    
import React, { useState } from "react";
import { Modal, Button, Form } from "react-bootstrap";
 
const DEFAULT_MINUTES = "5";
 
const ChatCooldownModal = ({ show, username, onClose, onSubmit }) => {
  const testId = "ChatCooldownModal";
  const [minutes, setMinutes] = useState(DEFAULT_MINUTES);
  const [error, setError] = useState("");
 
  const handleClose = () => {
    setMinutes(DEFAULT_MINUTES);
    setError("");
    onClose();
  };
 
  const handleSubmit = () => {
    const value = Number(minutes);
    if (!Number.isInteger(value) || value <= 0) {
      setError("Please enter a whole number of minutes greater than 0.");
      return;
    }
    onSubmit(value * 60);
    handleClose();
  };
 
  return (
    <Modal data-testid={testId} show={show} onHide={handleClose}>
      <Modal.Header closeButton>
        <Modal.Title data-testid={`${testId}-title`}>
          {username ? `Chat cooldown for ${username}` : "Chat cooldown"}
        </Modal.Title>
      </Modal.Header>
      <Modal.Body>
        <Form.Label htmlFor="chatCooldownMinutes">
          Cooldown duration (minutes)
        </Form.Label>
        <Form.Control
          id="chatCooldownMinutes"
          data-testid={`${testId}-minutes`}
          type="number"
          min="1"
          value={minutes}
          onChange={(e) => setMinutes(e.target.value)}
          isInvalid={error !== ""}
        />
        <Form.Control.Feedback type="invalid">{error}</Form.Control.Feedback>
      </Modal.Body>
      <Modal.Footer>
        <Button
          data-testid={`${testId}-cancel`}
          variant="secondary"
          onClick={handleClose}
        >
          Cancel
        </Button>
        <Button
          data-testid={`${testId}-submit`}
          variant="warning"
          onClick={handleSubmit}
        >
          Set cooldown
        </Button>
      </Modal.Footer>
    </Modal>
  );
};
 
export default ChatCooldownModal;