@zextras/carbonio-shell-ui
Version:
The Zextras Carbonio web client
88 lines • 4.28 kB
JavaScript
;
/*
* SPDX-FileCopyrightText: 2021 Zextras <https://www.zextras.com>
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
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;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.RouteLeavingGuard = void 0;
const react_1 = __importStar(require("react"));
const carbonio_design_system_1 = require("@zextras/carbonio-design-system");
const lodash_1 = require("lodash");
const react_i18next_1 = require("react-i18next");
const react_router_dom_1 = require("react-router-dom");
function areLocationsDifferent(loc1, loc2) {
return loc1.pathname !== loc2.pathname || loc1.search !== loc2.search || loc1.hash !== loc2.hash;
}
const RouteLeavingGuard = ({ children, when, onSave, dataHasError = false }) => {
const [t] = (0, react_i18next_1.useTranslation)();
const blockerFunction = ({ currentLocation, nextLocation }) => {
const areDifferent = areLocationsDifferent(currentLocation, nextLocation);
return when && areDifferent;
};
// Block navigating elsewhere when data has been entered into the input
const blocker = (0, react_router_dom_1.useBlocker)(blockerFunction);
const cancel = (0, react_1.useCallback)(() => {
blocker.reset?.();
}, [blocker]);
const onConfirm = (0, react_1.useCallback)(() => {
onSave()
.then((results) => {
const rejected = (0, lodash_1.filter)(results, (result) => result.status === 'rejected');
if (rejected.length > 0) {
console.error(rejected);
cancel();
}
else {
blocker.proceed?.();
}
})
.catch((reason) => {
console.error(reason);
cancel();
});
}, [blocker, cancel, onSave]);
const onSecondaryAction = (0, react_1.useCallback)(() => {
blocker.proceed?.();
}, [blocker]);
return (react_1.default.createElement(carbonio_design_system_1.Modal, { showCloseIcon: true, closeIconTooltip: t('label.close', 'Close'), open: blocker.state === 'blocked', title: dataHasError
? t('label.cannot_save_changes', 'Some changes cannot be saved')
: t('label.unsaved_changes', 'You have unsaved changes'), onClose: cancel, onConfirm: dataHasError ? onSecondaryAction : onConfirm, confirmLabel: dataHasError
? t('label.leave_anyway', 'Leave anyway')
: t('label.save_and_leave', 'Save and leave'), onSecondaryAction: dataHasError ? cancel : onSecondaryAction, secondaryActionLabel: dataHasError ? t('label.cancel', 'Cancel') : t('label.leave_anyway', 'Leave anyway'), optionalFooter: !dataHasError ? (react_1.default.createElement(carbonio_design_system_1.Button, { color: "secondary", type: "outlined", label: t('label.cancel', 'Cancel'), onClick: cancel })) : undefined }, children));
};
exports.RouteLeavingGuard = RouteLeavingGuard;
//# sourceMappingURL=nav-guard.js.map