@strato-admin/faker-ecommerce 0.1.1 → 0.3.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/dist/dataProvider.js +1 -2
- package/dist/generate.d.ts +2 -1
- package/dist/generate.js +15 -8
- package/dist/ra-core/src/auth/LogoutOnMount.d.ts +8 -0
- package/dist/ra-core/src/auth/LogoutOnMount.js +16 -0
- package/dist/ra-core/src/auth/addRefreshAuthToAuthProvider.d.ts +22 -0
- package/dist/ra-core/src/auth/addRefreshAuthToAuthProvider.js +41 -0
- package/dist/ra-core/src/auth/addRefreshAuthToDataProvider.d.ts +21 -0
- package/dist/ra-core/src/auth/addRefreshAuthToDataProvider.js +30 -0
- package/dist/ra-core/src/auth/convertLegacyAuthProvider.d.ts +12 -0
- package/dist/ra-core/src/auth/convertLegacyAuthProvider.js +19 -0
- package/dist/ra-core/src/auth/index.d.ts +27 -0
- package/dist/ra-core/src/auth/index.js +34 -0
- package/dist/ra-core/src/auth/types.d.ts +7 -0
- package/dist/ra-core/src/auth/types.js +5 -0
- package/dist/ra-core/src/auth/useAuthProvider.d.ts +10 -0
- package/dist/ra-core/src/auth/useAuthProvider.js +11 -0
- package/dist/ra-core/src/auth/useAuthState.d.ts +48 -0
- package/dist/ra-core/src/auth/useAuthState.js +171 -0
- package/dist/ra-core/src/auth/useAuthenticated.d.ts +36 -0
- package/dist/ra-core/src/auth/useAuthenticated.js +34 -0
- package/dist/ra-core/src/auth/useCanAccess.d.ts +56 -0
- package/dist/ra-core/src/auth/useCanAccess.js +108 -0
- package/dist/ra-core/src/auth/useCanAccessCallback.d.ts +39 -0
- package/dist/ra-core/src/auth/useCanAccessCallback.js +45 -0
- package/dist/ra-core/src/auth/useCanAccessResources.d.ts +67 -0
- package/dist/ra-core/src/auth/useCanAccessResources.js +92 -0
- package/dist/ra-core/src/auth/useCheckAuth.d.ts +48 -0
- package/dist/ra-core/src/auth/useCheckAuth.js +76 -0
- package/dist/ra-core/src/auth/useGetIdentity.d.ts +41 -0
- package/dist/ra-core/src/auth/useGetIdentity.js +86 -0
- package/dist/ra-core/src/auth/useGetPermissions.d.ts +41 -0
- package/dist/ra-core/src/auth/useGetPermissions.js +47 -0
- package/dist/ra-core/src/auth/useHandleAuthCallback.d.ts +20 -0
- package/dist/ra-core/src/auth/useHandleAuthCallback.js +83 -0
- package/dist/ra-core/src/auth/useIsAuthPending.d.ts +13 -0
- package/dist/ra-core/src/auth/useIsAuthPending.js +30 -0
- package/dist/ra-core/src/auth/useLogin.d.ts +34 -0
- package/dist/ra-core/src/auth/useLogin.js +78 -0
- package/dist/ra-core/src/auth/useLogout.d.ts +31 -0
- package/dist/ra-core/src/auth/useLogout.js +117 -0
- package/dist/ra-core/src/auth/useLogoutIfAccessDenied.d.ts +42 -0
- package/dist/ra-core/src/auth/useLogoutIfAccessDenied.js +111 -0
- package/dist/ra-core/src/auth/usePermissions.d.ts +41 -0
- package/dist/ra-core/src/auth/usePermissions.js +123 -0
- package/dist/ra-core/src/controller/button/index.d.ts +7 -0
- package/dist/ra-core/src/controller/button/index.js +7 -0
- package/dist/ra-core/src/controller/button/useBulkDeleteController.d.ts +14 -0
- package/dist/ra-core/src/controller/button/useBulkDeleteController.js +66 -0
- package/dist/ra-core/src/controller/create/index.d.ts +6 -0
- package/dist/ra-core/src/controller/create/index.js +6 -0
- package/dist/ra-core/src/controller/create/useCreateController.d.ts +50 -0
- package/dist/ra-core/src/controller/create/useCreateController.js +149 -0
- package/dist/ra-core/src/controller/edit/index.d.ts +6 -0
- package/dist/ra-core/src/controller/edit/index.js +6 -0
- package/dist/ra-core/src/controller/edit/useEditController.d.ts +72 -0
- package/dist/ra-core/src/controller/edit/useEditController.js +206 -0
- package/dist/ra-core/src/controller/field/index.d.ts +12 -0
- package/dist/ra-core/src/controller/field/index.js +12 -0
- package/dist/ra-core/src/controller/field/sanitizeFieldRestProps.d.ts +1 -0
- package/dist/ra-core/src/controller/field/sanitizeFieldRestProps.js +1 -0
- package/dist/ra-core/src/controller/field/types.d.ts +28 -0
- package/dist/ra-core/src/controller/field/types.js +1 -0
- package/dist/ra-core/src/controller/field/useReferenceArrayFieldController.d.ts +39 -0
- package/dist/ra-core/src/controller/field/useReferenceArrayFieldController.js +82 -0
- package/dist/ra-core/src/controller/field/useReferenceFieldController.d.ts +14 -0
- package/dist/ra-core/src/controller/field/useReferenceFieldController.js +31 -0
- package/dist/ra-core/src/controller/field/useReferenceManyFieldController.d.ts +49 -0
- package/dist/ra-core/src/controller/field/useReferenceManyFieldController.js +264 -0
- package/dist/ra-core/src/controller/index.d.ts +17 -0
- package/dist/ra-core/src/controller/index.js +16 -0
- package/dist/ra-core/src/controller/input/ArrayInputContext.d.ts +9 -0
- package/dist/ra-core/src/controller/input/ArrayInputContext.js +8 -0
- package/dist/ra-core/src/controller/input/SimpleFormIteratorContext.d.ts +15 -0
- package/dist/ra-core/src/controller/input/SimpleFormIteratorContext.js +8 -0
- package/dist/ra-core/src/controller/input/SimpleFormIteratorItemContext.d.ts +13 -0
- package/dist/ra-core/src/controller/input/SimpleFormIteratorItemContext.js +8 -0
- package/dist/ra-core/src/controller/input/index.d.ts +17 -0
- package/dist/ra-core/src/controller/input/index.js +17 -0
- package/dist/ra-core/src/controller/input/referenceDataStatus.d.ts +32 -0
- package/dist/ra-core/src/controller/input/referenceDataStatus.js +74 -0
- package/dist/ra-core/src/controller/input/sanitizeInputRestProps.d.ts +1 -0
- package/dist/ra-core/src/controller/input/sanitizeInputRestProps.js +1 -0
- package/dist/ra-core/src/controller/input/types.d.ts +3 -0
- package/dist/ra-core/src/controller/input/types.js +1 -0
- package/dist/ra-core/src/controller/input/useArrayInput.d.ts +8 -0
- package/dist/ra-core/src/controller/input/useArrayInput.js +29 -0
- package/dist/ra-core/src/controller/input/useGetArrayInputNewItemDefaults.d.ts +3 -0
- package/dist/ra-core/src/controller/input/useGetArrayInputNewItemDefaults.js +37 -0
- package/dist/ra-core/src/controller/input/useReferenceArrayInputController.d.ts +48 -0
- package/dist/ra-core/src/controller/input/useReferenceArrayInputController.js +137 -0
- package/dist/ra-core/src/controller/input/useReferenceInputController.d.ts +54 -0
- package/dist/ra-core/src/controller/input/useReferenceInputController.js +208 -0
- package/dist/ra-core/src/controller/input/useReferenceParams.d.ts +106 -0
- package/dist/ra-core/src/controller/input/useReferenceParams.js +226 -0
- package/dist/ra-core/src/controller/input/useSimpleFormIterator.d.ts +7 -0
- package/dist/ra-core/src/controller/input/useSimpleFormIterator.js +15 -0
- package/dist/ra-core/src/controller/input/useSimpleFormIteratorItem.d.ts +7 -0
- package/dist/ra-core/src/controller/input/useSimpleFormIteratorItem.js +15 -0
- package/dist/ra-core/src/controller/list/InfinitePaginationContext.d.ts +25 -0
- package/dist/ra-core/src/controller/list/InfinitePaginationContext.js +32 -0
- package/dist/ra-core/src/controller/list/index.d.ts +30 -0
- package/dist/ra-core/src/controller/list/index.js +30 -0
- package/dist/ra-core/src/controller/list/queryReducer.d.ts +45 -0
- package/dist/ra-core/src/controller/list/queryReducer.js +85 -0
- package/dist/ra-core/src/controller/list/useFilterContext.d.ts +2 -0
- package/dist/ra-core/src/controller/list/useFilterContext.js +5 -0
- package/dist/ra-core/src/controller/list/useInfiniteListController.d.ts +44 -0
- package/dist/ra-core/src/controller/list/useInfiniteListController.js +175 -0
- package/dist/ra-core/src/controller/list/useInfinitePaginationContext.d.ts +16 -0
- package/dist/ra-core/src/controller/list/useInfinitePaginationContext.js +17 -0
- package/dist/ra-core/src/controller/list/useList.d.ts +55 -0
- package/dist/ra-core/src/controller/list/useList.js +223 -0
- package/dist/ra-core/src/controller/list/useListContext.d.ts +59 -0
- package/dist/ra-core/src/controller/list/useListContext.js +65 -0
- package/dist/ra-core/src/controller/list/useListContextWithProps.d.ts +44 -0
- package/dist/ra-core/src/controller/list/useListContextWithProps.js +84 -0
- package/dist/ra-core/src/controller/list/useListController.d.ts +296 -0
- package/dist/ra-core/src/controller/list/useListController.js +234 -0
- package/dist/ra-core/src/controller/list/useListFilterContext.d.ts +11 -0
- package/dist/ra-core/src/controller/list/useListFilterContext.js +18 -0
- package/dist/ra-core/src/controller/list/useListPaginationContext.d.ts +12 -0
- package/dist/ra-core/src/controller/list/useListPaginationContext.js +19 -0
- package/dist/ra-core/src/controller/list/useListParams.d.ts +116 -0
- package/dist/ra-core/src/controller/list/useListParams.js +281 -0
- package/dist/ra-core/src/controller/list/useListSortContext.d.ts +12 -0
- package/dist/ra-core/src/controller/list/useListSortContext.js +19 -0
- package/dist/ra-core/src/controller/list/useRecordSelection.d.ts +32 -0
- package/dist/ra-core/src/controller/list/useRecordSelection.js +93 -0
- package/dist/ra-core/src/controller/list/useSavedQueries.d.ts +14 -0
- package/dist/ra-core/src/controller/list/useSavedQueries.js +29 -0
- package/dist/ra-core/src/controller/list/useUnselect.d.ts +10 -0
- package/dist/ra-core/src/controller/list/useUnselect.js +18 -0
- package/dist/ra-core/src/controller/list/useUnselectAll.d.ts +9 -0
- package/dist/ra-core/src/controller/list/useUnselectAll.js +18 -0
- package/dist/ra-core/src/controller/record/index.d.ts +5 -0
- package/dist/ra-core/src/controller/record/index.js +5 -0
- package/dist/ra-core/src/controller/record/useRecordContext.d.ts +34 -0
- package/dist/ra-core/src/controller/record/useRecordContext.js +36 -0
- package/dist/ra-core/src/controller/saveContext/SaveContext.d.ts +20 -0
- package/dist/ra-core/src/controller/saveContext/SaveContext.js +2 -0
- package/dist/ra-core/src/controller/saveContext/index.d.ts +6 -0
- package/dist/ra-core/src/controller/saveContext/index.js +6 -0
- package/dist/ra-core/src/controller/saveContext/useMutationMiddlewares.d.ts +39 -0
- package/dist/ra-core/src/controller/saveContext/useMutationMiddlewares.js +79 -0
- package/dist/ra-core/src/controller/saveContext/usePickSaveContext.d.ts +5 -0
- package/dist/ra-core/src/controller/saveContext/usePickSaveContext.js +25 -0
- package/dist/ra-core/src/controller/saveContext/useRegisterMutationMiddleware.d.ts +6 -0
- package/dist/ra-core/src/controller/saveContext/useRegisterMutationMiddleware.js +18 -0
- package/dist/ra-core/src/controller/saveContext/useSaveContext.d.ts +14 -0
- package/dist/ra-core/src/controller/saveContext/useSaveContext.js +17 -0
- package/dist/ra-core/src/controller/show/index.d.ts +6 -0
- package/dist/ra-core/src/controller/show/index.js +6 -0
- package/dist/ra-core/src/controller/show/useShowController.d.ts +75 -0
- package/dist/ra-core/src/controller/show/useShowController.js +110 -0
- package/dist/ra-core/src/controller/useFilterState.d.ts +46 -0
- package/dist/ra-core/src/controller/useFilterState.js +70 -0
- package/dist/ra-core/src/controller/usePaginationState.d.ts +33 -0
- package/dist/ra-core/src/controller/usePaginationState.js +45 -0
- package/dist/ra-core/src/controller/usePrevNextController.d.ts +127 -0
- package/dist/ra-core/src/controller/usePrevNextController.js +215 -0
- package/dist/ra-core/src/controller/useReference.d.ts +49 -0
- package/dist/ra-core/src/controller/useReference.js +42 -0
- package/dist/ra-core/src/controller/useSortState.d.ts +61 -0
- package/dist/ra-core/src/controller/useSortState.js +96 -0
- package/dist/ra-core/src/core/DefaultTitleContext.d.ts +15 -0
- package/dist/ra-core/src/core/DefaultTitleContext.js +15 -0
- package/dist/ra-core/src/core/HasDashboardContext.d.ts +21 -0
- package/dist/ra-core/src/core/HasDashboardContext.js +22 -0
- package/dist/ra-core/src/core/ResourceContext.d.ts +19 -0
- package/dist/ra-core/src/core/ResourceContext.js +19 -0
- package/dist/ra-core/src/core/UserMenuContext.d.ts +34 -0
- package/dist/ra-core/src/core/UserMenuContext.js +28 -0
- package/dist/ra-core/src/core/index.d.ts +27 -0
- package/dist/ra-core/src/core/index.js +27 -0
- package/dist/ra-core/src/core/useGetRecordRepresentation.d.ts +13 -0
- package/dist/ra-core/src/core/useGetRecordRepresentation.js +44 -0
- package/dist/ra-core/src/core/useGetResourceLabel.d.ts +22 -0
- package/dist/ra-core/src/core/useGetResourceLabel.js +42 -0
- package/dist/ra-core/src/core/useIsOffline.d.ts +7 -0
- package/dist/ra-core/src/core/useIsOffline.js +18 -0
- package/dist/ra-core/src/core/useResourceContext.d.ts +36 -0
- package/dist/ra-core/src/core/useResourceContext.js +38 -0
- package/dist/ra-core/src/core/useResourceDefinition.d.ts +31 -0
- package/dist/ra-core/src/core/useResourceDefinition.js +48 -0
- package/dist/ra-core/src/core/useResourceDefinitionContext.d.ts +1 -0
- package/dist/ra-core/src/core/useResourceDefinitionContext.js +3 -0
- package/dist/ra-core/src/core/useResourceDefinitions.d.ts +21 -0
- package/dist/ra-core/src/core/useResourceDefinitions.js +20 -0
- package/dist/ra-core/src/core/useUserMenu.d.ts +28 -0
- package/dist/ra-core/src/core/useUserMenu.js +30 -0
- package/dist/ra-core/src/core/useWrappedSource.d.ts +11 -0
- package/dist/ra-core/src/core/useWrappedSource.js +15 -0
- package/dist/ra-core/src/dataProvider/DataProviderContext.d.ts +3 -0
- package/dist/ra-core/src/dataProvider/DataProviderContext.js +4 -0
- package/dist/ra-core/src/dataProvider/HttpError.d.ts +6 -0
- package/dist/ra-core/src/dataProvider/HttpError.js +19 -0
- package/dist/ra-core/src/dataProvider/combineDataProviders.d.ts +21 -0
- package/dist/ra-core/src/dataProvider/combineDataProviders.js +32 -0
- package/dist/ra-core/src/dataProvider/convertLegacyDataProvider.d.ts +12 -0
- package/dist/ra-core/src/dataProvider/convertLegacyDataProvider.js +40 -0
- package/dist/ra-core/src/dataProvider/dataFetchActions.d.ts +15 -0
- package/dist/ra-core/src/dataProvider/dataFetchActions.js +49 -0
- package/dist/ra-core/src/dataProvider/defaultDataProvider.d.ts +2 -0
- package/dist/ra-core/src/dataProvider/defaultDataProvider.js +12 -0
- package/dist/ra-core/src/dataProvider/fetch.d.ts +32 -0
- package/dist/ra-core/src/dataProvider/fetch.js +78 -0
- package/dist/ra-core/src/dataProvider/index.d.ts +34 -0
- package/dist/ra-core/src/dataProvider/index.js +33 -0
- package/dist/ra-core/src/dataProvider/populateQueryCache.d.ts +22 -0
- package/dist/ra-core/src/dataProvider/populateQueryCache.js +30 -0
- package/dist/ra-core/src/dataProvider/testDataProvider.d.ts +2 -0
- package/dist/ra-core/src/dataProvider/testDataProvider.js +41 -0
- package/dist/ra-core/src/dataProvider/undo/index.d.ts +6 -0
- package/dist/ra-core/src/dataProvider/undo/index.js +6 -0
- package/dist/ra-core/src/dataProvider/undo/types.d.ts +3 -0
- package/dist/ra-core/src/dataProvider/undo/types.js +1 -0
- package/dist/ra-core/src/dataProvider/undoableEventEmitter.d.ts +3 -0
- package/dist/ra-core/src/dataProvider/undoableEventEmitter.js +2 -0
- package/dist/ra-core/src/dataProvider/useCreate.d.ts +89 -0
- package/dist/ra-core/src/dataProvider/useCreate.js +155 -0
- package/dist/ra-core/src/dataProvider/useDataProvider.d.ts +2 -0
- package/dist/ra-core/src/dataProvider/useDataProvider.js +140 -0
- package/dist/ra-core/src/dataProvider/useDelete.d.ts +83 -0
- package/dist/ra-core/src/dataProvider/useDelete.js +171 -0
- package/dist/ra-core/src/dataProvider/useDeleteMany.d.ts +81 -0
- package/dist/ra-core/src/dataProvider/useDeleteMany.js +193 -0
- package/dist/ra-core/src/dataProvider/useGetList.d.ts +58 -0
- package/dist/ra-core/src/dataProvider/useGetList.js +131 -0
- package/dist/ra-core/src/dataProvider/useGetMany.d.ts +49 -0
- package/dist/ra-core/src/dataProvider/useGetMany.js +143 -0
- package/dist/ra-core/src/dataProvider/useGetManyAggregate.d.ts +62 -0
- package/dist/ra-core/src/dataProvider/useGetManyAggregate.js +295 -0
- package/dist/ra-core/src/dataProvider/useGetManyReference.d.ts +62 -0
- package/dist/ra-core/src/dataProvider/useGetManyReference.js +117 -0
- package/dist/ra-core/src/dataProvider/useGetOne.d.ts +48 -0
- package/dist/ra-core/src/dataProvider/useGetOne.js +94 -0
- package/dist/ra-core/src/dataProvider/useGetRecordId.d.ts +14 -0
- package/dist/ra-core/src/dataProvider/useGetRecordId.js +22 -0
- package/dist/ra-core/src/dataProvider/useInfiniteGetList.d.ts +64 -0
- package/dist/ra-core/src/dataProvider/useInfiniteGetList.js +177 -0
- package/dist/ra-core/src/dataProvider/useIsDataLoaded.d.ts +13 -0
- package/dist/ra-core/src/dataProvider/useIsDataLoaded.js +35 -0
- package/dist/ra-core/src/dataProvider/useLoading.d.ts +12 -0
- package/dist/ra-core/src/dataProvider/useLoading.js +41 -0
- package/dist/ra-core/src/dataProvider/useMutationWithMutationMode.d.ts +51 -0
- package/dist/ra-core/src/dataProvider/useMutationWithMutationMode.js +238 -0
- package/dist/ra-core/src/dataProvider/useRefresh.d.ts +14 -0
- package/dist/ra-core/src/dataProvider/useRefresh.js +21 -0
- package/dist/ra-core/src/dataProvider/useUpdate.d.ts +89 -0
- package/dist/ra-core/src/dataProvider/useUpdate.js +183 -0
- package/dist/ra-core/src/dataProvider/useUpdateMany.d.ts +81 -0
- package/dist/ra-core/src/dataProvider/useUpdateMany.js +171 -0
- package/dist/ra-core/src/dataProvider/validateResponseFormat.d.ts +5 -0
- package/dist/ra-core/src/dataProvider/validateResponseFormat.js +35 -0
- package/dist/ra-core/src/dataProvider/withLifecycleCallbacks.d.ts +162 -0
- package/dist/ra-core/src/dataProvider/withLifecycleCallbacks.js +377 -0
- package/dist/ra-core/src/dataTable/DataTableCallbacksContext.d.ts +18 -0
- package/dist/ra-core/src/dataTable/DataTableCallbacksContext.js +3 -0
- package/dist/ra-core/src/dataTable/DataTableColumnFilterContext.d.ts +2 -0
- package/dist/ra-core/src/dataTable/DataTableColumnFilterContext.js +3 -0
- package/dist/ra-core/src/dataTable/DataTableColumnRankContext.d.ts +2 -0
- package/dist/ra-core/src/dataTable/DataTableColumnRankContext.js +3 -0
- package/dist/ra-core/src/dataTable/DataTableConfigContext.d.ts +22 -0
- package/dist/ra-core/src/dataTable/DataTableConfigContext.js +7 -0
- package/dist/ra-core/src/dataTable/DataTableDataContext.d.ts +4 -0
- package/dist/ra-core/src/dataTable/DataTableDataContext.js +3 -0
- package/dist/ra-core/src/dataTable/DataTableRenderContext.d.ts +3 -0
- package/dist/ra-core/src/dataTable/DataTableRenderContext.js +3 -0
- package/dist/ra-core/src/dataTable/DataTableSelectedIdsContext.d.ts +3 -0
- package/dist/ra-core/src/dataTable/DataTableSelectedIdsContext.js +3 -0
- package/dist/ra-core/src/dataTable/DataTableSortContext.d.ts +3 -0
- package/dist/ra-core/src/dataTable/DataTableSortContext.js +3 -0
- package/dist/ra-core/src/dataTable/DataTableStoreContext.d.ts +8 -0
- package/dist/ra-core/src/dataTable/DataTableStoreContext.js +6 -0
- package/dist/ra-core/src/dataTable/index.d.ts +11 -0
- package/dist/ra-core/src/dataTable/index.js +11 -0
- package/dist/ra-core/src/dataTable/types.d.ts +2 -0
- package/dist/ra-core/src/dataTable/types.js +1 -0
- package/dist/ra-core/src/export/ExporterContext.d.ts +2 -0
- package/dist/ra-core/src/export/ExporterContext.js +4 -0
- package/dist/ra-core/src/export/defaultExporter.d.ts +2 -0
- package/dist/ra-core/src/export/defaultExporter.js +3 -0
- package/dist/ra-core/src/export/downloadCSV.d.ts +1 -0
- package/dist/ra-core/src/export/downloadCSV.js +17 -0
- package/dist/ra-core/src/export/fetchRelatedRecords.d.ts +14 -0
- package/dist/ra-core/src/export/fetchRelatedRecords.js +19 -0
- package/dist/ra-core/src/export/getRelatedIds.d.ts +24 -0
- package/dist/ra-core/src/export/getRelatedIds.js +26 -0
- package/dist/ra-core/src/export/index.d.ts +5 -0
- package/dist/ra-core/src/export/index.js +5 -0
- package/dist/ra-core/src/export/useBulkExport.d.ts +11 -0
- package/dist/ra-core/src/export/useBulkExport.js +30 -0
- package/dist/ra-core/src/form/WarnWhenUnsavedChanges.d.ts +5 -0
- package/dist/ra-core/src/form/WarnWhenUnsavedChanges.js +5 -0
- package/dist/ra-core/src/form/choices/ChoicesContext.d.ts +65 -0
- package/dist/ra-core/src/form/choices/ChoicesContext.js +7 -0
- package/dist/ra-core/src/form/choices/index.d.ts +4 -0
- package/dist/ra-core/src/form/choices/index.js +4 -0
- package/dist/ra-core/src/form/choices/useChoicesContext.d.ts +5 -0
- package/dist/ra-core/src/form/choices/useChoicesContext.js +62 -0
- package/dist/ra-core/src/form/getFormInitialValues.d.ts +8 -0
- package/dist/ra-core/src/form/getFormInitialValues.js +14 -0
- package/dist/ra-core/src/form/groups/FormGroupContext.d.ts +9 -0
- package/dist/ra-core/src/form/groups/FormGroupContext.js +9 -0
- package/dist/ra-core/src/form/groups/index.d.ts +6 -0
- package/dist/ra-core/src/form/groups/index.js +6 -0
- package/dist/ra-core/src/form/groups/useFormGroup.d.ts +67 -0
- package/dist/ra-core/src/form/groups/useFormGroup.js +146 -0
- package/dist/ra-core/src/form/groups/useFormGroupContext.d.ts +4 -0
- package/dist/ra-core/src/form/groups/useFormGroupContext.js +9 -0
- package/dist/ra-core/src/form/groups/useFormGroups.d.ts +4 -0
- package/dist/ra-core/src/form/groups/useFormGroups.js +9 -0
- package/dist/ra-core/src/form/index.d.ts +14 -0
- package/dist/ra-core/src/form/index.js +14 -0
- package/dist/ra-core/src/form/sanitizeEmptyValues.d.ts +7 -0
- package/dist/ra-core/src/form/sanitizeEmptyValues.js +26 -0
- package/dist/ra-core/src/form/useApplyInputDefaultValues.d.ts +19 -0
- package/dist/ra-core/src/form/useApplyInputDefaultValues.js +71 -0
- package/dist/ra-core/src/form/useAugmentedForm.d.ts +32 -0
- package/dist/ra-core/src/form/useAugmentedForm.js +101 -0
- package/dist/ra-core/src/form/useFormIsDirty.d.ts +4 -0
- package/dist/ra-core/src/form/useFormIsDirty.js +39 -0
- package/dist/ra-core/src/form/useInput.d.ts +33 -0
- package/dist/ra-core/src/form/useInput.js +100 -0
- package/dist/ra-core/src/form/useRecordFromLocation.d.ts +22 -0
- package/dist/ra-core/src/form/useRecordFromLocation.js +59 -0
- package/dist/ra-core/src/form/useSuggestions.d.ts +43 -0
- package/dist/ra-core/src/form/useSuggestions.js +186 -0
- package/dist/ra-core/src/form/validation/getSimpleValidationResolver.d.ts +31 -0
- package/dist/ra-core/src/form/validation/getSimpleValidationResolver.js +87 -0
- package/dist/ra-core/src/form/validation/index.d.ts +7 -0
- package/dist/ra-core/src/form/validation/index.js +7 -0
- package/dist/ra-core/src/form/validation/setSubmissionErrors.d.ts +21 -0
- package/dist/ra-core/src/form/validation/setSubmissionErrors.js +34 -0
- package/dist/ra-core/src/form/validation/useGetValidationErrorMessage.d.ts +26 -0
- package/dist/ra-core/src/form/validation/useGetValidationErrorMessage.js +35 -0
- package/dist/ra-core/src/form/validation/useNotifyIsFormInvalid.d.ts +9 -0
- package/dist/ra-core/src/form/validation/useNotifyIsFormInvalid.js +30 -0
- package/dist/ra-core/src/form/validation/useUnique.d.ts +57 -0
- package/dist/ra-core/src/form/validation/useUnique.js +107 -0
- package/dist/ra-core/src/form/validation/validate.d.ts +168 -0
- package/dist/ra-core/src/form/validation/validate.js +218 -0
- package/dist/ra-core/src/i18n/I18nContext.d.ts +3 -0
- package/dist/ra-core/src/i18n/I18nContext.js +11 -0
- package/dist/ra-core/src/i18n/TranslatableContext.d.ts +16 -0
- package/dist/ra-core/src/i18n/TranslatableContext.js +2 -0
- package/dist/ra-core/src/i18n/TranslationMessages.d.ts +221 -0
- package/dist/ra-core/src/i18n/TranslationMessages.js +1 -0
- package/dist/ra-core/src/i18n/TranslationUtils.d.ts +60 -0
- package/dist/ra-core/src/i18n/TranslationUtils.js +70 -0
- package/dist/ra-core/src/i18n/index.d.ts +20 -0
- package/dist/ra-core/src/i18n/index.js +20 -0
- package/dist/ra-core/src/i18n/substituteTokens.d.ts +8 -0
- package/dist/ra-core/src/i18n/substituteTokens.js +18 -0
- package/dist/ra-core/src/i18n/useI18nProvider.d.ts +12 -0
- package/dist/ra-core/src/i18n/useI18nProvider.js +14 -0
- package/dist/ra-core/src/i18n/useLocales.d.ts +31 -0
- package/dist/ra-core/src/i18n/useLocales.js +28 -0
- package/dist/ra-core/src/i18n/useResourceTranslation.d.ts +8 -0
- package/dist/ra-core/src/i18n/useResourceTranslation.js +18 -0
- package/dist/ra-core/src/i18n/useTranslatable.d.ts +29 -0
- package/dist/ra-core/src/i18n/useTranslatable.js +98 -0
- package/dist/ra-core/src/i18n/useTranslatableContext.d.ts +31 -0
- package/dist/ra-core/src/i18n/useTranslatableContext.js +38 -0
- package/dist/ra-core/src/i18n/useTranslate.d.ts +21 -0
- package/dist/ra-core/src/i18n/useTranslate.js +29 -0
- package/dist/ra-core/src/i18n/useTranslateLabel.d.ts +6 -0
- package/dist/ra-core/src/i18n/useTranslateLabel.js +26 -0
- package/dist/ra-core/src/index.d.ts +16 -0
- package/dist/ra-core/src/index.js +16 -0
- package/dist/ra-core/src/inference/InferredElement.d.ts +12 -0
- package/dist/ra-core/src/inference/InferredElement.js +40 -0
- package/dist/ra-core/src/inference/assertions.d.ts +26 -0
- package/dist/ra-core/src/inference/assertions.js +38 -0
- package/dist/ra-core/src/inference/getElementsFromRecords.d.ts +35 -0
- package/dist/ra-core/src/inference/getElementsFromRecords.js +39 -0
- package/dist/ra-core/src/inference/getValuesFromRecords.d.ts +28 -0
- package/dist/ra-core/src/inference/getValuesFromRecords.js +40 -0
- package/dist/ra-core/src/inference/index.d.ts +6 -0
- package/dist/ra-core/src/inference/index.js +6 -0
- package/dist/ra-core/src/inference/inferTypeFromValues.d.ts +21 -0
- package/dist/ra-core/src/inference/inferTypeFromValues.js +141 -0
- package/dist/ra-core/src/inference/types.d.ts +9 -0
- package/dist/ra-core/src/inference/types.js +1 -0
- package/dist/ra-core/src/notification/CloseNotificationContext.d.ts +2 -0
- package/dist/ra-core/src/notification/CloseNotificationContext.js +2 -0
- package/dist/ra-core/src/notification/NotificationContext.d.ts +37 -0
- package/dist/ra-core/src/notification/NotificationContext.js +35 -0
- package/dist/ra-core/src/notification/index.d.ts +9 -0
- package/dist/ra-core/src/notification/index.js +9 -0
- package/dist/ra-core/src/notification/types.d.ts +14 -0
- package/dist/ra-core/src/notification/types.js +1 -0
- package/dist/ra-core/src/notification/useCloseNotification.d.ts +1 -0
- package/dist/ra-core/src/notification/useCloseNotification.js +9 -0
- package/dist/ra-core/src/notification/useNotificationContext.d.ts +1 -0
- package/dist/ra-core/src/notification/useNotificationContext.js +3 -0
- package/dist/ra-core/src/notification/useNotify.d.ts +20 -0
- package/dist/ra-core/src/notification/useNotify.js +28 -0
- package/dist/ra-core/src/preferences/index.d.ts +7 -0
- package/dist/ra-core/src/preferences/index.js +7 -0
- package/dist/ra-core/src/preferences/usePreference.d.ts +18 -0
- package/dist/ra-core/src/preferences/usePreference.js +10 -0
- package/dist/ra-core/src/preferences/usePreferenceInput.d.ts +22 -0
- package/dist/ra-core/src/preferences/usePreferenceInput.js +43 -0
- package/dist/ra-core/src/preferences/usePreferencesEditor.d.ts +2 -0
- package/dist/ra-core/src/preferences/usePreferencesEditor.js +9 -0
- package/dist/ra-core/src/preferences/useSetInspectorTitle.d.ts +7 -0
- package/dist/ra-core/src/preferences/useSetInspectorTitle.js +19 -0
- package/dist/ra-core/src/routing/BasenameContext.d.ts +1 -0
- package/dist/ra-core/src/routing/BasenameContext.js +2 -0
- package/dist/ra-core/src/routing/RouterProvider.d.ts +163 -0
- package/dist/ra-core/src/routing/RouterProvider.js +1 -0
- package/dist/ra-core/src/routing/index.d.ts +28 -0
- package/dist/ra-core/src/routing/index.js +28 -0
- package/dist/ra-core/src/routing/types.d.ts +3 -0
- package/dist/ra-core/src/routing/types.js +1 -0
- package/dist/ra-core/src/routing/useBasename.d.ts +21 -0
- package/dist/ra-core/src/routing/useBasename.js +23 -0
- package/dist/ra-core/src/routing/useBlocker.d.ts +14 -0
- package/dist/ra-core/src/routing/useBlocker.js +17 -0
- package/dist/ra-core/src/routing/useCanBlock.d.ts +7 -0
- package/dist/ra-core/src/routing/useCanBlock.js +11 -0
- package/dist/ra-core/src/routing/useCreatePath.d.ts +42 -0
- package/dist/ra-core/src/routing/useCreatePath.js +69 -0
- package/dist/ra-core/src/routing/useGetPathForRecord.d.ts +42 -0
- package/dist/ra-core/src/routing/useGetPathForRecord.js +135 -0
- package/dist/ra-core/src/routing/useGetPathForRecordCallback.d.ts +6 -0
- package/dist/ra-core/src/routing/useGetPathForRecordCallback.js +82 -0
- package/dist/ra-core/src/routing/useInRouterContext.d.ts +11 -0
- package/dist/ra-core/src/routing/useInRouterContext.js +15 -0
- package/dist/ra-core/src/routing/useLocation.d.ts +10 -0
- package/dist/ra-core/src/routing/useLocation.js +13 -0
- package/dist/ra-core/src/routing/useMatch.d.ts +15 -0
- package/dist/ra-core/src/routing/useMatch.js +15 -0
- package/dist/ra-core/src/routing/useMatchPath.d.ts +17 -0
- package/dist/ra-core/src/routing/useMatchPath.js +17 -0
- package/dist/ra-core/src/routing/useNavigate.d.ts +12 -0
- package/dist/ra-core/src/routing/useNavigate.js +15 -0
- package/dist/ra-core/src/routing/useParams.d.ts +9 -0
- package/dist/ra-core/src/routing/useParams.js +13 -0
- package/dist/ra-core/src/routing/useRedirect.d.ts +30 -0
- package/dist/ra-core/src/routing/useRedirect.js +66 -0
- package/dist/ra-core/src/routing/useResetErrorBoundaryOnLocationChange.d.ts +7 -0
- package/dist/ra-core/src/routing/useResetErrorBoundaryOnLocationChange.js +17 -0
- package/dist/ra-core/src/routing/useRestoreScrollPosition.d.ts +40 -0
- package/dist/ra-core/src/routing/useRestoreScrollPosition.js +70 -0
- package/dist/ra-core/src/routing/useSplatPathBase.d.ts +13 -0
- package/dist/ra-core/src/routing/useSplatPathBase.js +21 -0
- package/dist/ra-core/src/store/index.d.ts +11 -0
- package/dist/ra-core/src/store/index.js +11 -0
- package/dist/ra-core/src/store/localStorageStore.d.ts +27 -0
- package/dist/ra-core/src/store/localStorageStore.js +198 -0
- package/dist/ra-core/src/store/types.d.ts +11 -0
- package/dist/ra-core/src/store/types.js +1 -0
- package/dist/ra-core/src/store/useRemoveFromStore.d.ts +17 -0
- package/dist/ra-core/src/store/useRemoveFromStore.js +29 -0
- package/dist/ra-core/src/store/useRemoveItemsFromStore.d.ts +17 -0
- package/dist/ra-core/src/store/useRemoveItemsFromStore.js +29 -0
- package/dist/ra-core/src/store/useResetStore.d.ts +17 -0
- package/dist/ra-core/src/store/useResetStore.js +21 -0
- package/dist/ra-core/src/store/useStore.d.ts +48 -0
- package/dist/ra-core/src/store/useStore.js +32 -0
- package/dist/ra-core/src/store/useStoreContext.d.ts +4 -0
- package/dist/ra-core/src/store/useStoreContext.js +6 -0
- package/dist/ra-core/src/test-ui/defaultI18nProvider.d.ts +2 -0
- package/dist/ra-core/src/test-ui/defaultI18nProvider.js +10 -0
- package/dist/ra-core/src/test-ui/index.d.ts +21 -0
- package/dist/ra-core/src/test-ui/index.js +21 -0
- package/dist/ra-core/src/types.d.ts +284 -0
- package/dist/ra-core/src/types.js +5 -0
- package/dist/ra-core/src/util/asyncDebounce.d.ts +7 -0
- package/dist/ra-core/src/util/asyncDebounce.js +29 -0
- package/dist/ra-core/src/util/escapePath.d.ts +11 -0
- package/dist/ra-core/src/util/escapePath.js +10 -0
- package/dist/ra-core/src/util/genericMemo.d.ts +5 -0
- package/dist/ra-core/src/util/genericMemo.js +14 -0
- package/dist/ra-core/src/util/getFieldLabelTranslationArgs.d.ts +22 -0
- package/dist/ra-core/src/util/getFieldLabelTranslationArgs.js +73 -0
- package/dist/ra-core/src/util/getMutationMode.d.ts +1 -0
- package/dist/ra-core/src/util/getMutationMode.js +13 -0
- package/dist/ra-core/src/util/hooks.d.ts +15 -0
- package/dist/ra-core/src/util/hooks.js +73 -0
- package/dist/ra-core/src/util/index.d.ts +20 -0
- package/dist/ra-core/src/util/index.js +19 -0
- package/dist/ra-core/src/util/mergeRefs.d.ts +2 -0
- package/dist/ra-core/src/util/mergeRefs.js +13 -0
- package/dist/ra-core/src/util/removeEmpty.d.ts +2 -0
- package/dist/ra-core/src/util/removeEmpty.js +16 -0
- package/dist/ra-core/src/util/removeKey.d.ts +2 -0
- package/dist/ra-core/src/util/removeKey.js +22 -0
- package/dist/ra-core/src/util/shallowEqual.d.ts +1 -0
- package/dist/ra-core/src/util/shallowEqual.js +29 -0
- package/dist/ra-core/src/util/useCheckForApplicationUpdate.d.ts +21 -0
- package/dist/ra-core/src/util/useCheckForApplicationUpdate.js +84 -0
- package/dist/ra-core/src/util/useDebouncedEvent.d.ts +8 -0
- package/dist/ra-core/src/util/useDebouncedEvent.js +28 -0
- package/dist/ra-core/src/util/useEvent.d.ts +7 -0
- package/dist/ra-core/src/util/useEvent.js +19 -0
- package/dist/ra-core/src/util/useFieldValue.d.ts +21 -0
- package/dist/ra-core/src/util/useFieldValue.js +27 -0
- package/dist/ra-core/src/util/useWhyDidYouUpdate.d.ts +12 -0
- package/dist/ra-core/src/util/useWhyDidYouUpdate.js +42 -0
- package/dist/ra-core/src/util/warning.d.ts +2 -0
- package/dist/ra-core/src/util/warning.js +5 -0
- package/dist/strato-faker-ecommerce/src/dataProvider.d.ts +2 -0
- package/dist/strato-faker-ecommerce/src/dataProvider.js +6 -0
- package/dist/strato-faker-ecommerce/src/generate.d.ts +8 -0
- package/dist/strato-faker-ecommerce/src/generate.js +148 -0
- package/dist/strato-faker-ecommerce/src/index.d.ts +3 -0
- package/dist/strato-faker-ecommerce/src/index.js +3 -0
- package/dist/strato-faker-ecommerce/src/types.d.ts +69 -0
- package/dist/strato-faker-ecommerce/src/types.js +1 -0
- package/dist/types.d.ts +1 -1
- package/package.json +5 -4
- package/src/dataProvider.ts +3 -3
- package/src/generate.test.ts +12 -12
- package/src/generate.ts +21 -14
- package/src/types.ts +1 -1
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
import { useEffect, useMemo, useRef } from 'react';
|
|
2
|
+
import { useQueryClient, useQuery, } from '@tanstack/react-query';
|
|
3
|
+
import union from 'lodash/union.js';
|
|
4
|
+
import { useDataProvider } from './useDataProvider';
|
|
5
|
+
import { useEvent } from '../util';
|
|
6
|
+
/**
|
|
7
|
+
* Call the dataProvider.getMany() method and return the resolved result
|
|
8
|
+
* as well as the loading state.
|
|
9
|
+
*
|
|
10
|
+
* The return value updates according to the request state:
|
|
11
|
+
*
|
|
12
|
+
* - start: { isPending: true, isFetching: true, refetch }
|
|
13
|
+
* - success: { data: [data from response], isPending: false, isFetching: false, refetch }
|
|
14
|
+
* - error: { error: [error from response], isPending: false, isFetching: false, refetch }
|
|
15
|
+
*
|
|
16
|
+
* This hook will return the cached result when called a second time
|
|
17
|
+
* with the same parameters, until the response arrives.
|
|
18
|
+
*
|
|
19
|
+
* This hook aggregates and deduplicates calls to the same resource, so for instance, if an app calls:
|
|
20
|
+
*
|
|
21
|
+
* useGetManyAggregate('tags', [1, 2, 3]);
|
|
22
|
+
* useGetManyAggregate('tags', [3, 4]);
|
|
23
|
+
*
|
|
24
|
+
* during the same tick, the hook will only call the dataProvider once with the following parameters:
|
|
25
|
+
*
|
|
26
|
+
* dataProvider.getMany('tags', [1, 2, 3, 4])
|
|
27
|
+
*
|
|
28
|
+
* @param resource The resource name, e.g. 'posts'
|
|
29
|
+
* @param {Params} params The getMany parameters { ids, meta }
|
|
30
|
+
* @param {Object} options Options object to pass to the dataProvider.
|
|
31
|
+
* @param {boolean} options.enabled Flag to conditionally run the query. If it's false, the query will not run
|
|
32
|
+
* @param {Function} options.onSuccess Side effect function to be executed upon success, e.g. { onSuccess: { refresh: true } }
|
|
33
|
+
* @param {Function} options.onError Side effect function to be executed upon failure, e.g. { onError: error => notify(error.message) }
|
|
34
|
+
*
|
|
35
|
+
* @typedef Params
|
|
36
|
+
* @prop params.ids The ids to get, e.g. [123, 456, 789]
|
|
37
|
+
* @prop params.meta Optional meta parameters
|
|
38
|
+
|
|
39
|
+
* @returns The current request state. Destructure as { data, error, isPending, isFetching, refetch }.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
*
|
|
43
|
+
* import { useGetManyAggregate, useRecordContext } from 'react-admin';
|
|
44
|
+
*
|
|
45
|
+
* const PostTags = () => {
|
|
46
|
+
* const record = useRecordContext();
|
|
47
|
+
* const { data, isPending, error } = useGetManyAggregate('tags', { ids: record.tagIds });
|
|
48
|
+
* if (isPending) { return <Loading />; }
|
|
49
|
+
* if (error) { return <p>ERROR</p>; }
|
|
50
|
+
* return (
|
|
51
|
+
* <ul>
|
|
52
|
+
* {data.map(tag => (
|
|
53
|
+
* <li key={tag.id}>{tag.name}</li>
|
|
54
|
+
* ))}
|
|
55
|
+
* </ul>
|
|
56
|
+
* );
|
|
57
|
+
* };
|
|
58
|
+
*/
|
|
59
|
+
export const useGetManyAggregate = (resource, params, options = {}) => {
|
|
60
|
+
const dataProvider = useDataProvider();
|
|
61
|
+
const queryClient = useQueryClient();
|
|
62
|
+
const { onError = noop, onSuccess = noop, onSettled = noop, enabled, ...queryOptions } = options;
|
|
63
|
+
const onSuccessEvent = useEvent(onSuccess);
|
|
64
|
+
const onErrorEvent = useEvent(onError);
|
|
65
|
+
const onSettledEvent = useEvent(onSettled);
|
|
66
|
+
const { ids, meta } = params;
|
|
67
|
+
const placeholderData = useMemo(() => {
|
|
68
|
+
const records = (Array.isArray(ids) ? ids : [ids]).map(id => queryClient.getQueryData([
|
|
69
|
+
resource,
|
|
70
|
+
'getOne',
|
|
71
|
+
{ id: String(id), meta },
|
|
72
|
+
]));
|
|
73
|
+
if (records.some(record => record === undefined)) {
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
return records;
|
|
78
|
+
}
|
|
79
|
+
}, [ids, queryClient, resource, meta]);
|
|
80
|
+
const result = useQuery({
|
|
81
|
+
queryKey: [
|
|
82
|
+
resource,
|
|
83
|
+
'getMany',
|
|
84
|
+
{
|
|
85
|
+
ids: (Array.isArray(ids) ? ids : [ids]).map(id => String(id)),
|
|
86
|
+
meta,
|
|
87
|
+
},
|
|
88
|
+
],
|
|
89
|
+
queryFn: queryParams => new Promise((resolve, reject) => {
|
|
90
|
+
if (!ids || ids.length === 0) {
|
|
91
|
+
// no need to call the dataProvider
|
|
92
|
+
return resolve([]);
|
|
93
|
+
}
|
|
94
|
+
// debounced / batched fetch
|
|
95
|
+
return callGetManyQueries({
|
|
96
|
+
resource,
|
|
97
|
+
ids,
|
|
98
|
+
meta,
|
|
99
|
+
resolve,
|
|
100
|
+
reject,
|
|
101
|
+
dataProvider,
|
|
102
|
+
queryClient,
|
|
103
|
+
signal: dataProvider.supportAbortSignal === true
|
|
104
|
+
? queryParams.signal
|
|
105
|
+
: undefined,
|
|
106
|
+
});
|
|
107
|
+
}),
|
|
108
|
+
placeholderData,
|
|
109
|
+
enabled: enabled ?? ids != null,
|
|
110
|
+
retry: false,
|
|
111
|
+
...queryOptions,
|
|
112
|
+
});
|
|
113
|
+
const metaValue = useRef(meta);
|
|
114
|
+
const resourceValue = useRef(resource);
|
|
115
|
+
useEffect(() => {
|
|
116
|
+
metaValue.current = meta;
|
|
117
|
+
}, [meta]);
|
|
118
|
+
useEffect(() => {
|
|
119
|
+
resourceValue.current = resource;
|
|
120
|
+
}, [resource]);
|
|
121
|
+
useEffect(() => {
|
|
122
|
+
if (result.data === undefined ||
|
|
123
|
+
result.error != null ||
|
|
124
|
+
result.isFetching)
|
|
125
|
+
return;
|
|
126
|
+
// optimistically populate the getOne cache
|
|
127
|
+
(result.data ?? []).forEach(record => {
|
|
128
|
+
queryClient.setQueryData([
|
|
129
|
+
resourceValue.current,
|
|
130
|
+
'getOne',
|
|
131
|
+
{ id: String(record.id), meta: metaValue.current },
|
|
132
|
+
], oldRecord => oldRecord ?? record);
|
|
133
|
+
});
|
|
134
|
+
onSuccessEvent(result.data);
|
|
135
|
+
}, [
|
|
136
|
+
queryClient,
|
|
137
|
+
onSuccessEvent,
|
|
138
|
+
result.data,
|
|
139
|
+
result.error,
|
|
140
|
+
result.isFetching,
|
|
141
|
+
]);
|
|
142
|
+
useEffect(() => {
|
|
143
|
+
if (result.error == null || result.isFetching)
|
|
144
|
+
return;
|
|
145
|
+
onErrorEvent(result.error);
|
|
146
|
+
}, [onErrorEvent, result.error, result.isFetching]);
|
|
147
|
+
useEffect(() => {
|
|
148
|
+
if (result.status === 'pending' || result.isFetching)
|
|
149
|
+
return;
|
|
150
|
+
onSettledEvent(result.data, result.error);
|
|
151
|
+
}, [
|
|
152
|
+
onSettledEvent,
|
|
153
|
+
result.data,
|
|
154
|
+
result.error,
|
|
155
|
+
result.status,
|
|
156
|
+
result.isFetching,
|
|
157
|
+
]);
|
|
158
|
+
return result;
|
|
159
|
+
};
|
|
160
|
+
/**
|
|
161
|
+
* Batch all calls to a function into one single call with the arguments of all the calls.
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* let sum = 0;
|
|
165
|
+
* const add = (args) => { sum = args.reduce((arg, total) => total + arg, 0); };
|
|
166
|
+
* const addBatched = batch(add);
|
|
167
|
+
* addBatched(2);
|
|
168
|
+
* addBatched(8);
|
|
169
|
+
* // add will be called once with arguments [2, 8]
|
|
170
|
+
* // and sum will be equal to 10
|
|
171
|
+
*/
|
|
172
|
+
const batch = fn => {
|
|
173
|
+
let capturedArgs = [];
|
|
174
|
+
let timeout = null;
|
|
175
|
+
return (arg) => {
|
|
176
|
+
capturedArgs.push(arg);
|
|
177
|
+
if (timeout)
|
|
178
|
+
clearTimeout(timeout);
|
|
179
|
+
timeout = setTimeout(() => {
|
|
180
|
+
timeout = null;
|
|
181
|
+
fn([...capturedArgs]);
|
|
182
|
+
capturedArgs = [];
|
|
183
|
+
}, 0);
|
|
184
|
+
};
|
|
185
|
+
};
|
|
186
|
+
/**
|
|
187
|
+
* Group and execute all calls to the dataProvider.getMany() method for the current tick
|
|
188
|
+
*
|
|
189
|
+
* Thanks to batch(), this function executes at most once per tick,
|
|
190
|
+
* whatever the number of calls to useGetManyAggregate().
|
|
191
|
+
*/
|
|
192
|
+
const callGetManyQueries = batch((calls) => {
|
|
193
|
+
const dataProvider = calls[0].dataProvider;
|
|
194
|
+
const queryClient = calls[0].queryClient;
|
|
195
|
+
/**
|
|
196
|
+
* Aggregate calls by resource and meta
|
|
197
|
+
*
|
|
198
|
+
* callsByResourceAndMeta will look like:
|
|
199
|
+
* {
|
|
200
|
+
* 'posts|{"test":true}': [{ resource, ids, resolve, reject, dataProvider, queryClient }, ...],
|
|
201
|
+
* 'posts|{"test":false}': [{ resource, ids, resolve, reject, dataProvider, queryClient }, ...],
|
|
202
|
+
* tags: [{ resource, ids, resolve, reject, dataProvider, queryClient }, ...],
|
|
203
|
+
* }
|
|
204
|
+
*/
|
|
205
|
+
const callsByResourceAndMeta = calls.reduce((acc, callArgs) => {
|
|
206
|
+
const key = `${callArgs.resource}|${JSON.stringify(callArgs.meta)}`;
|
|
207
|
+
if (!acc[key]) {
|
|
208
|
+
acc[key] = [];
|
|
209
|
+
}
|
|
210
|
+
acc[key].push(callArgs);
|
|
211
|
+
return acc;
|
|
212
|
+
}, {});
|
|
213
|
+
/**
|
|
214
|
+
* For each resource/meta association, aggregate ids and call dataProvider.getMany() once
|
|
215
|
+
*/
|
|
216
|
+
Object.keys(callsByResourceAndMeta).forEach(resource => {
|
|
217
|
+
const callsForResource = callsByResourceAndMeta[resource];
|
|
218
|
+
const uniqueResource = callsForResource.reduce((acc, { resource }) => resource || acc, '' // Should never happen as we always have a resource in callArgs but makes TS happy
|
|
219
|
+
);
|
|
220
|
+
/**
|
|
221
|
+
* Extract ids from queries, aggregate and deduplicate them
|
|
222
|
+
*
|
|
223
|
+
* @example from [[1, 2], [2, null, 3], [4, null]] to [1, 2, 3, 4]
|
|
224
|
+
*/
|
|
225
|
+
const aggregatedIds = callsForResource
|
|
226
|
+
.reduce((acc, { ids }) => union(acc, ids), []) // concat + unique
|
|
227
|
+
.filter(v => v != null && v !== ''); // remove null values
|
|
228
|
+
const uniqueMeta = callsForResource.reduce((acc, { meta }) => meta || acc, undefined);
|
|
229
|
+
if (aggregatedIds.length === 0) {
|
|
230
|
+
// no need to call the data provider if all the ids are null
|
|
231
|
+
callsForResource.forEach(({ resolve }) => {
|
|
232
|
+
resolve([]);
|
|
233
|
+
});
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
const callThatHasAllAggregatedIds = callsForResource.find(({ ids, signal }) => JSON.stringify(ids) === JSON.stringify(aggregatedIds) &&
|
|
237
|
+
!signal?.aborted);
|
|
238
|
+
if (callThatHasAllAggregatedIds) {
|
|
239
|
+
// There is only one call (no aggregation), or one of the calls has the same ids as the sum of all calls.
|
|
240
|
+
// Either way, we can't trigger a new fetchQuery with the same signature, as it's already pending.
|
|
241
|
+
// Therefore, we reply with the dataProvider
|
|
242
|
+
const { dataProvider, resource, ids, meta, signal } = callThatHasAllAggregatedIds;
|
|
243
|
+
dataProvider
|
|
244
|
+
.getMany(resource, { ids, meta, signal })
|
|
245
|
+
.then(({ data }) => data)
|
|
246
|
+
.then(data => {
|
|
247
|
+
// We must then resolve all the pending calls with the data they requested
|
|
248
|
+
callsForResource.forEach(({ ids, resolve }) => {
|
|
249
|
+
resolve(data.filter(record => ids
|
|
250
|
+
.map(id => String(id))
|
|
251
|
+
.includes(String(record.id))));
|
|
252
|
+
});
|
|
253
|
+
}, error => {
|
|
254
|
+
// All pending calls must also receive the error
|
|
255
|
+
callsForResource.forEach(({ reject }) => {
|
|
256
|
+
reject(error);
|
|
257
|
+
});
|
|
258
|
+
});
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Call dataProvider.getMany() with the aggregatedIds,
|
|
263
|
+
* and resolve each of the promises using the results
|
|
264
|
+
*/
|
|
265
|
+
queryClient
|
|
266
|
+
.fetchQuery({
|
|
267
|
+
queryKey: [
|
|
268
|
+
uniqueResource,
|
|
269
|
+
'getMany',
|
|
270
|
+
{
|
|
271
|
+
ids: aggregatedIds.map(id => String(id)),
|
|
272
|
+
meta: uniqueMeta,
|
|
273
|
+
},
|
|
274
|
+
],
|
|
275
|
+
queryFn: queryParams => dataProvider
|
|
276
|
+
.getMany(uniqueResource, {
|
|
277
|
+
ids: aggregatedIds,
|
|
278
|
+
meta: uniqueMeta,
|
|
279
|
+
signal: dataProvider.supportAbortSignal === true
|
|
280
|
+
? queryParams.signal
|
|
281
|
+
: undefined,
|
|
282
|
+
})
|
|
283
|
+
.then(({ data }) => data),
|
|
284
|
+
})
|
|
285
|
+
.then(data => {
|
|
286
|
+
callsForResource.forEach(({ ids, resolve }) => {
|
|
287
|
+
resolve(data.filter(record => ids
|
|
288
|
+
.map(id => String(id))
|
|
289
|
+
.includes(String(record.id))));
|
|
290
|
+
});
|
|
291
|
+
})
|
|
292
|
+
.catch(error => callsForResource.forEach(({ reject }) => reject(error)));
|
|
293
|
+
});
|
|
294
|
+
});
|
|
295
|
+
const noop = () => undefined;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { UseQueryOptions, UseQueryResult } from '@tanstack/react-query';
|
|
2
|
+
import { RaRecord, GetManyReferenceParams, GetManyReferenceResult } from '../types';
|
|
3
|
+
/**
|
|
4
|
+
* Call the dataProvider.getManyReference() method and return the resolved result
|
|
5
|
+
* as well as the loading state.
|
|
6
|
+
*
|
|
7
|
+
* The return value updates according to the request state:
|
|
8
|
+
*
|
|
9
|
+
* - start: { isPending: true, refetch }
|
|
10
|
+
* - success: { data: [data from store], total: [total from response], isPending: false, refetch }
|
|
11
|
+
* - error: { error: [error from response], isPending: false, refetch }
|
|
12
|
+
*
|
|
13
|
+
* This hook will return the cached result when called a second time
|
|
14
|
+
* with the same parameters, until the response arrives.
|
|
15
|
+
*
|
|
16
|
+
* @param {string} resource The resource name, e.g. 'posts'
|
|
17
|
+
* @param {Params} params The getManyReference parameters { target, id, pagination, sort, filter, meta }
|
|
18
|
+
* @param {Object} options Options object to pass to the queryClient.
|
|
19
|
+
* May include side effects to be executed upon success or failure, e.g. { onSuccess: () => { refresh(); } }
|
|
20
|
+
*
|
|
21
|
+
* @typedef Params
|
|
22
|
+
* @prop params.target The target resource key, e.g. 'post_id'
|
|
23
|
+
* @prop params.id The identifier of the record to look for in target, e.g. '123'
|
|
24
|
+
* @prop params.pagination The request pagination { page, perPage }, e.g. { page: 1, perPage: 10 }
|
|
25
|
+
* @prop params.sort The request sort { field, order }, e.g. { field: 'id', order: 'DESC' }
|
|
26
|
+
* @prop params.filter The request filters, e.g. { title: 'hello, world' }
|
|
27
|
+
* @prop params.meta Optional meta parameters
|
|
28
|
+
*
|
|
29
|
+
* @returns The current request state. Destructure as { data, total, error, isPending, refetch }.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
*
|
|
33
|
+
* import { useGetManyReference, useRecordContext } from 'react-admin';
|
|
34
|
+
*
|
|
35
|
+
* const PostComments = () => {
|
|
36
|
+
* const record = useRecordContext();
|
|
37
|
+
* // fetch all comments related to the current record
|
|
38
|
+
* const { data, isPending, error } = useGetManyReference(
|
|
39
|
+
* 'comments',
|
|
40
|
+
* { target: 'post_id', id: record.id, pagination: { page: 1, perPage: 10 }, sort: { field: 'published_at', order: 'DESC' } }
|
|
41
|
+
* );
|
|
42
|
+
* if (isPending) { return <Loading />; }
|
|
43
|
+
* if (error) { return <p>ERROR</p>; }
|
|
44
|
+
* return <ul>{data.map(comment =>
|
|
45
|
+
* <li key={comment.id}>{comment.body}</li>
|
|
46
|
+
* )}</ul>;
|
|
47
|
+
* };
|
|
48
|
+
*/
|
|
49
|
+
export declare const useGetManyReference: <RecordType extends RaRecord = any, ErrorType = Error>(resource: string, params?: Partial<GetManyReferenceParams>, options?: UseGetManyReferenceHookOptions<RecordType, ErrorType>) => UseGetManyReferenceHookValue<RecordType, ErrorType>;
|
|
50
|
+
export type UseGetManyReferenceHookOptions<RecordType extends RaRecord = any, ErrorType = Error> = Omit<UseQueryOptions<GetManyReferenceResult<RecordType>, ErrorType>, 'queryKey' | 'queryFn'> & {
|
|
51
|
+
onSuccess?: (data: GetManyReferenceResult<RecordType>) => void;
|
|
52
|
+
onError?: (error: ErrorType) => void;
|
|
53
|
+
onSettled?: (data?: GetManyReferenceResult<RecordType>, error?: ErrorType | null) => void;
|
|
54
|
+
};
|
|
55
|
+
export type UseGetManyReferenceHookValue<RecordType extends RaRecord = any, ErrorType = Error> = UseQueryResult<RecordType[], ErrorType> & {
|
|
56
|
+
total?: number;
|
|
57
|
+
pageInfo?: {
|
|
58
|
+
hasNextPage?: boolean;
|
|
59
|
+
hasPreviousPage?: boolean;
|
|
60
|
+
};
|
|
61
|
+
meta?: any;
|
|
62
|
+
};
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { useEffect, useMemo } from 'react';
|
|
2
|
+
import { useQuery, useQueryClient, } from '@tanstack/react-query';
|
|
3
|
+
import { useDataProvider } from './useDataProvider';
|
|
4
|
+
import { useEvent } from '../util';
|
|
5
|
+
/**
|
|
6
|
+
* Call the dataProvider.getManyReference() method and return the resolved result
|
|
7
|
+
* as well as the loading state.
|
|
8
|
+
*
|
|
9
|
+
* The return value updates according to the request state:
|
|
10
|
+
*
|
|
11
|
+
* - start: { isPending: true, refetch }
|
|
12
|
+
* - success: { data: [data from store], total: [total from response], isPending: false, refetch }
|
|
13
|
+
* - error: { error: [error from response], isPending: false, refetch }
|
|
14
|
+
*
|
|
15
|
+
* This hook will return the cached result when called a second time
|
|
16
|
+
* with the same parameters, until the response arrives.
|
|
17
|
+
*
|
|
18
|
+
* @param {string} resource The resource name, e.g. 'posts'
|
|
19
|
+
* @param {Params} params The getManyReference parameters { target, id, pagination, sort, filter, meta }
|
|
20
|
+
* @param {Object} options Options object to pass to the queryClient.
|
|
21
|
+
* May include side effects to be executed upon success or failure, e.g. { onSuccess: () => { refresh(); } }
|
|
22
|
+
*
|
|
23
|
+
* @typedef Params
|
|
24
|
+
* @prop params.target The target resource key, e.g. 'post_id'
|
|
25
|
+
* @prop params.id The identifier of the record to look for in target, e.g. '123'
|
|
26
|
+
* @prop params.pagination The request pagination { page, perPage }, e.g. { page: 1, perPage: 10 }
|
|
27
|
+
* @prop params.sort The request sort { field, order }, e.g. { field: 'id', order: 'DESC' }
|
|
28
|
+
* @prop params.filter The request filters, e.g. { title: 'hello, world' }
|
|
29
|
+
* @prop params.meta Optional meta parameters
|
|
30
|
+
*
|
|
31
|
+
* @returns The current request state. Destructure as { data, total, error, isPending, refetch }.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
*
|
|
35
|
+
* import { useGetManyReference, useRecordContext } from 'react-admin';
|
|
36
|
+
*
|
|
37
|
+
* const PostComments = () => {
|
|
38
|
+
* const record = useRecordContext();
|
|
39
|
+
* // fetch all comments related to the current record
|
|
40
|
+
* const { data, isPending, error } = useGetManyReference(
|
|
41
|
+
* 'comments',
|
|
42
|
+
* { target: 'post_id', id: record.id, pagination: { page: 1, perPage: 10 }, sort: { field: 'published_at', order: 'DESC' } }
|
|
43
|
+
* );
|
|
44
|
+
* if (isPending) { return <Loading />; }
|
|
45
|
+
* if (error) { return <p>ERROR</p>; }
|
|
46
|
+
* return <ul>{data.map(comment =>
|
|
47
|
+
* <li key={comment.id}>{comment.body}</li>
|
|
48
|
+
* )}</ul>;
|
|
49
|
+
* };
|
|
50
|
+
*/
|
|
51
|
+
export const useGetManyReference = (resource, params = {}, options = {}) => {
|
|
52
|
+
const { target, id, pagination = { page: 1, perPage: 25 }, sort = { field: 'id', order: 'DESC' }, filter = {}, meta, } = params;
|
|
53
|
+
const dataProvider = useDataProvider();
|
|
54
|
+
const queryClient = useQueryClient();
|
|
55
|
+
const { onError = noop, onSuccess = noop, onSettled = noop, ...queryOptions } = options;
|
|
56
|
+
const onSuccessEvent = useEvent(onSuccess);
|
|
57
|
+
const onErrorEvent = useEvent(onError);
|
|
58
|
+
const onSettledEvent = useEvent(onSettled);
|
|
59
|
+
const result = useQuery({
|
|
60
|
+
queryKey: [
|
|
61
|
+
resource,
|
|
62
|
+
'getManyReference',
|
|
63
|
+
{ target, id, pagination, sort, filter, meta },
|
|
64
|
+
],
|
|
65
|
+
queryFn: queryParams => {
|
|
66
|
+
if (!target || id == null) {
|
|
67
|
+
// check at runtime to support partial parameters with the enabled option
|
|
68
|
+
return Promise.reject(new Error('target and id are required'));
|
|
69
|
+
}
|
|
70
|
+
return dataProvider
|
|
71
|
+
.getManyReference(resource, {
|
|
72
|
+
target,
|
|
73
|
+
id,
|
|
74
|
+
pagination,
|
|
75
|
+
sort,
|
|
76
|
+
filter,
|
|
77
|
+
meta,
|
|
78
|
+
signal: dataProvider.supportAbortSignal === true
|
|
79
|
+
? queryParams.signal
|
|
80
|
+
: undefined,
|
|
81
|
+
})
|
|
82
|
+
.then(({ data, total, pageInfo, meta }) => ({
|
|
83
|
+
data,
|
|
84
|
+
total,
|
|
85
|
+
pageInfo,
|
|
86
|
+
meta,
|
|
87
|
+
}));
|
|
88
|
+
},
|
|
89
|
+
...queryOptions,
|
|
90
|
+
});
|
|
91
|
+
useEffect(() => {
|
|
92
|
+
if (result.data === undefined)
|
|
93
|
+
return;
|
|
94
|
+
// optimistically populate the getOne cache
|
|
95
|
+
result.data?.data?.forEach(record => {
|
|
96
|
+
queryClient.setQueryData([resource, 'getOne', { id: String(record.id), meta }], oldRecord => oldRecord ?? record);
|
|
97
|
+
});
|
|
98
|
+
onSuccessEvent(result.data);
|
|
99
|
+
}, [queryClient, meta, onSuccessEvent, resource, result.data]);
|
|
100
|
+
useEffect(() => {
|
|
101
|
+
if (result.error == null)
|
|
102
|
+
return;
|
|
103
|
+
onErrorEvent(result.error);
|
|
104
|
+
}, [onErrorEvent, result.error]);
|
|
105
|
+
useEffect(() => {
|
|
106
|
+
if (result.status === 'pending')
|
|
107
|
+
return;
|
|
108
|
+
onSettledEvent(result.data, result.error);
|
|
109
|
+
}, [onSettledEvent, result.data, result.error, result.status]);
|
|
110
|
+
return useMemo(() => result.data
|
|
111
|
+
? {
|
|
112
|
+
...result,
|
|
113
|
+
...result.data,
|
|
114
|
+
}
|
|
115
|
+
: result, [result]);
|
|
116
|
+
};
|
|
117
|
+
const noop = () => undefined;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { RaRecord, GetOneParams, GetOneResult } from '../types';
|
|
2
|
+
import { UseQueryOptions, UseQueryResult } from '@tanstack/react-query';
|
|
3
|
+
/**
|
|
4
|
+
* Call the dataProvider.getOne() method and return the resolved value
|
|
5
|
+
* as well as the loading state.
|
|
6
|
+
*
|
|
7
|
+
* The return value updates according to the request state:
|
|
8
|
+
*
|
|
9
|
+
* - start: { isPending: true, isFetching: true, refetch }
|
|
10
|
+
* - success: { data: [data from response], isPending: false, refetch }
|
|
11
|
+
* - error: { error: [error from response], isPending: false, refetch }
|
|
12
|
+
*
|
|
13
|
+
* This hook will return the cached result when called a second time
|
|
14
|
+
* with the same parameters, until the response arrives.
|
|
15
|
+
*
|
|
16
|
+
* @param resource The resource name, e.g. 'posts'
|
|
17
|
+
* @param {Params} params The getOne parameters { id, meta }, e.g. { id: 123 }
|
|
18
|
+
* @param {Options} options Options object to pass to the react-query queryClient.
|
|
19
|
+
*
|
|
20
|
+
* @typedef Params
|
|
21
|
+
* @prop id a resource identifier, e.g. 123
|
|
22
|
+
*
|
|
23
|
+
* @typedef Options
|
|
24
|
+
* @prop enabled Flag to conditionally run the query. If it's false, the query will not run
|
|
25
|
+
* @prop onSuccess Side effect function to be executed upon success, e.g. { onSuccess: { refresh: true } }
|
|
26
|
+
* @prop onError Side effect function to be executed upon failure, e.g. { onError: error => notify(error.message) }
|
|
27
|
+
*
|
|
28
|
+
* @returns The current request state. Destructure as { data, error, isPending, refetch }.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
*
|
|
32
|
+
* import { useGetOne, useRecordContext } from 'react-admin';
|
|
33
|
+
*
|
|
34
|
+
* const UserProfile = () => {
|
|
35
|
+
* const record = useRecordContext();
|
|
36
|
+
* const { data, isPending, error } = useGetOne('users', { id: record.id });
|
|
37
|
+
* if (isPending) { return <Loading />; }
|
|
38
|
+
* if (error) { return <p>ERROR</p>; }
|
|
39
|
+
* return <div>User {data.username}</div>;
|
|
40
|
+
* };
|
|
41
|
+
*/
|
|
42
|
+
export declare const useGetOne: <RecordType extends RaRecord = any, ErrorType = Error>(resource: string, { id, meta }: Partial<GetOneParams<RecordType>>, options?: UseGetOneOptions<RecordType, ErrorType>) => UseGetOneHookValue<RecordType, ErrorType>;
|
|
43
|
+
export type UseGetOneOptions<RecordType extends RaRecord = any, ErrorType = Error> = Omit<UseQueryOptions<GetOneResult<RecordType>['data'], ErrorType>, 'queryKey' | 'queryFn'> & {
|
|
44
|
+
onSuccess?: (data: GetOneResult<RecordType>['data']) => void;
|
|
45
|
+
onError?: (error: ErrorType) => void;
|
|
46
|
+
onSettled?: (data?: GetOneResult<RecordType>['data'], error?: ErrorType | null) => void;
|
|
47
|
+
};
|
|
48
|
+
export type UseGetOneHookValue<RecordType extends RaRecord = any, ErrorType = Error> = UseQueryResult<GetOneResult<RecordType>['data'], ErrorType>;
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { useQuery, } from '@tanstack/react-query';
|
|
2
|
+
import { useDataProvider } from './useDataProvider';
|
|
3
|
+
import { useEffect } from 'react';
|
|
4
|
+
import { useEvent } from '../util';
|
|
5
|
+
/**
|
|
6
|
+
* Call the dataProvider.getOne() method and return the resolved value
|
|
7
|
+
* as well as the loading state.
|
|
8
|
+
*
|
|
9
|
+
* The return value updates according to the request state:
|
|
10
|
+
*
|
|
11
|
+
* - start: { isPending: true, isFetching: true, refetch }
|
|
12
|
+
* - success: { data: [data from response], isPending: false, refetch }
|
|
13
|
+
* - error: { error: [error from response], isPending: false, refetch }
|
|
14
|
+
*
|
|
15
|
+
* This hook will return the cached result when called a second time
|
|
16
|
+
* with the same parameters, until the response arrives.
|
|
17
|
+
*
|
|
18
|
+
* @param resource The resource name, e.g. 'posts'
|
|
19
|
+
* @param {Params} params The getOne parameters { id, meta }, e.g. { id: 123 }
|
|
20
|
+
* @param {Options} options Options object to pass to the react-query queryClient.
|
|
21
|
+
*
|
|
22
|
+
* @typedef Params
|
|
23
|
+
* @prop id a resource identifier, e.g. 123
|
|
24
|
+
*
|
|
25
|
+
* @typedef Options
|
|
26
|
+
* @prop enabled Flag to conditionally run the query. If it's false, the query will not run
|
|
27
|
+
* @prop onSuccess Side effect function to be executed upon success, e.g. { onSuccess: { refresh: true } }
|
|
28
|
+
* @prop onError Side effect function to be executed upon failure, e.g. { onError: error => notify(error.message) }
|
|
29
|
+
*
|
|
30
|
+
* @returns The current request state. Destructure as { data, error, isPending, refetch }.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
*
|
|
34
|
+
* import { useGetOne, useRecordContext } from 'react-admin';
|
|
35
|
+
*
|
|
36
|
+
* const UserProfile = () => {
|
|
37
|
+
* const record = useRecordContext();
|
|
38
|
+
* const { data, isPending, error } = useGetOne('users', { id: record.id });
|
|
39
|
+
* if (isPending) { return <Loading />; }
|
|
40
|
+
* if (error) { return <p>ERROR</p>; }
|
|
41
|
+
* return <div>User {data.username}</div>;
|
|
42
|
+
* };
|
|
43
|
+
*/
|
|
44
|
+
export const useGetOne = (resource, { id, meta }, options = {}) => {
|
|
45
|
+
const dataProvider = useDataProvider();
|
|
46
|
+
const { onError = noop, onSuccess = noop, onSettled = noop, enabled, ...queryOptions } = options;
|
|
47
|
+
const onSuccessEvent = useEvent(onSuccess);
|
|
48
|
+
const onErrorEvent = useEvent(onError);
|
|
49
|
+
const onSettledEvent = useEvent(onSettled);
|
|
50
|
+
const result = useQuery({
|
|
51
|
+
// Sometimes the id comes as a string (e.g. when read from the URL in a Show view).
|
|
52
|
+
// Sometimes the id comes as a number (e.g. when read from a Record in useGetList response).
|
|
53
|
+
// As the react-query cache is type-sensitive, we always stringify the identifier to get a match
|
|
54
|
+
queryKey: [resource, 'getOne', { id: String(id), meta }],
|
|
55
|
+
queryFn: queryParams => id == null
|
|
56
|
+
? Promise.reject('useGetOne: id cannot be null')
|
|
57
|
+
: dataProvider
|
|
58
|
+
.getOne(resource, {
|
|
59
|
+
id,
|
|
60
|
+
meta,
|
|
61
|
+
signal: dataProvider.supportAbortSignal === true
|
|
62
|
+
? queryParams.signal
|
|
63
|
+
: undefined,
|
|
64
|
+
})
|
|
65
|
+
.then(({ data }) => data),
|
|
66
|
+
enabled: enabled ?? id != null,
|
|
67
|
+
...queryOptions,
|
|
68
|
+
});
|
|
69
|
+
useEffect(() => {
|
|
70
|
+
if (result.data === undefined ||
|
|
71
|
+
result.error != null ||
|
|
72
|
+
result.isFetching)
|
|
73
|
+
return;
|
|
74
|
+
onSuccessEvent(result.data);
|
|
75
|
+
}, [onSuccessEvent, result.data, result.error, result.isFetching]);
|
|
76
|
+
useEffect(() => {
|
|
77
|
+
if (result.error == null || result.isFetching)
|
|
78
|
+
return;
|
|
79
|
+
onErrorEvent(result.error);
|
|
80
|
+
}, [onErrorEvent, result.error, result.isFetching]);
|
|
81
|
+
useEffect(() => {
|
|
82
|
+
if (result.status === 'pending' || result.isFetching)
|
|
83
|
+
return;
|
|
84
|
+
onSettledEvent(result.data, result.error);
|
|
85
|
+
}, [
|
|
86
|
+
onSettledEvent,
|
|
87
|
+
result.data,
|
|
88
|
+
result.error,
|
|
89
|
+
result.status,
|
|
90
|
+
result.isFetching,
|
|
91
|
+
]);
|
|
92
|
+
return result;
|
|
93
|
+
};
|
|
94
|
+
const noop = () => undefined;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Identifier } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Helper hook to get the current `recordId`.
|
|
4
|
+
*
|
|
5
|
+
* `recordId` is obtained from parameters if passed as a parameter, or from the `RecordContext` if there is one, or, lastly, from the router URL.
|
|
6
|
+
*
|
|
7
|
+
* @param {any} recordId optional if used inside a RecordContextProvider or if recordId can be guessed from the URL
|
|
8
|
+
*
|
|
9
|
+
* @returns The `recordId` determined in this manner.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* const recordId = useGetRecordId();
|
|
13
|
+
*/
|
|
14
|
+
export declare function useGetRecordId(recordId?: Identifier): Identifier;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { useRecordContext } from '../controller';
|
|
2
|
+
import { useParams } from '../routing';
|
|
3
|
+
/**
|
|
4
|
+
* Helper hook to get the current `recordId`.
|
|
5
|
+
*
|
|
6
|
+
* `recordId` is obtained from parameters if passed as a parameter, or from the `RecordContext` if there is one, or, lastly, from the router URL.
|
|
7
|
+
*
|
|
8
|
+
* @param {any} recordId optional if used inside a RecordContextProvider or if recordId can be guessed from the URL
|
|
9
|
+
*
|
|
10
|
+
* @returns The `recordId` determined in this manner.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* const recordId = useGetRecordId();
|
|
14
|
+
*/
|
|
15
|
+
export function useGetRecordId(recordId) {
|
|
16
|
+
const contextRecord = useRecordContext();
|
|
17
|
+
const { id: routeId } = useParams();
|
|
18
|
+
const actualRecordId = recordId ?? contextRecord?.id ?? routeId;
|
|
19
|
+
if (actualRecordId == null)
|
|
20
|
+
throw new Error(`useGetRecordId could not find the current record id. You need to use it inside a RecordContextProvider, or inside a supported route, or provide the record id to the hook yourself.`);
|
|
21
|
+
return actualRecordId;
|
|
22
|
+
}
|