@openmrs/esm-react-utils 5.1.1-pre.916 → 5.1.1-pre.922
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.
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openmrs/esm-react-utils",
|
|
3
|
-
"version": "5.1.1-pre.
|
|
3
|
+
"version": "5.1.1-pre.922",
|
|
4
4
|
"license": "MPL-2.0",
|
|
5
5
|
"description": "React utilities for OpenMRS.",
|
|
6
6
|
"browser": "dist/openmrs-esm-react-utils.js",
|
|
@@ -56,12 +56,12 @@
|
|
|
56
56
|
"react-i18next": "11.x"
|
|
57
57
|
},
|
|
58
58
|
"devDependencies": {
|
|
59
|
-
"@openmrs/esm-api": "^5.1.1-pre.
|
|
60
|
-
"@openmrs/esm-config": "^5.1.1-pre.
|
|
61
|
-
"@openmrs/esm-error-handling": "^5.1.1-pre.
|
|
62
|
-
"@openmrs/esm-extensions": "^5.1.1-pre.
|
|
63
|
-
"@openmrs/esm-feature-flags": "^5.1.1-pre.
|
|
64
|
-
"@openmrs/esm-globals": "^5.1.1-pre.
|
|
59
|
+
"@openmrs/esm-api": "^5.1.1-pre.922",
|
|
60
|
+
"@openmrs/esm-config": "^5.1.1-pre.922",
|
|
61
|
+
"@openmrs/esm-error-handling": "^5.1.1-pre.922",
|
|
62
|
+
"@openmrs/esm-extensions": "^5.1.1-pre.922",
|
|
63
|
+
"@openmrs/esm-feature-flags": "^5.1.1-pre.922",
|
|
64
|
+
"@openmrs/esm-globals": "^5.1.1-pre.922",
|
|
65
65
|
"dayjs": "^1.10.8",
|
|
66
66
|
"i18next": "^19.6.0",
|
|
67
67
|
"react": "^18.1.0",
|
|
@@ -70,5 +70,5 @@
|
|
|
70
70
|
"rxjs": "^6.5.3",
|
|
71
71
|
"webpack": "^5.88.0"
|
|
72
72
|
},
|
|
73
|
-
"gitHead": "
|
|
73
|
+
"gitHead": "24aac0ad2dce05e4e78accabb4c21b313e074162"
|
|
74
74
|
}
|
package/src/usePagination.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/** @module @category UI */
|
|
2
|
-
import { useMemo, useState } from "react";
|
|
2
|
+
import { useCallback, useMemo, useState } from "react";
|
|
3
3
|
|
|
4
4
|
const defaultResultsPerPage = 10;
|
|
5
5
|
|
|
@@ -22,25 +22,47 @@ export function usePagination<T>(
|
|
|
22
22
|
return data.slice(lowerBound, upperBound);
|
|
23
23
|
}, [data, page, resultsPerPage]);
|
|
24
24
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
totalPages,
|
|
28
|
-
currentPage: page,
|
|
29
|
-
paginated: data.length > resultsPerPage,
|
|
30
|
-
showNextButton: page < totalPages,
|
|
31
|
-
showPreviousButton: page > 1,
|
|
32
|
-
goTo(page: number) {
|
|
25
|
+
const goTo = useCallback(
|
|
26
|
+
(page: number) => {
|
|
33
27
|
setPage(Math.max(1, Math.min(totalPages, page)));
|
|
34
28
|
},
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
29
|
+
[setPage, totalPages]
|
|
30
|
+
);
|
|
31
|
+
const goToNext = useCallback(() => {
|
|
32
|
+
if (page < totalPages) {
|
|
33
|
+
setPage(page + 1);
|
|
34
|
+
}
|
|
35
|
+
}, [page, totalPages, setPage]);
|
|
36
|
+
|
|
37
|
+
const goToPrevious = useCallback(() => {
|
|
38
|
+
if (page > 1) {
|
|
39
|
+
setPage(page - 1);
|
|
40
|
+
}
|
|
41
|
+
}, [page, setPage]);
|
|
42
|
+
|
|
43
|
+
const memoisedPaginatedData = useMemo(
|
|
44
|
+
() => ({
|
|
45
|
+
results,
|
|
46
|
+
totalPages,
|
|
47
|
+
currentPage: page,
|
|
48
|
+
paginated: data.length > resultsPerPage,
|
|
49
|
+
showNextButton: page < totalPages,
|
|
50
|
+
showPreviousButton: page > 1,
|
|
51
|
+
goTo,
|
|
52
|
+
goToNext,
|
|
53
|
+
goToPrevious,
|
|
54
|
+
}),
|
|
55
|
+
[
|
|
56
|
+
results,
|
|
57
|
+
totalPages,
|
|
58
|
+
data.length,
|
|
59
|
+
resultsPerPage,
|
|
60
|
+
page,
|
|
61
|
+
goTo,
|
|
62
|
+
goToNext,
|
|
63
|
+
goToPrevious,
|
|
64
|
+
]
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
return memoisedPaginatedData;
|
|
46
68
|
}
|
package/src/useVisit.ts
CHANGED
|
@@ -17,17 +17,30 @@ export interface VisitReturnType {
|
|
|
17
17
|
error: Error;
|
|
18
18
|
mutate: () => void;
|
|
19
19
|
isValidating: boolean;
|
|
20
|
+
activeVisit: Visit | null;
|
|
20
21
|
currentVisit: Visit | null;
|
|
21
|
-
|
|
22
|
+
currentVisitIsRetrospective: boolean;
|
|
22
23
|
isLoading: boolean;
|
|
23
24
|
}
|
|
24
25
|
|
|
25
26
|
/**
|
|
26
|
-
* This React hook returns
|
|
27
|
-
*
|
|
28
|
-
*
|
|
27
|
+
* This React hook returns visit information if the patient UUID is not null. There are
|
|
28
|
+
* potentially two relevant visits at a time: "active" and "current".
|
|
29
|
+
*
|
|
30
|
+
* The active visit is the most recent visit without an end date. The presence of an active
|
|
31
|
+
* visit generally means that the patient is in the facility.
|
|
32
|
+
*
|
|
33
|
+
* The current visit is the active visit, unless a retrospective visit has been selected.
|
|
34
|
+
* If there is no active visit and no selected retrospective visit, then there is no
|
|
35
|
+
* current visit. If there is no active visit but there is a retrospective visit, then
|
|
36
|
+
* the retrospective visit is the current visit. `currentVisitIsRetrospective` tells you
|
|
37
|
+
* whether the current visit is a retrospective visit.
|
|
38
|
+
*
|
|
39
|
+
* The active visit and current visit require two separate API calls. `error` contains
|
|
40
|
+
* the error from either one, if there is an error. `isValidating` is true if either
|
|
41
|
+
* API call is in progress. `mutate` refreshes the data from both API calls.
|
|
42
|
+
*
|
|
29
43
|
* @param patientUuid Unique patient identifier `string`
|
|
30
|
-
* @returns Object {`error` `isValidating`, `currentVisit`, `mutate`}
|
|
31
44
|
*/
|
|
32
45
|
export function useVisit(patientUuid: string): VisitReturnType {
|
|
33
46
|
const { patientUuid: visitStorePatientUuid, manuallySetVisitUuid } = useStore(
|
|
@@ -38,31 +51,60 @@ export function useVisit(patientUuid: string): VisitReturnType {
|
|
|
38
51
|
patientUuid && visitStorePatientUuid == patientUuid
|
|
39
52
|
? manuallySetVisitUuid
|
|
40
53
|
: null;
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
const {
|
|
54
|
+
const activeVisitUrlSuffix = `?patient=${patientUuid}&v=${defaultVisitCustomRepresentation}&includeInactive=false`;
|
|
55
|
+
const retrospectiveVisitUrlSuffix = `/${retrospectiveVisitUuid}`;
|
|
56
|
+
|
|
57
|
+
const {
|
|
58
|
+
data: activeData,
|
|
59
|
+
error: activeError,
|
|
60
|
+
mutate: activeMutate,
|
|
61
|
+
isValidating: activeIsValidating,
|
|
62
|
+
} = useSWR<{
|
|
45
63
|
data: Visit | { results: Array<Visit> };
|
|
46
64
|
}>(
|
|
47
|
-
patientUuid ? `/ws/rest/v1/visit${
|
|
65
|
+
patientUuid ? `/ws/rest/v1/visit${activeVisitUrlSuffix}` : null,
|
|
48
66
|
openmrsFetch
|
|
49
67
|
);
|
|
50
68
|
|
|
51
|
-
const
|
|
69
|
+
const {
|
|
70
|
+
data: retroData,
|
|
71
|
+
error: retroError,
|
|
72
|
+
mutate: retroMutate,
|
|
73
|
+
isValidating: retroIsValidating,
|
|
74
|
+
} = useSWR<{
|
|
75
|
+
data: Visit | { results: Array<Visit> };
|
|
76
|
+
}>(
|
|
77
|
+
patientUuid && retrospectiveVisitUuid
|
|
78
|
+
? `/ws/rest/v1/visit${retrospectiveVisitUrlSuffix}`
|
|
79
|
+
: null,
|
|
80
|
+
openmrsFetch
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
const activeVisit = useMemo(
|
|
52
84
|
() =>
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
85
|
+
activeData?.data.results.find((visit) => visit.stopDatetime === null) ??
|
|
86
|
+
null,
|
|
87
|
+
[activeData]
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
const currentVisit = useMemo(
|
|
91
|
+
() => (retrospectiveVisitUuid ? retroData?.data : activeVisit ?? null),
|
|
92
|
+
[retroData, activeVisit, retrospectiveVisitUuid]
|
|
58
93
|
);
|
|
59
94
|
|
|
60
95
|
return {
|
|
61
|
-
error,
|
|
62
|
-
mutate
|
|
63
|
-
|
|
96
|
+
error: activeError || retroError,
|
|
97
|
+
mutate: () => {
|
|
98
|
+
activeMutate();
|
|
99
|
+
retroMutate();
|
|
100
|
+
},
|
|
101
|
+
isValidating: activeIsValidating || retroIsValidating,
|
|
102
|
+
activeVisit,
|
|
64
103
|
currentVisit,
|
|
65
|
-
|
|
66
|
-
isLoading:
|
|
104
|
+
currentVisitIsRetrospective: Boolean(retrospectiveVisitUuid),
|
|
105
|
+
isLoading: Boolean(
|
|
106
|
+
(!activeData || (retrospectiveVisitUuid && !retroData)) &&
|
|
107
|
+
(!activeError || !retroError)
|
|
108
|
+
),
|
|
67
109
|
};
|
|
68
110
|
}
|