UNPKG

ra-core

Version:

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

43 lines 2.01 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ReferenceManyCountBase = void 0; const react_1 = __importDefault(require("react")); const useReferenceManyFieldController_1 = require("./useReferenceManyFieldController.cjs"); const hooks_1 = require("../../util/hooks.cjs"); /** * Fetch and render the number of records related to the current one * * Relies on dataProvider.getManyReference() returning a total property * * @example // Display the number of comments for the current post * <ReferenceManyCountBase reference="comments" target="post_id" /> * * @example // Display the number of published comments for the current post * <ReferenceManyCountBase reference="comments" target="post_id" filter={{ is_published: true }} /> */ const ReferenceManyCountBase = (props) => { const { loading, error, offline, timeout = 1000, ...rest } = props; const oneSecondHasPassed = (0, hooks_1.useTimeout)(timeout); const { isPaused, isPending, error: fetchError, total, } = (0, useReferenceManyFieldController_1.useReferenceManyFieldController)({ ...rest, page: 1, perPage: 1, }); const shouldRenderLoading = isPending && !isPaused && loading !== undefined && loading !== false; const shouldRenderOffline = isPending && isPaused && offline !== undefined && offline !== false; const shouldRenderError = !isPending && fetchError && error !== undefined && error !== false; return (react_1.default.createElement(react_1.default.Fragment, null, shouldRenderLoading ? oneSecondHasPassed ? loading : null : shouldRenderOffline ? offline : shouldRenderError ? error : total)); }; exports.ReferenceManyCountBase = ReferenceManyCountBase; //# sourceMappingURL=ReferenceManyCountBase.js.map