forked from openmrs/openmrs-esm-cohortbuilder-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cohort-builder.tsx
137 lines (128 loc) · 3.82 KB
/
cohort-builder.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import React, { useState } from "react";
import { showNotification } from "@openmrs/esm-framework";
import { Tab, Tabs } from "carbon-components-react";
import { useTranslation } from "react-i18next";
import { search } from "./cohort-builder.resource";
import styles from "./cohort-builder.scss";
import { addToHistory } from "./cohort-builder.utils";
import SearchButtonSet from "./components/search-button-set/search-button-set";
import { SearchByConcepts } from "./components/search-by-concepts/search-by-concepts.component";
import { SearchHistory } from "./components/search-history/search-history.component";
import { SearchResultsTable } from "./components/search-results-table/search-results-table.component";
import { Patient, SearchParams } from "./types/types";
interface TabItem {
name: string;
component: JSX.Element;
}
const CohortBuilder: React.FC = () => {
const { t } = useTranslation();
const [patients, setPatients] = useState<Patient[]>([]);
const [isLoading, setIsLoading] = useState(false);
const [resetInputs, setResetInputs] = useState(false);
const [isHistoryUpdated, setIsHistoryUpdated] = useState(true);
const [searchParams, setSearchParams] = useState<SearchParams>({
query: null,
});
const [queryDescription, setQueryDescription] = useState("");
const tabs: TabItem[] = [
{
name: t("concept", "Concept"),
component: (
<SearchByConcepts
resetInputs={resetInputs}
setSearchParams={setSearchParams}
setQueryDescription={setQueryDescription}
/>
),
},
{
name: t("demographics", "Demographics"),
component: <span></span>,
},
{
name: t("encounters", "Encounters"),
component: <span></span>,
},
{
name: t("enrollments", "Enrollments"),
component: <span></span>,
},
{
name: t("drugOrder", "Drug Order"),
component: <span></span>,
},
{
name: "SQL",
component: <span></span>,
},
{
name: t("composition", "Composition"),
component: <span></span>,
},
{
name: t("savedDefinitions", "Saved Definitions"),
component: <span></span>,
},
];
const handleReset = () => {
setPatients([]);
setResetInputs(true);
};
const handleSubmit = async () => {
setPatients([]);
setIsLoading(true);
try {
const {
data: { rows },
} = await search(searchParams);
rows.map((patient: Patient) => {
patient.id = patient.patientId.toString();
patient.name = `${patient.firstname} ${patient.lastname}`;
});
setPatients(rows);
addToHistory(queryDescription, rows, searchParams.query);
showNotification({
title: t("success", "Success!"),
kind: "success",
critical: true,
description: t(
"searchIsCompleted",
`Search is completed with ${rows.length} result(s)`,
{ numOfResults: rows.length }
),
});
setIsLoading(false);
setIsHistoryUpdated(true);
} catch (error) {
showNotification({
title: t("error", "Error"),
kind: "error",
critical: true,
description: error?.message,
});
setIsLoading(false);
}
};
return (
<div className={`omrs-main-content ${styles.container}`}>
<Tabs className={styles.tab}>
{tabs.map((tab: TabItem, index: number) => (
<Tab key={index} label={tab.name}>
{tab.component}
</Tab>
))}
</Tabs>
<SearchButtonSet
onHandleReset={handleReset}
onHandleSubmit={handleSubmit}
isLoading={isLoading}
/>
<SearchResultsTable patients={patients} />
<SearchHistory
isHistoryUpdated={isHistoryUpdated}
setIsHistoryUpdated={setIsHistoryUpdated}
/>
</div>
);
};
export default CohortBuilder;