All files / components/Chat ChatCooldownModal.jsx

100% Statements 13/13
100% Branches 5/5
100% Functions 5/5
100% Lines 13/13

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      2x   2x 21x 21x   21x   21x   21x 6x 6x     21x                                   7x           2x     63x                                                                          
import React, { useState } from "react";
import { Modal, Button, Form } from "react-bootstrap";
 
const UNIT_SECONDS = { seconds: 1, minutes: 60, hours: 3600 };
 
const ChatCooldownModal = ({ username, onClose, onConfirm }) => {
  const [amount, setAmount] = useState("5");
  const [unit, setUnit] = useState("minutes");
 
  const parsed = Number(amount);
  // Number("") is 0, so reject empty input explicitly
  const isValid = amount !== "" && Number.isInteger(parsed) && parsed >= 0;
 
  const handleConfirm = () => {
    onConfirm(parsed * UNIT_SECONDS[unit]);
    onClose();
  };
 
  return (
    <Modal show onHide={onClose} data-testid="ChatCooldownModal">
      <Modal.Header closeButton>
        <Modal.Title>Chat cooldown for {username}</Modal.Title>
      </Modal.Header>
      <Modal.Body>
        <Form.Label htmlFor="ChatCooldownModal-amount">
          How long should this user be unable to send chat messages?
        </Form.Label>
        <div className="d-flex gap-2">
          <Form.Control
            id="ChatCooldownModal-amount"
            data-testid="ChatCooldownModal-amount"
            type="number"
            min="0"
            step="1"
            value={amount}
            isInvalid={!isValid}
            onChange={(e) => setAmount(e.target.value)}
          />
          <Form.Select
            aria-label="Cooldown unit"
            data-testid="ChatCooldownModal-unit"
            value={unit}
            onChange={(e) => setUnit(e.target.value)}
          >
            {Object.keys(UNIT_SECONDS).map((u) => (
              <option key={u} value={u}>
                {u}
              </option>
            ))}
          </Form.Select>
        </div>
        {!isValid && (
          <div
            className="text-danger mt-1"
            data-testid="ChatCooldownModal-error"
          >
            Enter a whole number 0 or greater (0 clears the cooldown).
          </div>
        )}
      </Modal.Body>
      <Modal.Footer>
        <Button
          variant="secondary"
          data-testid="ChatCooldownModal-cancel"
          onClick={onClose}
        >
          Cancel
        </Button>
        <Button
          variant="warning"
          data-testid="ChatCooldownModal-confirm"
          disabled={!isValid}
          onClick={handleConfirm}
        >
          Start cooldown
        </Button>
      </Modal.Footer>
    </Modal>
  );
};
 
export default ChatCooldownModal;