Skip to content

Commit

Permalink
Allow user to create air fills in Happihäkki (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
Akzuu authored Aug 10, 2024
1 parent 33d72ca commit d4b4ece
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 58 deletions.
11 changes: 8 additions & 3 deletions src/components/BlenderLogbook/BlenderLogbook.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { FillingTile } from './components/FillingTile';
import { SavingTile } from './components/SavingTile';
import {
AvailableMixtureCompositions,
type AvailableMixtures,
AvailableMixtures,
formalizeGasMixture,
formatEurToEurCents,
} from '../../lib/utils';
Expand Down Expand Up @@ -60,7 +60,7 @@ const EMPTY_FILLING_EVENT_BASIC_INFO: FillingEventBasicInfo = {
divingCylinderSetId: '',
heliumPercentage: '0',
gasMixture: AvailableMixtureCompositions[0].id,
oxygenPercentage: '0',
oxygenPercentage: '21',
userConfirm: false,
compressorId: '',
};
Expand Down Expand Up @@ -112,11 +112,16 @@ export const NewBlenderFillingEvent: React.FC<NewFillingEventProps> = ({
.reduce((partialSum, price) => partialSum + price, 0),
);

// Allow user to do pure air fills via Happihäkki page
const filledAir =
values.gasMixture === AvailableMixtures.Nitrox &&
values.fillingEventRows.length === 0;

fillEventMutation.mutate(
{
cylinderSetId: values.divingCylinderSetId,
description: values.additionalInformation,
filledAir: false,
filledAir,
gasMixture: formalizedGasMixture,
price: totalPriceEurCents,
storageCylinderUsageArr:
Expand Down
43 changes: 14 additions & 29 deletions src/components/BlenderLogbook/components/FillingTile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,24 @@ import { type CommonTileProps, emptyFillingRow } from '../BlenderLogbook';

type FillingEventRowProps = CommonTileProps & {
index: number;
replace: (index: number, newValue: unknown) => void;
remove: (index: number) => void;
push: (value: unknown) => void;
setFieldValue: (
field: string,
value: unknown,
shouldValidate?: boolean,
) => void;
storageCylinders: StorageCylinder[];
gases: GasWithPricing[];
lastItem: boolean;
};

const FillingEventRowComponent: React.FC<FillingEventRowProps> = ({
index,
errors,
values,
replace,
remove,
push,
setFieldValue,
storageCylinders,
gases,
lastItem,
}) => {
const startPressure = values.fillingEventRows.at(index)?.startPressure;
const endPressure = values.fillingEventRows.at(index)?.endPressure;
Expand Down Expand Up @@ -106,11 +100,7 @@ const FillingEventRowComponent: React.FC<FillingEventRowProps> = ({
disabled={values.userConfirm}
element={<BsTrash />}
onClick={() => {
index === 0 && values.fillingEventRows.length === 1
? replace(index, {
...emptyFillingRow(),
})
: remove(index);
remove(index);
}}
/>
</div>
Expand Down Expand Up @@ -164,20 +154,6 @@ const FillingEventRowComponent: React.FC<FillingEventRowProps> = ({
unit="€"
/>
</div>
{lastItem ? (
<div className={styles.addRow}>
<PrimaryButton
disabled={values.userConfirm}
onClick={() => {
push({
...emptyFillingRow(),
});
}}
type={ButtonType.button}
text="Lisää uusi rivi"
/>
</div>
) : null}
</div>
);
};
Expand All @@ -203,23 +179,32 @@ export const FillingTile: React.FC<FillingTileProps> = ({
<div className="pt-3 pb-3 border-bottom">
<h2>Täyttö</h2>
<FieldArray name="fillingEventRows">
{({ replace, remove, push }) => (
{({ remove, push }) => (
<>
{values.fillingEventRows.map((row, index) => (
<FillingEventRowComponent
key={row.uniqueId}
errors={errors}
index={index}
push={push}
gases={gases}
lastItem={values.fillingEventRows.length === index + 1}
remove={remove}
replace={replace}
setFieldValue={setFieldValue}
storageCylinders={storageCylinders}
values={values}
/>
))}
<div className={styles.addRow}>
<PrimaryButton
disabled={values.userConfirm}
onClick={() => {
push({
...emptyFillingRow(),
});
}}
type={ButtonType.button}
text="Lisää uusi rivi"
/>
</div>
</>
)}
</FieldArray>
Expand Down
48 changes: 22 additions & 26 deletions src/components/BlenderLogbook/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,27 @@ export const BLENDER_FILLING_EVENT_VALIDATION_SCHEMA = yup.object().shape({
.max(100)
.optional(),
userConfirm: yup.boolean().isTrue(),
fillingEventRows: yup
.array()
.of(
yup.object().shape({
consumption: yup.number(),
priceEurCents: yup.number(),
startPressure: yup
.number()
.typeError(FIELD_NUMBER)
.min(0)
.required(FIELD_REQUIRED),
endPressure: yup
.number()
.typeError(FIELD_NUMBER)
.min(0)
.required(FIELD_REQUIRED)
.when(['startPressure'], (startPressure, schema) =>
schema.max(
startPressure as unknown as number,
'Loppupaine liian korkea',
),
fillingEventRows: yup.array().of(
yup.object().shape({
consumption: yup.number(),
priceEurCents: yup.number(),
startPressure: yup
.number()
.typeError(FIELD_NUMBER)
.min(0)
.required(FIELD_REQUIRED),
endPressure: yup
.number()
.typeError(FIELD_NUMBER)
.min(0)
.required(FIELD_REQUIRED)
.when(['startPressure'], (startPressure, schema) =>
schema.max(
startPressure as unknown as number,
'Loppupaine liian korkea',
),
storageCylinderId: yup.string().required(FIELD_REQUIRED),
}),
)
.required()
.min(1),
),
storageCylinderId: yup.string().required(FIELD_REQUIRED),
}),
),
});

0 comments on commit d4b4ece

Please sign in to comment.