@openmrs/esm-react-utils 3.2.1-pre.972 → 3.3.1-pre.1173

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/src/useVisit.ts CHANGED
@@ -1,49 +1,42 @@
1
- import { useState, useEffect } from "react";
2
- import dayjs from "dayjs";
1
+ /** @module @category API */
3
2
  import {
4
- getVisitsForPatient,
5
- getStartedVisit,
6
- VisitMode,
7
- VisitStatus,
3
+ defaultVisitCustomRepresentation,
4
+ openmrsFetch,
8
5
  Visit,
9
6
  } from "@openmrs/esm-api";
7
+ import useSWR from "swr";
8
+ import dayjs from "dayjs";
9
+ import isToday from "dayjs/plugin/isToday";
10
10
 
11
- export function useVisit(patientUuid: string) {
12
- const [currentVisit, setCurrentVisit] = useState<Visit | null>(null);
13
- const [error, setError] = useState(null);
14
-
15
- useEffect(() => {
16
- const sub = getStartedVisit.subscribe((visit) => {
17
- if (visit) {
18
- setCurrentVisit(visit?.visitData ?? null);
19
- } else {
20
- setCurrentVisit(null);
21
- }
22
- });
11
+ dayjs.extend(isToday);
23
12
 
24
- return () => sub.unsubscribe();
25
- }, [patientUuid]);
13
+ interface VisitReturnType {
14
+ error: Error;
15
+ mutate: () => void;
16
+ isValidating: boolean;
17
+ currentVisit: Visit | null;
18
+ }
26
19
 
27
- useEffect(() => {
28
- const abortController = new AbortController();
29
- const sub = getVisitsForPatient(patientUuid, abortController).subscribe(
30
- ({ data }) => {
31
- const currentVisit = data.results.find(
32
- (visit) => visit.stopDatetime === null
33
- );
20
+ /**
21
+ * This React hook returns a visit object. If the `patientUuid` is provided
22
+ * as a parameter, then the currentVisit, error and mutate function
23
+ * for that patient visit is returned.
24
+ * @param patientUuid Unique patient identifier `string`
25
+ * @returns Object {`error` `isValidating`, `currentVisit`, `mutate`}
26
+ */
27
+ export function useVisit(patientUuid: string): VisitReturnType {
28
+ const { data, error, mutate, isValidating } = useSWR<{
29
+ data: { results: Array<Visit> };
30
+ }>(
31
+ `/ws/rest/v1/visit?patient=${patientUuid}&v=${defaultVisitCustomRepresentation}&includeInactive=false`,
32
+ openmrsFetch
33
+ );
34
34
 
35
- if (currentVisit) {
36
- getStartedVisit.next({
37
- mode: VisitMode.LOADING,
38
- visitData: currentVisit,
39
- status: VisitStatus.ONGOING,
40
- });
41
- }
42
- },
43
- setError
44
- );
45
- return () => sub && sub.unsubscribe();
46
- }, [patientUuid]);
35
+ const currentVisit =
36
+ data?.data.results.find(
37
+ (visit) =>
38
+ visit.stopDatetime === null && dayjs(visit.startDatetime).isToday()
39
+ ) ?? null;
47
40
 
48
- return { currentVisit, error };
41
+ return { error, mutate, isValidating, currentVisit };
49
42
  }
@@ -1,3 +1,4 @@
1
+ /** @module @category API */
1
2
  import { getVisitTypes, VisitType } from "@openmrs/esm-api";
2
3
  import { useEffect, useState } from "react";
3
4
 
package/webpack.config.js CHANGED
@@ -19,7 +19,7 @@ module.exports = (env) => ({
19
19
  {
20
20
  test: /\.m?(js|ts|tsx)$/,
21
21
  exclude: /(node_modules|bower_components)/,
22
- use: "babel-loader",
22
+ use: "swc-loader",
23
23
  },
24
24
  ],
25
25
  },
package/.babelrc DELETED
@@ -1,8 +0,0 @@
1
- {
2
- "presets": [
3
- "@babel/preset-env",
4
- "@babel/preset-typescript",
5
- "@babel/preset-react"
6
- ],
7
- "plugins": ["@babel/plugin-proposal-class-properties"]
8
- }
package/babel.config.js DELETED
@@ -1,3 +0,0 @@
1
- const { readFileSync } = require("fs");
2
-
3
- module.exports = JSON.parse(readFileSync("./.babelrc", "utf8"));
@@ -1,24 +0,0 @@
1
- import { useCallback } from "react";
2
- import {
3
- ExtensionSlotConfigObject,
4
- ExtensionSlotConfigStore,
5
- getExtensionSlotConfigStore,
6
- } from "@openmrs/esm-config";
7
- import { useStoreState } from "./useStoreState";
8
-
9
- const defaultConfig: ExtensionSlotConfigObject = {
10
- add: [],
11
- order: [],
12
- remove: [],
13
- };
14
-
15
- /** @internal */
16
- export function useExtensionSlotConfig(slotName: string) {
17
- const store = getExtensionSlotConfigStore(slotName);
18
- const select = useCallback(
19
- (s: ExtensionSlotConfigStore) => s.config,
20
- [slotName]
21
- );
22
- const config = useStoreState(store, select);
23
- return config || defaultConfig;
24
- }
@@ -1,12 +0,0 @@
1
- import { useEffect } from "react";
2
- import {
3
- NavigationContext,
4
- pushNavigationContext,
5
- } from "@openmrs/esm-extensions";
6
-
7
- /**
8
- * @deprecated Don't use this anymore.
9
- */
10
- export function useNavigationContext(context: NavigationContext) {
11
- useEffect(() => pushNavigationContext(context), []);
12
- }
@@ -1,11 +0,0 @@
1
- import { subscribeTo } from "@openmrs/esm-state";
2
- import { useEffect, useState } from "react";
3
- import { Store } from "unistore";
4
-
5
- export function useStoreState<T, U>(store: Store<T>, select: (state: T) => U) {
6
- const [state, setState] = useState<U>(() => select(store.getState()));
7
-
8
- useEffect(() => subscribeTo(store, select, setState), [store, select]);
9
-
10
- return state;
11
- }