diff --git a/src/grain/GrainTransaction.tsx b/src/grain/GrainTransaction.tsx index f39cdf4..66fd329 100644 --- a/src/grain/GrainTransaction.tsx +++ b/src/grain/GrainTransaction.tsx @@ -125,17 +125,19 @@ export default function GrainTransaction(props: Props) { .then(resp => { //let sourceOps: Option[] = sourceOptions let binOps: Option[] = []; - resp.data.bins.forEach(bin => { - let b = Bin.create(bin); - if (mainObject.key() !== b.key()) { - let op: Option = { - label: b.name(), - value: b, - group: "Bins" - }; - (!restrictMatching || grainTypesMatch(op.value, mainObject)) && binOps.push(op); - } - }); + if(resp.data.bins){ + resp.data.bins.forEach(bin => { + let b = Bin.create(bin); + if (mainObject.key() !== b.key()) { + let op: Option = { + label: b.name(), + value: b, + group: "Bins" + }; + (!restrictMatching || grainTypesMatch(op.value, mainObject) || op.value.grainName() === mainObject.grainName()) && binOps.push(op); + } + }); + } setBinOptions([...binOps]); }) .finally(() => { @@ -151,17 +153,19 @@ export default function GrainTransaction(props: Props) { .then(resp => { //let sourceOps: Option[] = sourceOptions let bagOps: Option[] = []; - resp.data.grainBags.forEach(bag => { - let b = GrainBag.create(bag); - if (mainObject.key() !== b.key()) { - let op: Option = { - label: b.name(), - value: b, - group: "Grain Bags" - }; - (!restrictMatching || grainTypesMatch(op.value, mainObject)) && bagOps.push(op); - } - }); + if(resp.data.grainBags){ + resp.data.grainBags.forEach(bag => { + let b = GrainBag.create(bag); + if (mainObject.key() !== b.key()) { + let op: Option = { + label: b.name(), + value: b, + group: "Grain Bags" + }; + (!restrictMatching || grainTypesMatch(op.value, mainObject) || op.value.grainName() === mainObject.grainName()) && bagOps.push(op); + } + }); + } setBagOptions([...bagOps]); }) .finally(() => { @@ -184,7 +188,7 @@ export default function GrainTransaction(props: Props) { value: f, group: "Fields" }; - (!restrictMatching || grainTypesMatch(op.value, mainObject)) && fieldOps.push(op); + (!restrictMatching || grainTypesMatch(op.value, mainObject) || op.value.grainName() === mainObject.grainName()) && fieldOps.push(op); }); setFieldOptions([...fieldOps]); }) @@ -356,13 +360,6 @@ export default function GrainTransaction(props: Props) { if (source.grain() === destination.grain()) { matching = true; } - } else if (source.storage() === pond.BinStorage.BIN_STORAGE_UNSUPPORTED_GRAIN) { - if ( - source.customType().toLowerCase() === destination.customType().toLowerCase() && - source.bushelsPerTonne() === destination.bushelsPerTonne() - ) { - matching = true; - } } } return matching; @@ -373,7 +370,7 @@ export default function GrainTransaction(props: Props) { if (grainTypesMatch(selectedSource.value, selectedDestination.value)) { updateInventory(); } else { - //open dialog saying that the destinations grain type will change from this action + //open dialog saying that the grain types do not match or because it is a custom type setGrainChangeDialog(true); } } else { @@ -381,6 +378,36 @@ export default function GrainTransaction(props: Props) { } }; + /** + * the function is used to determine what text to display to the user when the grain types dont match + * or when it sees any of the objects involved as having a custom grain type + * @param source the source of the grain + * @param destination the destination of the grain + * @returns the text describing the situation + */ + const confirmationText = () => { + if(selectedSource && selectedDestination){ + + let source = selectedSource?.value + let destination = selectedDestination?.value + //both are custom + if(source.storage() === pond.BinStorage.BIN_STORAGE_UNSUPPORTED_GRAIN && destination.storage() === pond.BinStorage.BIN_STORAGE_UNSUPPORTED_GRAIN){ + return "Both source and destination are using custom grain types, they may not match. Would you like to continue with the transaction?" + } + //source is custom destination is not + if(source.storage() === pond.BinStorage.BIN_STORAGE_UNSUPPORTED_GRAIN){ + return "The source is using a custom grain type and may not match the destination. Would you like to continue with the transaction?" + } + //destination is custom source is not + if(destination.storage() === pond.BinStorage.BIN_STORAGE_UNSUPPORTED_GRAIN){ + return "The destination is using a custom grain type and may not match the source. Would you like to continue with the transaction?" + } + } + + //neither are custom + return "The Grain types do not match. Are you sure you would like to perform this transaction?" + } + const grainChange = () => { return ( - The grain type of the destination may change as a result of this action. + {confirmationText()}