ra-core
Version:
Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React
144 lines • 4.83 kB
JavaScript
import { useEffect, useRef } from 'react';
import { useQuery, useQueryClient, } from '@tanstack/react-query';
import { useDataProvider } from "./useDataProvider.js";
import { useEvent } from "../util/index.js";
/**
* Call the dataProvider.getMany() method and return the resolved result
* as well as the loading state.
*
* The return value updates according to the request state:
*
* - start: { isPending: true, refetch }
* - success: { data: [data from store], isPending: false, refetch }
* - error: { error: [error from response], isPending: false, refetch }
*
* This hook will return the cached result when called a second time
* with the same parameters, until the response arrives.
*
* @param {string} resource The resource name, e.g. 'posts'
* @param {Params} params The getMany parameters { ids, meta }
* @param {Object} options Options object to pass to the queryClient.
* May include side effects to be executed upon success or failure, e.g. { onSuccess: () => { refresh(); } }
*
* @typedef Params
* @prop params.ids The ids to get, e.g. [123, 456, 789]
* @prop params.meta Optional meta parameters
*
* @returns The current request state. Destructure as { data, error, isPending, refetch }.
*
* @example
*
* import { useGetMany } from 'react-admin';
*
* const PostTags = ({ post }) => {
* const { data, isPending, error } = useGetMany(
* 'tags',
* { ids: post.tags },
* );
* if (isPending) { return <Loading />; }
* if (error) { return <p>ERROR</p>; }
* return <ul>{data.map(tag =>
* <li key={tag.id}>{tag.name}</li>
* )}</ul>;
* };
*/
export const useGetMany = (resource, params, options = {}) => {
const { ids, meta } = params;
const dataProvider = useDataProvider();
const queryClient = useQueryClient();
const { onError = noop, onSuccess = noop, onSettled = noop, enabled, ...queryOptions } = options;
const onSuccessEvent = useEvent(onSuccess);
const onErrorEvent = useEvent(onError);
const onSettledEvent = useEvent(onSettled);
const result = useQuery({
queryKey: [
resource,
'getMany',
{
ids: !ids || ids.length === 0 ? [] : ids.map(id => String(id)),
meta,
},
],
queryFn: queryParams => {
if (!ids || ids.length === 0) {
// no need to call the dataProvider
return Promise.resolve([]);
}
return dataProvider
.getMany(resource, {
ids,
meta,
signal: dataProvider.supportAbortSignal === true
? queryParams.signal
: undefined,
})
.then(({ data }) => data);
},
placeholderData: () => {
const records = !ids || ids.length === 0
? []
: ids.map(id => queryClient.getQueryData([
resource,
'getOne',
{ id: String(id), meta },
]));
if (records.some(record => record === undefined)) {
return undefined;
}
else {
return records;
}
},
retry: false,
enabled: enabled ?? ids != null,
...queryOptions,
});
const metaValue = useRef(meta);
const resourceValue = useRef(resource);
useEffect(() => {
metaValue.current = meta;
}, [meta]);
useEffect(() => {
resourceValue.current = resource;
}, [resource]);
useEffect(() => {
if (result.data === undefined ||
result.error != null ||
result.isFetching)
return;
// optimistically populate the getOne cache
result.data.forEach(record => {
queryClient.setQueryData([
resourceValue.current,
'getOne',
{ id: String(record.id), meta: metaValue.current },
], oldRecord => oldRecord ?? record);
});
onSuccessEvent(result.data);
}, [
queryClient,
onSuccessEvent,
result.data,
result.error,
result.isFetching,
]);
useEffect(() => {
if (result.error == null || result.isFetching)
return;
onErrorEvent(result.error);
}, [onErrorEvent, result.error, result.isFetching]);
useEffect(() => {
if (result.status === 'pending' || result.isFetching)
return;
onSettledEvent(result.data, result.error);
}, [
onSettledEvent,
result.data,
result.error,
result.status,
result.isFetching,
]);
return result;
};
const noop = () => undefined;
//# sourceMappingURL=useGetMany.js.map