@entur-partner/common 5.3.1 → 6.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,9 @@
1
1
  import React, { Children, useState, useRef, useEffect, Component } from 'react';
2
2
  import cx from 'classnames';
3
3
  import { Link as Link$1, Heading6, Paragraph } from '@entur/typography';
4
- import { NorwayIcon, UKIcon, ChannelsIcon, UserIcon, LogOutIcon, DownArrowIcon } from '@entur/icons';
4
+ import { NorwayIcon, UKIcon, UserIcon, LogOutIcon, DownArrowIcon, ChannelsIcon } from '@entur/icons';
5
5
  import { SecondaryButton, Button, PrimaryButton, ButtonGroup } from '@entur/button';
6
- import { useHistory, Link as Link$2, Prompt, Redirect, Route } from 'react-router-dom';
6
+ import { Link as Link$2, useNavigate, useInRouterContext, useBlocker, redirect, Route } from 'react-router-dom';
7
7
  import { SearchableDropdown } from '@entur/dropdown';
8
8
  import { SegmentedControl, SegmentedChoice } from '@entur/form';
9
9
  import { assertIsDefined, localeDate, localeDateTime, isDate, isDateString, isString, hasAllPermissions, hasOneOfPermissions, splitUrlPath } from '@entur-partner/util';
@@ -338,7 +338,6 @@ var UserMenu = function UserMenu(_ref) {
338
338
  showVersionItem = _ref.showVersionItem,
339
339
  showMyProfileItem = _ref.showMyProfileItem,
340
340
  onNavigateToMyProfile = _ref.onNavigateToMyProfile;
341
- var history = useHistory();
342
341
  var triggerClassList = cx(['eps-user-menu__trigger-button', className]);
343
342
 
344
343
  var _useState = useState(false),
@@ -379,19 +378,10 @@ var UserMenu = function UserMenu(_ref) {
379
378
  }, React.createElement(UKIcon, {
380
379
  size: "1rem",
381
380
  inline: true
382
- })), messages.switchLanguage), showVersionItem && messages.appVersion && React.createElement(CustomOverflowMenuItem, {
383
- onClick: function onClick() {
384
- history.push('/app-version');
385
- setOpen(false);
386
- },
387
- className: "eps-overflow-menu__item"
388
- }, React.createElement("span", {
389
- "aria-hidden": true
390
- }, React.createElement(ChannelsIcon, {
391
- className: "eps-version-icon",
392
- size: "1rem",
393
- inline: true
394
- })), messages.appVersion), showMyProfileItem && messages.myProfile && React.createElement(Link$2, {
381
+ })), messages.switchLanguage), showVersionItem && messages.appVersion && React.createElement(VersionCustomOverflowMenuItems, {
382
+ title: messages.appVersion,
383
+ setOpen: setOpen
384
+ }), showMyProfileItem && messages.myProfile && React.createElement(Link$2, {
395
385
  to: "/permission-admin/my-profile",
396
386
  style: {
397
387
  textDecoration: 'none'
@@ -479,6 +469,26 @@ var CustomOverflowMenuItem = function CustomOverflowMenuItem(props) {
479
469
  }, props));
480
470
  };
481
471
 
472
+ var VersionCustomOverflowMenuItems = function VersionCustomOverflowMenuItems(_ref3) {
473
+ var title = _ref3.title,
474
+ setOpen = _ref3.setOpen;
475
+ var navigate = useNavigate(); // Hide useNavigate from standalone micro-frontends because it is not under RouterProvider
476
+
477
+ return React.createElement(CustomOverflowMenuItem, {
478
+ onClick: function onClick() {
479
+ navigate('/app-version');
480
+ setOpen(false);
481
+ },
482
+ className: "eps-overflow-menu__item"
483
+ }, React.createElement("span", {
484
+ "aria-hidden": true
485
+ }, React.createElement(ChannelsIcon, {
486
+ className: "eps-version-icon",
487
+ size: "1rem",
488
+ inline: true
489
+ })), title);
490
+ };
491
+
482
492
  function elementContainsEventTarget(element, event) {
483
493
  if (!element) {
484
494
  return false;
@@ -931,41 +941,25 @@ var RouteLeavingGuard = function RouteLeavingGuard(_ref) {
931
941
  shouldBlockNavigation = _ref.shouldBlockNavigation,
932
942
  rest = _objectWithoutPropertiesLoose(_ref, _excluded$2);
933
943
 
934
- var history = useHistory();
935
-
936
- var _useState = useState('ALLOW'),
937
- status = _useState[0],
938
- setStatus = _useState[1];
939
-
940
- var _useState2 = useState(history.location),
941
- lastLocation = _useState2[0],
942
- setLastLocation = _useState2[1];
943
-
944
- var closeModal = function closeModal() {
945
- setStatus('ALLOW');
946
- };
947
-
948
- var allowNavigation = function allowNavigation(nextLocation) {
949
- if (shouldBlockNavigation(nextLocation) && status === 'ALLOW') {
950
- setStatus('BLOCKED');
951
- setLastLocation(nextLocation);
952
- return false;
944
+ assertIsDefined(useInRouterContext() ? true : undefined, "RouteLeavingGuard must be used within a data router.");
945
+ var blocker = useBlocker(function (_ref2) {
946
+ var currentLocation = _ref2.currentLocation,
947
+ nextLocation = _ref2.nextLocation;
948
+ return currentLocation.pathname !== nextLocation.pathname && shouldBlockNavigation(nextLocation);
949
+ });
950
+ return React.createElement(ConfirmModal, _extends({
951
+ onDismiss: function onDismiss() {
952
+ if (blocker.state === 'blocked') {
953
+ blocker.reset();
954
+ }
955
+ },
956
+ open: blocker.state === 'blocked',
957
+ onConfirm: function onConfirm() {
958
+ if (blocker.state === 'blocked') {
959
+ blocker.proceed();
960
+ }
953
961
  }
954
-
955
- return true;
956
- };
957
-
958
- var handleConfirm = function handleConfirm() {
959
- history.push(lastLocation.pathname);
960
- };
961
-
962
- return React.createElement(React.Fragment, null, React.createElement(Prompt, {
963
- message: allowNavigation
964
- }), React.createElement(ConfirmModal, _extends({
965
- onDismiss: closeModal,
966
- open: status === 'BLOCKED',
967
- onConfirm: handleConfirm
968
- }, rest), children));
962
+ }, rest), children);
969
963
  };
970
964
 
971
965
  var Pager = function Pager(_ref) {
@@ -1035,19 +1029,11 @@ var PermissionCheckedRoute = function PermissionCheckedRoute(_ref) {
1035
1029
  onReject = _ref.onReject,
1036
1030
  props = _objectWithoutPropertiesLoose(_ref, _excluded$1);
1037
1031
 
1038
- var redirect = React.createElement(Redirect, {
1039
- to: {
1040
- pathname: '/unauthorized',
1041
- state: {
1042
- from: props.location
1043
- }
1044
- }
1045
- });
1046
1032
  return React.createElement(PermissionCheck, {
1047
1033
  permissions: actualPermissions,
1048
1034
  all: all,
1049
1035
  oneOf: oneOf,
1050
- fallback: onReject ? onReject : redirect
1036
+ fallback: onReject != null ? onReject : redirect('/unauthorized')
1051
1037
  }, React.createElement(Route, _extends({}, props), children));
1052
1038
  };
1053
1039