Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
shivankacker committed May 21, 2024
1 parent aa6d33f commit e5ecbe4
Show file tree
Hide file tree
Showing 5 changed files with 348 additions and 292 deletions.
216 changes: 10 additions & 206 deletions src/Components/Facility/ConsultationForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import {
PATIENT_CATEGORIES,
REVIEW_AT_CHOICES,
TELEMEDICINE_ACTIONS,
CONSENT_TYPE_CHOICES,
CONSENT_PATIENT_CODE_STATUS_CHOICES,
} from "../../Common/constants";
import { Cancel, Submit } from "../Common/components/ButtonV2";
import { DraftSection, useAutoSaveReducer } from "../../Utils/AutoSave";
Expand Down Expand Up @@ -71,13 +69,6 @@ const PageTitle = lazy(() => import("../Common/PageTitle"));

type BooleanStrings = "true" | "false";

export type ConsentRecord = {
id: string;
type: (typeof CONSENT_TYPE_CHOICES)[number]["id"];
patient_code_status?: (typeof CONSENT_PATIENT_CODE_STATUS_CHOICES)[number]["id"];
deleted?: boolean;
};

type FormDetails = {
symptoms: number[];
other_symptoms: string;
Expand Down Expand Up @@ -126,7 +117,6 @@ type FormDetails = {
death_confirmed_doctor: string;
InvestigationAdvice: InvestigationType[];
procedures: ProcedureType[];
consent_records: ConsentRecord[];
};

const initForm: FormDetails = {
Expand Down Expand Up @@ -177,7 +167,6 @@ const initForm: FormDetails = {
death_confirmed_doctor: "",
InvestigationAdvice: [],
procedures: [],
consent_records: [],
};

const initError = Object.assign(
Expand Down Expand Up @@ -228,7 +217,6 @@ type ConsultationFormSection =
| "Consultation Details"
| "Diagnosis"
| "Treatment Plan"
| "Consent Records"
| "Bed Status";

type Props = {
Expand Down Expand Up @@ -261,14 +249,9 @@ export const ConsultationForm = ({ facilityId, patientId, id }: Props) => {
const [diagnosisVisible, diagnosisRef] = useVisibility(-300);
const [treatmentPlanVisible, treatmentPlanRef] = useVisibility(-300);
const [bedStatusVisible, bedStatusRef] = useVisibility(-300);
const [consentRecordsVisible, consentRecordsRef] = useVisibility(-300);

const [disabledFields, setDisabledFields] = useState<string[]>([]);
const [collapsedConsentRecords, setCollapsedConsentRecords] = useState<
number[]
>([]);
const [showDeleteConsent, setShowDeleteConsent] = useState<string | null>(
null,
);


const { min_encounter_date } = useConfig();

Expand All @@ -288,11 +271,6 @@ export const ConsultationForm = ({ facilityId, patientId, id }: Props) => {
visible: treatmentPlanVisible,
ref: treatmentPlanRef,
},
"Consent Records": {
iconClass: "l-file-alt",
visible: consentRecordsVisible,
ref: consentRecordsRef,
},
"Bed Status": {
iconClass: "l-bed",
visible: bedStatusVisible,
Expand All @@ -305,15 +283,13 @@ export const ConsultationForm = ({ facilityId, patientId, id }: Props) => {
if (consultationDetailsVisible) return "Consultation Details";
if (diagnosisVisible) return "Diagnosis";
if (treatmentPlanVisible) return "Treatment Plan";
if (consentRecordsVisible) return "Consent Records";
if (bedStatusVisible) return "Bed Status";
return prev;
});
}, [
consultationDetailsVisible,
diagnosisVisible,
treatmentPlanVisible,
consentRecordsVisible,
bedStatusVisible,
]);

Expand Down Expand Up @@ -408,7 +384,7 @@ export const ConsultationForm = ({ facilityId, patientId, id }: Props) => {
admitted_to: data.admitted_to ? data.admitted_to : "",
category: data.category
? PATIENT_CATEGORIES.find((i) => i.text === data.category)?.id ??
""
""
: "",
patient_no: data.patient_no ?? "",
OPconsultation: data.consultation_notes,
Expand Down Expand Up @@ -738,12 +714,12 @@ export const ConsultationForm = ({ facilityId, patientId, id }: Props) => {
: undefined,
referred_from_facility:
state.form.route_to_facility === 20 &&
!state.form.referred_from_facility_external
!state.form.referred_from_facility_external
? state.form.referred_from_facility
: undefined,
referred_from_facility_external:
state.form.route_to_facility === 20 &&
!state.form.referred_from_facility
!state.form.referred_from_facility
? state.form.referred_from_facility_external
: undefined,
referred_by_external:
Expand Down Expand Up @@ -771,7 +747,6 @@ export const ConsultationForm = ({ facilityId, patientId, id }: Props) => {
height: Number(state.form.height),
bed: bed && bed instanceof Array ? bed[0]?.id : bed?.id,
patient_no: state.form.patient_no || null,
consent_records: state.form.consent_records || [],
};

const { data: obj } = await request(
Expand Down Expand Up @@ -919,64 +894,6 @@ export const ConsultationForm = ({ facilityId, patientId, id }: Props) => {
};
};

const handleConsentTypeChange: FieldChangeEventHandler<number> = async (
event,
) => {
if (!id) return;
const consentRecords = [...state.form.consent_records];
if (
consentRecords
.filter((cr) => cr.deleted !== true)
.map((cr) => cr.type)
.includes(event.value)
) {
return;
} else {
const randomId = "consent-" + new Date().getTime().toString();
const newRecords = [
...consentRecords,
{ id: randomId, type: event.value },
];
await request(routes.partialUpdateConsultation, {
pathParams: { id },
body: { consent_records: newRecords },
});
dispatch({
type: "set_form",
form: { ...state.form, consent_records: newRecords },
});
}
};

const handleConsentPCSChange: FieldChangeEventHandler<number> = (event) => {
dispatch({
type: "set_form",
form: {
...state.form,
consent_records: state.form.consent_records.map((cr) =>
cr.type === 2 ? { ...cr, patient_code_status: event.value } : cr,
),
},
});
};

const handleDeleteConsent = async () => {
const consent_id = showDeleteConsent;
if (!consent_id || !id) return;
const newRecords = state.form.consent_records.map((cr) =>
cr.id === consent_id ? { ...cr, deleted: true } : cr,
);
await request(routes.partialUpdateConsultation, {
pathParams: { id },
body: { consent_records: newRecords },
});
dispatch({
type: "set_form",
form: { ...state.form, consent_records: newRecords },
});
setShowDeleteConsent(null);
};

return (
<div className="relative flex flex-col pb-2">
<PageTitle
Expand All @@ -998,17 +915,16 @@ export const ConsultationForm = ({ facilityId, patientId, id }: Props) => {
{Object.keys(sections).map((sectionTitle) => {
if (
!isUpdate &&
["Bed Status", "Consent Records"].includes(sectionTitle)
["Bed Status"].includes(sectionTitle)
) {
return null;
}
const isCurrent = currentSection === sectionTitle;
const section = sections[sectionTitle as ConsultationFormSection];
return (
<button
className={`flex w-full items-center justify-start gap-3 rounded-l-lg px-5 py-3 font-medium ${
isCurrent ? "bg-white text-primary-500" : "bg-transparent"
} transition-all duration-100 ease-in hover:bg-white hover:tracking-wider`}
className={`flex w-full items-center justify-start gap-3 rounded-l-lg px-5 py-3 font-medium ${isCurrent ? "bg-white text-primary-500" : "bg-transparent"
} transition-all duration-100 ease-in hover:bg-white hover:tracking-wider`}
onClick={() => {
section.ref.current?.scrollIntoView({
behavior: "smooth",
Expand Down Expand Up @@ -1171,7 +1087,7 @@ export const ConsultationForm = ({ facilityId, patientId, id }: Props) => {
{Math.sqrt(
(Number(state.form.weight) *
Number(state.form.height)) /
3600,
3600,
).toFixed(2)}
m<sup>2</sup>
</span>
Expand Down Expand Up @@ -1334,7 +1250,7 @@ export const ConsultationForm = ({ facilityId, patientId, id }: Props) => {
className={classNames(
"col-span-6",
["A", "DC"].includes(state.form.suggestion) &&
"xl:col-span-3",
"xl:col-span-3",
)}
ref={fieldRef["icu_admission_date"]}
>
Expand Down Expand Up @@ -1553,118 +1469,6 @@ export const ConsultationForm = ({ facilityId, patientId, id }: Props) => {
</>
)}
</div>
{id && (
<>
<div className="flex flex-col gap-4 pb-4">
{sectionTitle("Consent Records", true)}
</div>
<ConfirmDialog
show={showDeleteConsent !== null}
onClose={() => setShowDeleteConsent(null)}
onConfirm={handleDeleteConsent}
action="Delete"
variant="danger"
description={
"Are you sure you want to delete this consent record?"
}
title="Delete Consent"
className="w-auto"
/>
<SelectFormField
{...selectField("consent_type")}
onChange={handleConsentTypeChange}
label="Add Consent Type"
options={CONSENT_TYPE_CHOICES.filter(
(c) =>
!state.form.consent_records
.filter((r) => r.deleted !== true)
.map((record) => record.type)
.includes(c.id),
)}
/>
<div className="flex flex-col gap-4">
{state.form.consent_records
.filter((record) => record.deleted !== true)
.map((record, index) => (
<div
className="overflow-hidden rounded-xl border border-gray-300 bg-gray-100"
key={index}
>
<div className="flex items-center justify-between bg-gray-200 p-4">
<button
type="button"
className="font-bold"
onClick={() =>
setCollapsedConsentRecords((prev) =>
prev.includes(record.type)
? prev.filter((r) => r !== record.type)
: [...prev, record.type],
)
}
>
<CareIcon
icon={
collapsedConsentRecords.includes(
record.type,
)
? "l-arrow-down"
: "l-arrow-up"
}
className="mr-2"
/>
{
CONSENT_TYPE_CHOICES.find(
(c) => c.id === record.type,
)?.text
}
</button>
<button
className="text-red-400"
type="button"
onClick={() => {
setShowDeleteConsent(record.id);
}}
>
<CareIcon
icon="l-trash-alt"
className="h-4 w-4"
/>
</button>
</div>
<div
className={`${
collapsedConsentRecords.includes(record.type)
? "hidden"
: ""
}`}
>
<div className="px-4 pt-4">
{record.type === 2 && (
<SelectFormField
{...selectField("consent_type")}
onChange={handleConsentPCSChange}
label="Patient Code Status"
value={record.patient_code_status}
options={
CONSENT_PATIENT_CODE_STATUS_CHOICES
}
/>
)}
</div>
<FileUpload
changePageMetadata={false}
type="CONSENT_RECORD"
hideBack
unspecified
className="w-full"
consentId={record.id}
/>
</div>
</div>
))}
</div>
</>
)}
<div className="mt-6 flex flex-col justify-end gap-3 sm:flex-row">
<Cancel
onClick={() =>
Expand Down
Loading

0 comments on commit e5ecbe4

Please sign in to comment.