All files / components/Game ManageCowsModal.jsx

100% Statements 34/34
100% Branches 18/18
100% Functions 7/7
100% Lines 34/34

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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161          2x                     98x 98x 5x 2x   5x     98x 13x   13x 2x     2x     11x 3x 3x       8x 2x     2x     6x 2x   4x 1x 1x   3x     5x 5x     98x 4x 4x     98x 64x     34x                                                                               5x     17x   13x                                                                                          
import React from "react";
import { Modal, Button, Form } from "react-bootstrap";
import { toast } from "react-toastify";
import "react-toastify/dist/ReactToastify.css";
 
const ManageCowsModal = ({
  number,
  setNumber,
  isOpen,
  onClose,
  message,
  farmer,
  onBuy,
  onSell,
  herds,
}) => {
  const [selectedHerdId, setSelectedHerdId] = React.useState("");
  const handleInputChange = (e) => {
    if (e.target.value < 0) {
      toast.warn(`Warning: You cannot ${message} a negative number of cows!`);
    }
    setNumber(e.target.value);
  };
 
  const handleSubmit = (e) => {
    e.preventDefault();
 
    if (Number(number) < 0) {
      window.alert(
        `You cannot ${message} a negative number of cows! Please enter a non-negative number.`,
      );
      return;
    }
 
    if (Number(number) === 0) {
      window.alert(`You must ${message} at least one cow!`);
      return;
    }
 
    // Cows must be purchased or sold in whole numbers.
    if (!Number.isInteger(Number(number))) {
      window.alert(
        `You cannot ${message} a fraction of a cow! Please enter a whole number.`,
      );
      return;
    }
 
    if (message.includes("buy")) {
      onBuy(farmer, number);
    } else {
      if (!selectedHerdId) {
        window.alert("Please select a herd first!");
        return;
      }
      onSell(farmer, number, Number(selectedHerdId));
    }
 
    setNumber(1);
    onClose(); // Close the modal
  };
 
  const handleClose = () => {
    setNumber(1);
    onClose();
  };
 
  if (!isOpen) {
    return null;
  }
 
  return (
    <Modal
      data-testid={"buy-sell-cow-modal"}
      show={isOpen}
      onHide={handleClose}
    >
      <Modal.Body>
        <Form onSubmit={handleSubmit} noValidate>
          <Form.Group
            data-testid={"buy-sell-cow-modal-closeGroup"}
            style={{ display: "flex", justifyContent: "right" }}
          >
            <Button
              data-testid={"buy-sell-cow-modal-close"}
              style={{
                color: "#000",
                borderColor: "#fff",
                backgroundColor: "#fff",
              }}
              type="button"
              className="close"
              aria-label="Close"
              onClick={handleClose}
            >
              <span aria-hidden="true">&times;</span>
            </Button>
          </Form.Group>
          <Form.Group>
            <Form.Label>
              Please specify the number of cows you'd like to {message} below.
            </Form.Label>
          </Form.Group>
 
          {message === "sell" && (
            <Form.Group className="mb-3">
              <Form.Label>Select a herd:</Form.Label>
              <Form.Control
                as="select"
                data-testid="sell-herd-select"
                value={selectedHerdId}
                onChange={(e) => setSelectedHerdId(e.target.value)}
              >
                <option value="">Choose a herd</option>
                {(herds?.filter((herd) => herd.numOfCows > 0) ?? []).map(
                  (herd) => (
                    <option key={herd.id} value={herd.id}>
                      Herd {herd.id} - {herd.numOfCows} cows - {herd.cowHealth}%
                      health
                    </option>
                  ),
                )}
              </Form.Control>
            </Form.Group>
          )}
          <Form.Group>
            <Form.Control
              data-testid={"buy-sell-cow-modal-input"}
              style={{ width: "20%" }}
              type="number"
              value={number}
              onChange={handleInputChange}
              min="1"
            />
          </Form.Group>
        </Form>
      </Modal.Body>
      <Modal.Footer
        data-testid={"buy-sell-cow-modal-footer"}
        style={{ borderTop: "0px" }}
      >
        <Button
          data-testid={"buy-sell-cow-modal-cancel"}
          variant="secondary"
          onClick={handleClose}
        >
          Cancel
        </Button>
        <Button
          data-testid={"buy-sell-cow-modal-submit"}
          variant="primary"
          onClick={handleSubmit}
        >
          {message[0].toUpperCase() + message.slice(1)}
        </Button>
      </Modal.Footer>
    </Modal>
  );
};
 
export default ManageCowsModal;