@bigbinary/neetoui
Version:
neetoUI drives the experience at all neeto products
178 lines (172 loc) • 6.29 kB
JavaScript
'use strict';
var _extends = require('@babel/runtime/helpers/extends');
var _objectWithoutProperties = require('@babel/runtime/helpers/objectWithoutProperties');
var React = require('react');
var formik = require('formik');
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
var reactRouterDom = require('react-router-dom');
require('@bigbinary/neeto-hotkeys');
require('../overlayManager.js');
require('@babel/runtime/helpers/defineProperty');
require('@babel/runtime/helpers/classCallCheck');
require('@babel/runtime/helpers/createClass');
require('@bigbinary/neeto-cist');
require('qs');
require('ramda');
var index = require('../index--BeRLo6L.js');
var reactI18next = require('react-i18next');
var Button = require('../Button.js');
var Modal = require('../Modal.js');
var Typography = require('../Typography.js');
require('../en-K2ZIAV2F.js');
require('@babel/runtime/helpers/toConsumableArray');
require('dayjs');
require('dayjs/plugin/localeData');
require('dayjs/plugin/utc');
require('dayjs/plugin/weekday');
require('dayjs/plugin/weekOfYear');
require('classnames');
require('../Spinner.js');
require('../Tooltip.js');
require('@tippyjs/react');
require('tippy.js');
require('@bigbinary/neeto-icons');
require('../useOverlayManager-CxBh9gVG.js');
require('@babel/runtime/helpers/esm/extends');
require('@babel/runtime/helpers/esm/objectWithoutPropertiesLoose');
require('@babel/runtime/helpers/esm/inheritsLoose');
require('react-dom');
var useNavPrompt = function useNavPrompt(_ref) {
var _ref$shouldBlock = _ref.shouldBlock,
shouldBlock = _ref$shouldBlock === void 0 ? true : _ref$shouldBlock;
var _useState = React.useState(false),
_useState2 = _slicedToArray(_useState, 2),
isBlocked = _useState2[0],
setIsBlocked = _useState2[1];
var history = reactRouterDom.useHistory();
var unblockRef = React.useRef();
var navRef = React.useRef();
var continueNavigation = function continueNavigation() {
if (isBlocked && unblockRef.current) {
var _unblockRef$current;
(_unblockRef$current = unblockRef.current) === null || _unblockRef$current === void 0 || _unblockRef$current.call(unblockRef);
setIsBlocked(false);
if (navRef.current) {
var _navRef$current = navRef.current,
action = _navRef$current.action,
transition = _navRef$current.transition;
handleNavigation(action, transition);
}
}
};
var hidePrompt = function hidePrompt() {
return setIsBlocked(false);
};
var handleNavigation = function handleNavigation(action, transition) {
switch (action) {
case "PUSH":
history.push(transition);
break;
case "REPLACE":
history.replace(transition);
break;
case "POP":
history.goBack();
break;
}
};
React.useEffect(function () {
if (shouldBlock) {
unblockRef.current = history.block(function (transition, action) {
setIsBlocked(true);
navRef.current = {
transition: transition,
action: action
};
return false;
});
return function () {
if (unblockRef.current) {
unblockRef.current();
}
};
}
return undefined;
}, [shouldBlock]);
return {
isBlocked: isBlocked,
continueNavigation: continueNavigation,
hidePrompt: hidePrompt
};
};
/* eslint-disable @bigbinary/neeto/file-name-and-export-name-standards */
var Alert = function Alert(_ref) {
var _ref$isOpen = _ref.isOpen,
isOpen = _ref$isOpen === void 0 ? false : _ref$isOpen,
onClose = _ref.onClose,
onSubmit = _ref.onSubmit,
onDiscardChanges = _ref.onDiscardChanges;
var _useTranslation = reactI18next.useTranslation(),
t = _useTranslation.t,
i18n = _useTranslation.i18n;
var submitButtonRef = React.useRef(null);
var cancelButtonLabel = index.getLocale(i18n, t, "neetoui.blockNavigation.cancelButtonLabel");
var submitButtonLabel = index.getLocale(i18n, t, "neetoui.blockNavigation.submitButtonLabel");
return /*#__PURE__*/React.createElement(Modal, {
isOpen: isOpen,
onClose: onClose,
closeButton: true,
closeOnEsc: true,
closeOnOutsideClick: true,
"data-cy": "alert-box",
initialFocusRef: submitButtonRef,
size: "medium"
}, /*#__PURE__*/React.createElement(Modal.Header, null, /*#__PURE__*/React.createElement(Typography, {
"data-cy": "alert-title",
style: "h2"
}, index.getLocale(i18n, t, "neetoui.blockNavigation.alertTitle"))), /*#__PURE__*/React.createElement(Modal.Body, null, /*#__PURE__*/React.createElement(Typography, {
"data-cy": "alert-message",
lineHeight: "normal",
style: "body2"
}, index.getLocale(i18n, t, "neetoui.blockNavigation.alertMessage"))), /*#__PURE__*/React.createElement(Modal.Footer, {
className: "neeto-ui-gap-2 neeto-ui-flex neeto-ui-justify-end neeto-ui-items-center"
}, /*#__PURE__*/React.createElement(Button, {
"data-cy": "alert-cancel-button",
label: cancelButtonLabel,
style: "danger",
onClick: onDiscardChanges
}), /*#__PURE__*/React.createElement(Button, {
"data-cy": "alert-submit-button",
label: submitButtonLabel,
ref: submitButtonRef,
style: "primary",
onClick: onSubmit
})));
};
var _excluded = ["isDirty"];
var BlockNavigation = function BlockNavigation(_ref) {
var _ref$isDirty = _ref.isDirty,
isDirty = _ref$isDirty === void 0 ? false : _ref$isDirty,
otherProps = _objectWithoutProperties(_ref, _excluded);
var formikContext = formik.useFormikContext();
var shouldBlock = isDirty || Boolean(formikContext) && Boolean(formikContext.dirty);
var _useNavPrompt = useNavPrompt({
shouldBlock: shouldBlock
}),
isBlocked = _useNavPrompt.isBlocked,
continueNavigation = _useNavPrompt.continueNavigation,
hidePrompt = _useNavPrompt.hidePrompt;
var handleDiscardChanges = function handleDiscardChanges() {
if (formikContext) formikContext.resetForm();
hidePrompt();
continueNavigation();
};
return /*#__PURE__*/React.createElement(Alert, _extends({
isOpen: isBlocked,
onClose: hidePrompt,
onDiscardChanges: handleDiscardChanges,
onSubmit: hidePrompt
}, otherProps));
};
module.exports = BlockNavigation;
//# sourceMappingURL=BlockNavigation.js.map