@kenyaemr/esm-bed-management-app 1.0.1-pre.4 → 1.0.1-pre.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,217 @@
1
+ import React, { useMemo, useState } from "react";
2
+ import { useTranslation } from "react-i18next";
3
+ import {
4
+ Button,
5
+ DataTable,
6
+ DataTableSkeleton,
7
+ InlineLoading,
8
+ Pagination,
9
+ Table,
10
+ TableBody,
11
+ TableCell,
12
+ TableContainer,
13
+ TableHead,
14
+ TableHeader,
15
+ TableRow,
16
+ Tile,
17
+ } from "@carbon/react";
18
+ import { Add, Edit } from "@carbon/react/icons";
19
+ import {
20
+ isDesktop as desktopLayout,
21
+ useLayoutType,
22
+ } from "@openmrs/esm-framework";
23
+ import { CardHeader, ErrorState } from "@openmrs/esm-patient-common-lib";
24
+ import type { BedTagData } from "../../types";
25
+ import { useBedTag } from "../../summary/summary.resource";
26
+ import Header from "../../header/header.component";
27
+ import styles from "../../bed-administration/bed-administration-table.scss";
28
+ import BedTagForm from "./new-tag-form.component";
29
+ import EditBedTagForm from "./edit-tag-form.component";
30
+
31
+ const BedTagAdministrationTable: React.FC = () => {
32
+ const { t } = useTranslation();
33
+ const headerTitle = t("bedTag", "Bed Tag");
34
+ const layout = useLayoutType();
35
+ const isTablet = layout === "tablet";
36
+ const responsiveSize = isTablet ? "lg" : "sm";
37
+ const isDesktop = desktopLayout(layout);
38
+ const [isBedDataLoading] = useState(false);
39
+ const [showBedTagsModal, setAddBedTagsModal] = useState(false);
40
+ const [showEditBedModal, setShowEditBedModal] = useState(false);
41
+ const [editData, setEditData] = useState<BedTagData>();
42
+ const [currentPage, setCurrentPage] = useState(1);
43
+ const [pageSize] = useState(10);
44
+ const { bedTypeData, isError, loading, validate, mutate } = useBedTag();
45
+ const [currentPageSize, setPageSize] = useState(10);
46
+ const pageSizes = [10, 20, 30, 40, 50];
47
+
48
+ const tableHeaders = [
49
+ {
50
+ header: t("ids", "Id"),
51
+ key: "ids",
52
+ },
53
+ {
54
+ header: t("name", "Name"),
55
+ key: "name",
56
+ },
57
+ {
58
+ header: t("actions", "Actions"),
59
+ key: "actions",
60
+ },
61
+ ];
62
+
63
+ const tableRows = useMemo(() => {
64
+ return bedTypeData?.map((entry) => ({
65
+ id: entry.uuid,
66
+ ids: entry.id,
67
+ name: entry?.name,
68
+ actions: (
69
+ <>
70
+ <Button
71
+ enterDelayMs={300}
72
+ renderIcon={Edit}
73
+ onClick={(e) => {
74
+ e.preventDefault();
75
+ setEditData(entry);
76
+ setShowEditBedModal(true);
77
+ setAddBedTagsModal(false);
78
+ }}
79
+ kind={"ghost"}
80
+ iconDescription={t("editBedTag", "Edit Bed Tag")}
81
+ hasIconOnly
82
+ size={responsiveSize}
83
+ tooltipAlignment="start"
84
+ />
85
+ </>
86
+ ),
87
+ }));
88
+ }, [responsiveSize, bedTypeData, t]);
89
+
90
+ if (isBedDataLoading || loading) {
91
+ return (
92
+ <>
93
+ <Header route="Bed Tag" />
94
+ <div className={styles.widgetCard}>
95
+ <DataTableSkeleton role="progressbar" compact={isDesktop} zebra />
96
+ </div>
97
+ </>
98
+ );
99
+ }
100
+
101
+ if (isError) {
102
+ return (
103
+ <>
104
+ <Header route="Bed Tag" />
105
+ <div className={styles.widgetCard}>
106
+ <ErrorState error={isError} headerTitle={headerTitle} />
107
+ </div>
108
+ </>
109
+ );
110
+ }
111
+
112
+ return (
113
+ <>
114
+ <Header route="Bed Tag" />
115
+
116
+ <div className={styles.widgetCard}>
117
+ {showBedTagsModal ? (
118
+ <BedTagForm
119
+ onModalChange={setAddBedTagsModal}
120
+ showModal={showBedTagsModal}
121
+ mutate={mutate}
122
+ />
123
+ ) : null}
124
+ {showEditBedModal ? (
125
+ <EditBedTagForm
126
+ onModalChange={setShowEditBedModal}
127
+ showModal={showEditBedModal}
128
+ editData={editData}
129
+ mutate={mutate}
130
+ />
131
+ ) : null}
132
+ <CardHeader title={headerTitle}>
133
+ <span className={styles.backgroundDataFetchingIndicator}>
134
+ <span>{validate ? <InlineLoading /> : null}</span>
135
+ </span>
136
+ {bedTypeData?.length ? (
137
+ <Button
138
+ kind="ghost"
139
+ renderIcon={(props) => <Add size={16} {...props} />}
140
+ onClick={() => setAddBedTagsModal(true)}
141
+ >
142
+ {t("addBedTag", "Add Bed Tag")}
143
+ </Button>
144
+ ) : null}
145
+ </CardHeader>
146
+ <DataTable
147
+ rows={tableRows}
148
+ headers={tableHeaders}
149
+ isSortable
150
+ size={isTablet ? "lg" : "sm"}
151
+ useZebraStyles
152
+ >
153
+ {({ rows, headers, getTableProps }) => (
154
+ <TableContainer>
155
+ <Table {...getTableProps()}>
156
+ <TableHead>
157
+ <TableRow>
158
+ {headers.map((header) => (
159
+ <TableHeader>
160
+ {header.header?.content ?? header.header}
161
+ </TableHeader>
162
+ ))}
163
+ </TableRow>
164
+ </TableHead>
165
+ <TableBody>
166
+ {rows.map((row) => (
167
+ <TableRow key={row.id}>
168
+ {row.cells.map((cell) => (
169
+ <TableCell key={cell.id}>
170
+ {cell.value?.content ?? cell.value}
171
+ </TableCell>
172
+ ))}
173
+ </TableRow>
174
+ ))}
175
+ </TableBody>
176
+ </Table>
177
+ {rows.length === 0 ? (
178
+ <div className={styles.tileContainer}>
179
+ <Tile className={styles.tile}>
180
+ <div className={styles.tileContent}>
181
+ <p className={styles.content}>
182
+ {t("No data", "No data to display")}
183
+ </p>
184
+ <p className={styles.helper}>
185
+ {t("checkFilters", "Check the filters above")}
186
+ </p>
187
+ </div>
188
+ <p className={styles.separator}>{t("or", "or")}</p>
189
+ <Button
190
+ kind="ghost"
191
+ size="sm"
192
+ renderIcon={(props) => <Add size={16} {...props} />}
193
+ onClick={() => setAddBedTagsModal(true)}
194
+ >
195
+ {t("bedTag", "Add Bed Tag")}
196
+ </Button>
197
+ </Tile>
198
+ </div>
199
+ ) : null}
200
+ <Pagination
201
+ page={currentPage}
202
+ pageSize={pageSize}
203
+ pageSizes={[10, 20, 30, 40, 50]}
204
+ totalItems={bedTypeData.length}
205
+ onChange={({ page, pageSize }) => {
206
+ setCurrentPage(page);
207
+ setPageSize(pageSize);
208
+ }}
209
+ />
210
+ </TableContainer>
211
+ )}
212
+ </DataTable>
213
+ </div>
214
+ </>
215
+ );
216
+ };
217
+ export default BedTagAdministrationTable;
@@ -0,0 +1,131 @@
1
+ import React, { useState } from "react";
2
+ import { z } from "zod";
3
+ import { useForm, Controller } from "react-hook-form";
4
+ import { zodResolver } from "@hookform/resolvers/zod";
5
+ import {
6
+ Button,
7
+ ComposedModal,
8
+ Form,
9
+ FormGroup,
10
+ ModalBody,
11
+ ModalFooter,
12
+ ModalHeader,
13
+ Stack,
14
+ TextInput,
15
+ InlineNotification,
16
+ } from "@carbon/react";
17
+ import { useTranslation } from "react-i18next";
18
+ import { Location } from "@openmrs/esm-framework";
19
+ import type { BedTagData } from "../../types";
20
+
21
+ const BedTagAdministrationSchema = z.object({
22
+ name: z.string().max(255),
23
+ });
24
+
25
+ interface BedTagAdministrationFormProps {
26
+ showModal: boolean;
27
+ onModalChange: (showModal: boolean) => void;
28
+ availableBedTypes: Array<BedTagData>;
29
+ allLocations: Location[];
30
+ handleCreateQuestion?: (formData: BedTagData) => void;
31
+ handleDeleteBedTag?: () => void;
32
+ headerTitle: string;
33
+ initialData: BedTagData;
34
+ }
35
+
36
+ interface ErrorType {
37
+ message: string;
38
+ }
39
+
40
+ const BedTagsAdministrationForm: React.FC<BedTagAdministrationFormProps> = ({
41
+ showModal,
42
+ onModalChange,
43
+ handleCreateQuestion,
44
+ headerTitle,
45
+ initialData,
46
+ }) => {
47
+ const { t } = useTranslation();
48
+
49
+ const [showErrorNotification, setShowErrorNotification] = useState(false);
50
+ const [formStateError, setFormStateError] = useState("");
51
+
52
+ const {
53
+ handleSubmit,
54
+ control,
55
+ formState: { isDirty },
56
+ } = useForm<BedTagData>({
57
+ mode: "all",
58
+ resolver: zodResolver(BedTagAdministrationSchema),
59
+ defaultValues: {
60
+ name: initialData.name || "",
61
+ },
62
+ });
63
+
64
+ const onSubmit = (formData: BedTagData) => {
65
+ const result = BedTagAdministrationSchema.safeParse(formData);
66
+ if (result.success) {
67
+ setShowErrorNotification(false);
68
+ handleCreateQuestion(formData);
69
+ }
70
+ };
71
+
72
+ const onError = (error: { [key: string]: ErrorType }) => {
73
+ setFormStateError(Object.entries(error)[0][1].message);
74
+ setShowErrorNotification(true);
75
+ };
76
+
77
+ return (
78
+ <ComposedModal
79
+ open={showModal}
80
+ onClose={() => onModalChange(false)}
81
+ preventCloseOnClickOutside
82
+ >
83
+ <ModalHeader title={headerTitle} />
84
+ <Form onSubmit={handleSubmit(onSubmit, onError)}>
85
+ <ModalBody hasScrollingContent>
86
+ <Stack gap={3}>
87
+ <FormGroup legendText={""}>
88
+ <Controller
89
+ name="name"
90
+ control={control}
91
+ render={({ field, fieldState }) => (
92
+ <>
93
+ <TextInput
94
+ id="bedTag"
95
+ labelText={t("bedTag", "Bed Tag Name")}
96
+ placeholder={t("bedTagPlaceholder", "")}
97
+ invalidText={fieldState.error?.message}
98
+ {...field}
99
+ />
100
+ </>
101
+ )}
102
+ />
103
+ </FormGroup>
104
+
105
+ {showErrorNotification && (
106
+ <InlineNotification
107
+ lowContrast
108
+ title={t("error", "Error")}
109
+ style={{ minWidth: "100%", margin: "0rem", padding: "0rem" }}
110
+ role="alert"
111
+ kind="error"
112
+ subtitle={t("pleaseFillField", formStateError) + "."}
113
+ onClose={() => setShowErrorNotification(false)}
114
+ />
115
+ )}
116
+ </Stack>
117
+ </ModalBody>
118
+ <ModalFooter>
119
+ <Button onClick={() => onModalChange(false)} kind="secondary">
120
+ {t("cancel", "Cancel")}
121
+ </Button>
122
+ <Button disabled={!isDirty} type="submit">
123
+ <span>{t("save", "Save")}</span>
124
+ </Button>
125
+ </ModalFooter>
126
+ </Form>
127
+ </ComposedModal>
128
+ );
129
+ };
130
+
131
+ export default BedTagsAdministrationForm;
@@ -0,0 +1,80 @@
1
+ import React, { useCallback } from "react";
2
+ import { useTranslation } from "react-i18next";
3
+ import { showToast, showNotification, useConfig } from "@openmrs/esm-framework";
4
+
5
+ import { editBedTag, useBedTag } from "../../summary/summary.resource";
6
+ import { BedTagDataAdministration } from "../../bed-administration/bed-administration-types";
7
+ import BedTagsAdministrationForm from "./bed-tags-admin-form.component";
8
+ import { BedTagData, Mutator } from "../../types";
9
+
10
+ interface EditBedTagFormProps {
11
+ showModal: boolean;
12
+ onModalChange: (showModal: boolean) => void;
13
+ editData: BedTagData;
14
+ mutate: Mutator;
15
+ }
16
+
17
+ const EditBedTagForm: React.FC<EditBedTagFormProps> = ({
18
+ showModal,
19
+ onModalChange,
20
+ editData,
21
+ mutate,
22
+ }) => {
23
+ const { t } = useTranslation();
24
+
25
+ const headerTitle = t("editBed", "Edit Tag");
26
+ const { bedTypeData } = useBedTag();
27
+ const availableBedTypes = bedTypeData ? bedTypeData : [];
28
+
29
+ const handleCreateQuestion = useCallback(
30
+ (formData: BedTagDataAdministration) => {
31
+ const bedUuid = editData.uuid;
32
+ const { name } = formData;
33
+ const bedPayload = {
34
+ name,
35
+ };
36
+ editBedTag({ bedPayload, bedTagId: bedUuid })
37
+ .then(() => {
38
+ showToast({
39
+ title: t("formSaved", "Bed Tag"),
40
+ kind: "success",
41
+ critical: true,
42
+ description:
43
+ bedPayload.name +
44
+ " " +
45
+ t("saveSuccessMessage", "was saved successfully."),
46
+ });
47
+
48
+ mutate();
49
+ onModalChange(false);
50
+ })
51
+ .catch((error) => {
52
+ showNotification({
53
+ title: t("errorCreatingForm", "Error creating bed"),
54
+ kind: "error",
55
+ critical: true,
56
+ description: error?.message,
57
+ });
58
+ onModalChange(false);
59
+ });
60
+ onModalChange(false);
61
+ },
62
+ [onModalChange, mutate, editData, t]
63
+ );
64
+
65
+ return (
66
+ <>
67
+ <BedTagsAdministrationForm
68
+ onModalChange={onModalChange}
69
+ availableBedTypes={availableBedTypes}
70
+ showModal={showModal}
71
+ handleCreateQuestion={handleCreateQuestion}
72
+ headerTitle={headerTitle}
73
+ initialData={editData}
74
+ allLocations={[]}
75
+ />
76
+ </>
77
+ );
78
+ };
79
+
80
+ export default EditBedTagForm;
@@ -0,0 +1,83 @@
1
+ import React, { useCallback } from "react";
2
+ import { useTranslation } from "react-i18next";
3
+ import { showToast, showNotification, useConfig } from "@openmrs/esm-framework";
4
+ import { useBedType } from "../../bed-administration/bed-administration.resource";
5
+ import BedTagsAdministrationForm from "./bed-tags-admin-form.component";
6
+ import { saveBedTag, useLocationsByTag } from "../../summary/summary.resource";
7
+ import { BedTagData, Mutator } from "../../types";
8
+
9
+ interface BedTagFormProps {
10
+ showModal: boolean;
11
+ onModalChange: (showModal: boolean) => void;
12
+ mutate: Mutator;
13
+ }
14
+
15
+ const NewTagForm: React.FC<BedTagFormProps> = ({
16
+ showModal,
17
+ onModalChange,
18
+ mutate,
19
+ }) => {
20
+ const { t } = useTranslation();
21
+ const { admissionLocationTagUuid } = useConfig();
22
+ const { data: admissionLocations } = useLocationsByTag(
23
+ admissionLocationTagUuid
24
+ );
25
+ const headerTitle = t("addBedTag", "Create Bed Tag");
26
+ const { bedTypes } = useBedType();
27
+ const availableBedTypes = bedTypes ? bedTypes : [];
28
+
29
+ const initialData: BedTagData = {
30
+ uuid: "",
31
+ name: "",
32
+ };
33
+
34
+ const handleCreateQuestion = useCallback(
35
+ (formData: BedTagData) => {
36
+ const { name } = formData;
37
+
38
+ const bedObject = {
39
+ name,
40
+ };
41
+
42
+ saveBedTag({ bedPayload: bedObject })
43
+ .then(() => {
44
+ showToast({
45
+ title: t("formCreated", "Add Bed Tag"),
46
+ kind: "success",
47
+ critical: true,
48
+ description: `Tag ${name} was created successfully.`,
49
+ });
50
+
51
+ mutate();
52
+ onModalChange(false);
53
+ })
54
+ .catch((error) => {
55
+ showNotification({
56
+ title: t("errorCreatingForm", "Error creating bed"),
57
+ kind: "error",
58
+ critical: true,
59
+ description: error?.message,
60
+ });
61
+ onModalChange(false);
62
+ });
63
+ onModalChange(false);
64
+ },
65
+ [onModalChange, mutate, t]
66
+ );
67
+
68
+ return (
69
+ <>
70
+ <BedTagsAdministrationForm
71
+ onModalChange={onModalChange}
72
+ allLocations={admissionLocations}
73
+ availableBedTypes={availableBedTypes}
74
+ showModal={showModal}
75
+ handleCreateQuestion={handleCreateQuestion}
76
+ headerTitle={headerTitle}
77
+ initialData={initialData}
78
+ />
79
+ </>
80
+ );
81
+ };
82
+
83
+ export default NewTagForm;
@@ -0,0 +1,173 @@
1
+ import React, { useState } from "react";
2
+ import { z } from "zod";
3
+ import { useForm, Controller } from "react-hook-form";
4
+ import { zodResolver } from "@hookform/resolvers/zod";
5
+ import {
6
+ Button,
7
+ ComposedModal,
8
+ Form,
9
+ FormGroup,
10
+ ModalBody,
11
+ ModalFooter,
12
+ ModalHeader,
13
+ Stack,
14
+ TextArea,
15
+ TextInput,
16
+ InlineNotification,
17
+ } from "@carbon/react";
18
+ import { useTranslation } from "react-i18next";
19
+ import { Location } from "@openmrs/esm-framework";
20
+ import type { BedType, BedTypeData } from "../../types";
21
+
22
+ const BedTypeAdministrationSchema = z.object({
23
+ name: z.string().max(255),
24
+ displayName: z.string().max(255),
25
+ description: z.string().max(255),
26
+ });
27
+
28
+ interface BedAdministrationFormProps {
29
+ showModal: boolean;
30
+ onModalChange: (showModal: boolean) => void;
31
+ availableBedTypes: Array<BedType>;
32
+ allLocations: Location[];
33
+ handleCreateQuestion?: (formData: BedTypeData) => void;
34
+ headerTitle: string;
35
+ initialData: BedTypeData;
36
+ }
37
+
38
+ interface ErrorType {
39
+ message: string;
40
+ }
41
+
42
+ const BedTypeAdministrationForm: React.FC<BedAdministrationFormProps> = ({
43
+ showModal,
44
+ onModalChange,
45
+ handleCreateQuestion,
46
+ headerTitle,
47
+ initialData,
48
+ }) => {
49
+ const { t } = useTranslation();
50
+
51
+ const [showErrorNotification, setShowErrorNotification] = useState(false);
52
+ const [formStateError, setFormStateError] = useState("");
53
+
54
+ const {
55
+ handleSubmit,
56
+ control,
57
+ formState: { isDirty },
58
+ } = useForm<BedTypeData>({
59
+ mode: "all",
60
+ resolver: zodResolver(BedTypeAdministrationSchema),
61
+ defaultValues: {
62
+ name: initialData.name || "",
63
+ displayName: initialData.displayName || "",
64
+ description: initialData.description || "",
65
+ },
66
+ });
67
+
68
+ const onSubmit = (formData: BedTypeData) => {
69
+ const result = BedTypeAdministrationSchema.safeParse(formData);
70
+ if (result.success) {
71
+ setShowErrorNotification(false);
72
+ handleCreateQuestion(formData);
73
+ }
74
+ };
75
+
76
+ const onError = (error: { [key: string]: ErrorType }) => {
77
+ setFormStateError(Object.entries(error)[0][1].message);
78
+ setShowErrorNotification(true);
79
+ };
80
+
81
+ return (
82
+ <ComposedModal
83
+ open={showModal}
84
+ onClose={() => onModalChange(false)}
85
+ preventCloseOnClickOutside
86
+ >
87
+ <ModalHeader title={headerTitle} />
88
+ <Form onSubmit={handleSubmit(onSubmit, onError)}>
89
+ <ModalBody hasScrollingContent>
90
+ <Stack gap={3}>
91
+ <FormGroup legendText={""}>
92
+ <Controller
93
+ name="name"
94
+ control={control}
95
+ render={({ field, fieldState }) => (
96
+ <>
97
+ <TextInput
98
+ id="bedName"
99
+ labelText={t("bedName", "Bed Name")}
100
+ placeholder={t("bedTypePlaceholder", "")}
101
+ invalidText={fieldState.error?.message}
102
+ {...field}
103
+ />
104
+ </>
105
+ )}
106
+ />
107
+ </FormGroup>
108
+ <FormGroup legendText={""}>
109
+ <Controller
110
+ name="displayName"
111
+ control={control}
112
+ render={({ field, fieldState }) => (
113
+ <>
114
+ <TextInput
115
+ id="displayName"
116
+ labelText={t("displayName", "Display Name")}
117
+ placeholder={t("displayNamePlaceholder", "")}
118
+ invalidText={fieldState.error?.message}
119
+ {...field}
120
+ />
121
+ </>
122
+ )}
123
+ />
124
+ </FormGroup>
125
+ <FormGroup>
126
+ <Controller
127
+ name="description"
128
+ control={control}
129
+ render={({ field, fieldState }) => (
130
+ <>
131
+ <TextArea
132
+ rows={2}
133
+ id="description"
134
+ invalidText={fieldState?.error?.message}
135
+ labelText={t("description", "Description")}
136
+ {...field}
137
+ placeholder={t(
138
+ "description",
139
+ "Enter the bed description"
140
+ )}
141
+ />
142
+ </>
143
+ )}
144
+ />
145
+ </FormGroup>
146
+
147
+ {showErrorNotification && (
148
+ <InlineNotification
149
+ lowContrast
150
+ title={t("error", "Error")}
151
+ style={{ minWidth: "100%", margin: "0rem", padding: "0rem" }}
152
+ role="alert"
153
+ kind="error"
154
+ subtitle={t("pleaseFillField", formStateError) + "."}
155
+ onClose={() => setShowErrorNotification(false)}
156
+ />
157
+ )}
158
+ </Stack>
159
+ </ModalBody>
160
+ <ModalFooter>
161
+ <Button onClick={() => onModalChange(false)} kind="secondary">
162
+ {t("cancel", "Cancel")}
163
+ </Button>
164
+ <Button disabled={!isDirty} type="submit">
165
+ <span>{t("save", "Save")}</span>
166
+ </Button>
167
+ </ModalFooter>
168
+ </Form>
169
+ </ComposedModal>
170
+ );
171
+ };
172
+
173
+ export default BedTypeAdministrationForm;