@aws-amplify/ui-react-core 3.3.2 → 3.3.4
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/dist/esm/hooks/{useDataState.mjs → useDataState/useDataState.mjs} +15 -4
- package/dist/esm/index.mjs +1 -1
- package/dist/index.js +14 -3
- package/dist/types/hooks/index.d.ts +1 -1
- package/dist/types/hooks/useDataState/index.d.ts +2 -0
- package/dist/types/hooks/{useDataState.d.ts → useDataState/types.d.ts} +0 -4
- package/dist/types/hooks/useDataState/useDataState.d.ts +8 -0
- package/dist/types/hooks/useDataState/useDataState.native.d.ts +1 -0
- package/package.json +2 -2
- package/src/hooks/index.ts +1 -1
- package/src/hooks/useDataState/index.ts +2 -0
- package/src/hooks/useDataState/types.ts +13 -0
- package/src/hooks/useDataState/useDataState.native.ts +3 -0
- package/src/hooks/{useDataState.ts → useDataState/useDataState.ts} +20 -18
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { isFunction } from '@aws-amplify/ui';
|
|
2
1
|
import React__default from 'react';
|
|
2
|
+
import { isFunction } from '@aws-amplify/ui';
|
|
3
3
|
|
|
4
4
|
// default state
|
|
5
5
|
const INITIAL_STATE = { hasError: false, isLoading: false, message: undefined };
|
|
@@ -9,25 +9,36 @@ const resolveMaybeAsync = async (value) => {
|
|
|
9
9
|
const awaited = await value;
|
|
10
10
|
return awaited;
|
|
11
11
|
};
|
|
12
|
+
/**
|
|
13
|
+
* @internal may be updated in future versions
|
|
14
|
+
*/
|
|
12
15
|
function useDataState(action, initialData, options) {
|
|
13
16
|
const [dataState, setDataState] = React__default.useState(() => ({
|
|
14
17
|
...INITIAL_STATE,
|
|
15
18
|
data: initialData,
|
|
16
19
|
}));
|
|
17
20
|
const prevData = React__default.useRef(initialData);
|
|
21
|
+
const pendingId = React__default.useRef();
|
|
18
22
|
const { onSuccess, onError } = options ?? {};
|
|
19
23
|
const handleAction = React__default.useCallback((input) => {
|
|
24
|
+
const id = crypto.randomUUID();
|
|
25
|
+
pendingId.current = id;
|
|
20
26
|
setDataState(({ data }) => ({ ...LOADING_STATE, data }));
|
|
21
27
|
resolveMaybeAsync(action(prevData.current, input))
|
|
22
28
|
.then((data) => {
|
|
29
|
+
if (pendingId.current !== id)
|
|
30
|
+
return;
|
|
31
|
+
prevData.current = data;
|
|
23
32
|
if (isFunction(onSuccess))
|
|
24
33
|
onSuccess(data);
|
|
25
|
-
prevData.current = data;
|
|
26
34
|
setDataState({ ...INITIAL_STATE, data });
|
|
27
35
|
})
|
|
28
|
-
.catch((
|
|
36
|
+
.catch((error) => {
|
|
37
|
+
if (pendingId.current !== id)
|
|
38
|
+
return;
|
|
29
39
|
if (isFunction(onError))
|
|
30
|
-
onError(
|
|
40
|
+
onError(error);
|
|
41
|
+
const { message } = error;
|
|
31
42
|
setDataState(({ data }) => ({ ...ERROR_STATE, data, message }));
|
|
32
43
|
});
|
|
33
44
|
}, [action, onError, onSuccess]);
|
package/dist/esm/index.mjs
CHANGED
|
@@ -9,7 +9,7 @@ export { default as useForm } from './components/FormCore/useForm.mjs';
|
|
|
9
9
|
export { default as FormProvider } from './components/FormCore/FormProvider.mjs';
|
|
10
10
|
export { default as withFormProvider } from './components/FormCore/withFormProvider.mjs';
|
|
11
11
|
export { default as RenderNothing } from './components/RenderNothing/RenderNothing.mjs';
|
|
12
|
-
export { default as useDataState } from './hooks/useDataState.mjs';
|
|
12
|
+
export { default as useDataState } from './hooks/useDataState/useDataState.mjs';
|
|
13
13
|
export { default as useDeprecationWarning } from './hooks/useDeprecationWarning.mjs';
|
|
14
14
|
export { default as useGetUrl } from './hooks/useGetUrl.mjs';
|
|
15
15
|
export { default as useHasValueUpdated } from './hooks/useHasValueUpdated.mjs';
|
package/dist/index.js
CHANGED
|
@@ -563,25 +563,36 @@ const resolveMaybeAsync = async (value) => {
|
|
|
563
563
|
const awaited = await value;
|
|
564
564
|
return awaited;
|
|
565
565
|
};
|
|
566
|
+
/**
|
|
567
|
+
* @internal may be updated in future versions
|
|
568
|
+
*/
|
|
566
569
|
function useDataState(action, initialData, options) {
|
|
567
570
|
const [dataState, setDataState] = React__namespace["default"].useState(() => ({
|
|
568
571
|
...INITIAL_STATE,
|
|
569
572
|
data: initialData,
|
|
570
573
|
}));
|
|
571
574
|
const prevData = React__namespace["default"].useRef(initialData);
|
|
575
|
+
const pendingId = React__namespace["default"].useRef();
|
|
572
576
|
const { onSuccess, onError } = options ?? {};
|
|
573
577
|
const handleAction = React__namespace["default"].useCallback((input) => {
|
|
578
|
+
const id = crypto.randomUUID();
|
|
579
|
+
pendingId.current = id;
|
|
574
580
|
setDataState(({ data }) => ({ ...LOADING_STATE, data }));
|
|
575
581
|
resolveMaybeAsync(action(prevData.current, input))
|
|
576
582
|
.then((data) => {
|
|
583
|
+
if (pendingId.current !== id)
|
|
584
|
+
return;
|
|
585
|
+
prevData.current = data;
|
|
577
586
|
if (ui.isFunction(onSuccess))
|
|
578
587
|
onSuccess(data);
|
|
579
|
-
prevData.current = data;
|
|
580
588
|
setDataState({ ...INITIAL_STATE, data });
|
|
581
589
|
})
|
|
582
|
-
.catch((
|
|
590
|
+
.catch((error) => {
|
|
591
|
+
if (pendingId.current !== id)
|
|
592
|
+
return;
|
|
583
593
|
if (ui.isFunction(onError))
|
|
584
|
-
onError(
|
|
594
|
+
onError(error);
|
|
595
|
+
const { message } = error;
|
|
585
596
|
setDataState(({ data }) => ({ ...ERROR_STATE, data, message }));
|
|
586
597
|
});
|
|
587
598
|
}, [action, onError, onSuccess]);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { useDataState, AsyncDataAction, DataAction, DataState, } from './useDataState';
|
|
2
2
|
export { default as useDeprecationWarning, UseDeprecationWarning, } from './useDeprecationWarning';
|
|
3
3
|
export { default as useGetUrl } from './useGetUrl';
|
|
4
4
|
export { default as useHasValueUpdated } from './useHasValueUpdated';
|
|
@@ -6,7 +6,3 @@ export interface DataState<T> {
|
|
|
6
6
|
}
|
|
7
7
|
export type DataAction<T = any, K = any> = (prevData: T, input: K) => T;
|
|
8
8
|
export type AsyncDataAction<T = any, K = any> = (prevData: T, input: K) => Promise<T>;
|
|
9
|
-
export default function useDataState<T, K>(action: DataAction<T, K> | AsyncDataAction<T, K>, initialData: T, options?: {
|
|
10
|
-
onSuccess?: (data: T) => void;
|
|
11
|
-
onError?: (message: string) => void;
|
|
12
|
-
}): [state: DataState<T>, handleAction: (input: K) => void];
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { AsyncDataAction, DataAction, DataState } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* @internal may be updated in future versions
|
|
4
|
+
*/
|
|
5
|
+
export default function useDataState<T, K>(action: DataAction<T, K> | AsyncDataAction<T, K>, initialData: T, options?: {
|
|
6
|
+
onSuccess?: (data: T) => void;
|
|
7
|
+
onError?: (error: Error) => void;
|
|
8
|
+
}): [state: DataState<T>, handleAction: (input: K) => void];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function useDataState(): void;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aws-amplify/ui-react-core",
|
|
3
|
-
"version": "3.3.
|
|
3
|
+
"version": "3.3.4",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"module": "dist/esm/index.mjs",
|
|
6
6
|
"react-native": "src/index.ts",
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
"typecheck": "tsc --noEmit"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@aws-amplify/ui": "6.
|
|
43
|
+
"@aws-amplify/ui": "6.9.1",
|
|
44
44
|
"@xstate/react": "^3.2.2",
|
|
45
45
|
"lodash": "4.17.21",
|
|
46
46
|
"react-hook-form": "^7.53.2",
|
package/src/hooks/index.ts
CHANGED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export interface DataState<T> {
|
|
2
|
+
data: T;
|
|
3
|
+
hasError: boolean;
|
|
4
|
+
isLoading: boolean;
|
|
5
|
+
message: string | undefined;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export type DataAction<T = any, K = any> = (prevData: T, input: K) => T;
|
|
9
|
+
|
|
10
|
+
export type AsyncDataAction<T = any, K = any> = (
|
|
11
|
+
prevData: T,
|
|
12
|
+
input: K
|
|
13
|
+
) => Promise<T>;
|
|
@@ -1,19 +1,7 @@
|
|
|
1
|
-
import { isFunction } from '@aws-amplify/ui';
|
|
2
1
|
import React from 'react';
|
|
2
|
+
import { isFunction } from '@aws-amplify/ui';
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
data: T;
|
|
6
|
-
hasError: boolean;
|
|
7
|
-
isLoading: boolean;
|
|
8
|
-
message: string | undefined;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export type DataAction<T = any, K = any> = (prevData: T, input: K) => T;
|
|
12
|
-
|
|
13
|
-
export type AsyncDataAction<T = any, K = any> = (
|
|
14
|
-
prevData: T,
|
|
15
|
-
input: K
|
|
16
|
-
) => Promise<T>;
|
|
4
|
+
import { AsyncDataAction, DataAction, DataState } from './types';
|
|
17
5
|
|
|
18
6
|
// default state
|
|
19
7
|
const INITIAL_STATE = { hasError: false, isLoading: false, message: undefined };
|
|
@@ -27,12 +15,15 @@ const resolveMaybeAsync = async <T>(
|
|
|
27
15
|
return awaited;
|
|
28
16
|
};
|
|
29
17
|
|
|
18
|
+
/**
|
|
19
|
+
* @internal may be updated in future versions
|
|
20
|
+
*/
|
|
30
21
|
export default function useDataState<T, K>(
|
|
31
22
|
action: DataAction<T, K> | AsyncDataAction<T, K>,
|
|
32
23
|
initialData: T,
|
|
33
24
|
options?: {
|
|
34
25
|
onSuccess?: (data: T) => void;
|
|
35
|
-
onError?: (
|
|
26
|
+
onError?: (error: Error) => void;
|
|
36
27
|
}
|
|
37
28
|
): [state: DataState<T>, handleAction: (input: K) => void] {
|
|
38
29
|
const [dataState, setDataState] = React.useState<DataState<T>>(() => ({
|
|
@@ -41,22 +32,33 @@ export default function useDataState<T, K>(
|
|
|
41
32
|
}));
|
|
42
33
|
|
|
43
34
|
const prevData = React.useRef(initialData);
|
|
35
|
+
const pendingId = React.useRef<string | undefined>();
|
|
44
36
|
|
|
45
37
|
const { onSuccess, onError } = options ?? {};
|
|
46
38
|
|
|
47
39
|
const handleAction: (input: K) => void = React.useCallback(
|
|
48
40
|
(input) => {
|
|
41
|
+
const id = crypto.randomUUID();
|
|
42
|
+
pendingId.current = id;
|
|
43
|
+
|
|
49
44
|
setDataState(({ data }) => ({ ...LOADING_STATE, data }));
|
|
50
45
|
|
|
51
46
|
resolveMaybeAsync(action(prevData.current, input))
|
|
52
47
|
.then((data: T) => {
|
|
53
|
-
if (
|
|
48
|
+
if (pendingId.current !== id) return;
|
|
54
49
|
|
|
55
50
|
prevData.current = data;
|
|
51
|
+
|
|
52
|
+
if (isFunction(onSuccess)) onSuccess(data);
|
|
53
|
+
|
|
56
54
|
setDataState({ ...INITIAL_STATE, data });
|
|
57
55
|
})
|
|
58
|
-
.catch((
|
|
59
|
-
if (
|
|
56
|
+
.catch((error: Error) => {
|
|
57
|
+
if (pendingId.current !== id) return;
|
|
58
|
+
|
|
59
|
+
if (isFunction(onError)) onError(error);
|
|
60
|
+
|
|
61
|
+
const { message } = error;
|
|
60
62
|
|
|
61
63
|
setDataState(({ data }) => ({ ...ERROR_STATE, data, message }));
|
|
62
64
|
});
|