Skip to content

Commit

Permalink
Update edit prescription form
Browse files Browse the repository at this point in the history
and fix dosage validation
  • Loading branch information
GokulramGHV committed Nov 10, 2023
1 parent 3391b1e commit ba9cf64
Show file tree
Hide file tree
Showing 12 changed files with 224 additions and 59 deletions.
21 changes: 21 additions & 0 deletions src/Components/Form/FormFields/DosageFormField.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import NumericWithUnitsFormField from "./NumericWithUnitsFormField";
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;
};

export default function DosageFormField(props: Props) {
return (
<NumericWithUnitsFormField
{...props}
units={["mg", "g", "ml", "drop(s)", "ampule(s)", "tsp"]}
/>
);
}
2 changes: 2 additions & 0 deletions src/Components/Form/FormFields/NumericWithUnitsFormField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export default function NumericWithUnitsFormField(props: Props) {
required={field.required}
value={numValue}
onChange={(e) => field.handleChange(e.target.value + " " + unitValue)}
disabled={props.disabled}
/>
<div className="absolute inset-y-0 right-0 flex items-center">
<select
Expand All @@ -46,6 +47,7 @@ export default function NumericWithUnitsFormField(props: Props) {
onChange={(e) =>
field.handleChange(numValue + " " + e.target.value)
}
disabled={props.disabled}
>
{props.units.map((unit) => (
<option key={unit}>{unit}</option>
Expand Down
27 changes: 12 additions & 15 deletions src/Components/Medicine/AdministerMedicine.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import { useTranslation } from "react-i18next";
import CheckBoxFormField from "../Form/FormFields/CheckBoxFormField";
import TextFormField from "../Form/FormFields/TextFormField";
import dayjs from "../../Utils/dayjs";
import NumericWithUnitsFormField from "../Form/FormFields/NumericWithUnitsFormField";
import useSlug from "../../Common/hooks/useSlug";
import request from "../../Utils/request/request";
import MedicineRoutes from "./routes";
import DosageFormField from "../Form/FormFields/DosageFormField";
import { AdministrationDosageValidator } from "./validators";

interface Props {
prescription: Prescription;
Expand Down Expand Up @@ -68,18 +69,14 @@ export default function AdministerMedicine({ prescription, ...props }: Props) {
show
onClose={() => props.onClose(false)}
onConfirm={async () => {
if (!dosage && prescription.dosage_type === "TITRATED") {
setError("This field is required");
return;
}
if (
(prescription.dosage_type === "TITRATED" &&
Number(dosage?.split(" ")[0]) <
Number(prescription.base_dosage?.split(" ")[0])) ||
Number(dosage?.split(" ")[0]) >
Number(prescription.target_dosage?.split(" ")[0])
) {
setError("Dosage should be between start and target dosage");
if (prescription.dosage_type === "TITRATED") {
setError(
AdministrationDosageValidator(
dosage,
prescription.base_dosage,
prescription.target_dosage
)
);
return;
}

Expand All @@ -104,7 +101,7 @@ export default function AdministerMedicine({ prescription, ...props }: Props) {
<PrescriptionDetailCard prescription={prescription} readonly />

{prescription.dosage_type === "TITRATED" && (
<NumericWithUnitsFormField
<DosageFormField
name="dosage"
label={
t("dosage") +
Expand All @@ -113,11 +110,11 @@ export default function AdministerMedicine({ prescription, ...props }: Props) {
value={dosage}
onChange={({ value }) => setDosage(value)}
required
units={["mg", "g", "ml", "drop(s)", "ampule(s)", "tsp"]}
min={prescription.base_dosage}
max={prescription.target_dosage}
disabled={isLoading}
error={error}
errorClassName={error ? "block" : "hidden"}
/>
)}

Expand Down
19 changes: 8 additions & 11 deletions src/Components/Medicine/CreatePrescriptionForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import TextAreaFormField from "../Form/FormFields/TextAreaFormField";
import TextFormField from "../Form/FormFields/TextFormField";
import { MedicineAdministrationRecord, Prescription } from "./models";
import { useState } from "react";
import NumericWithUnitsFormField from "../Form/FormFields/NumericWithUnitsFormField";
import { useTranslation } from "react-i18next";
import MedibaseAutocompleteFormField from "./MedibaseAutocompleteFormField";
import dayjs from "../../Utils/dayjs";
Expand All @@ -15,6 +14,7 @@ import MedicineRoutes from "./routes";
import request from "../../Utils/request/request";
import useSlug from "../../Common/hooks/useSlug";
import { Success } from "../../Utils/Notifications";
import DosageFormField from "../Form/FormFields/DosageFormField";

export default function CreatePrescriptionForm(props: {
prescription: Prescription;
Expand Down Expand Up @@ -63,7 +63,7 @@ export default function CreatePrescriptionForm(props: {
/>
{props.prescription.dosage_type !== "PRN" && (
<CheckBoxFormField
label="Titrate Dosage"
label={t("titrate_dosage")}
name="Titrate Dosage"
value={field("dosage_type").value === "TITRATED"}
onChange={(e) => {
Expand Down Expand Up @@ -92,30 +92,27 @@ export default function CreatePrescriptionForm(props: {
/>
{field("dosage_type").value === "TITRATED" ? (
<div className="flex w-full gap-4">
<NumericWithUnitsFormField
<DosageFormField
className="flex-1"
label="Start Dosage"
label={t("start_dosage")}
{...field("base_dosage", RequiredFieldValidator())}
required
units={["mg", "g", "ml", "drop(s)", "ampule(s)", "tsp"]}
min={0}
/>
<NumericWithUnitsFormField
<DosageFormField
className="flex-1"
label="Target Dosage"
label={t("target_dosage")}
{...field("target_dosage", RequiredFieldValidator())}
required
units={["mg", "g", "ml", "drop(s)", "ampule(s)", "tsp"]}
min={0}
/>
</div>
) : (
<NumericWithUnitsFormField
<DosageFormField
className="flex-1"
label={t("dosage")}
{...field("base_dosage", RequiredFieldValidator())}
required={field("dosage_type").value !== "TITRATED"}
units={["mg", "g", "ml", "drop(s)", "ampule(s)", "tsp"]}
min={0}
/>
)}
Expand Down Expand Up @@ -169,7 +166,7 @@ export default function CreatePrescriptionForm(props: {

{field("dosage_type").value === "TITRATED" && (
<TextAreaFormField
label="Instructions on titration"
label={t("instruction_on_titration")}
{...field("instruction_on_titration")}
/>
)}
Expand Down
65 changes: 56 additions & 9 deletions src/Components/Medicine/EditPrescriptionForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ 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,
Expand All @@ -16,6 +15,8 @@ import TextFormField from "../Form/FormFields/TextFormField";
import TextAreaFormField from "../Form/FormFields/TextAreaFormField";
import { EditPrescriptionFormValidator } from "./validators";
import MedicineRoutes from "./routes";
import CheckBoxFormField from "../Form/FormFields/CheckBoxFormField";
import DosageFormField from "../Form/FormFields/DosageFormField";

interface Props {
initial: Prescription;
Expand Down Expand Up @@ -88,6 +89,27 @@ export default function EditPrescriptionForm(props: Props) {
{...field("discontinued_reason")}
/>

{props.initial.dosage_type !== "PRN" && (
<CheckBoxFormField
label={t("titrate_dosage")}
name="Titrate Dosage"
value={field("dosage_type").value === "TITRATED"}
onChange={(e) => {
if (e.value) {
field("dosage_type").onChange({
name: "dosage_type",
value: "TITRATED",
});
} else {
field("dosage_type").onChange({
name: "dosage_type",
value: "REGULAR",
});
}
}}
/>
)}

<div className="flex items-center gap-4">
<SelectFormField
className="flex-1"
Expand All @@ -97,14 +119,32 @@ export default function EditPrescriptionForm(props: Props) {
optionLabel={(key) => t("PRESCRIPTION_ROUTE_" + key)}
optionValue={(key) => key}
/>
<NumericWithUnitsFormField
className="flex-1"
label={t("dosage")}
{...field("base_dosage", RequiredFieldValidator())}
required
units={["mg", "g", "ml", "drop(s)", "ampule(s)", "tsp"]}
min={0}
/>
{field("dosage_type").value === "TITRATED" ? (
<div className="flex w-full gap-4">
<DosageFormField
className="flex-1"
label={t("start_dosage")}
{...field("base_dosage", RequiredFieldValidator())}
required
min={0}
/>
<DosageFormField
className="flex-1"
label={t("target_dosage")}
{...field("target_dosage", RequiredFieldValidator())}
required
min={0}
/>
</div>
) : (
<DosageFormField
className="flex-1"
label={t("dosage")}
{...field("base_dosage", RequiredFieldValidator())}
required={field("dosage_type").value !== "TITRATED"}
min={0}
/>
)}
</div>

{props.initial.dosage_type === "PRN" ? (
Expand Down Expand Up @@ -153,6 +193,13 @@ export default function EditPrescriptionForm(props: Props) {
</div>
)}

{field("dosage_type").value === "TITRATED" && (
<TextAreaFormField
label={t("instruction_on_titration")}
{...field("instruction_on_titration")}
/>
)}

<TextAreaFormField label={t("notes")} {...field("notes")} />
</>
)}
Expand Down
Loading

0 comments on commit ba9cf64

Please sign in to comment.