UNPKG

ra-core

Version:

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

115 lines 4.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useShowController = void 0; const auth_1 = require("../../auth/index.cjs"); const dataProvider_1 = require("../../dataProvider/index.cjs"); const i18n_1 = require("../../i18n/index.cjs"); const routing_1 = require("../../routing/index.cjs"); const notification_1 = require("../../notification/index.cjs"); const core_1 = require("../../core/index.cjs"); /** * Prepare data for the Show view. * * useShowController does a few things: * - it grabs the id from the URL and the resource name from the ResourceContext, * - it fetches the record via useGetOne, * - it prepares the page title. * * @param {Object} props The props passed to the Show component. * * @return {Object} controllerProps Fetched data and callbacks for the Show view * * @example * * import { useShowController } from 'react-admin'; * import ShowView from './ShowView'; * * const MyShow = () => { * const controllerProps = useShowController(); * return <ShowView {...controllerProps} />; * }; * * @example // useShowController can also take its parameters from props * * import { useShowController } from 'react-admin'; * import ShowView from './ShowView'; * * const MyShow = () => { * const controllerProps = useShowController({ resource: 'posts', id: 1234 }); * return <ShowView {...controllerProps} />; * }; */ const useShowController = (props = {}) => { const { disableAuthentication = false, id: propsId, queryOptions = {}, redirectOnError = DefaultRedirectOnError, } = props; const resource = (0, core_1.useResourceContext)(props); if (!resource) { throw new Error(`useShowController requires a non-empty resource prop or context`); } const { isPending: isPendingAuthenticated } = (0, auth_1.useAuthenticated)({ enabled: !disableAuthentication, }); const { isPending: isPendingCanAccess } = (0, auth_1.useRequireAccess)({ action: 'show', resource, enabled: !disableAuthentication && !isPendingAuthenticated, }); const getRecordRepresentation = (0, core_1.useGetRecordRepresentation)(resource); const translate = (0, i18n_1.useTranslate)(); const notify = (0, notification_1.useNotify)(); const redirect = (0, routing_1.useRedirect)(); const { id: routeId } = (0, routing_1.useParams)(); if (!routeId && !propsId) { throw new Error('useShowController requires an id prop or a route with an /:id? parameter.'); } const id = propsId != null ? propsId : routeId; const { meta, ...otherQueryOptions } = queryOptions; const { data: record, error, isLoading, isFetching, isPaused, isPending, isPlaceholderData, refetch, } = (0, dataProvider_1.useGetOne)(resource, { id, meta }, { enabled: (!isPendingAuthenticated && !isPendingCanAccess) || disableAuthentication, onError: () => { notify('ra.notification.item_doesnt_exist', { type: 'error', }); redirect(redirectOnError, resource, id); }, retry: false, ...otherQueryOptions, }); // eslint-disable-next-line eqeqeq if (record && record.id && record.id != id) { throw new Error(`useShowController: Fetched record's id attribute (${record.id}) must match the requested 'id' (${id})`); } const getResourceLabel = (0, core_1.useGetResourceLabel)(); const recordRepresentation = getRecordRepresentation(record); const defaultTitle = translate(`resources.${resource}.page.show`, { id, record, recordRepresentation: typeof recordRepresentation === 'string' ? recordRepresentation : '', _: translate('ra.page.show', { name: getResourceLabel(resource, 1), id, record, recordRepresentation: typeof recordRepresentation === 'string' ? recordRepresentation : '', }), }); return { defaultTitle, error, isLoading, isFetching, isPaused, isPending, isPlaceholderData, record, redirectOnError, refetch, resource, }; }; exports.useShowController = useShowController; const DefaultRedirectOnError = 'list'; //# sourceMappingURL=useShowController.js.map