@refinedev/core 4.22.0 → 4.24.0
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/CHANGELOG.md +180 -0
- package/dist/components/canAccess/index.d.ts.map +1 -1
- package/dist/contexts/refine/IRefineContext.d.ts +3 -0
- package/dist/contexts/refine/IRefineContext.d.ts.map +1 -1
- package/dist/contexts/refine/index.d.ts.map +1 -1
- package/dist/definitions/helpers/generateDocumentTitle/index.d.ts.map +1 -1
- package/dist/definitions/helpers/handleRefineOptions/index.d.ts.map +1 -1
- package/dist/definitions/helpers/pick-resource/index.d.ts.map +1 -1
- package/dist/esm/index.js +6 -6
- package/dist/esm/index.js.map +1 -1
- package/dist/hooks/data/useCreate.d.ts +3 -2
- package/dist/hooks/data/useCreate.d.ts.map +1 -1
- package/dist/hooks/data/useCreateMany.d.ts +3 -2
- package/dist/hooks/data/useCreateMany.d.ts.map +1 -1
- package/dist/hooks/data/useCustom.d.ts +3 -2
- package/dist/hooks/data/useCustom.d.ts.map +1 -1
- package/dist/hooks/data/useCustomMutation.d.ts +3 -2
- package/dist/hooks/data/useCustomMutation.d.ts.map +1 -1
- package/dist/hooks/data/useDelete.d.ts +4 -3
- package/dist/hooks/data/useDelete.d.ts.map +1 -1
- package/dist/hooks/data/useDeleteMany.d.ts +4 -3
- package/dist/hooks/data/useDeleteMany.d.ts.map +1 -1
- package/dist/hooks/data/useInfiniteList.d.ts +3 -2
- package/dist/hooks/data/useInfiniteList.d.ts.map +1 -1
- package/dist/hooks/data/useList.d.ts +3 -2
- package/dist/hooks/data/useList.d.ts.map +1 -1
- package/dist/hooks/data/useMany.d.ts +3 -2
- package/dist/hooks/data/useMany.d.ts.map +1 -1
- package/dist/hooks/data/useOne.d.ts +3 -2
- package/dist/hooks/data/useOne.d.ts.map +1 -1
- package/dist/hooks/data/useUpdate.d.ts +4 -3
- package/dist/hooks/data/useUpdate.d.ts.map +1 -1
- package/dist/hooks/data/useUpdateMany.d.ts +4 -3
- package/dist/hooks/data/useUpdateMany.d.ts.map +1 -1
- package/dist/hooks/export/index.d.ts.map +1 -1
- package/dist/hooks/form/useForm.d.ts +4 -3
- package/dist/hooks/form/useForm.d.ts.map +1 -1
- package/dist/hooks/index.d.ts +1 -0
- package/dist/hooks/index.d.ts.map +1 -1
- package/dist/hooks/live/useResourceSubscription/index.d.ts +1 -1
- package/dist/hooks/live/useResourceSubscription/index.d.ts.map +1 -1
- package/dist/hooks/resource/useResource/index.d.ts +11 -1
- package/dist/hooks/resource/useResource/index.d.ts.map +1 -1
- package/dist/hooks/show/useShow.d.ts +4 -3
- package/dist/hooks/show/useShow.d.ts.map +1 -1
- package/dist/hooks/useLoadingOvertime/index.d.ts +49 -0
- package/dist/hooks/useLoadingOvertime/index.d.ts.map +1 -0
- package/dist/hooks/useSelect/index.d.ts +3 -2
- package/dist/hooks/useSelect/index.d.ts.map +1 -1
- package/dist/hooks/useTable/index.d.ts +4 -3
- package/dist/hooks/useTable/index.d.ts.map +1 -1
- package/dist/iife/index.js +6 -6
- package/dist/iife/index.js.map +1 -1
- package/dist/index.js +6 -6
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/components/canAccess/index.tsx +17 -92
- package/src/contexts/refine/IRefineContext.ts +3 -0
- package/src/contexts/refine/index.tsx +3 -0
- package/src/definitions/helpers/generateDocumentTitle/index.ts +5 -3
- package/src/definitions/helpers/handleRefineOptions/index.ts +1 -0
- package/src/definitions/helpers/pick-resource/index.ts +4 -3
- package/src/hooks/data/useCreate.ts +49 -23
- package/src/hooks/data/useCreateMany.ts +54 -26
- package/src/hooks/data/useCustom.ts +17 -3
- package/src/hooks/data/useCustomMutation.ts +16 -3
- package/src/hooks/data/useDelete.ts +60 -31
- package/src/hooks/data/useDeleteMany.ts +64 -31
- package/src/hooks/data/useInfiniteList.ts +30 -14
- package/src/hooks/data/useList.ts +30 -20
- package/src/hooks/data/useMany.ts +34 -23
- package/src/hooks/data/useOne.ts +36 -22
- package/src/hooks/data/useUpdate.ts +55 -32
- package/src/hooks/data/useUpdateMany.ts +69 -33
- package/src/hooks/export/index.ts +12 -11
- package/src/hooks/form/useForm.ts +46 -98
- package/src/hooks/import/index.tsx +17 -17
- package/src/hooks/index.ts +1 -0
- package/src/hooks/live/useResourceSubscription/index.ts +6 -3
- package/src/hooks/resource/useResource/index.ts +48 -1
- package/src/hooks/show/useShow.ts +34 -80
- package/src/hooks/useLoadingOvertime/index.ts +114 -0
- package/src/hooks/useSelect/index.ts +21 -14
- package/src/hooks/useTable/index.ts +22 -8
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@refinedev/core",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.24.0",
|
|
4
4
|
"description": "refine is a React-based framework for building internal tools, rapidly. It ships with Ant Design System, an enterprise-level UI toolkit.",
|
|
5
5
|
"private": false,
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -1,20 +1,7 @@
|
|
|
1
1
|
import React from "react";
|
|
2
2
|
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
useParsed,
|
|
6
|
-
useResource,
|
|
7
|
-
useResourceWithRoute,
|
|
8
|
-
useRouterContext,
|
|
9
|
-
useRouterType,
|
|
10
|
-
} from "@hooks";
|
|
11
|
-
import {
|
|
12
|
-
BaseKey,
|
|
13
|
-
IResourceItem,
|
|
14
|
-
ITreeMenu,
|
|
15
|
-
ResourceRouterParams,
|
|
16
|
-
} from "../../interfaces";
|
|
17
|
-
import { pickResource } from "@definitions/helpers/pick-resource";
|
|
3
|
+
import { useCan, useResource } from "@hooks";
|
|
4
|
+
import { BaseKey, IResourceItem, ITreeMenu } from "../../interfaces";
|
|
18
5
|
|
|
19
6
|
type CanAccessBaseProps = {
|
|
20
7
|
/**
|
|
@@ -54,95 +41,33 @@ export type CanAccessProps = CanAccessBaseProps | CanAccessWithoutParamsProps;
|
|
|
54
41
|
|
|
55
42
|
export const CanAccess: React.FC<CanAccessProps> = ({
|
|
56
43
|
resource: resourceFromProp,
|
|
57
|
-
action,
|
|
44
|
+
action: actionFromProp,
|
|
58
45
|
params,
|
|
59
46
|
fallback,
|
|
60
47
|
children,
|
|
61
48
|
...rest
|
|
62
49
|
}) => {
|
|
63
|
-
const routerType = useRouterType();
|
|
64
|
-
const { resources } = useResource();
|
|
65
|
-
const { useParams } = useRouterContext();
|
|
66
50
|
const {
|
|
67
|
-
resource
|
|
68
|
-
id:
|
|
69
|
-
action:
|
|
70
|
-
} =
|
|
51
|
+
resource,
|
|
52
|
+
id: idFromRoute,
|
|
53
|
+
action: actionFromRoute,
|
|
54
|
+
} = useResource(resourceFromProp);
|
|
55
|
+
const { identifier } = useResource();
|
|
71
56
|
|
|
72
|
-
const {
|
|
73
|
-
|
|
74
|
-
id: legacyIdFromParams,
|
|
75
|
-
action: legacyActionFromParams,
|
|
76
|
-
} = useParams<ResourceRouterParams>();
|
|
77
|
-
|
|
78
|
-
const newResourceNameFromRouter = resourceFromRouter?.name;
|
|
79
|
-
|
|
80
|
-
/** We only accept `id` from URL params if `resource` is not explicitly passed. */
|
|
81
|
-
/** This is done to avoid sending wrong requests for custom `resource` and an async `id` */
|
|
82
|
-
const defaultId =
|
|
83
|
-
!resourceFromProp ||
|
|
84
|
-
resourceFromProp ===
|
|
85
|
-
(routerType === "legacy"
|
|
86
|
-
? legacyResourceFromRoute
|
|
87
|
-
: newResourceNameFromRouter)
|
|
88
|
-
? params?.id ??
|
|
89
|
-
(routerType === "legacy" ? legacyIdFromParams : idFromRouter)
|
|
90
|
-
: params?.id;
|
|
91
|
-
|
|
92
|
-
/** `resourceName` fallback value depends on the router type */
|
|
93
|
-
const resourceName =
|
|
94
|
-
resourceFromProp ??
|
|
95
|
-
(routerType === "legacy"
|
|
96
|
-
? legacyResourceFromRoute
|
|
97
|
-
: newResourceNameFromRouter);
|
|
98
|
-
|
|
99
|
-
let resource: IResourceItem | undefined;
|
|
57
|
+
const getDefaultId = () => {
|
|
58
|
+
const idFromPropsOrRoute = params?.id ?? idFromRoute;
|
|
100
59
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
if (routerType === "legacy") {
|
|
104
|
-
if (resourceName) {
|
|
105
|
-
resource = resourceWithRoute(resourceName);
|
|
106
|
-
}
|
|
107
|
-
} else {
|
|
108
|
-
/** If `resource` is provided by the user, then try to pick the resource of create a dummy one */
|
|
109
|
-
if (resourceFromProp) {
|
|
110
|
-
const picked = pickResource(resourceFromProp, resources);
|
|
111
|
-
if (picked) {
|
|
112
|
-
resource = picked;
|
|
113
|
-
} else {
|
|
114
|
-
resource = {
|
|
115
|
-
name: resourceFromProp,
|
|
116
|
-
route: resourceFromProp,
|
|
117
|
-
};
|
|
118
|
-
}
|
|
119
|
-
} else {
|
|
120
|
-
/** If `resource` is not provided, check the resource from the router params */
|
|
121
|
-
if (typeof resourceFromRouter === "string") {
|
|
122
|
-
const picked = pickResource(resourceFromRouter, resources);
|
|
123
|
-
if (picked) {
|
|
124
|
-
resource = picked;
|
|
125
|
-
} else {
|
|
126
|
-
resource = {
|
|
127
|
-
name: resourceFromRouter,
|
|
128
|
-
route: resourceFromRouter,
|
|
129
|
-
};
|
|
130
|
-
}
|
|
131
|
-
} else {
|
|
132
|
-
/** If `resource` is passed as an IResourceItem, use it or `resource` is undefined and cannot be inferred. */
|
|
133
|
-
resource = resourceFromRouter;
|
|
134
|
-
}
|
|
60
|
+
if (resourceFromProp && resourceFromProp !== identifier) {
|
|
61
|
+
return params?.id;
|
|
135
62
|
}
|
|
136
|
-
|
|
63
|
+
|
|
64
|
+
return idFromPropsOrRoute;
|
|
65
|
+
};
|
|
66
|
+
const defaultId = getDefaultId();
|
|
137
67
|
|
|
138
68
|
const { data } = useCan({
|
|
139
69
|
resource: resourceFromProp ?? resource?.name,
|
|
140
|
-
action:
|
|
141
|
-
action ??
|
|
142
|
-
(routerType === "legacy"
|
|
143
|
-
? legacyActionFromParams
|
|
144
|
-
: actionFromRouter) ??
|
|
145
|
-
"",
|
|
70
|
+
action: actionFromProp ?? actionFromRoute ?? "",
|
|
146
71
|
params: params ?? {
|
|
147
72
|
id: defaultId,
|
|
148
73
|
resource: resource,
|
|
@@ -10,6 +10,7 @@ import {
|
|
|
10
10
|
LiveModeProps,
|
|
11
11
|
RedirectAction,
|
|
12
12
|
} from "../../interfaces";
|
|
13
|
+
import { UseLoadingOvertimeRefineContext } from "../../hooks/useLoadingOvertime";
|
|
13
14
|
|
|
14
15
|
export interface IRefineOptions {
|
|
15
16
|
breadcrumb?: ReactNode;
|
|
@@ -28,6 +29,7 @@ export interface IRefineOptions {
|
|
|
28
29
|
clientConfig?: QueryClientConfig | InstanceType<typeof QueryClient>;
|
|
29
30
|
devtoolConfig?: React.ComponentProps<typeof ReactQueryDevtools> | false;
|
|
30
31
|
};
|
|
32
|
+
overtime?: UseLoadingOvertimeRefineContext;
|
|
31
33
|
}
|
|
32
34
|
|
|
33
35
|
export interface IRefineContextOptions {
|
|
@@ -42,6 +44,7 @@ export interface IRefineContextOptions {
|
|
|
42
44
|
afterClone: RedirectAction;
|
|
43
45
|
afterEdit: RedirectAction;
|
|
44
46
|
};
|
|
47
|
+
overtime: UseLoadingOvertimeRefineContext;
|
|
45
48
|
}
|
|
46
49
|
|
|
47
50
|
export interface IRefineContext {
|
|
@@ -26,12 +26,14 @@ export function generateDefaultDocumentTitle(
|
|
|
26
26
|
list: "",
|
|
27
27
|
};
|
|
28
28
|
|
|
29
|
+
const identifier = resource?.identifier ?? resource?.name;
|
|
30
|
+
|
|
29
31
|
const resourceName =
|
|
30
32
|
resource?.label ??
|
|
31
33
|
resource?.meta?.label ??
|
|
32
34
|
capitalize(
|
|
33
35
|
userFriendlyResourceName(
|
|
34
|
-
|
|
36
|
+
identifier,
|
|
35
37
|
action === "list" ? "plural" : "singular",
|
|
36
38
|
),
|
|
37
39
|
);
|
|
@@ -40,9 +42,9 @@ export function generateDefaultDocumentTitle(
|
|
|
40
42
|
const suffix = translate("documentTitle.suffix", " | refine");
|
|
41
43
|
let autoGeneratedTitle = defaultTitle;
|
|
42
44
|
|
|
43
|
-
if (action &&
|
|
45
|
+
if (action && identifier) {
|
|
44
46
|
autoGeneratedTitle = translate(
|
|
45
|
-
`documentTitle.${
|
|
47
|
+
`documentTitle.${identifier}.${action}`,
|
|
46
48
|
{ id },
|
|
47
49
|
`${
|
|
48
50
|
actionPrefixMatcher[
|
|
@@ -32,9 +32,10 @@ export const pickResource = (
|
|
|
32
32
|
return resource;
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
35
|
+
let resource = resources.find((r) => r.identifier === identifier);
|
|
36
|
+
if (!resource) {
|
|
37
|
+
resource = resources.find((r) => r.name === identifier);
|
|
38
|
+
}
|
|
38
39
|
|
|
39
40
|
return resource;
|
|
40
41
|
};
|
|
@@ -29,6 +29,11 @@ import {
|
|
|
29
29
|
useOnError,
|
|
30
30
|
useMeta,
|
|
31
31
|
} from "@hooks";
|
|
32
|
+
import {
|
|
33
|
+
useLoadingOvertime,
|
|
34
|
+
UseLoadingOvertimeOptionsProps,
|
|
35
|
+
UseLoadingOvertimeReturnType,
|
|
36
|
+
} from "../useLoadingOvertime";
|
|
32
37
|
|
|
33
38
|
type useCreateParams<TData, TError, TVariables> = {
|
|
34
39
|
/**
|
|
@@ -83,7 +88,7 @@ export type UseCreateProps<
|
|
|
83
88
|
>,
|
|
84
89
|
"mutationFn" | "onError" | "onSuccess"
|
|
85
90
|
>;
|
|
86
|
-
};
|
|
91
|
+
} & UseLoadingOvertimeOptionsProps;
|
|
87
92
|
|
|
88
93
|
/**
|
|
89
94
|
* `useCreate` is a modified version of `react-query`'s {@link https://react-query.tanstack.com/reference/useMutation `useMutation`} for create mutations.
|
|
@@ -104,18 +109,20 @@ export const useCreate = <
|
|
|
104
109
|
TVariables = {},
|
|
105
110
|
>({
|
|
106
111
|
mutationOptions,
|
|
112
|
+
overtimeOptions,
|
|
107
113
|
}: UseCreateProps<TData, TError, TVariables> = {}): UseCreateReturnType<
|
|
108
114
|
TData,
|
|
109
115
|
TError,
|
|
110
116
|
TVariables
|
|
111
|
-
>
|
|
117
|
+
> &
|
|
118
|
+
UseLoadingOvertimeReturnType => {
|
|
112
119
|
const authProvider = useActiveAuthProvider();
|
|
113
120
|
const { mutate: checkError } = useOnError({
|
|
114
121
|
v3LegacyAuthProviderCompatible: Boolean(authProvider?.isLegacy),
|
|
115
122
|
});
|
|
116
123
|
const dataProvider = useDataProvider();
|
|
117
124
|
const invalidateStore = useInvalidate();
|
|
118
|
-
const { resources } = useResource();
|
|
125
|
+
const { resources, select } = useResource();
|
|
119
126
|
const translate = useTranslate();
|
|
120
127
|
const publish = usePublish();
|
|
121
128
|
const { log } = useLog();
|
|
@@ -129,20 +136,23 @@ export const useCreate = <
|
|
|
129
136
|
unknown
|
|
130
137
|
>(
|
|
131
138
|
({
|
|
132
|
-
resource,
|
|
139
|
+
resource: resourceName,
|
|
133
140
|
values,
|
|
134
141
|
meta,
|
|
135
142
|
metaData,
|
|
136
143
|
dataProviderName,
|
|
137
144
|
}: useCreateParams<TData, TError, TVariables>) => {
|
|
145
|
+
const { resource, identifier } = select(resourceName);
|
|
146
|
+
|
|
138
147
|
const combinedMeta = getMeta({
|
|
148
|
+
resource,
|
|
139
149
|
meta: pickNotDeprecated(meta, metaData),
|
|
140
150
|
});
|
|
141
151
|
|
|
142
152
|
return dataProvider(
|
|
143
|
-
pickDataProvider(
|
|
153
|
+
pickDataProvider(identifier, dataProviderName, resources),
|
|
144
154
|
).create<TData, TVariables>({
|
|
145
|
-
resource,
|
|
155
|
+
resource: resource.name,
|
|
146
156
|
variables: values,
|
|
147
157
|
meta: combinedMeta,
|
|
148
158
|
metaData: combinedMeta,
|
|
@@ -152,7 +162,7 @@ export const useCreate = <
|
|
|
152
162
|
onSuccess: (
|
|
153
163
|
data,
|
|
154
164
|
{
|
|
155
|
-
resource,
|
|
165
|
+
resource: resourceName,
|
|
156
166
|
successNotification: successNotificationFromProp,
|
|
157
167
|
dataProviderName,
|
|
158
168
|
invalidates = ["list", "many"],
|
|
@@ -161,20 +171,22 @@ export const useCreate = <
|
|
|
161
171
|
metaData,
|
|
162
172
|
},
|
|
163
173
|
) => {
|
|
164
|
-
const
|
|
174
|
+
const { resource, identifier } = select(resourceName);
|
|
175
|
+
|
|
176
|
+
const resourceSingular = pluralize.singular(identifier);
|
|
165
177
|
|
|
166
178
|
const notificationConfig =
|
|
167
179
|
typeof successNotificationFromProp === "function"
|
|
168
|
-
? successNotificationFromProp(data, values,
|
|
180
|
+
? successNotificationFromProp(data, values, identifier)
|
|
169
181
|
: successNotificationFromProp;
|
|
170
182
|
|
|
171
183
|
handleNotification(notificationConfig, {
|
|
172
|
-
key: `create-${
|
|
184
|
+
key: `create-${identifier}-notification`,
|
|
173
185
|
message: translate(
|
|
174
186
|
"notifications.createSuccess",
|
|
175
187
|
{
|
|
176
188
|
resource: translate(
|
|
177
|
-
`${
|
|
189
|
+
`${identifier}.${identifier}`,
|
|
178
190
|
resourceSingular,
|
|
179
191
|
),
|
|
180
192
|
},
|
|
@@ -185,9 +197,9 @@ export const useCreate = <
|
|
|
185
197
|
});
|
|
186
198
|
|
|
187
199
|
invalidateStore({
|
|
188
|
-
resource,
|
|
200
|
+
resource: identifier,
|
|
189
201
|
dataProviderName: pickDataProvider(
|
|
190
|
-
|
|
202
|
+
identifier,
|
|
191
203
|
dataProviderName,
|
|
192
204
|
resources,
|
|
193
205
|
),
|
|
@@ -195,7 +207,7 @@ export const useCreate = <
|
|
|
195
207
|
});
|
|
196
208
|
|
|
197
209
|
publish?.({
|
|
198
|
-
channel: `resources/${resource}`,
|
|
210
|
+
channel: `resources/${resource.name}`,
|
|
199
211
|
type: "created",
|
|
200
212
|
payload: {
|
|
201
213
|
ids: data?.data?.id ? [data.data.id] : undefined,
|
|
@@ -203,16 +215,21 @@ export const useCreate = <
|
|
|
203
215
|
date: new Date(),
|
|
204
216
|
});
|
|
205
217
|
|
|
218
|
+
const combinedMeta = getMeta({
|
|
219
|
+
resource,
|
|
220
|
+
meta: pickNotDeprecated(meta, metaData),
|
|
221
|
+
});
|
|
222
|
+
|
|
206
223
|
const { fields, operation, variables, ...rest } =
|
|
207
|
-
|
|
224
|
+
combinedMeta || {};
|
|
208
225
|
|
|
209
226
|
log?.mutate({
|
|
210
227
|
action: "create",
|
|
211
|
-
resource,
|
|
228
|
+
resource: resource.name,
|
|
212
229
|
data: values,
|
|
213
230
|
meta: {
|
|
214
231
|
dataProviderName: pickDataProvider(
|
|
215
|
-
|
|
232
|
+
identifier,
|
|
216
233
|
dataProviderName,
|
|
217
234
|
resources,
|
|
218
235
|
),
|
|
@@ -224,27 +241,30 @@ export const useCreate = <
|
|
|
224
241
|
onError: (
|
|
225
242
|
err: TError,
|
|
226
243
|
{
|
|
227
|
-
resource,
|
|
244
|
+
resource: resourceName,
|
|
228
245
|
errorNotification: errorNotificationFromProp,
|
|
229
246
|
values,
|
|
230
247
|
},
|
|
231
248
|
) => {
|
|
232
249
|
checkError(err);
|
|
233
|
-
|
|
250
|
+
|
|
251
|
+
const { identifier } = select(resourceName);
|
|
252
|
+
|
|
253
|
+
const resourceSingular = pluralize.singular(identifier);
|
|
234
254
|
|
|
235
255
|
const notificationConfig =
|
|
236
256
|
typeof errorNotificationFromProp === "function"
|
|
237
|
-
? errorNotificationFromProp(err, values,
|
|
257
|
+
? errorNotificationFromProp(err, values, identifier)
|
|
238
258
|
: errorNotificationFromProp;
|
|
239
259
|
|
|
240
260
|
handleNotification(notificationConfig, {
|
|
241
|
-
key: `create-${
|
|
261
|
+
key: `create-${identifier}-notification`,
|
|
242
262
|
description: err.message,
|
|
243
263
|
message: translate(
|
|
244
264
|
"notifications.createError",
|
|
245
265
|
{
|
|
246
266
|
resource: translate(
|
|
247
|
-
`${
|
|
267
|
+
`${identifier}.${identifier}`,
|
|
248
268
|
resourceSingular,
|
|
249
269
|
),
|
|
250
270
|
statusCode: err.statusCode,
|
|
@@ -258,5 +278,11 @@ export const useCreate = <
|
|
|
258
278
|
},
|
|
259
279
|
);
|
|
260
280
|
|
|
261
|
-
|
|
281
|
+
const { elapsedTime } = useLoadingOvertime({
|
|
282
|
+
isLoading: mutation.isLoading,
|
|
283
|
+
interval: overtimeOptions?.interval,
|
|
284
|
+
onInterval: overtimeOptions?.onInterval,
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
return { ...mutation, overtime: { elapsedTime } };
|
|
262
288
|
};
|
|
@@ -28,6 +28,11 @@ import {
|
|
|
28
28
|
pickDataProvider,
|
|
29
29
|
pickNotDeprecated,
|
|
30
30
|
} from "@definitions";
|
|
31
|
+
import {
|
|
32
|
+
useLoadingOvertime,
|
|
33
|
+
UseLoadingOvertimeOptionsProps,
|
|
34
|
+
UseLoadingOvertimeReturnType,
|
|
35
|
+
} from "../useLoadingOvertime";
|
|
31
36
|
|
|
32
37
|
type useCreateManyParams<TData, TError, TVariables> = {
|
|
33
38
|
resource: string;
|
|
@@ -62,7 +67,7 @@ export type UseCreateManyProps<
|
|
|
62
67
|
>,
|
|
63
68
|
"mutationFn" | "onError" | "onSuccess"
|
|
64
69
|
>;
|
|
65
|
-
};
|
|
70
|
+
} & UseLoadingOvertimeOptionsProps;
|
|
66
71
|
|
|
67
72
|
/**
|
|
68
73
|
* `useCreateMany` is a modified version of `react-query`'s {@link https://react-query.tanstack.com/reference/useMutation `useMutation`} for multiple create mutations.
|
|
@@ -82,13 +87,15 @@ export const useCreateMany = <
|
|
|
82
87
|
TVariables = {},
|
|
83
88
|
>({
|
|
84
89
|
mutationOptions,
|
|
90
|
+
overtimeOptions,
|
|
85
91
|
}: UseCreateManyProps<TData, TError, TVariables> = {}): UseCreateManyReturnType<
|
|
86
92
|
TData,
|
|
87
93
|
TError,
|
|
88
94
|
TVariables
|
|
89
|
-
>
|
|
95
|
+
> &
|
|
96
|
+
UseLoadingOvertimeReturnType => {
|
|
90
97
|
const dataProvider = useDataProvider();
|
|
91
|
-
const { resources } = useResource();
|
|
98
|
+
const { resources, select } = useResource();
|
|
92
99
|
const translate = useTranslate();
|
|
93
100
|
const publish = usePublish();
|
|
94
101
|
const handleNotification = useHandleNotification();
|
|
@@ -102,23 +109,26 @@ export const useCreateMany = <
|
|
|
102
109
|
useCreateManyParams<TData, TError, TVariables>
|
|
103
110
|
>(
|
|
104
111
|
({
|
|
105
|
-
resource,
|
|
112
|
+
resource: resourceName,
|
|
106
113
|
values,
|
|
107
114
|
meta,
|
|
108
115
|
metaData,
|
|
109
116
|
dataProviderName,
|
|
110
117
|
}: useCreateManyParams<TData, TError, TVariables>) => {
|
|
118
|
+
const { resource, identifier } = select(resourceName);
|
|
119
|
+
|
|
111
120
|
const combinedMeta = getMeta({
|
|
121
|
+
resource,
|
|
112
122
|
meta: pickNotDeprecated(meta, metaData),
|
|
113
123
|
});
|
|
114
124
|
|
|
115
125
|
const selectedDataProvider = dataProvider(
|
|
116
|
-
pickDataProvider(
|
|
126
|
+
pickDataProvider(identifier, dataProviderName, resources),
|
|
117
127
|
);
|
|
118
128
|
|
|
119
129
|
if (selectedDataProvider.createMany) {
|
|
120
130
|
return selectedDataProvider.createMany<TData, TVariables>({
|
|
121
|
-
resource,
|
|
131
|
+
resource: resource.name,
|
|
122
132
|
variables: values,
|
|
123
133
|
meta: combinedMeta,
|
|
124
134
|
metaData: combinedMeta,
|
|
@@ -127,7 +137,7 @@ export const useCreateMany = <
|
|
|
127
137
|
return handleMultiple(
|
|
128
138
|
values.map((val) =>
|
|
129
139
|
selectedDataProvider.create<TData, TVariables>({
|
|
130
|
-
resource,
|
|
140
|
+
resource: resource.name,
|
|
131
141
|
variables: val,
|
|
132
142
|
meta: combinedMeta,
|
|
133
143
|
metaData: combinedMeta,
|
|
@@ -140,7 +150,7 @@ export const useCreateMany = <
|
|
|
140
150
|
onSuccess: (
|
|
141
151
|
response,
|
|
142
152
|
{
|
|
143
|
-
resource,
|
|
153
|
+
resource: resourceName,
|
|
144
154
|
successNotification,
|
|
145
155
|
dataProviderName,
|
|
146
156
|
invalidates = ["list", "many"],
|
|
@@ -149,21 +159,23 @@ export const useCreateMany = <
|
|
|
149
159
|
metaData,
|
|
150
160
|
},
|
|
151
161
|
) => {
|
|
152
|
-
const
|
|
162
|
+
const { resource, identifier } = select(resourceName);
|
|
163
|
+
|
|
164
|
+
const resourcePlural = pluralize.plural(identifier);
|
|
153
165
|
|
|
154
166
|
const notificationConfig =
|
|
155
167
|
typeof successNotification === "function"
|
|
156
|
-
? successNotification(response, values,
|
|
168
|
+
? successNotification(response, values, identifier)
|
|
157
169
|
: successNotification;
|
|
158
170
|
|
|
159
171
|
handleNotification(notificationConfig, {
|
|
160
|
-
key: `createMany-${
|
|
172
|
+
key: `createMany-${identifier}-notification`,
|
|
161
173
|
message: translate(
|
|
162
174
|
"notifications.createSuccess",
|
|
163
175
|
{
|
|
164
176
|
resource: translate(
|
|
165
|
-
`${
|
|
166
|
-
|
|
177
|
+
`${identifier}.${identifier}`,
|
|
178
|
+
identifier,
|
|
167
179
|
),
|
|
168
180
|
},
|
|
169
181
|
`Successfully created ${resourcePlural}`,
|
|
@@ -173,9 +185,9 @@ export const useCreateMany = <
|
|
|
173
185
|
});
|
|
174
186
|
|
|
175
187
|
invalidateStore({
|
|
176
|
-
resource,
|
|
188
|
+
resource: identifier,
|
|
177
189
|
dataProviderName: pickDataProvider(
|
|
178
|
-
|
|
190
|
+
identifier,
|
|
179
191
|
dataProviderName,
|
|
180
192
|
resources,
|
|
181
193
|
),
|
|
@@ -187,7 +199,7 @@ export const useCreateMany = <
|
|
|
187
199
|
.map((item) => item.id!);
|
|
188
200
|
|
|
189
201
|
publish?.({
|
|
190
|
-
channel: `resources/${resource}`,
|
|
202
|
+
channel: `resources/${resource.name}`,
|
|
191
203
|
type: "created",
|
|
192
204
|
payload: {
|
|
193
205
|
ids,
|
|
@@ -195,16 +207,21 @@ export const useCreateMany = <
|
|
|
195
207
|
date: new Date(),
|
|
196
208
|
});
|
|
197
209
|
|
|
210
|
+
const combinedMeta = getMeta({
|
|
211
|
+
resource,
|
|
212
|
+
meta: pickNotDeprecated(meta, metaData),
|
|
213
|
+
});
|
|
214
|
+
|
|
198
215
|
const { fields, operation, variables, ...rest } =
|
|
199
|
-
|
|
216
|
+
combinedMeta || {};
|
|
200
217
|
|
|
201
218
|
log?.mutate({
|
|
202
219
|
action: "createMany",
|
|
203
|
-
resource,
|
|
220
|
+
resource: resource.name,
|
|
204
221
|
data: values,
|
|
205
222
|
meta: {
|
|
206
223
|
dataProviderName: pickDataProvider(
|
|
207
|
-
|
|
224
|
+
identifier,
|
|
208
225
|
dataProviderName,
|
|
209
226
|
resources,
|
|
210
227
|
),
|
|
@@ -213,25 +230,30 @@ export const useCreateMany = <
|
|
|
213
230
|
},
|
|
214
231
|
});
|
|
215
232
|
},
|
|
216
|
-
onError: (
|
|
233
|
+
onError: (
|
|
234
|
+
err: TError,
|
|
235
|
+
{ resource: resourceName, errorNotification, values },
|
|
236
|
+
) => {
|
|
237
|
+
const { identifier } = select(resourceName);
|
|
238
|
+
|
|
217
239
|
const notificationConfig =
|
|
218
240
|
typeof errorNotification === "function"
|
|
219
|
-
? errorNotification(err, values,
|
|
241
|
+
? errorNotification(err, values, identifier)
|
|
220
242
|
: errorNotification;
|
|
221
243
|
|
|
222
244
|
handleNotification(notificationConfig, {
|
|
223
|
-
key: `createMany-${
|
|
245
|
+
key: `createMany-${identifier}-notification`,
|
|
224
246
|
description: err.message,
|
|
225
247
|
message: translate(
|
|
226
248
|
"notifications.createError",
|
|
227
249
|
{
|
|
228
250
|
resource: translate(
|
|
229
|
-
`${
|
|
230
|
-
|
|
251
|
+
`${identifier}.${identifier}`,
|
|
252
|
+
identifier,
|
|
231
253
|
),
|
|
232
254
|
statusCode: err.statusCode,
|
|
233
255
|
},
|
|
234
|
-
`There was an error creating ${
|
|
256
|
+
`There was an error creating ${identifier} (status code: ${err.statusCode}`,
|
|
235
257
|
),
|
|
236
258
|
type: "error",
|
|
237
259
|
});
|
|
@@ -240,5 +262,11 @@ export const useCreateMany = <
|
|
|
240
262
|
},
|
|
241
263
|
);
|
|
242
264
|
|
|
243
|
-
|
|
265
|
+
const { elapsedTime } = useLoadingOvertime({
|
|
266
|
+
isLoading: mutation.isLoading,
|
|
267
|
+
interval: overtimeOptions?.interval,
|
|
268
|
+
onInterval: overtimeOptions?.onInterval,
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
return { ...mutation, overtime: { elapsedTime } };
|
|
244
272
|
};
|