Skip to content

Commit

Permalink
(feat) Patient name sorting (#76)
Browse files Browse the repository at this point in the history
* Patient sorting by name

* Fixed session patient list toggle issue

* Removed unnecessary div tag

* Use name instead of deprecated extensionSlotName property

* Adjusted AttendanceTable rendering to prevent undefined patient issues

* Fixed AttendanceTable

* Added cleanup function to SortedPatientLists effect

* Consider only patient name and not id when sorting

* Refactored patient list ordering

* Removed unnecessary sort on reducer
  • Loading branch information
icrc-psousa authored Apr 1, 2024
1 parent 9393be2 commit 0493326
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 25 deletions.
60 changes: 40 additions & 20 deletions src/add-group-modal/AddGroupModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ import {
} from "@carbon/react";
import { TrashCan } from "@carbon/react/icons";
import { useTranslation } from "react-i18next";
import { ExtensionSlot, showToast, usePatient } from "@openmrs/esm-framework";
import {
ExtensionSlot,
fetchCurrentPatient,
showToast,
usePatient,
} from "@openmrs/esm-framework";
import styles from "./styles.scss";
import GroupFormWorkflowContext from "../context/GroupFormWorkflowContext";
import { usePostCohort } from "../hooks";
Expand Down Expand Up @@ -111,7 +116,7 @@ const NewGroupForm = (props) => {
<PatientRow
patient={patient}
removePatient={removePatient}
key={index}
key={patient.uuid}
/>
))}
</ul>
Expand All @@ -125,7 +130,7 @@ const NewGroupForm = (props) => {
</FormLabel>
<div className={styles.searchBar}>
<MemExtension
extensionSlotName="patient-search-bar-slot"
name="patient-search-bar-slot"
state={{
selectPatientAction: updatePatientList,
buttonProps: {
Expand All @@ -139,14 +144,14 @@ const NewGroupForm = (props) => {
};

const AddGroupModal = ({
patients = undefined,
isCreate = undefined,
groupName = "",
cohortUuid = undefined,
isOpen,
handleCancel,
onPostSubmit,
}) => {
patients = undefined,
isCreate = undefined,
groupName = "",
cohortUuid = undefined,
isOpen,
onPostCancel,
onPostSubmit,
}) => {
const { setGroup } = useContext(GroupFormWorkflowContext);
const { t } = useTranslation();
const [errors, setErrors] = useState({});
Expand Down Expand Up @@ -191,17 +196,25 @@ const AddGroupModal = ({
);

const updatePatientList = useCallback(
(patient) => {
setPatientList((patientList) => {
if (!patientList.find((p) => p.uuid === patient)) {
return [...patientList, { uuid: patient }];
} else {
return patientList;
}
});
(patientUuid) => {
if (!patientList.find((p) => p.uuid === patientUuid)) {
fetchCurrentPatient(patientUuid).then((result) => {
const newPatient = {
uuid: patientUuid,
name: [result?.name?.[0]?.given, result?.name?.[0]?.family].join(
" "
),
};
setPatientList(
[...patientList, newPatient].sort((a, b) =>
a.name?.localeCompare(b?.name, { sensitivity: "base" })
)
);
});
}
setErrors((errors) => ({ ...errors, patientList: null }));
},
[setPatientList]
[patientList, setPatientList]
);

const handleSubmit = () => {
Expand All @@ -217,6 +230,13 @@ const AddGroupModal = ({
}
};

const handleCancel = () => {
setPatientList(patients || []);
if (onPostCancel) {
onPostCancel();
}
};

useEffect(() => {
if (result) {
setGroup({
Expand Down
4 changes: 2 additions & 2 deletions src/group-form-entry-workflow/SessionDetailsForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ const SessionDetailsForm = () => {
control,
} = useFormContext();

const { patientUuids } = useContext(GroupFormWorkflowContext);
const { patients, isLoading } = useGetPatients(patientUuids);
const { activeGroupMembers} = useContext(GroupFormWorkflowContext);
const { patients, isLoading } = useGetPatients(activeGroupMembers);

return (
<div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const AttendanceTable = ({ patients }) => {
t("patientIsPresent", "Patient is present"),
];

const handleCancel = useCallback(() => {
const onPostCancel = useCallback(() => {
setOpen(false);
}, []);

Expand Down Expand Up @@ -116,7 +116,7 @@ const AttendanceTable = ({ patients }) => {
isCreate: false,
groupName: activeGroupName,
isOpen: isOpen,
handleCancel: handleCancel,
onPostCancel: onPostCancel,
onPostSubmit: onPostSubmit,
}}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ const GroupSearchHeader = () => {
);
const [isOpen, setOpen] = useState(false);
const handleSelectGroup = (group) => {
group.cohortMembers.sort((a, b) =>
a.display?.localeCompare(b?.display, { sensitivity: "base" })
);
setGroup(group);
};

Expand Down Expand Up @@ -50,7 +53,7 @@ const GroupSearchHeader = () => {
{...{
isCreate: true,
isOpen: isOpen,
handleCancel: handleCancel,
onPostCancel: handleCancel,
onPostSubmit: onPostSubmit,
}}
/>
Expand Down

0 comments on commit 0493326

Please sign in to comment.