mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2024-11-13 11:13:50 +01:00
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:
parent
8d56c41dd6
commit
0fbb8553e6
@ -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,37 +18,6 @@ interface IBulkPurchaseTextProps {
|
|||||||
amount: string;
|
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 {
|
interface IBPProps {
|
||||||
onClose: () => void;
|
onClose: () => void;
|
||||||
mat: Material;
|
mat: Material;
|
||||||
@ -58,6 +27,41 @@ interface IBPProps {
|
|||||||
function BulkPurchaseSection(props: IBPProps): React.ReactElement {
|
function BulkPurchaseSection(props: IBPProps): React.ReactElement {
|
||||||
const corp = useCorporation();
|
const corp = useCorporation();
|
||||||
const [buyAmt, setBuyAmt] = useState("");
|
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 {
|
function bulkPurchase(): void {
|
||||||
try {
|
try {
|
||||||
@ -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>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user