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 | 2x 111x 2x 211x 211x 10x 10x 211x 17x 17x 1x 16x 17x 248x 211x 211x 111x 211x 7x 12x 1x 6x 1x 5x 5x 8x 6x 5x 7x 211x 111x 17x | import React, { useState } from "react";
import { Modal, Button, Form, Table } from "react-bootstrap";
import { toast } from "react-toastify";
// What one cow from a herd sells for: the cow price scaled by the herd's
// health (as a percentage), minus the selling fee. Matches the backend.
const cowSaleValue = (game, health) =>
(game.cowPrice * health) / 100 - (game.sellingFee ?? 0);
const SellHerdsModal = ({ isOpen, onClose, herds = [], game, onSell }) => {
// Map of herd id -> number of cows to sell from that herd. A herd with an
// empty input has no entry.
const [counts, setCounts] = useState({});
const handleClose = () => {
setCounts({});
onClose();
};
// Keep each entry between 0 and the number of cows in that herd.
const handleChange = (herd, value) => {
const next = { ...counts };
if (value === "") {
delete next[herd.id];
} else {
next[herd.id] = Math.min(Math.max(Number(value), 0), herd.numCows);
}
setCounts(next);
};
const countFor = (herd) => counts[herd.id] ?? 0;
const totalCows = herds.reduce((sum, herd) => sum + countFor(herd), 0);
const totalValue = herds.reduce(
(sum, herd) => sum + countFor(herd) * cowSaleValue(game, herd.health),
0,
);
const handleSubmit = (e) => {
e.preventDefault();
if (herds.some((herd) => !Number.isInteger(countFor(herd)))) {
toast.error(
"You cannot sell a fractional number of cows! Please enter a whole number.",
);
} else if (totalCows < 1) {
toast.error("You must sell at least 1 cow.");
} else {
const herdCounts = {};
herds
.filter((herd) => countFor(herd) > 0)
.forEach((herd) => {
herdCounts[herd.id] = countFor(herd);
});
onSell(herdCounts);
}
handleClose();
};
return (
<Modal data-testid="sell-herds-modal" show={isOpen} onHide={handleClose}>
<Modal.Header closeButton>
<Modal.Title>Sell cows</Modal.Title>
</Modal.Header>
<Modal.Body>
{herds.length === 0 ? (
<p>You have no herds to sell.</p>
) : (
<Form onSubmit={handleSubmit}>
<Form.Label>
Choose how many cows to sell from each herd.
</Form.Label>
<Table bordered size="sm" aria-label="Sell from your herds">
<thead>
<tr>
<th scope="col">Herd</th>
<th scope="col">Cows</th>
<th scope="col">Health</th>
<th scope="col">Sell</th>
</tr>
</thead>
<tbody>
{herds.map((herd) => (
<tr key={herd.id}>
<th scope="row">Herd #{herd.id}</th>
<td>{herd.numCows}</td>
<td>{herd.health.toFixed(2)}%</td>
<td>
<Form.Control
data-testid={`sell-herds-modal-input-${herd.id}`}
aria-label={`Cows to sell from Herd #${herd.id}`}
type="number"
min="0"
max={herd.numCows}
// Stryker disable next-line StringLiteral: a number input shows any non-number text as empty
value={counts[herd.id] ?? ""}
placeholder="0"
onChange={(e) => handleChange(herd, e.target.value)}
/>
</td>
</tr>
))}
</tbody>
</Table>
<Form.Text data-testid="sell-herds-modal-total">
Total sale value: ${totalValue.toFixed(2)}
</Form.Text>
</Form>
)}
</Modal.Body>
<Modal.Footer>
<Button
data-testid="sell-herds-modal-cancel"
variant="secondary"
onClick={handleClose}
>
Cancel
</Button>
<Button
data-testid="sell-herds-modal-submit"
variant="primary"
onClick={handleSubmit}
>
Sell
</Button>
</Modal.Footer>
</Modal>
);
};
export default SellHerdsModal;
|