-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathindex.ts
More file actions
372 lines (329 loc) · 12 KB
/
Copy pathindex.ts
File metadata and controls
372 lines (329 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
import React from "react";
import warnOnce from "warn-once";
import {
useMeta,
useOne,
useCreate,
useUpdate,
useResourceParams,
useInvalidate,
useMutationMode,
useRefineOptions,
useLoadingOvertime,
useWarnAboutChange,
useRedirectionAfterSubmission,
} from "@hooks";
import {
redirectPage,
asyncDebounce,
deferExecution,
} from "@definitions/helpers";
import type { UpdateParams } from "../data/useUpdate";
import type { UseCreateParams } from "../data/useCreate";
import type { UseFormProps, UseFormReturnType } from "./types";
import type {
BaseKey,
BaseRecord,
CreateResponse,
HttpError,
UpdateResponse,
} from "../../contexts/data/types";
export type {
ActionParams,
UseFormProps,
UseFormReturnType,
AutoSaveIndicatorElements,
AutoSaveProps,
AutoSaveReturnType,
FormAction,
RedirectAction,
FormWithSyncWithLocationParams,
} from "./types";
/**
* This hook orchestrates Refine's data hooks to create, edit, and clone data. It also provides a set of features to make it easier for users to implement their real world needs and handle edge cases such as redirects, invalidation, auto-save and more.
*
* @see {@link https://refine.dev/docs/data/hooks/use-form} for more details.
*
* @typeParam TQueryFnData - Result data returned by the query function. Extends {@link https://refine.dev/docs/core/interface-references/#baserecord `BaseRecord`}
* @typeParam TError - Custom error object that extends {@link https://refine.dev/docs/core/interface-references/#httperror `HttpError`}
* @typeParam TVariables - Values for params. default `{}`
* @typeParam TData - Result data returned by the `select` function. Extends {@link https://refine.dev/docs/core/interface-references/#baserecord `BaseRecord`}. Defaults to `TQueryFnData`
* @typeParam TResponse - Result data returned by the mutation function. Extends {@link https://refine.dev/docs/core/interface-references/#baserecord `BaseRecord`}. Defaults to `TData`
* @typeParam TResponseError - Custom error object that extends {@link https://refine.dev/docs/core/interface-references/#httperror `HttpError`}. Defaults to `TError`
*
*/
export const useForm = <
TQueryFnData extends BaseRecord = BaseRecord,
TError extends HttpError = HttpError,
TVariables = {},
TData extends BaseRecord = TQueryFnData,
TResponse extends BaseRecord = TData,
TResponseError extends HttpError = TError,
>(
props: UseFormProps<
TQueryFnData,
TError,
TVariables,
TData,
TResponse,
TResponseError
> = {},
): UseFormReturnType<
TQueryFnData,
TError,
TVariables,
TData,
TResponse,
TResponseError
> => {
const getMeta = useMeta();
const invalidate = useInvalidate();
const { redirect: defaultRedirect } = useRefineOptions();
const { mutationMode: defaultMutationMode } = useMutationMode();
const { setWarnWhen } = useWarnAboutChange();
const handleSubmitWithRedirect = useRedirectionAfterSubmission();
const pickedMeta = props.meta;
const mutationMode = props.mutationMode ?? defaultMutationMode;
const {
id,
setId,
resource,
identifier,
formAction: action,
} = useResourceParams({
resource: props.resource,
id: props.id,
action: props.action,
});
const [autosaved, setAutosaved] = React.useState(false);
const isEdit = action === "edit";
const isClone = action === "clone";
const isCreate = action === "create";
const combinedMeta = getMeta({
resource,
meta: pickedMeta,
});
const isIdRequired = (isEdit || isClone) && Boolean(props.resource);
const isIdDefined = typeof props.id !== "undefined";
const isQueryDisabled = props.queryOptions?.enabled === false;
/**
* When a custom resource is provided through props, `id` will not be inferred from the URL to avoid any potential faulty requests.
* In this case, `id` is required to be passed through props.
* If `id` is not handled, a warning will be thrown in development mode.
*/
warnOnce(
isIdRequired && !isIdDefined && !isQueryDisabled,
idWarningMessage(action, identifier, id),
);
/**
* Target action to redirect after form submission.
*/
const redirectAction = redirectPage({
redirectFromProps: props.redirect,
action,
redirectOptions: defaultRedirect,
});
/**
* Redirection function to be used in internal redirects and to be provided to the user.
*/
const redirect: UseFormReturnType["redirect"] = (
redirect = isEdit ? "list" : "edit",
redirectId = id,
routeParams = {},
) => {
handleSubmitWithRedirect({
redirect: redirect,
resource,
id: redirectId,
meta: { ...pickedMeta, ...routeParams },
});
};
const queryResult = useOne<TQueryFnData, TError, TData>({
resource: identifier,
id: isCreate ? undefined : id,
queryOptions: {
// Only enable the query if it's not a create action and the `id` is defined
...props.queryOptions,
// AND the external enabled condition (if provided) is also true
enabled:
!isCreate && id !== undefined && (props.queryOptions?.enabled ?? true),
},
liveMode: props.liveMode,
onLiveEvent: props.onLiveEvent,
liveParams: props.liveParams,
meta: { ...combinedMeta, ...props.queryMeta },
dataProviderName: props.dataProviderName,
overtimeOptions: { enabled: false },
});
const createMutation = useCreate<TResponse, TResponseError, TVariables>({
mutationOptions: props.createMutationOptions,
overtimeOptions: { enabled: false },
});
const updateMutation = useUpdate<TResponse, TResponseError, TVariables>({
mutationOptions: props.updateMutationOptions,
overtimeOptions: { enabled: false },
});
const mutationResult = isEdit ? updateMutation : createMutation;
const isMutationLoading = mutationResult.mutation.isPending;
const formLoading = isMutationLoading || queryResult.query.isFetching;
const { elapsedTime } = useLoadingOvertime({
...props.overtimeOptions,
isLoading: formLoading,
});
React.useEffect(() => {
// After `autosaved` is set to `true`, it won't be set to `false` again.
// Therefore, the `invalidate` function will be called only once at the end of the hooks lifecycle.
return () => {
if (
props.autoSave?.invalidateOnUnmount &&
autosaved &&
identifier &&
typeof id !== "undefined"
) {
invalidate({
id,
invalidates: props.invalidates || ["list", "many", "detail"],
dataProviderName: props.dataProviderName,
resource: identifier,
});
}
};
}, [props.autoSave?.invalidateOnUnmount, autosaved]);
const onFinish = async (
values: TVariables,
{ isAutosave = false }: { isAutosave?: boolean } = {},
) => {
const isPessimistic = mutationMode === "pessimistic";
// Disable warning trigger when the form is being submitted
setWarnWhen(false);
// Redirect after a successful form submission
const onSuccessRedirect = (id?: BaseKey) => redirect(redirectAction, id);
const submissionPromise = new Promise<
CreateResponse<TResponse> | UpdateResponse<TResponse> | void
>((resolve, reject) => {
// Reject the mutation if the resource is not defined
if (!resource) return reject(missingResourceError);
// Reject the mutation if the `id` is not defined in edit action
// This line is commented out because the `id` might not be set for some cases and edit is done on a resource.
// if (isEdit && !id) return reject(missingIdError);
// Reject the mutation if the `id` is not defined in clone action
if (isClone && !id) return reject(missingIdError);
// Reject the mutation if there's no `values` passed
if (!values) return reject(missingValuesError);
// Auto Save is only allowed in edit action
if (isAutosave && !isEdit) return reject(autosaveOnNonEditError);
if (!isPessimistic && !isAutosave) {
// If the mutation mode is not pessimistic, handle the redirect immediately in an async manner
// `setWarnWhen` blocks the redirects until set to `false`
// If redirect is done before the value is properly set, it will be blocked.
// We're deferring the execution of the redirect to ensure that the value is set properly.
deferExecution(() => onSuccessRedirect());
// Resolve the promise immediately
resolve();
}
const variables:
| UpdateParams<TResponse, TResponseError, TVariables>
| UseCreateParams<TResponse, TResponseError, TVariables> = {
values,
resource: identifier ?? resource.name,
meta: { ...combinedMeta, ...props.mutationMeta },
dataProviderName: props.dataProviderName,
invalidates: isAutosave ? [] : props.invalidates,
successNotification: isAutosave ? false : props.successNotification,
errorNotification: isAutosave ? false : props.errorNotification,
// Update specific variables
...(isEdit
? {
id: id ?? "",
mutationMode,
undoableTimeout: props.undoableTimeout,
optimisticUpdateMap: props.optimisticUpdateMap,
}
: {}),
};
const { mutateAsync } = isEdit ? updateMutation : createMutation;
mutateAsync(variables as any, {
// Call user-defined `onMutationSuccess` and `onMutationError` callbacks if provided
// These callbacks will not have an effect on the submission promise
onSuccess: props.onMutationSuccess
? (data, _, context) => {
props.onMutationSuccess?.(data, values, context, isAutosave);
}
: undefined,
onError: props.onMutationError
? (error: TResponseError, _, context) => {
props.onMutationError?.(error, values, context, isAutosave);
}
: undefined,
})
// If the mutation mode is pessimistic, resolve the promise after the mutation is succeeded
.then((data) => {
if (isPessimistic && !isAutosave) {
deferExecution(() => onSuccessRedirect(data?.data?.id));
}
if (isAutosave) {
setAutosaved(true);
}
resolve(data);
})
// If the mutation mode is pessimistic, reject the promise after the mutation is failed
.catch(reject);
});
return submissionPromise;
};
const onFinishRef = React.useRef(onFinish);
React.useEffect(() => {
onFinishRef.current = onFinish;
}, [onFinish]);
const onFinishAutoSave = React.useMemo(
() =>
asyncDebounce(
(values: TVariables) =>
onFinishRef.current(values, { isAutosave: true }),
props.autoSave?.debounce ?? 1000,
"Cancelled by debounce",
),
[props.autoSave?.debounce],
);
React.useEffect(() => {
return () => {
onFinishAutoSave.cancel();
};
}, [onFinishAutoSave]);
const overtime = {
elapsedTime,
};
const autoSaveProps = {
status: updateMutation.mutation.status,
data: updateMutation.mutation.data,
error: updateMutation.mutation.error,
};
return {
onFinish,
onFinishAutoSave,
formLoading,
mutation: mutationResult.mutation,
query: queryResult.query,
autoSaveProps,
id,
setId,
redirect,
overtime,
};
};
const missingResourceError = new Error(
"[useForm]: `resource` is not defined or not matched but is required",
);
const missingIdError = new Error(
"[useForm]: `id` is not defined but is required in edit and clone actions",
);
const missingValuesError = new Error(
"[useForm]: `values` is not provided but is required",
);
const autosaveOnNonEditError = new Error(
"[useForm]: `autoSave` is only allowed in edit action",
);
const idWarningMessage = (action?: string, identifier?: string, id?: BaseKey) =>
`[useForm]: action: "${action}", resource: "${identifier}", id: ${id}
If you don't use the \`setId\` method to set the \`id\`, you should pass the \`id\` prop to \`useForm\`. Otherwise, \`useForm\` will not be able to infer the \`id\` from the current URL with custom resource provided.
See https://refine.dev/docs/data/hooks/use-form/#id-`;