add fix for bulk buy when purchase amount exceeds warehouse size

* add logic to disable button when input is invalid
This commit is contained in:
phyzical 2022-04-02 19:38:26 +08:00
parent 8d56c41dd6
commit 0fbb8553e6
2 changed files with 39 additions and 33 deletions

@ -260,7 +260,7 @@ export function BulkPurchase(corp: ICorporation, warehouse: Warehouse, material:
if (isNaN(amt) || amt < 0) {
throw new Error(`Invalid input amount`);
}
if (amt * matSize <= maxAmount) {
if (amt >= maxAmount) {
throw new Error(`You do not have enough warehouse size to fit this purchase`);
}
const cost = amt * material.bCost;

@ -18,37 +18,6 @@ interface IBulkPurchaseTextProps {
amount: string;
}
function BulkPurchaseText(props: IBulkPurchaseTextProps): React.ReactElement {
const parsedAmt = parseFloat(props.amount);
const cost = parsedAmt * props.mat.bCost;
const matSize = MaterialSizes[props.mat.name];
const maxAmount = (props.warehouse.size - props.warehouse.sizeUsed) / matSize;
if (parsedAmt * matSize > maxAmount) {
return (
<>
<Typography color={"error"}>Not enough warehouse space to purchase this amount</Typography>
</>
);
} else if (isNaN(cost) || parsedAmt < 0) {
return (
<>
<Typography color={"error"}>Invalid put for Bulk Purchase amount</Typography>
</>
);
} else {
return (
<>
<Typography>
Purchasing {numeralWrapper.format(parsedAmt, "0,0.00")} of {props.mat.name} will cost{" "}
{numeralWrapper.formatMoney(cost)}
</Typography>
</>
);
}
}
interface IBPProps {
onClose: () => void;
mat: Material;
@ -58,6 +27,41 @@ interface IBPProps {
function BulkPurchaseSection(props: IBPProps): React.ReactElement {
const corp = useCorporation();
const [buyAmt, setBuyAmt] = useState("");
const [disabled, setDisabled] = useState(false);
function BulkPurchaseText(props: IBulkPurchaseTextProps): React.ReactElement {
const parsedAmt = parseFloat(props.amount);
const cost = parsedAmt * props.mat.bCost;
const matSize = MaterialSizes[props.mat.name];
const maxAmount = (props.warehouse.size - props.warehouse.sizeUsed) / matSize;
if (parsedAmt * matSize > maxAmount) {
setDisabled(true);
return (
<>
<Typography color={"error"}>Not enough warehouse space to purchase this amount</Typography>
</>
);
} else if (isNaN(cost) || parsedAmt < 0) {
setDisabled(true);
return (
<>
<Typography color={"error"}>Invalid input for Bulk Purchase amount</Typography>
</>
);
} else {
setDisabled(false);
return (
<>
<Typography>
Purchasing {numeralWrapper.format(parsedAmt, "0,0.00")} of {props.mat.name} will cost{" "}
{numeralWrapper.formatMoney(cost)}
</Typography>
</>
);
}
}
function bulkPurchase(): void {
try {
@ -90,7 +94,9 @@ function BulkPurchaseSection(props: IBPProps): React.ReactElement {
placeholder="Bulk Purchase amount"
onKeyDown={onKeyDown}
/>
<Button onClick={bulkPurchase}>Confirm Bulk Purchase</Button>
<Button disabled={disabled} onClick={bulkPurchase}>
Confirm Bulk Purchase
</Button>
</>
);
}