Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed hacks for loading investigations in Consultation Form, updated type definitions #8416

Merged
275 changes: 143 additions & 132 deletions src/Components/Common/prescription-builder/InvestigationBuilder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,164 +96,175 @@ export default function InvestigationBuilder(
};

useEffect(() => {
const load = async () => setInvestigationsList(await loadInvestigations());
const load = async () => {
try {
const loadedInvestigations = await loadInvestigations();
setInvestigationsList(loadedInvestigations);
} catch (error) {
console.error("Error loading investigations:", error);
setInvestigationsList([]);
}
};
load();
}, []);

return (
<div className="mt-2">
{investigations?.map((investigation, i) => {
const setFrequency = (frequency: string) => {
setItem(
{
...investigation,
frequency,
},
i,
);
};
{(Array.isArray(investigations) ? investigations : []).map(
(investigation, i) => {
const setFrequency = (frequency: string) => {
setItem(
{
...investigation,
frequency,
},
i,
);
};

const setType = (type: string[]) => {
setItem(
{
...investigation,
type,
},
i,
);
};
const setType = (type: string[]) => {
setItem(
{
...investigation,
type,
},
i,
);
};

return (
<div
key={i}
className={`border-2 ${
activeIdx === i ? "border-primary-500" : "border-secondary-500"
} mb-2 border-spacing-2 rounded-md border-dashed p-3 text-sm text-secondary-600`}
>
<div className="mb-2 flex flex-wrap items-center gap-2 md:flex-row md:gap-4">
<h4 className="text-base font-medium text-secondary-700">
Investigation No. {i + 1}
</h4>
<button
type="button"
className="flex h-full items-center justify-center gap-1.5 rounded-md bg-red-500 px-3 py-1 text-sm text-secondary-100 transition hover:bg-red-600"
onClick={() =>
setInvestigations(
investigations.filter((investigation, index) => i != index),
)
}
>
Delete Investigation
<CareIcon icon="l-trash-alt" className="h-4 w-4" />
</button>
</div>
<div className="flex flex-col gap-2">
<div className="flex w-full flex-col justify-between">
<div>
Investigations Recommended
<span className="text-danger-500">{" *"}</span>
</div>
<div id="search-patient-investigation">
<PrescriptionMultiDropdown
options={investigationsList}
placeholder="Search Investigations"
selectedValues={
investigation.type?.constructor === Array
? investigation.type
: []
}
setSelectedValues={setType}
onFocus={() => setActiveIdx(i)}
onBlur={() => setActiveIdx(null)}
/>
</div>
return (
<div
key={i}
className={`border-2 ${
activeIdx === i ? "border-primary-500" : "border-secondary-500"
} mb-2 border-spacing-2 rounded-md border-dashed p-3 text-sm text-secondary-600`}
>
<div className="mb-2 flex flex-wrap items-center gap-2 md:flex-row md:gap-4">
<h4 className="text-base font-medium text-secondary-700">
Investigation No. {i + 1}
</h4>
<button
type="button"
className="flex h-full items-center justify-center gap-1.5 rounded-md bg-red-500 px-3 py-1 text-sm text-secondary-100 transition hover:bg-red-600"
onClick={() =>
setInvestigations(
investigations.filter((_, index) => i !== index),
)
}
>
Delete Investigation
<CareIcon icon="l-trash-alt" className="h-4 w-4" />
</button>
</div>
<div className="flex w-full shrink-0 flex-col justify-between">
<div className="flex flex-col gap-4 md:flex-row">
<div className="flex shrink-0 cursor-pointer items-center gap-2 md:mt-3">
Is the investigation repetitive?
<br />
<input
id="investigation-checkbox"
type="checkbox"
<div className="flex flex-col gap-2">
<div className="flex w-full flex-col justify-between">
<div>
Investigations Recommended
<span className="text-danger-500">{" *"}</span>
</div>
<div id="search-patient-investigation">
<PrescriptionMultiDropdown
options={investigationsList}
placeholder="Search Investigations"
selectedValues={
Array.isArray(investigation.type)
? investigation.type
: []
}
setSelectedValues={setType}
onFocus={() => setActiveIdx(i)}
onBlur={() => setActiveIdx(null)}
className="inline-block h-[18px] w-[18px] rounded-md"
checked={investigation?.repetitive || false}
onChange={(e) => {
setItem(
{
...investigation,
repetitive: e.currentTarget.checked,
},
i,
);
}}
/>
</div>
{investigation.repetitive ? (
<div className="w-full">
<div className="mb-1">
Frequency<span className="text-danger-500">{" *"}</span>
</div>
<div id="investigation-frequency">
<PrescriptionDropdown
placeholder="Frequency"
options={FREQUENCY}
value={investigation.frequency || ""}
setValue={setFrequency}
onFocus={() => setActiveIdx(i)}
onBlur={() => setActiveIdx(null)}
/>
</div>
</div>
) : (
<div className="w-full">
Time<span className="text-danger-500">{" *"}</span>
</div>
<div className="flex w-full shrink-0 flex-col justify-between">
<div className="flex flex-col gap-4 md:flex-row">
<div className="flex shrink-0 cursor-pointer items-center gap-2 md:mt-3">
Is the investigation repetitive?
<br />
<input
type="datetime-local"
className="block w-[calc(100%-5px)] rounded border border-secondary-400 bg-secondary-100 px-4 py-2 text-sm hover:bg-secondary-200 focus:border-primary-500 focus:bg-white focus:outline-none focus:ring-primary-500"
value={investigation.time || ""}
id="investigation-checkbox"
type="checkbox"
onFocus={() => setActiveIdx(i)}
onBlur={() => setActiveIdx(null)}
className="inline-block h-[18px] w-[18px] rounded-md"
checked={investigation?.repetitive || false}
onChange={(e) => {
setItem(
{
...investigation,
time: e.currentTarget.value,
repetitive: e.currentTarget.checked,
},
i,
);
}}
onFocus={() => setActiveIdx(i)}
onBlur={() => setActiveIdx(null)}
/>
</div>
)}
</div>
<div className="w-full">
<div className="mb-1">Notes</div>
<input
type="text"
className="cui-input-base py-2"
placeholder="Notes"
value={investigation.notes || ""}
onChange={(e) => {
setItem(
{
...investigation,
notes: e.currentTarget.value,
},
i,
);
}}
onFocus={() => setActiveIdx(i)}
onBlur={() => setActiveIdx(null)}
/>
{investigation.repetitive ? (
<div className="w-full">
<div className="mb-1">
Frequency
<span className="text-danger-500">{" *"}</span>
</div>
<div id="investigation-frequency">
<PrescriptionDropdown
placeholder="Frequency"
options={FREQUENCY}
value={investigation.frequency || ""}
setValue={setFrequency}
onFocus={() => setActiveIdx(i)}
onBlur={() => setActiveIdx(null)}
/>
</div>
</div>
) : (
<div className="w-full">
Time<span className="text-danger-500">{" *"}</span>
<input
type="datetime-local"
className="block w-[calc(100%-5px)] rounded border border-secondary-400 bg-secondary-100 px-4 py-2 text-sm hover:bg-secondary-200 focus:border-primary-500 focus:bg-white focus:outline-none focus:ring-primary-500"
value={investigation.time || ""}
onChange={(e) => {
setItem(
{
...investigation,
time: e.currentTarget.value,
},
i,
);
}}
onFocus={() => setActiveIdx(i)}
onBlur={() => setActiveIdx(null)}
/>
</div>
)}
</div>
<div className="w-full">
<div className="mb-1">Notes</div>
<input
type="text"
className="cui-input-base py-2"
placeholder="Notes"
value={investigation.notes || ""}
onChange={(e) => {
setItem(
{
...investigation,
notes: e.currentTarget.value,
},
i,
);
}}
onFocus={() => setActiveIdx(i)}
onBlur={() => setActiveIdx(null)}
/>
</div>
</div>
</div>
</div>
</div>
);
})}
);
},
)}
<button
type="button"
onClick={() => {
Expand Down
Loading