-
Notifications
You must be signed in to change notification settings - Fork 480
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 + Adds
useSlug
hook (#6369)
* Adds hook: `useSlug` * bug fix: NumericWithUnits field not showing intial value * Form: support for showing global errors * Adds support for editing prescriptions (fixes #6340)
- Loading branch information
1 parent
a4a4671
commit c7c37c6
Showing
11 changed files
with
318 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { usePath } from "raviger"; | ||
|
||
/** | ||
* Returns the slug from the current path. | ||
* @param prefix The prefix of the slug. | ||
* @returns The slug. | ||
* @example | ||
* // Current path: /consultation/94b9a | ||
* const consultation = useSlug("consultation"); // consultation = "94b9a" | ||
*/ | ||
export default function useSlug(prefix: string) { | ||
const path = usePath() ?? ""; | ||
return findSlug(path.split("/"), prefix); | ||
} | ||
|
||
/** | ||
* Returns the slugs from the current path. | ||
* @param prefix The prefixes of the slug. | ||
* @returns The slugs | ||
* @example | ||
* // Current path: /facility/5b0a/consultation/94b9a | ||
* const [facility, consultation] = useSlug("facility", "consultation"); | ||
* // facility = "5b0a" | ||
* // consultation = "94b9a" | ||
*/ | ||
export const useSlugs = (...prefix: string[]) => { | ||
const path = usePath() ?? ""; | ||
return prefix.map((p) => findSlug(path.split("/"), p)); | ||
}; | ||
|
||
const findSlug = (segments: string[], prefix: string) => { | ||
const index = segments.findIndex((segment) => segment === prefix); | ||
if (index === -1) { | ||
throw new Error( | ||
`Prefix "${prefix}" not found in path "${segments.join("/")}"` | ||
); | ||
} | ||
|
||
const slug = segments[index + 1]; | ||
if (!slug) { | ||
throw new Error(`Slug not found in path "${segments.join("/")}"`); | ||
} | ||
|
||
return slug; | ||
}; |
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
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
Oops, something went wrong.