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.04 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 { useCallback, useMemo } from 'react'; import { useUpdate } from "../../dataProvider/useUpdate.js"; import { useRedirect } from "../../routing/useRedirect.js"; import { useNotify } from "../../notification/useNotify.js"; import { useRecordContext } from "../record/useRecordContext.js"; import { useResourceContext } from "../../core/useResourceContext.js"; import { useTranslate } from "../../i18n/useTranslate.js"; /** * Prepare a set of callbacks for an update button * * @example * const UpdateButton = ({ * resource, * record, * redirect, * ...rest * }) => { * const { * isPending, * handleUpdate, * } = useUpdateController({ * mutationMode: 'pessimistic', * resource, * record, * redirect, * }); * * const [open, setOpen] = useState(false); * * return ( * <Fragment> * <Button * onClick={() => setOpen(true)} * label="ra.action.update" * {...rest} * > * {icon} * </Button> * <Confirm * isOpen={open} * loading={isPending} * title="ra.message.update_title" * content="ra.message.update_content" * titleTranslateOptions={{ * name: resource, * id: record.id, * }} * contentTranslateOptions={{ * name: resource, * id: record.id, * }} * onConfirm={() => handleUpdate()} * onClose={() => setOpen(false)} * /> * </Fragment> * ); * }; */ export var useUpdateController = function (props) { var _a = props.redirect, redirectTo = _a === void 0 ? false : _a, _b = props.mutationMode, mutationMode = _b === void 0 ? 'undoable' : _b, _c = props.mutationOptions, mutationOptions = _c === void 0 ? {} : _c, successMessage = props.successMessage; var mutationMeta = mutationOptions.meta, otherMutationOptions = __rest(mutationOptions, ["meta"]); var record = useRecordContext(props); var resource = useResourceContext(props); var notify = useNotify(); var redirect = useRedirect(); var translate = useTranslate(); var _d = useUpdate(resource, undefined, __assign({ onSuccess: function () { notify(successMessage !== null && successMessage !== void 0 ? successMessage : "resources.".concat(resource, ".notifications.updated"), { type: 'info', messageArgs: { smart_count: 1, _: translate('ra.notification.updated', { smart_count: 1, }), }, undoable: mutationMode === 'undoable', }); redirect(redirectTo, resource); }, onError: function (error) { notify(typeof error === 'string' ? error : (error === null || error === void 0 ? void 0 : error.message) || 'ra.notification.http_error', { type: 'error', messageArgs: { _: typeof error === 'string' ? error : error === null || error === void 0 ? void 0 : error.message, }, }); }, mutationMode: mutationMode }, otherMutationOptions)), updateOne = _d[0], isPending = _d[1].isPending; var handleUpdate = useCallback(function (data) { if (!record) { throw new Error('The record cannot be updated because no record has been passed'); } updateOne(resource, { id: record.id, data: data, previousData: record, meta: mutationMeta, }); }, [updateOne, mutationMeta, record, resource]); return useMemo(function () { return ({ isPending: isPending, isLoading: isPending, handleUpdate: handleUpdate, }); }, [isPending, handleUpdate]); }; //# sourceMappingURL=useUpdateController.js.map