@refinedev/core 4.46.1 → 4.46.2

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.
Files changed (51) hide show
  1. package/CHANGELOG.md +69 -0
  2. package/dist/definitions/helpers/flatten-object-keys/index.d.ts.map +1 -1
  3. package/dist/esm/index.js +6 -6
  4. package/dist/esm/index.js.map +1 -1
  5. package/dist/hooks/accessControl/useCan/index.d.ts.map +1 -1
  6. package/dist/hooks/auditLog/useLogList/index.d.ts.map +1 -1
  7. package/dist/hooks/auth/useOnError/index.d.ts.map +1 -1
  8. package/dist/hooks/data/useCreate.d.ts.map +1 -1
  9. package/dist/hooks/data/useCreateMany.d.ts.map +1 -1
  10. package/dist/hooks/data/useDelete.d.ts.map +1 -1
  11. package/dist/hooks/data/useDeleteMany.d.ts.map +1 -1
  12. package/dist/hooks/data/useInfiniteList.d.ts.map +1 -1
  13. package/dist/hooks/data/useList.d.ts.map +1 -1
  14. package/dist/hooks/data/useMany.d.ts.map +1 -1
  15. package/dist/hooks/data/useOne.d.ts.map +1 -1
  16. package/dist/hooks/data/useUpdate.d.ts.map +1 -1
  17. package/dist/hooks/data/useUpdateMany.d.ts.map +1 -1
  18. package/dist/hooks/router/use-go/index.d.ts +2 -0
  19. package/dist/hooks/router/use-go/index.d.ts.map +1 -1
  20. package/dist/iife/index.js +6 -6
  21. package/dist/iife/index.js.map +1 -1
  22. package/dist/index.js +6 -6
  23. package/dist/index.js.map +1 -1
  24. package/dist/interfaces/auth.d.ts +6 -6
  25. package/dist/interfaces/auth.d.ts.map +1 -1
  26. package/package.json +1 -1
  27. package/src/definitions/helpers/flatten-object-keys/index.ts +2 -0
  28. package/src/hooks/accessControl/useCan/index.ts +10 -12
  29. package/src/hooks/auditLog/useLogList/index.ts +10 -12
  30. package/src/hooks/auth/useForgotPassword/index.ts +51 -54
  31. package/src/hooks/auth/useGetIdentity/index.ts +23 -26
  32. package/src/hooks/auth/useIsAuthenticated/index.ts +20 -20
  33. package/src/hooks/auth/useLogin/index.ts +33 -35
  34. package/src/hooks/auth/useLogout/index.ts +32 -34
  35. package/src/hooks/auth/useOnError/index.ts +29 -33
  36. package/src/hooks/auth/usePermissions/index.ts +19 -22
  37. package/src/hooks/auth/useRegister/index.ts +60 -66
  38. package/src/hooks/auth/useUpdatePassword/index.ts +51 -54
  39. package/src/hooks/data/useCreate.ts +115 -117
  40. package/src/hooks/data/useCreateMany.ts +113 -118
  41. package/src/hooks/data/useDelete.ts +212 -227
  42. package/src/hooks/data/useDeleteMany.ts +230 -248
  43. package/src/hooks/data/useInfiniteList.ts +36 -42
  44. package/src/hooks/data/useList.ts +56 -62
  45. package/src/hooks/data/useMany.ts +36 -37
  46. package/src/hooks/data/useOne.ts +54 -55
  47. package/src/hooks/data/useUpdate.ts +288 -315
  48. package/src/hooks/data/useUpdateMany.ts +294 -325
  49. package/src/hooks/menu/useMenu.tsx +1 -1
  50. package/src/hooks/router/use-go/index.tsx +3 -0
  51. package/src/interfaces/auth.tsx +6 -6
@@ -189,8 +189,8 @@ export const useUpdateMany = <
189
189
  TError,
190
190
  UpdateManyParams<TData, TError, TVariables>,
191
191
  UpdateContext<TData>
