UNPKG

ra-core

Version:

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

150 lines 5.99 kB
import { useCallback } from 'react'; import { useAuthenticated, useRequireAccess } from "../../auth/index.js"; import { HttpError, useCreate, } from "../../dataProvider/index.js"; import { useRedirect } from "../../routing/index.js"; import { useNotify } from "../../notification/index.js"; import { useMutationMiddlewares, } from "../saveContext/index.js"; import { useTranslate } from "../../i18n/index.js"; import { useResourceContext, useResourceDefinition, useGetResourceLabel, } from "../../core/index.js"; /** * Prepare data for the Create view * * @param {Object} props The props passed to the Create component. * * @return {Object} controllerProps Fetched data and callbacks for the Create view * * @example * * import { useCreateController } from 'react-admin'; * import CreateView from './CreateView'; * * const MyCreate = props => { * const controllerProps = useCreateController(props); * return <CreateView {...controllerProps} {...props} />; * } */ export const useCreateController = (props = {}) => { const { disableAuthentication, record, redirect: redirectTo, transform, mutationMode = 'pessimistic', mutationOptions = {}, } = props; const resource = useResourceContext(props); if (!resource) { throw new Error('useCreateController requires a non-empty resource prop or context'); } const { isPending: isPendingAuthenticated } = useAuthenticated({ enabled: !disableAuthentication, }); const { isPending: isPendingCanAccess } = useRequireAccess({ action: 'create', resource, enabled: !disableAuthentication && !isPendingAuthenticated, }); const { hasEdit, hasShow } = useResourceDefinition(props); const finalRedirectTo = redirectTo ?? getDefaultRedirectRoute(hasShow, hasEdit); const translate = useTranslate(); const notify = useNotify(); const redirect = useRedirect(); const { onSuccess, onError, meta, ...otherMutationOptions } = mutationOptions; const { registerMutationMiddleware, getMutateWithMiddlewares, unregisterMutationMiddleware, } = useMutationMiddlewares(); const [create, { isPending: saving }] = useCreate(resource, undefined, { onSuccess: async (...args) => { if (onSuccess) { return onSuccess(...args); } const [data] = args; notify(`resources.${resource}.notifications.created`, { type: 'info', messageArgs: { smart_count: 1, _: translate(`ra.notification.created`, { smart_count: 1, }), }, undoable: mutationMode === 'undoable', }); redirect(finalRedirectTo, resource, data.id, data); }, onError: (...args) => { if (onError) { return onError(...args); } const [error] = args; // Don't trigger a notification if this is a validation error // (notification will be handled by the useNotifyIsFormInvalid hook) const validationErrors = error?.body?.errors; const hasValidationErrors = !!validationErrors && Object.keys(validationErrors).length > 0; if (!hasValidationErrors || mutationMode !== 'pessimistic') { notify(typeof error === 'string' ? error : error.message || 'ra.notification.http_error', { type: 'error', messageArgs: { _: typeof error === 'string' ? error : error instanceof Error || (typeof error === 'object' && error !== null && error.hasOwnProperty('message')) ? // @ts-ignore error.message : undefined, }, }); } }, ...otherMutationOptions, mutationMode, returnPromise: mutationMode === 'pessimistic', getMutateWithMiddlewares, }); const save = useCallback((data, { onSuccess: onSuccessFromSave, onError: onErrorFromSave, transform: transformFromSave, meta: metaFromSave, } = {}) => Promise.resolve(transformFromSave ? transformFromSave(data) : transform ? transform(data) : data).then(async (data) => { try { await create(resource, { data, meta: metaFromSave ?? meta }, { onError: onErrorFromSave, onSuccess: onSuccessFromSave, }); } catch (error) { if ((error instanceof HttpError || (typeof error === 'object' && error !== null && error.hasOwnProperty('body'))) && error.body?.errors != null) { return error.body.errors; } } }), [create, meta, resource, transform]); const getResourceLabel = useGetResourceLabel(); const defaultTitle = translate(`resources.${resource}.page.create`, { _: translate('ra.page.create', { name: getResourceLabel(resource, 1), }), }); return { isFetching: false, isLoading: false, isPending: disableAuthentication ? false : isPendingCanAccess, mutationMode, saving, defaultTitle, save, record, resource, redirect: finalRedirectTo, registerMutationMiddleware, unregisterMutationMiddleware, }; }; const getDefaultRedirectRoute = (hasShow, hasEdit) => { if (hasEdit) { return 'edit'; } if (hasShow) { return 'show'; } return 'list'; }; //# sourceMappingURL=useCreateController.js.map