Skip to content

Commit

Permalink
Refactor administration dosage validation
Browse files Browse the repository at this point in the history
  • Loading branch information
GokulramGHV committed Nov 10, 2023
1 parent 2a7ea33 commit 63662fb
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 38 deletions.
3 changes: 0 additions & 3 deletions src/Components/Form/FormFields/DosageFormField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import { FormFieldBaseProps } from "./Utils";

type Props = FormFieldBaseProps<string> & {
placeholder?: string;
value?: string;
autoComplete?: string;
className?: string | undefined;
disabled?: boolean;
min?: string | number;
max?: string | number;
};
Expand Down
14 changes: 6 additions & 8 deletions src/Components/Medicine/AdministerMedicine.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,12 @@ export default function AdministerMedicine({ prescription, ...props }: Props) {
onClose={() => props.onClose(false)}
onConfirm={async () => {
if (prescription.dosage_type === "TITRATED") {
setError(
AdministrationDosageValidator(
dosage,
prescription.base_dosage,
prescription.target_dosage
)
);
return;
const error = AdministrationDosageValidator(
prescription.base_dosage,
prescription.target_dosage
)(dosage);
setError(error);
if (error) return;
}

setIsLoading(true);
Expand Down
19 changes: 8 additions & 11 deletions src/Components/Medicine/MedicineAdministration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,16 @@ export default function MedicineAdministration(props: Props) {
for (let i = 0; i < prescriptions.length; i++) {
if (shouldAdminister[i]) {
if (prescriptions[i].dosage_type === "TITRATED") {
const errorText = AdministrationDosageValidator(
dosages[i].dosage,
const error = AdministrationDosageValidator(
prescriptions[i].base_dosage,
prescriptions[i].target_dosage
);
if (errorText) {
setDosages((dosages) => {
const newDosages = [...dosages];
newDosages[i].error = errorText;
return newDosages;
});
return;
}
)(dosages[i].dosage);
setDosages((dosages) => {
const newDosages = [...dosages];
newDosages[i].error = error;
return newDosages;
});
if (error) return;
}
const administration = {
prescription: prescriptions[i],
Expand Down
33 changes: 17 additions & 16 deletions src/Components/Medicine/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,27 +61,28 @@ export const comparePrescriptions = (a: Prescription, b: Prescription) => {
};

export const AdministrationDosageValidator = (
value: Prescription["base_dosage"],
base_dosage: Prescription["base_dosage"],
target_dosage: Prescription["target_dosage"]
) => {
const getDosageValue = (dosage: string | undefined) => {
return dosage ? Number(dosage.split(" ")[0]) : undefined;
};
return (value: Prescription["base_dosage"]) => {
const getDosageValue = (dosage: string | undefined) => {
return dosage ? Number(dosage.split(" ")[0]) : undefined;
};

const valueDosage = getDosageValue(value);
const baseDosage = getDosageValue(base_dosage);
const targetDosage = getDosageValue(target_dosage);
const valueDosage = getDosageValue(value);
const baseDosage = getDosageValue(base_dosage);
const targetDosage = getDosageValue(target_dosage);

if (!valueDosage) return "This field is required";
if (!valueDosage) return "This field is required";

if (value?.split(" ")[1] !== base_dosage?.split(" ")[1])
return "Unit must be the same as start and target dosage's unit";
if (value?.split(" ")[1] !== base_dosage?.split(" ")[1])
return "Unit must be the same as start and target dosage's unit";

if (
baseDosage &&
targetDosage &&
(valueDosage < baseDosage || valueDosage > targetDosage)
)
return "Dosage should be between start and target dosage";
if (
baseDosage &&
targetDosage &&
(valueDosage < baseDosage || valueDosage > targetDosage)
)
return "Dosage should be between start and target dosage";
};
};

0 comments on commit 63662fb

Please sign in to comment.