UNPKG

ra-core

Version:

Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React

74 lines 2.49 kB
import get from 'lodash/get.js'; import { useGetManyAggregate } from "../../dataProvider/index.js"; import { useList } from "../list/index.js"; import { useNotify } from "../../notification/index.js"; import { defaultExporter } from "../../export/index.js"; const emptyArray = []; const defaultFilter = {}; /** * Hook that fetches records from another resource specified * by an array of *ids* in current record. * * @example * * const { data, error, isFetching, isPending } = useReferenceArrayFieldController({ * record: { referenceIds: ['id1', 'id2']}; * reference: 'reference'; * resource: 'resource'; * source: 'referenceIds'; * }); * * @param {Object} props * @param {Object} props.record The current resource record * @param {string} props.reference The linked resource name * @param {string} props.resource The current resource name * @param {string} props.source The key of the linked resource identifier * * @param {Props} props * * @returns {ListControllerResult} The reference props */ export const useReferenceArrayFieldController = (props) => { const { filter = defaultFilter, exporter = defaultExporter, page = 1, perPage = 1000, record, reference, sort, source, queryOptions = {}, } = props; const notify = useNotify(); const value = get(record, source); const { meta, ...otherQueryOptions } = queryOptions; const ids = Array.isArray(value) ? value : emptyArray; const { data, error, isLoading, isFetching, isPaused, isPending, isPlaceholderData, refetch, } = useGetManyAggregate(reference, { ids, meta }, { onError: error => notify(typeof error === 'string' ? error : error?.message || 'ra.notification.http_error', { type: 'error', messageArgs: { _: typeof error === 'string' ? error : error?.message ? error.message : undefined, }, }), ...otherQueryOptions, }); const listProps = useList({ data, error, exporter, filter, isFetching, isLoading, isPaused, isPending, isPlaceholderData, page, perPage, sort, }); return { ...listProps, defaultTitle: undefined, refetch, resource: reference, }; }; //# sourceMappingURL=useReferenceArrayFieldController.js.map