UNPKG

ra-core

Version:

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

130 lines 5.54 kB
var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; import { useParams } from 'react-router-dom'; import { useAuthenticated, useRequireAccess } from "../../auth/index.js"; import { useGetOne, } from "../../dataProvider/index.js"; import { useTranslate } from "../../i18n/index.js"; import { useRedirect } from "../../routing/index.js"; import { useNotify } from "../../notification/index.js"; import { useResourceContext, useGetResourceLabel, useGetRecordRepresentation, } from "../../core/index.js"; /** * 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} />; * }; */ export var useShowController = function (props) { if (props === void 0) { props = {}; } var _a = props.disableAuthentication, disableAuthentication = _a === void 0 ? false : _a, propsId = props.id, _b = props.queryOptions, queryOptions = _b === void 0 ? {} : _b, _c = props.redirectOnError, redirectOnError = _c === void 0 ? DefaultRedirectOnError : _c; var resource = useResourceContext(props); if (!resource) { throw new Error("useShowController requires a non-empty resource prop or context"); } var isPendingAuthenticated = useAuthenticated({ enabled: !disableAuthentication, }).isPending; var isPendingCanAccess = useRequireAccess({ action: 'show', resource: resource, enabled: !disableAuthentication && !isPendingAuthenticated, }).isPending; var getRecordRepresentation = useGetRecordRepresentation(resource); var translate = useTranslate(); var notify = useNotify(); var redirect = useRedirect(); var routeId = useParams().id; if (!routeId && !propsId) { throw new Error('useShowController requires an id prop or a route with an /:id? parameter.'); } var id = propsId != null ? propsId : routeId; var meta = queryOptions.meta, otherQueryOptions = __rest(queryOptions, ["meta"]); var _d = useGetOne(resource, { id: id, meta: meta }, __assign({ enabled: (!isPendingAuthenticated && !isPendingCanAccess) || disableAuthentication, onError: function () { notify('ra.notification.item_doesnt_exist', { type: 'error', }); redirect(redirectOnError, resource, id); }, retry: false }, otherQueryOptions)), record = _d.data, error = _d.error, isLoading = _d.isLoading, isFetching = _d.isFetching, isPaused = _d.isPaused, isPending = _d.isPending, isPlaceholderData = _d.isPlaceholderData, refetch = _d.refetch; // eslint-disable-next-line eqeqeq if (record && record.id && record.id != id) { throw new Error("useShowController: Fetched record's id attribute (".concat(record.id, ") must match the requested 'id' (").concat(id, ")")); } var getResourceLabel = useGetResourceLabel(); var recordRepresentation = getRecordRepresentation(record); var defaultTitle = translate("resources.".concat(resource, ".page.show"), { id: id, record: record, recordRepresentation: typeof recordRepresentation === 'string' ? recordRepresentation : '', _: translate('ra.page.show', { name: getResourceLabel(resource, 1), id: id, record: record, recordRepresentation: typeof recordRepresentation === 'string' ? recordRepresentation : '', }), }); return { defaultTitle: defaultTitle, error: error, isLoading: isLoading, isFetching: isFetching, isPaused: isPaused, isPending: isPending, isPlaceholderData: isPlaceholderData, record: record, redirectOnError: redirectOnError, refetch: refetch, resource: resource, }; }; var DefaultRedirectOnError = 'list'; //# sourceMappingURL=useShowController.js.map