UNPKG

ra-core

Version:

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

154 lines 6.86 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 () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Notification = void 0; const React = __importStar(require("react")); const react_1 = require("react"); const useTranslate_1 = require("../i18n/useTranslate.cjs"); const useNotificationContext_1 = require("../notification/useNotificationContext.cjs"); const CloseNotificationContext_1 = require("../notification/CloseNotificationContext.cjs"); const undoableEventEmitter_1 = __importDefault(require("../dataProvider/undoableEventEmitter.cjs")); const useTakeUndoableMutation_1 = require("../dataProvider/undo/useTakeUndoableMutation.cjs"); /** * Provides a way to show a notification. * @see useNotify * * @example <caption>Basic usage</caption> * <Notification /> * * @param props The component props * @param {string} props.type The notification type. Defaults to 'info'. * @param {number} props.autoHideDuration Duration in milliseconds to wait until hiding a given notification. Defaults to 4000. * @param {boolean} props.multiLine Set it to `true` if the notification message should be shown in more than one line. */ const Notification = () => { const { notifications, takeNotification } = (0, useNotificationContext_1.useNotificationContext)(); const takeMutation = (0, useTakeUndoableMutation_1.useTakeUndoableMutation)(); const [open, setOpen] = (0, react_1.useState)(false); const [currentNotification, setCurrentNotification] = React.useState(undefined); const translate = (0, useTranslate_1.useTranslate)(); (0, react_1.useEffect)(() => { if (notifications.length && !currentNotification) { // Set a new snack when we don't have an active one const notification = takeNotification(); if (notification) { setCurrentNotification(notification); setOpen(true); } } if (currentNotification) { const beforeunload = (e) => { e.preventDefault(); const confirmationMessage = ''; e.returnValue = confirmationMessage; return confirmationMessage; }; if (currentNotification?.notificationOptions?.undoable) { window.addEventListener('beforeunload', beforeunload); return () => { window.removeEventListener('beforeunload', beforeunload); }; } } }, [notifications, currentNotification, open, takeNotification]); const handleRequestClose = (0, react_1.useCallback)(() => { setOpen(false); setCurrentNotification(undefined); }, [setOpen]); const handleExited = (0, react_1.useCallback)(() => { if (currentNotification && currentNotification.notificationOptions?.undoable) { const mutation = takeMutation(); if (mutation) { mutation({ isUndo: false }); } else { // FIXME kept for BC: remove in v6 undoableEventEmitter_1.default.emit('end', { isUndo: false }); } } setCurrentNotification(undefined); }, [currentNotification, takeMutation]); const handleUndo = (0, react_1.useCallback)(() => { const mutation = takeMutation(); if (mutation) { mutation({ isUndo: true }); } else { // FIXME kept for BC: remove in v6 undoableEventEmitter_1.default.emit('end', { isUndo: true }); } setOpen(false); }, [takeMutation]); const { message, notificationOptions } = currentNotification || {}; const { messageArgs, undoable } = notificationOptions || {}; (0, react_1.useEffect)(() => { if (!undoable) return; const timer = setTimeout(() => { handleExited(); }, notificationOptions?.autoHideDuration || 4000); return () => clearTimeout(timer); }, [undoable, handleExited, notificationOptions]); if (!currentNotification) return null; return (React.createElement(CloseNotificationContext_1.CloseNotificationContext.Provider, { value: handleRequestClose }, React.createElement("div", { className: "ra-notification", style: { color: 'white', position: 'fixed', bottom: 16, right: 16, zIndex: 1400, display: open ? 'flex' : 'none', paddingLeft: '16px', paddingRight: '16px', borderRadius: '4px', alignItems: 'center', justifyContent: 'center', gap: '16px', backgroundColor: 'rgba(0, 0, 0, 0.8)', } }, React.createElement("p", null, message && typeof message === 'string' ? translate(message, messageArgs) : message), React.createElement("div", { style: { display: 'flex', gap: '8px' } }, undoable ? (React.createElement("button", { onClick: handleUndo, type: "button" }, translate('ra.action.undo'))) : null, React.createElement("button", { onClick: handleExited, type: "button" }, translate('ra.action.close')))))); }; exports.Notification = Notification; //# sourceMappingURL=Notification.js.map