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

MF-641: Patient List Modal #22

Closed
Prev Previous commit
Next Next commit
Implemented search
  • Loading branch information
vasharma05 committed Jul 14, 2021
commit cef9b0c6779af79c15d323a9b9bca4f034cce4df
27 changes: 19 additions & 8 deletions packages/esm-patient-list-app/src/AddPatientToList/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useEffect } from 'react';
import React, { useState, useEffect, useMemo, useCallback } from 'react';
import Search from 'carbon-components-react/lib/components/Search';
import Button from 'carbon-components-react/lib/components/Button';
import Checkbox from 'carbon-components-react/lib/components/Checkbox';
@@ -9,12 +9,11 @@ import styles from './add-patient-to-list.scss';
import { OpenmrsCohort, addPatientToList, getPatientListsForPatient } from '../patientListData/api';
import SkeletonText from 'carbon-components-react/es/components/SkeletonText';
import { toOmrsIsoString } from '@openmrs/esm-framework';
import { useCallback } from 'react';

const AddPatient: React.FC<{ close: () => void; patientUuid: string }> = ({ close, patientUuid }) => {
const { t } = useTranslation();
const [searchValue, setSearchValue] = useState('');
const { loading, data } = usePatientListData(undefined, undefined, undefined, searchValue);
const { loading, data } = usePatientListData(undefined, undefined, undefined, undefined);
const [selectedLists, setSelectedList] = useState({});

useEffect(() => {
@@ -24,10 +23,22 @@ const AddPatient: React.FC<{ close: () => void; patientUuid: string }> = ({ clos
lists[patientList.uuid] = false;
});
}
getPatientListsForPatient(patientUuid).then((res) => console.log(res));
setSelectedList(lists);
}, [data]);

const searchResults = useMemo(() => {
if (data) {
if (searchValue && searchValue.trim() !== '') {
const search = searchValue.toLowerCase();
return data.filter((patientList) => patientList.name.toLowerCase().includes(search));
} else {
return data;
}
} else {
return [];
}
}, [searchValue, data]);

const handleChange = useCallback((uuid, e) => {
setSelectedList((selectedLists) => ({
...selectedLists,
@@ -74,14 +85,14 @@ const AddPatient: React.FC<{ close: () => void; patientUuid: string }> = ({ clos
<div className={styles.patientListList}>
<fieldset className="bx--fieldset">
<p className="bx--label">Patient Lists</p>
{!loading && data ? (
data.length > 0 ? (
data.map((patientList, ind) => (
{!loading && searchResults ? (
searchResults.length > 0 ? (
searchResults.map((patientList, ind) => (
<div key={ind} className={styles.checkbox}>
<Checkbox
key={ind}
onChange={(e) => handleChange(patientList.uuid, e)}
checked={selectedLists[patientList.uuid] === undefined}
checked={selectedLists[patientList.uuid]}
labelText={patientList.name}
id={patientList.uuid}
/>
10 changes: 5 additions & 5 deletions packages/esm-patient-list-app/src/patientListData/api.ts
Original file line number Diff line number Diff line change
@@ -51,7 +51,7 @@ export async function getAllPatientLists(filter?: PATIENT_LIST_TYPE, stared?: bo
results: Array<OpenmrsCohort>;
error: Error;
} = await (
await openmrsFetch<{ results: Array<OpenmrsCohort>; error: Error }>('/openmrs/ws/rest/v1/cohortm/cohort?v=default')
await openmrsFetch<{ results: Array<OpenmrsCohort>; error: Error }>('/ws/rest/v1/cohortm/cohort?v=default')
).data;

if (error) throw error;
@@ -70,13 +70,13 @@ export async function getPatientListMembers(cohortUuid: string) {
await openmrsFetch<{
results: Array<OpenmrsCohortMember>;
error: Error;
}>(`/openmrs/ws/rest/v1/cohortm/cohortmember?cohort=${cohortUuid}&v=default`)
}>(`/ws/rest/v1/cohortm/cohortmember?cohort=${cohortUuid}&v=default`)
).data;

if (error) throw error;

const patients: Array<OpenmrsResource> = (
await openmrsFetch('/openmrs/ws/fhir2/R4/Patient/_search?_id=' + results.map((p) => p.patient.uuid).join(','), {
await openmrsFetch('/ws/fhir2/R4/Patient/_search?_id=' + results.map((p) => p.patient.uuid).join(','), {
method: 'POST',
}).then((res) => res.json())
).entry.map((e) => e.resource);
@@ -103,11 +103,11 @@ export async function getPatientListsForPatient(patientUuid: string) {
}

export async function addPatientToList(data: { patient: string; cohort: string; startDate: string }) {
return postData('/openmrs/ws/rest/v1/cohortm/cohortmember', data);
return postData('/ws/rest/v1/cohortm/cohortmember', data);
}

export async function createPatientList(cohort: { name: string }) {
return postData('/openmrs/ws/rest/v1/cohortm/cohort', {
return postData('/ws/rest/v1/cohortm/cohort', {
...cohort,
cohortType: '6df786bf-f15a-49c2-8d2b-1832d961c270',
location: 'aff27d58-a15c-49a6-9beb-d30dcfc0c66e',