UNPKG

ra-core

Version:

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

50 lines 2.23 kB
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; import * as React from 'react'; import { useState, useCallback } from 'react'; import { AddUndoableMutationContext } from './AddUndoableMutationContext'; import { TakeUndoableMutationContext } from './TakeUndoableMutationContext'; /** * Exposes and manages a queue of undoable mutations * * This context is used in CoreAdminContext so that every react-admin app * can use the useAddUndoableMutation and useTakeUndoableMutation hooks. * * Note: We need a separate queue for mutations (instead of using the * notifications queue) because the mutations are not dequeued when the * notification is displayed, but when it is dismissed. */ export var UndoableMutationsContextProvider = function (_a) { var children = _a.children; var _b = useState([]), mutations = _b[0], setMutations = _b[1]; /** * Add a new mutation (pushes a new mutation to the queue). * * Used by optimistic data provider hooks, e.g., useDelete */ var addMutation = useCallback(function (mutation) { setMutations(function (mutations) { return __spreadArray(__spreadArray([], mutations, true), [mutation], false); }); }, []); /** * Get the next mutation to execute (shifts the first mutation from the queue) and returns it. * * Used by the Notification component to process or undo the mutation */ var takeMutation = useCallback(function () { if (mutations.length === 0) return; var mutation = mutations[0], rest = mutations.slice(1); setMutations(rest); return mutation; }, [mutations]); return (React.createElement(TakeUndoableMutationContext.Provider, { value: takeMutation }, React.createElement(AddUndoableMutationContext.Provider, { value: addMutation }, children))); }; //# sourceMappingURL=UndoableMutationsContextProvider.js.map