Skip to content

Commit

Permalink
Don't let user select same cylinder twice
Browse files Browse the repository at this point in the history
Also, when a new filling row is created, don't select
default cylinder for it.
  • Loading branch information
ilesoft committed Oct 17, 2023
1 parent 7c4f637 commit dd0221f
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions src/components/BlenderLogbook/components/FillingTile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,18 @@ const FillingEventRowComponent: React.FC<FillingEventRowProps> = ({
(sc) => sc.id === storageCylinderId
);

// User has managed to select cylinder that has invalid id from the dropdown menu
// => programming error
if (storageCylinder === undefined) {
throw new Error('Storage cylinder not found!');
}

const gasPriceEurCents = gases.find(
(price) => price.gasId === storageCylinder.gasId
(price) => price.gasId === storageCylinder?.gasId
)?.priceEurCents;
const storageCylinderVolume = storageCylinder.volume;
const storageCylinderVolume = storageCylinder?.volume;

// Calculate price and consumption when row values change
useEffect(() => {
setFieldValue(
`fillingEventRows.${index}.priceEurCents`,
formatEurCentsToEur(
calculateGasConsumption(
storageCylinderVolume,
storageCylinderVolume ?? 0,
startPressure ?? 0,
endPressure ?? 0
) * (gasPriceEurCents ?? 0)
Expand All @@ -85,7 +79,7 @@ const FillingEventRowComponent: React.FC<FillingEventRowProps> = ({
setFieldValue(
`fillingEventRows.${index}.consumption`,
calculateGasConsumption(
storageCylinderVolume,
storageCylinderVolume ?? 0,
startPressure ?? 0,
endPressure ?? 0
)
Expand All @@ -107,7 +101,13 @@ const FillingEventRowComponent: React.FC<FillingEventRowProps> = ({
errorText={errors.fillingEventRows?.at(index)?.storageCylinderId}
>
{storageCylinders.map((sc) => (
<option key={sc.id} value={sc.id}>
<option
disabled={values.fillingEventRows.some(
(row) => row.storageCylinderId === sc.id
)}
key={sc.id}
value={sc.id}
>
{sc.name} (
{mapGasToName(
gases.find((gas) => gas.gasId === sc.gasId)?.gasName
Expand Down Expand Up @@ -143,7 +143,6 @@ const FillingEventRowComponent: React.FC<FillingEventRowProps> = ({
index === 0 && values.fillingEventRows.length === 1
? replace(index, {
...EMPTY_FILLING_EVENT_ROW,
storageCylinderId: storageCylinders[0].id,
})
: remove(index)
}
Expand All @@ -155,7 +154,6 @@ const FillingEventRowComponent: React.FC<FillingEventRowProps> = ({
onClick={() =>
push({
...EMPTY_FILLING_EVENT_ROW,
storageCylinderId: storageCylinders[0].id,
})
}
type={ButtonType.button}
Expand Down

0 comments on commit dd0221f

Please sign in to comment.