@openmrs/esm-react-utils 5.1.1-pre.915 → 5.1.1-pre.918

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.915",
3
+ "version": "5.1.1-pre.918",
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.915",
60
- "@openmrs/esm-config": "^5.1.1-pre.915",
61
- "@openmrs/esm-error-handling": "^5.1.1-pre.915",
62
- "@openmrs/esm-extensions": "^5.1.1-pre.915",
63
- "@openmrs/esm-feature-flags": "^5.1.1-pre.915",
64
- "@openmrs/esm-globals": "^5.1.1-pre.915",
59
+ "@openmrs/esm-api": "^5.1.1-pre.918",
60
+ "@openmrs/esm-config": "^5.1.1-pre.918",
61
+ "@openmrs/esm-error-handling": "^5.1.1-pre.918",
62
+ "@openmrs/esm-extensions": "^5.1.1-pre.918",
63
+ "@openmrs/esm-feature-flags": "^5.1.1-pre.918",
64
+ "@openmrs/esm-globals": "^5.1.1-pre.918",
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": "6c2671a1ab45e4543acda991cdc7299c2323dd95"
73
+ "gitHead": "7172c914a4700a8d2984754ef6f67600b6bad1ae"
74
74
  }
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
- isRetrospective: boolean;
22
+ currentVisitIsRetrospective: boolean;
22
23
  isLoading: boolean;
23
24
  }
24
25
 
25
26
  /**
26
- * This React hook returns a visit object. If the `patientUuid` is provided
27
- * as a parameter, then the currentVisit, error and mutate function
28
- * for that patient visit is returned.
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 visitGetUrlSuffix = retrospectiveVisitUuid
42
- ? `/${retrospectiveVisitUuid}`
43
- : `?patient=${patientUuid}&v=${defaultVisitCustomRepresentation}&includeInactive=false`;
44
- const { data, error, mutate, isValidating } = useSWR<{
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${visitGetUrlSuffix}` : null,
65
+ patientUuid ? `/ws/rest/v1/visit${activeVisitUrlSuffix}` : null,
48
66
  openmrsFetch
49
67
  );
50
68
 
51
- const currentVisit = useMemo(
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
- retrospectiveVisitUuid
54
- ? data?.data
55
- : data?.data.results.find((visit) => visit.stopDatetime === null) ??
56
- null,
57
- [data]
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
- isValidating,
96
+ error: activeError || retroError,
97
+ mutate: () => {
98
+ activeMutate();
99
+ retroMutate();
100
+ },
101
+ isValidating: activeIsValidating || retroIsValidating,
102
+ activeVisit,
64
103
  currentVisit,
65
- isRetrospective: Boolean(retrospectiveVisitUuid),
66
- isLoading: !data && !error,
104
+ currentVisitIsRetrospective: Boolean(retrospectiveVisitUuid),
105
+ isLoading: Boolean(
106
+ (!activeData || (retrospectiveVisitUuid && !retroData)) &&
107
+ (!activeError || !retroError)
108
+ ),
67
109
  };
68
110
  }