192
- >(
193
- ({
192
+ >({
193
+ mutationFn: ({
194
194
  ids,
195
195
  values,
196
196
  resource: resourceName,
@@ -277,122 +277,154 @@ export const useUpdateMany = <
277
277
  );
278
278
  return updatePromise;
279
279
  },
280
+ onMutate: async ({
281
+ resource: resourceName,
282
+ ids,
283
+ values,
284
+ mutationMode,
285
+ dataProviderName,
286
+ meta,
287
+ metaData,
288
+ optimisticUpdateMap = { list: true, many: true, detail: true },
289
+ }) => {
290
+ const { identifier } = select(resourceName);
291
+ const {
292
+ gqlMutation: _,
293
+ gqlQuery: __,
294
+ ...preferredMeta
295
+ } = pickNotDeprecated(meta, metaData) ?? {};
296
+
297
+ const queryKey = queryKeysReplacement(preferLegacyKeys)(
298
+ identifier,
299
+ pickDataProvider(identifier, dataProviderName, resources),
300
+ preferredMeta,
301
+ );
280
302
 
281
- {
282
- onMutate: async ({
283
- resource: resourceName,
284
- ids,
285
- values,
286
- mutationMode,
287
- dataProviderName,
288
- meta,
289
- metaData,
290
- optimisticUpdateMap = { list: true, many: true, detail: true },
291
- }) => {
292
- const { identifier } = select(resourceName);
293
-
294
- const {
295
- gqlMutation: _,
296
- gqlQuery: __,
297
- ...preferredMeta
298
- } = pickNotDeprecated(meta, metaData) ?? {};
299
-
300
- const queryKey = queryKeysReplacement(preferLegacyKeys)(
301
- identifier,
302
- pickDataProvider(identifier, dataProviderName, resources),
303
- preferredMeta,
304
- );
303
+ const resourceKeys = keys()
304
+ .data(pickDataProvider(identifier, dataProviderName, resources))
305
+ .resource(identifier);
305
306
 
306
- const resourceKeys = keys()
307
- .data(
308
- pickDataProvider(
309
- identifier,
310
- dataProviderName,
311
- resources,
312
- ),
313
- )
314
- .resource(identifier);
307
+ const mutationModePropOrContext =
308
+ mutationMode ?? mutationModeContext;
315
309
 
316
- const mutationModePropOrContext =
317
- mutationMode ?? mutationModeContext;
310
+ await queryClient.cancelQueries(
311
+ resourceKeys.get(preferLegacyKeys),
312
+ undefined,
313
+ {
314
+ silent: true,
315
+ },
316
+ );
318
317
 
319
- await queryClient.cancelQueries(
320
- resourceKeys.get(preferLegacyKeys),
321
- undefined,
322
- {
323
- silent: true,
324
- },
325
- );
318
+ const previousQueries = queryClient.getQueriesData<
319
+ QueryResponse<TData>
320
+ >(resourceKeys.get(preferLegacyKeys));
321
+
322
+ if (mutationModePropOrContext !== "pessimistic") {
323
+ if (optimisticUpdateMap.list) {
324
+ // Set the previous queries to the new ones:
325
+ queryClient.setQueriesData(
326
+ resourceKeys
327
+ .action("list")
328
+ .params(preferredMeta ?? {})
329
+ .get(preferLegacyKeys),
330
+ (previous?: GetListResponse<TData> | null) => {
331
+ if (
332
+ typeof optimisticUpdateMap.list === "function"
333
+ ) {
334
+ return optimisticUpdateMap.list(
335
+ previous,
336
+ values,
337
+ ids,
338
+ );
339
+ }
326
340
 
327
- const previousQueries = queryClient.getQueriesData<
328
- QueryResponse<TData>
329
- >(resourceKeys.get(preferLegacyKeys));
341
+ if (!previous) {
342
+ return null;
343
+ }
330
344
 
331
- if (mutationModePropOrContext !== "pessimistic") {
332
- if (optimisticUpdateMap.list) {
333
- // Set the previous queries to the new ones:
334
- queryClient.setQueriesData(
335
- resourceKeys
336
- .action("list")
337
- .params(preferredMeta ?? {})
338
- .get(preferLegacyKeys),
339
- (previous?: GetListResponse<TData> | null) => {
345
+ const data = previous.data.map((record: TData) => {
340
346
  if (
341
- typeof optimisticUpdateMap.list ===
342
- "function"
347
+ record.id !== undefined &&
348
+ ids
349
+ .filter((id) => id !== undefined)
350
+ .map(String)
351
+ .includes(record.id.toString())
343
352
  ) {
344
- return optimisticUpdateMap.list(
345
- previous,
346
- values,
347
- ids,
348
- );
353
+ return {
354
+ ...record,
355
+ ...values,
356
+ };
349
357
  }
350
358
 
351
- if (!previous) {
352
- return null;
353
- }
359
+ return record;
360
+ });
361
+
362
+ return {
363
+ ...previous,
364
+ data,
365
+ };
366
+ },
367
+ );
368
+ }
354
369
 
355
- const data = previous.data.map(
356
- (record: TData) => {
357
- if (
358
- record.id !== undefined &&
359
- ids
360
- .filter(
361
- (id) => id !== undefined,
362
- )
363
- .map(String)
364
- .includes(record.id.toString())
365
- ) {
366
- return {
367
- ...record,
368
- ...values,
369
- };
370
- }
371
-
372
- return record;
373
- },
370
+ if (optimisticUpdateMap.many) {
371
+ queryClient.setQueriesData(
372
+ resourceKeys.action("many").get(preferLegacyKeys),
373
+ (previous?: GetManyResponse<TData> | null) => {
374
+ if (
375
+ typeof optimisticUpdateMap.many === "function"
376
+ ) {
377
+ return optimisticUpdateMap.many(
378
+ previous,
379
+ values,
380
+ ids,
374
381
  );
382
+ }
375
383
 
376
- return {
377
- ...previous,
378
- data,
379
- };
380
- },
381
- );
382
- }
384
+ if (!previous) {
385
+ return null;
386
+ }
387
+
388
+ const data = previous.data.map((record: TData) => {
389
+ if (
390
+ record.id !== undefined &&
391
+ ids
392
+ .filter((id) => id !== undefined)
393
+ .map(String)
394
+ .includes(record.id.toString())
395
+ ) {
396
+ return {
397
+ ...record,
398
+ ...values,
399
+ };
400
+ }
401
+ return record;
402
+ });
403
+ return {
404
+ ...previous,
405
+ data,
406
+ };
407
+ },
408
+ );
409
+ }
383
410
 
384
- if (optimisticUpdateMap.many) {
411
+ if (optimisticUpdateMap.detail) {
412
+ for (const id of ids) {
385
413
  queryClient.setQueriesData(
386
- resourceKeys.action("many").get(preferLegacyKeys),
387
- (previous?: GetManyResponse<TData> | null) => {
414
+ resourceKeys
415
+ .action("one")
416
+ .id(id)
417
+ .params(preferredMeta ?? {})
418
+ .get(preferLegacyKeys),
419
+ (previous?: GetOneResponse<TData> | null) => {
388
420
  if (
389
- typeof optimisticUpdateMap.many ===
421
+ typeof optimisticUpdateMap.detail ===
390
422
  "function"
391
423
  ) {
392
- return optimisticUpdateMap.many(
424
+ return optimisticUpdateMap.detail(
393
425
  previous,
394
426
  values,
395
- ids,
427
+ id,
396
428
  );
397
429
  }
398
430
 
@@ -400,25 +432,10 @@ export const useUpdateMany = <
400
432
  return null;
401
433
  }
402
434
 
403
- const data = previous.data.map(
404
- (record: TData) => {
405
- if (
406
- record.id !== undefined &&
407
- ids
408
- .filter(
409
- (id) => id !== undefined,
410
- )
411
- .map(String)
412
- .includes(record.id.toString())
413
- ) {
414
- return {
415
- ...record,
416
- ...values,
417
- };
418
- }
419
- return record;
420
- },
421
- );
435
+ const data = {
436
+ ...previous.data,
437
+ ...values,
438
+ };
422
439
  return {
423
440
  ...previous,
424
441
  data,
@@ -426,242 +443,194 @@ export const useUpdateMany = <
426
443
  },
427
444
  );
428
445
  }
429
-
430
- if (optimisticUpdateMap.detail) {
431
- for (const id of ids) {
432
- queryClient.setQueriesData(
433
- resourceKeys
434
- .action("one")
435
- .id(id)
436
- .params(preferredMeta ?? {})
437
- .get(preferLegacyKeys),
438
- (previous?: GetOneResponse<TData> | null) => {
439
- if (
440
- typeof optimisticUpdateMap.detail ===
441
- "function"
442
- ) {
443
- return optimisticUpdateMap.detail(
444
- previous,
445
- values,
446
- id,
447
- );
448
- }
449
-
450
- if (!previous) {
451
- return null;
452
- }
453
-
454
- const data = {
455
- ...previous.data,
456
- ...values,
457
- };
458
- return {
459
- ...previous,
460
- data,
461
- };
462
- },
463
- );
464
- }
465
- }
466
446
  }
447
+ }
467
448
 
468
- return {
469
- previousQueries,
470
- queryKey,
471
- };
472
- },
473
- onSettled: (
474
- _data,
475
- _error,
476
- { ids, resource: resourceName, dataProviderName },
477
- ) => {
478
- const { identifier } = select(resourceName);
479
-
480
- // invalidate the cache for the list and many queries:
449
+ return {
450
+ previousQueries,
451
+ queryKey,
452
+ };
453
+ },
454
+ onSettled: (
455
+ _data,
456
+ _error,
457
+ { ids, resource: resourceName, dataProviderName },
458
+ ) => {
459
+ const { identifier } = select(resourceName);
460
+
461
+ // invalidate the cache for the list and many queries:
462
+ invalidateStore({
463
+ resource: identifier,
464
+ invalidates: ["list", "many"],
465
+ dataProviderName: pickDataProvider(
466
+ identifier,
467
+ dataProviderName,
468
+ resources,
469
+ ),
470
+ });
471
+
472
+ ids.forEach((id) =>
481
473
  invalidateStore({
482
474
  resource: identifier,
483
- invalidates: ["list", "many"],
475
+ invalidates: ["detail"],
484
476
  dataProviderName: pickDataProvider(
485
477
  identifier,
486
478
  dataProviderName,
487
479
  resources,
488
480
  ),
489
- });
481
+ id,
482
+ }),
483
+ );
484
+
485
+ notificationDispatch({
486
+ type: ActionTypes.REMOVE,
487
+ payload: { id: ids, resource: identifier },
488
+ });
489
+ },
490
+ onSuccess: (
491
+ data,
492
+ {
493
+ ids,
494
+ resource: resourceName,
495
+ meta,
496
+ metaData,
497
+ dataProviderName: dataProviderNameFromProp,
498
+ successNotification,
499
+ values,
500
+ },
501
+ context,
502
+ ) => {
503
+ const { resource, identifier } = select(resourceName);
504
+ const resourceSingular = textTransformers.singular(identifier);
505
+
506
+ const dataProviderName = pickDataProvider(
507
+ identifier,
508
+ dataProviderNameFromProp,
509
+ resources,
510
+ );
490
511
 
491
- ids.forEach((id) =>
492
- invalidateStore({
493
- resource: identifier,
494
- invalidates: ["detail"],
495
- dataProviderName: pickDataProvider(
512
+ const combinedMeta = getMeta({
513
+ resource,
514
+ meta: pickNotDeprecated(meta, metaData),
515
+ });
516
+
517
+ const notificationConfig =
518
+ typeof successNotification === "function"
519
+ ? successNotification(data, { ids, values }, identifier)
520
+ : successNotification;
521
+
522
+ handleNotification(notificationConfig, {
523
+ key: `${ids}-${identifier}-notification`,
524
+ description: translate("notifications.success", "Successful"),
525
+ message: translate(
526
+ "notifications.editSuccess",
527
+ {
528
+ resource: translate(
529
+ `${identifier}.${identifier}`,
496
530
  identifier,
497
- dataProviderName,
498
- resources,
499
531
  ),
500
- id,
501
- }),
502
- );
532
+ },
533
+ `Successfully updated ${resourceSingular}`,
534
+ ),
535
+ type: "success",
536
+ });
503
537
 
504
- notificationDispatch({
505
- type: ActionTypes.REMOVE,
506
- payload: { id: ids, resource: identifier },
538
+ publish?.({
539
+ channel: `resources/${resource.name}`,
540
+ type: "updated",
541
+ payload: {
542
+ ids: ids.map(String),
543
+ },
544
+ date: new Date(),
545
+ meta: {
546
+ ...combinedMeta,
547
+ dataProviderName,
548
+ },
549
+ });
550
+
551
+ const previousData: any[] = [];
552
+ if (context) {
553
+ ids.forEach((id) => {
554
+ const queryData = queryClient.getQueryData<
555
+ UpdateManyResponse<TData>
556
+ >(context.queryKey.detail(id));
557
+
558
+ previousData.push(
559
+ Object.keys(values || {}).reduce<any>(
560
+ (acc, item: any) => {
561
+ acc[item] = queryData?.data?.[item];
562
+ return acc;
563
+ },
564
+ {},
565
+ ),
566
+ );
507
567
  });
508
- },
509
- onSuccess: (
510
- data,
511
- {
568
+ }
569
+
570
+ const {
571
+ fields: _fields,
572
+ operation: _operation,
573
+ variables: _variables,
574
+ ...rest
575
+ } = combinedMeta || {};
576
+ log?.mutate({
577
+ action: "updateMany",
578
+ resource: resource.name,
579
+ data: values,
580
+ previousData,
581
+ meta: {
512
582
  ids,
513
- resource: resourceName,
514
- meta,
515
- metaData,
516
- dataProviderName: dataProviderNameFromProp,
517
- successNotification,
518
- values,
583
+ dataProviderName,
584
+ ...rest,
519
585
  },
520
- context,
521
- ) => {
522
- const { resource, identifier } = select(resourceName);
523
- const resourceSingular = textTransformers.singular(identifier);
586
+ });
587
+ },
588
+ onError: (
589
+ err: TError,
590
+ { ids, resource: resourceName, errorNotification, values },
591
+ context,
592
+ ) => {
593
+ const { identifier } = select(resourceName);
594
+
595
+ // set back the queries to the context:
596
+ if (context) {
597
+ for (const query of context.previousQueries) {
598
+ queryClient.setQueryData(query[0], query[1]);
599
+ }
600
+ }
524
601
 
525
- const dataProviderName = pickDataProvider(
526
- identifier,
527
- dataProviderNameFromProp,
528
- resources,
529
- );
602
+ if (err.message !== "mutationCancelled") {
603
+ checkError?.(err);
530
604
 
531
- const combinedMeta = getMeta({
532
- resource,
533
- meta: pickNotDeprecated(meta, metaData),
534
- });
605
+ const resourceSingular = textTransformers.singular(identifier);
535
606
 
536
607
  const notificationConfig =
537
- typeof successNotification === "function"
538
- ? successNotification(data, { ids, values }, identifier)
539
- : successNotification;
608
+ typeof errorNotification === "function"
609
+ ? errorNotification(err, { ids, values }, identifier)
610
+ : errorNotification;
540
611
 
541
612
  handleNotification(notificationConfig, {
542
- key: `${ids}-${identifier}-notification`,
543
- description: translate(
544
- "notifications.success",
545
- "Successful",
546
- ),
613
+ key: `${ids}-${identifier}-updateMany-error-notification`,
547
614
  message: translate(
548
- "notifications.editSuccess",
615
+ "notifications.editError",
549
616
  {
550
- resource: translate(
551
- `${identifier}.${identifier}`,
552
- identifier,
553
- ),
617
+ resource: resourceSingular,
618
+ statusCode: err.statusCode,
554
619
  },
555
- `Successfully updated ${resourceSingular}`,
620
+ `Error when updating ${resourceSingular} (status code: ${err.statusCode})`,
556
621
  ),
557
- type: "success",
558
- });
559
-
560
- publish?.({
561
- channel: `resources/${resource.name}`,
562
- type: "updated",
563
- payload: {
564
- ids: ids.map(String),
565
- },
566
- date: new Date(),
567
- meta: {
568
- ...combinedMeta,
569
- dataProviderName,
570
- },
571
- });
572
-
573
- const previousData: any[] = [];
574
- if (context) {
575
- ids.forEach((id) => {
576
- const queryData = queryClient.getQueryData<
577
- UpdateManyResponse<TData>
578
- >(context.queryKey.detail(id));
579
-
580
- previousData.push(
581
- Object.keys(values || {}).reduce<any>(
582
- (acc, item: any) => {
583
- acc[item] = queryData?.data?.[item];
584
- return acc;
585
- },
586
- {},
587
- ),
588
- );
589
- });
590
- }
591
-
592
- const {
593
- fields: _fields,
594
- operation: _operation,
595
- variables: _variables,
596
- ...rest
597
- } = combinedMeta || {};
598
- log?.mutate({
599
- action: "updateMany",
600
- resource: resource.name,
601
- data: values,
602
- previousData,
603
- meta: {
604
- ids,
605
- dataProviderName,
606
- ...rest,
607
- },
622
+ description: err.message,
623
+ type: "error",
608
624
  });
609
- },
610
- onError: (
611
- err: TError,
612
- { ids, resource: resourceName, errorNotification, values },
613
- context,
614
- ) => {
615
- const { identifier } = select(resourceName);
616
-
617
- // set back the queries to the context:
618
- if (context) {
619
- for (const query of context.previousQueries) {
620
- queryClient.setQueryData(query[0], query[1]);
621
- }
622
- }
623
-
624
- if (err.message !== "mutationCancelled") {
625
- checkError?.(err);
626
-
627
- const resourceSingular =
628
- textTransformers.singular(identifier);
629
-
630
- const notificationConfig =
631
- typeof errorNotification === "function"
632
- ? errorNotification(
633
- err,
634
- { ids, values },
635
- identifier,
636
- )
637
- : errorNotification;
638
-
639
- handleNotification(notificationConfig, {
640
- key: `${ids}-${identifier}-updateMany-error-notification`,
641
- message: translate(
642
- "notifications.editError",
643
- {
644
- resource: resourceSingular,
645
- statusCode: err.statusCode,
646
- },
647
- `Error when updating ${resourceSingular} (status code: ${err.statusCode})`,
648
- ),
649
- description: err.message,
650
- type: "error",
651
- });
652
- }
653
- },
654
- mutationKey: keys()
655
- .data()
656
- .mutation("updateMany")
657
- .get(preferLegacyKeys),
658
- ...mutationOptions,
659
- meta: {
660
- ...mutationOptions?.meta,
661
- ...getXRay("useUpdateMany", preferLegacyKeys),
662
- },
625
+ }
663
626
  },
664
- );
627
+ mutationKey: keys().data().mutation("updateMany").get(preferLegacyKeys),
628
+ ...mutationOptions,
629
+ meta: {
630
+ ...mutationOptions?.meta,
631
+ ...getXRay("useUpdateMany", preferLegacyKeys),
632
+ },
633
+ });
665
634
 
666
635
  const { elapsedTime } = useLoadingOvertime({
667
636
  isLoading: mutation.isLoading,