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

(feat) Patient name sorting #76

Merged
merged 10 commits into from
Apr 1, 2024
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";
icrc-psousa marked this conversation as resolved.
Show resolved Hide resolved
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