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) { if (isNaN(amt) || amt < 0) {
throw new Error(`Invalid input amount`); 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`); throw new Error(`You do not have enough warehouse size to fit this purchase`);
} }
const cost = amt * material.bCost; const cost = amt * material.bCost;

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