UNPKG

ra-core

Version:

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

81 lines 2.93 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.useReferenceArrayFieldController = void 0; const get_js_1 = __importDefault(require("lodash/get.js")); const dataProvider_1 = require("../../dataProvider/index.cjs"); const list_1 = require("../list/index.cjs"); const notification_1 = require("../../notification/index.cjs"); const export_1 = require("../../export/index.cjs"); 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 */ const useReferenceArrayFieldController = (props) => { const { filter = defaultFilter, exporter = export_1.defaultExporter, page = 1, perPage = 1000, record, reference, sort, source, queryOptions = {}, } = props; const notify = (0, notification_1.useNotify)(); const value = (0, get_js_1.default)(record, source); const { meta, ...otherQueryOptions } = queryOptions; const ids = Array.isArray(value) ? value : emptyArray; const { data, error, isLoading, isFetching, isPaused, isPending, isPlaceholderData, refetch, } = (0, dataProvider_1.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 = (0, list_1.useList)({ data, error, exporter, filter, isFetching, isLoading, isPaused, isPending, isPlaceholderData, page, perPage, sort, }); return { ...listProps, defaultTitle: undefined, refetch, resource: reference, }; }; exports.useReferenceArrayFieldController = useReferenceArrayFieldController; //# sourceMappingURL=useReferenceArrayFieldController.js.map