-
Notifications
You must be signed in to change notification settings - Fork 489
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support for editing prescriptions (fixes #6340)
- Loading branch information
1 parent
1a174c2
commit 3208fac
Showing
7 changed files
with
264 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
import { useState } from "react"; | ||
import Form from "../Form/Form"; | ||
import { Prescription } from "./models"; | ||
import request from "../../Utils/request/request"; | ||
import routes from "../../Redux/api"; | ||
import * as Notification from "../../Utils/Notifications"; | ||
import useSlug from "../../Common/hooks/useSlug"; | ||
import { RequiredFieldValidator } from "../Form/FieldValidators"; | ||
import { useTranslation } from "react-i18next"; | ||
import { SelectFormField } from "../Form/FormFields/SelectFormField"; | ||
import NumericWithUnitsFormField from "../Form/FormFields/NumericWithUnitsFormField"; | ||
import { | ||
PRESCRIPTION_FREQUENCIES, | ||
PRESCRIPTION_ROUTES, | ||
} from "./CreatePrescriptionForm"; | ||
import TextFormField from "../Form/FormFields/TextFormField"; | ||
import TextAreaFormField from "../Form/FormFields/TextAreaFormField"; | ||
import { EditPrescriptionFormValidator } from "./validators"; | ||
|
||
interface Props { | ||
initial: Prescription; | ||
onDone: (created: boolean) => void; | ||
} | ||
|
||
const handleSubmit = async ( | ||
consultation_external_id: string, | ||
oldObj: Prescription, | ||
{ discontinued_reason, ...newObj }: Prescription | ||
) => { | ||
const discontinue = await request(routes.discontinuePrescription, { | ||
pathParams: { consultation_external_id, external_id: oldObj.id }, | ||
body: { | ||
discontinued_reason: discontinued_reason | ||
? `Edit: ${discontinued_reason}` | ||
: "Edited", | ||
}, | ||
}); | ||
|
||
if (discontinue.res?.status !== 200) { | ||
Notification.Error({ | ||
msg: "Failed to discontinue previous prescription", | ||
}); | ||
return; | ||
} | ||
|
||
const { res } = await request(routes.createPrescription, { | ||
pathParams: { consultation_external_id }, | ||
body: { | ||
...newObj, | ||
// Forcing the medicine to be the same as the old one | ||
medicine: oldObj.medicine_object?.id, | ||
medicine_old: oldObj.medicine_old, | ||
}, | ||
}); | ||
|
||
return res?.status === 201; | ||
}; | ||
|
||
export default function EditPrescriptionForm(props: Props) { | ||
const consultation = useSlug("consultation"); | ||
const [isLoading, setIsLoading] = useState(false); | ||
const { t } = useTranslation(); | ||
|
||
return ( | ||
<Form<Prescription> | ||
disabled={isLoading} | ||
defaults={props.initial} | ||
onCancel={() => props.onDone(false)} | ||
onSubmit={async (obj) => { | ||
setIsLoading(true); | ||
const success = await handleSubmit(consultation, props.initial, obj); | ||
setIsLoading(false); | ||
|
||
if (success) { | ||
props.onDone(true); | ||
} | ||
}} | ||
noPadding | ||
validate={EditPrescriptionFormValidator(props.initial)} | ||
> | ||
{(field) => ( | ||
<> | ||
<TextAreaFormField | ||
label={t("reason_for_edit")} | ||
{...field("discontinued_reason")} | ||
/> | ||
|
||
<div className="flex items-center gap-4"> | ||
<SelectFormField | ||
className="flex-1" | ||
label={t("route")} | ||
{...field("route")} | ||
options={PRESCRIPTION_ROUTES} | ||
optionLabel={(key) => t("PRESCRIPTION_ROUTE_" + key)} | ||
optionValue={(key) => key} | ||
/> | ||
<NumericWithUnitsFormField | ||
className="flex-1" | ||
label={t("dosage")} | ||
{...field("dosage", RequiredFieldValidator())} | ||
required | ||
units={["mg", "g", "ml", "drop(s)", "ampule(s)", "tsp"]} | ||
min={0} | ||
/> | ||
</div> | ||
|
||
{props.initial.is_prn ? ( | ||
<> | ||
<TextFormField | ||
label={t("indicator")} | ||
{...field("indicator", RequiredFieldValidator())} | ||
required | ||
/> | ||
<TextFormField | ||
label={t("max_dosage_24_hrs")} | ||
type="number" | ||
min={0} | ||
{...field("max_dosage")} | ||
/> | ||
<SelectFormField | ||
label={t("min_time_bw_doses")} | ||
{...field("min_hours_between_doses")} | ||
options={[1, 2, 3, 6, 12, 24]} | ||
optionLabel={(hours) => `${hours} hrs.`} | ||
optionValue={(hours) => hours} | ||
position="above" | ||
/> | ||
</> | ||
) : ( | ||
<div className="flex items-center gap-4"> | ||
<SelectFormField | ||
position="above" | ||
className="flex-1" | ||
label={t("frequency")} | ||
{...field("frequency", RequiredFieldValidator())} | ||
required | ||
options={Object.entries(PRESCRIPTION_FREQUENCIES)} | ||
optionLabel={([key]) => | ||
t("PRESCRIPTION_FREQUENCY_" + key.toUpperCase()) | ||
} | ||
optionValue={([key]) => key} | ||
/> | ||
<TextFormField | ||
className="flex-1" | ||
label={t("days")} | ||
type="number" | ||
min={0} | ||
{...field("days")} | ||
/> | ||
</div> | ||
)} | ||
|
||
<TextAreaFormField label={t("notes")} {...field("notes")} /> | ||
</> | ||
)} | ||
</Form> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { FieldError, RequiredFieldValidator } from "../Form/FieldValidators"; | ||
import { FormErrors } from "../Form/Utils"; | ||
import { Prescription } from "./models"; | ||
|
||
export const PrescriptionFormValidator = () => { | ||
return (form: Prescription): FormErrors<Prescription> => { | ||
const errors: Partial<Record<keyof Prescription, FieldError>> = {}; | ||
errors.medicine_object = RequiredFieldValidator()(form.medicine_object); | ||
errors.dosage = RequiredFieldValidator()(form.dosage); | ||
if (form.is_prn) | ||
errors.indicator = RequiredFieldValidator()(form.indicator); | ||
if (!form.is_prn) | ||
errors.frequency = RequiredFieldValidator()(form.frequency); | ||
return errors; | ||
}; | ||
}; | ||
|
||
export const EditPrescriptionFormValidator = (old: Prescription) => { | ||
return (form: Prescription): FormErrors<Prescription> => { | ||
const errors = PrescriptionFormValidator()(form); | ||
|
||
if (comparePrescriptions(old, form)) { | ||
errors.$all = "No changes made"; | ||
} | ||
|
||
return errors; | ||
}; | ||
}; | ||
|
||
const PRESCRIPTION_COMPARE_FIELDS: (keyof Prescription)[] = [ | ||
"medicine", | ||
"days", | ||
"discontinued", | ||
"dosage", | ||
"frequency", | ||
"indicator", | ||
"is_prn", | ||
"max_dosage", | ||
"min_hours_between_doses", | ||
"prescription_type", | ||
"route", | ||
]; | ||
|
||
export const comparePrescriptions = (a: Prescription, b: Prescription) => { | ||
return ( | ||
PRESCRIPTION_COMPARE_FIELDS.every((field) => a[field] === b[field]) && | ||
a.medicine_object?.id === b.medicine_object?.id | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters