diff --git a/src/Components/Form/FormFields/DosageFormField.tsx b/src/Components/Form/FormFields/DosageFormField.tsx index 82c2d78c43b..f8e40cc66b2 100644 --- a/src/Components/Form/FormFields/DosageFormField.tsx +++ b/src/Components/Form/FormFields/DosageFormField.tsx @@ -3,10 +3,7 @@ import { FormFieldBaseProps } from "./Utils"; type Props = FormFieldBaseProps & { placeholder?: string; - value?: string; autoComplete?: string; - className?: string | undefined; - disabled?: boolean; min?: string | number; max?: string | number; }; diff --git a/src/Components/Medicine/AdministerMedicine.tsx b/src/Components/Medicine/AdministerMedicine.tsx index c678b80b4e3..12b7be3e0da 100644 --- a/src/Components/Medicine/AdministerMedicine.tsx +++ b/src/Components/Medicine/AdministerMedicine.tsx @@ -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); diff --git a/src/Components/Medicine/MedicineAdministration.tsx b/src/Components/Medicine/MedicineAdministration.tsx index a2ba4b57375..23aef4b4dd1 100644 --- a/src/Components/Medicine/MedicineAdministration.tsx +++ b/src/Components/Medicine/MedicineAdministration.tsx @@ -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], diff --git a/src/Components/Medicine/validators.ts b/src/Components/Medicine/validators.ts index 6161617307c..d9ceef9b4bf 100644 --- a/src/Components/Medicine/validators.ts +++ b/src/Components/Medicine/validators.ts @@ -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"; + }; };