From 0fbb8553e6a1949d06bf8987c0848c47ccea16c4 Mon Sep 17 00:00:00 2001 From: phyzical Date: Sat, 2 Apr 2022 19:38:26 +0800 Subject: [PATCH] add fix for bulk buy when purchase amount exceeds warehouse size * add logic to disable button when input is invalid --- src/Corporation/Actions.ts | 2 +- .../ui/modals/PurchaseMaterialModal.tsx | 70 ++++++++++--------- 2 files changed, 39 insertions(+), 33 deletions(-) diff --git a/src/Corporation/Actions.ts b/src/Corporation/Actions.ts index 2eae1bfa7..591892659 100644 --- a/src/Corporation/Actions.ts +++ b/src/Corporation/Actions.ts @@ -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; diff --git a/src/Corporation/ui/modals/PurchaseMaterialModal.tsx b/src/Corporation/ui/modals/PurchaseMaterialModal.tsx index e4b52535e..d0f674ff5 100644 --- a/src/Corporation/ui/modals/PurchaseMaterialModal.tsx +++ b/src/Corporation/ui/modals/PurchaseMaterialModal.tsx @@ -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 ( - <> - Not enough warehouse space to purchase this amount - - ); - } else if (isNaN(cost) || parsedAmt < 0) { - return ( - <> - Invalid put for Bulk Purchase amount - - ); - } else { - return ( - <> - - Purchasing {numeralWrapper.format(parsedAmt, "0,0.00")} of {props.mat.name} will cost{" "} - {numeralWrapper.formatMoney(cost)} - - - ); - } -} - 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 ( + <> + Not enough warehouse space to purchase this amount + + ); + } else if (isNaN(cost) || parsedAmt < 0) { + setDisabled(true); + return ( + <> + Invalid input for Bulk Purchase amount + + ); + } else { + setDisabled(false); + return ( + <> + + Purchasing {numeralWrapper.format(parsedAmt, "0,0.00")} of {props.mat.name} will cost{" "} + {numeralWrapper.formatMoney(cost)} + + + ); + } + } function bulkPurchase(): void { try { @@ -90,7 +94,9 @@ function BulkPurchaseSection(props: IBPProps): React.ReactElement { placeholder="Bulk Purchase amount" onKeyDown={onKeyDown} /> - + ); }