@openmrs/esm-react-utils 4.0.0-pre.0 → 4.0.0-pre.1
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/.turbo/turbo-build.log +5 -5
- package/.turbo/turbo-lint.log +1 -1
- package/.turbo/turbo-test.log +45 -45
- package/dist/openmrs-esm-react-utils.js +1 -1
- package/dist/openmrs-esm-react-utils.js.LICENSE.txt +0 -0
- package/dist/openmrs-esm-react-utils.js.map +1 -1
- package/package.json +7 -7
- package/src/ConfigurableLink.tsx +22 -13
- package/src/UserHasAccess.tsx +5 -3
- package/src/useSession.tsx +2 -2
- package/src/useVisit.ts +20 -7
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openmrs/esm-react-utils",
|
|
3
|
-
"version": "4.0.0-pre.
|
|
3
|
+
"version": "4.0.0-pre.1",
|
|
4
4
|
"license": "MPL-2.0",
|
|
5
5
|
"description": "React utilities for OpenMRS.",
|
|
6
6
|
"browser": "dist/openmrs-esm-react-utils.js",
|
|
@@ -55,11 +55,11 @@
|
|
|
55
55
|
"react-i18next": "11.x"
|
|
56
56
|
},
|
|
57
57
|
"devDependencies": {
|
|
58
|
-
"@openmrs/esm-api": "^4.0.0-pre.
|
|
59
|
-
"@openmrs/esm-config": "^4.0.0-pre.
|
|
60
|
-
"@openmrs/esm-error-handling": "^4.0.0-pre.
|
|
61
|
-
"@openmrs/esm-extensions": "^4.0.0-pre.
|
|
62
|
-
"@openmrs/esm-globals": "^4.0.0-pre.
|
|
58
|
+
"@openmrs/esm-api": "^4.0.0-pre.1",
|
|
59
|
+
"@openmrs/esm-config": "^4.0.0-pre.1",
|
|
60
|
+
"@openmrs/esm-error-handling": "^4.0.0-pre.1",
|
|
61
|
+
"@openmrs/esm-extensions": "^4.0.0-pre.1",
|
|
62
|
+
"@openmrs/esm-globals": "^4.0.0-pre.1",
|
|
63
63
|
"dayjs": "^1.10.8",
|
|
64
64
|
"i18next": "^19.6.0",
|
|
65
65
|
"react": "^18.1.0",
|
|
@@ -68,5 +68,5 @@
|
|
|
68
68
|
"rxjs": "^6.5.3",
|
|
69
69
|
"unistore": "^3.5.2"
|
|
70
70
|
},
|
|
71
|
-
"gitHead": "
|
|
71
|
+
"gitHead": "9f58b801f07526083a9edc1cc5a1e58660d71eb0"
|
|
72
72
|
}
|
package/src/ConfigurableLink.tsx
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
/** @module @category Navigation */
|
|
2
2
|
import React, { MouseEvent, AnchorHTMLAttributes } from "react";
|
|
3
|
-
import { navigate, interpolateUrl } from "@openmrs/esm-config";
|
|
3
|
+
import { navigate, interpolateUrl, TemplateParams } from "@openmrs/esm-config";
|
|
4
4
|
|
|
5
|
-
function handleClick(
|
|
5
|
+
function handleClick(
|
|
6
|
+
event: MouseEvent,
|
|
7
|
+
to: string,
|
|
8
|
+
templateParams?: TemplateParams
|
|
9
|
+
) {
|
|
6
10
|
if (
|
|
7
11
|
!event.metaKey &&
|
|
8
12
|
!event.ctrlKey &&
|
|
@@ -10,7 +14,7 @@ function handleClick(event: MouseEvent, to: string) {
|
|
|
10
14
|
event.button == 0
|
|
11
15
|
) {
|
|
12
16
|
event.preventDefault();
|
|
13
|
-
navigate({ to });
|
|
17
|
+
navigate({ to, templateParams });
|
|
14
18
|
}
|
|
15
19
|
}
|
|
16
20
|
|
|
@@ -20,25 +24,30 @@ function handleClick(event: MouseEvent, to: string) {
|
|
|
20
24
|
export interface ConfigurableLinkProps
|
|
21
25
|
extends AnchorHTMLAttributes<HTMLAnchorElement> {
|
|
22
26
|
to: string;
|
|
27
|
+
templateParams?: TemplateParams;
|
|
23
28
|
}
|
|
24
29
|
|
|
25
30
|
/**
|
|
26
31
|
* A React link component which calls [[navigate]] when clicked
|
|
27
32
|
*
|
|
28
33
|
* @param to The target path or URL. Supports interpolation. See [[navigate]]
|
|
34
|
+
* @param urlParams: A dictionary of values to interpolate into the URL, in addition to the default keys `openmrsBase` and `openmrsSpaBase`.
|
|
29
35
|
* @param children Inline elements within the link
|
|
30
36
|
* @param otherProps Any other valid props for an <a> tag except `href` and `onClick`
|
|
31
37
|
*/
|
|
32
|
-
export
|
|
38
|
+
export function ConfigurableLink({
|
|
33
39
|
to,
|
|
40
|
+
templateParams,
|
|
34
41
|
children,
|
|
35
42
|
...otherProps
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
43
|
+
}: ConfigurableLinkProps) {
|
|
44
|
+
return (
|
|
45
|
+
<a
|
|
46
|
+
onClick={(event) => handleClick(event, to, templateParams)}
|
|
47
|
+
href={interpolateUrl(to, templateParams)}
|
|
48
|
+
{...otherProps}
|
|
49
|
+
>
|
|
50
|
+
{children}
|
|
51
|
+
</a>
|
|
52
|
+
);
|
|
53
|
+
}
|
package/src/UserHasAccess.tsx
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
/** @module @category API */
|
|
2
|
+
import { getCurrentUser, LoggedInUser, userHasAccess } from "@openmrs/esm-api";
|
|
2
3
|
import React, { useEffect, useState } from "react";
|
|
3
|
-
import { getCurrentUser, userHasAccess, LoggedInUser } from "@openmrs/esm-api";
|
|
4
4
|
|
|
5
5
|
export interface UserHasAccessProps {
|
|
6
6
|
privilege: string;
|
|
7
|
+
fallback?: React.ReactNode;
|
|
7
8
|
children?: React.ReactNode;
|
|
8
9
|
}
|
|
9
10
|
|
|
10
11
|
export const UserHasAccess: React.FC<UserHasAccessProps> = ({
|
|
11
12
|
privilege,
|
|
13
|
+
fallback,
|
|
12
14
|
children,
|
|
13
15
|
}) => {
|
|
14
16
|
const [user, setUser] = useState<LoggedInUser | null>(null);
|
|
@@ -22,7 +24,7 @@ export const UserHasAccess: React.FC<UserHasAccessProps> = ({
|
|
|
22
24
|
|
|
23
25
|
if (user && userHasAccess(privilege, user)) {
|
|
24
26
|
return <>{children}</>;
|
|
27
|
+
} else {
|
|
28
|
+
return fallback ? <>{fallback}</> : null;
|
|
25
29
|
}
|
|
26
|
-
|
|
27
|
-
return null;
|
|
28
30
|
};
|
package/src/useSession.tsx
CHANGED
|
@@ -98,11 +98,11 @@ export function useSession(): Session {
|
|
|
98
98
|
if (!result) {
|
|
99
99
|
if (promise) {
|
|
100
100
|
console.warn(
|
|
101
|
-
"
|
|
101
|
+
"useSession is in an unexpected state. Attempting to recover."
|
|
102
102
|
);
|
|
103
103
|
throw promise;
|
|
104
104
|
} else {
|
|
105
|
-
throw Error("
|
|
105
|
+
throw Error("useSession is in an invalid state.");
|
|
106
106
|
}
|
|
107
107
|
}
|
|
108
108
|
return result;
|
package/src/useVisit.ts
CHANGED
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
import useSWR from "swr";
|
|
8
8
|
import dayjs from "dayjs";
|
|
9
9
|
import isToday from "dayjs/plugin/isToday";
|
|
10
|
+
import { useMemo } from "react";
|
|
10
11
|
|
|
11
12
|
dayjs.extend(isToday);
|
|
12
13
|
|
|
@@ -15,6 +16,7 @@ interface VisitReturnType {
|
|
|
15
16
|
mutate: () => void;
|
|
16
17
|
isValidating: boolean;
|
|
17
18
|
currentVisit: Visit | null;
|
|
19
|
+
isLoading: boolean;
|
|
18
20
|
}
|
|
19
21
|
|
|
20
22
|
/**
|
|
@@ -28,15 +30,26 @@ export function useVisit(patientUuid: string): VisitReturnType {
|
|
|
28
30
|
const { data, error, mutate, isValidating } = useSWR<{
|
|
29
31
|
data: { results: Array<Visit> };
|
|
30
32
|
}>(
|
|
31
|
-
|
|
33
|
+
patientUuid
|
|
34
|
+
? `/ws/rest/v1/visit?patient=${patientUuid}&v=${defaultVisitCustomRepresentation}&includeInactive=false`
|
|
35
|
+
: null,
|
|
32
36
|
openmrsFetch
|
|
33
37
|
);
|
|
34
38
|
|
|
35
|
-
const currentVisit =
|
|
36
|
-
|
|
37
|
-
(
|
|
38
|
-
|
|
39
|
-
|
|
39
|
+
const currentVisit = useMemo(
|
|
40
|
+
() =>
|
|
41
|
+
data?.data.results.find(
|
|
42
|
+
(visit) =>
|
|
43
|
+
visit.stopDatetime === null && dayjs(visit.startDatetime).isToday()
|
|
44
|
+
) ?? null,
|
|
45
|
+
[data?.data.results]
|
|
46
|
+
);
|
|
40
47
|
|
|
41
|
-
return {
|
|
48
|
+
return {
|
|
49
|
+
error,
|
|
50
|
+
mutate,
|
|
51
|
+
isValidating,
|
|
52
|
+
currentVisit,
|
|
53
|
+
isLoading: !data && !error,
|
|
54
|
+
};
|
|
42
55
|
}
|