UNPKG

ra-core

Version:

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

77 lines 3.55 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; 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)); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.UndoableMutationsContextProvider = void 0; var React = __importStar(require("react")); var react_1 = require("react"); var AddUndoableMutationContext_1 = require("./AddUndoableMutationContext"); var TakeUndoableMutationContext_1 = require("./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. */ var UndoableMutationsContextProvider = function (_a) { var children = _a.children; var _b = (0, react_1.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 = (0, react_1.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 = (0, react_1.useCallback)(function () { if (mutations.length === 0) return; var mutation = mutations[0], rest = mutations.slice(1); setMutations(rest); return mutation; }, [mutations]); return (React.createElement(TakeUndoableMutationContext_1.TakeUndoableMutationContext.Provider, { value: takeMutation }, React.createElement(AddUndoableMutationContext_1.AddUndoableMutationContext.Provider, { value: addMutation }, children))); }; exports.UndoableMutationsContextProvider = UndoableMutationsContextProvider; //# sourceMappingURL=UndoableMutationsContextProvider.js.map