@bigbinary/neeto-commons-frontend 2.0.69 → 2.0.71

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.
package/react-utils.d.ts CHANGED
@@ -51,13 +51,21 @@ export const HoneybadgerErrorBoundary: React.FC<{
51
51
  }>;
52
52
  /**
53
53
  *
54
- * A route that will restrict access to it based on a specified condition.
54
+ * A route that will restrict access to it based on a specified condition and
55
55
  *
56
- * If the given condition is true, it acts identical to a normal route. Otherwise
56
+ * permissions.
57
57
  *
58
- * we have two options: it can either redirect user to a different path or it can
58
+ * If the given condition is true and the user has the required permissions, it
59
59
  *
60
- * render a supplied error page instead of the actual component in that route.
60
+ * acts identical to a normal route. If the condition is true and the user does not
61
+ *
62
+ * have the required permissions, it will show 403 error page or it will render a
63
+ *
64
+ * supplied error page. If the condition is false, it will redirect user to a
65
+ *
66
+ * different path if redirectRoute is specified or it will render a supplied error
67
+ *
68
+ * page.
61
69
  *
62
70
  * If condition is not specified, it will assume the value of
63
71
  *
@@ -70,6 +78,7 @@ export function PrivateRoute(props: {
70
78
  condition?: boolean;
71
79
  redirectRoute?: string;
72
80
  errorPage?: React.ReactNode;
81
+ permissions?: string[];
73
82
  } & RouteProps): JSX.Element;
74
83
  type OptionsType = {
75
84
  root?: Element | null;
@@ -604,4 +613,20 @@ type ConfigType = {
604
613
  * );
605
614
  * @endexample
606
615
  */
607
- export function useHotKeys(hotkey: string | string[], handler: (event: React.KeyboardEvent) => void, config?: ConfigType): React.MutableRefObject | null;
616
+ export function useHotKeys(hotkey: string | string[], handler: (event: React.KeyboardEvent) => void, config?: ConfigType): React.MutableRefObject | null;
617
+ /**
618
+ *
619
+ * This hook returns a state that automatically updates its value to defaultValue whenever defaultValue changes. We can also specify a dependencies array to listen to other variable changes.
620
+ *
621
+ * This hook accepts a defaultValue and an optional array called dependencies. If dependencies is passed, the hook returns a state variable whose value will be updated to defaultValue in a useEffect that depends on the dependencies array. Otherwise, the useEffect will depend only on the defaultValue.
622
+ *
623
+ * @example
624
+ *
625
+ * const [value, setValue] = useStateWithDependency(defaultValue);// reset state to `defaultValue` whenever `defaultValue` changes.
626
+ * const [value, setValue] = useStateWithDependency(defaultValue, [value1, value2]); // reset state to `defaultValue` only when `value1` or `value2` changes.
627
+ *
628
+ * // In a case, where the value needs to be reset when defaultValue changes too, defaultValue can be passed with the dependencies, as shown below.
629
+ * const [value, setValue] = useStateWithDependency(defaultValue, [ value1, value2, defaultValue ]);
630
+ * @endexample
631
+ */
632
+ export function useStateWithDependency<T>(defaultValue: T, dependencies?: any[]): T;
package/react-utils.js CHANGED
@@ -2,7 +2,7 @@ import * as React from 'react';
2
2
  import React__default, { useEffect, useState, useDebugValue, useRef, useMemo } from 'react';
3
3
  import { Honeybadger, HoneybadgerErrorBoundary as HoneybadgerErrorBoundary$1 } from '@honeybadger-io/react';
4
4
  import { Typography, Button } from '@bigbinary/neetoui';
5
- import { isNil, prop, toPairs, mergeLeft, isEmpty, omit, curry } from 'ramda';
5
+ import { isNil, includes, __, prop, toPairs, mergeLeft, isEmpty, omit, curry } from 'ramda';
6
6
  import { useTranslation, Trans } from 'react-i18next';
7
7
  import ErrorPage from '@bigbinary/neeto-molecules/ErrorPage';
8
8
  import { Route, Redirect } from 'react-router-dom';
@@ -152,7 +152,7 @@ function _objectWithoutProperties(source, excluded) {
152
152
  return target;
153
153
  }
154
154
 
155
- var _excluded = ["condition", "redirectRoute", "errorPage"];
155
+ var _excluded = ["condition", "redirectRoute", "errorPage", "permissions"];
156
156
  var PrivateRoute = function PrivateRoute(_ref) {
157
157
  var _ref$condition = _ref.condition,
158
158
  condition = _ref$condition === void 0 ? globalProps.authenticated : _ref$condition,
@@ -160,17 +160,27 @@ var PrivateRoute = function PrivateRoute(_ref) {
160
160
  redirectRoute = _ref$redirectRoute === void 0 ? undefined : _ref$redirectRoute,
161
161
  _ref$errorPage = _ref.errorPage,
162
162
  errorPage = _ref$errorPage === void 0 ? undefined : _ref$errorPage,
163
+ _ref$permissions = _ref.permissions,
164
+ permissions = _ref$permissions === void 0 ? undefined : _ref$permissions,
163
165
  props = _objectWithoutProperties(_ref, _excluded);
164
- return (
165
- // eslint-disable-next-line no-nested-ternary
166
- condition ? /*#__PURE__*/React__default.createElement(Route, props) : redirectRoute ? /*#__PURE__*/React__default.createElement(Redirect, {
166
+ if (condition) {
167
+ if (permissions) {
168
+ return globalProps.permissions.some(includes(__, permissions)) ? /*#__PURE__*/React__default.createElement(Route, props) : errorPage || /*#__PURE__*/React__default.createElement(ErrorPage, {
169
+ status: 403
170
+ });
171
+ }
172
+ return /*#__PURE__*/React__default.createElement(Route, props);
173
+ }
174
+ if (redirectRoute) {
175
+ return /*#__PURE__*/React__default.createElement(Redirect, {
167
176
  to: {
168
177
  pathname: redirectRoute
169
178
  }
170
- }) : errorPage || /*#__PURE__*/React__default.createElement(ErrorPage, {
171
- status: "403"
172
- })
173
- );
179
+ });
180
+ }
181
+ return errorPage || /*#__PURE__*/React__default.createElement(ErrorPage, {
182
+ status: 403
183
+ });
174
184
  };
175
185
 
176
186
  function _arrayWithHoles(arr) {
@@ -3442,6 +3452,18 @@ var usePrevious = function usePrevious(value) {
3442
3452
  return ref.current;
3443
3453
  };
3444
3454
 
3455
+ var useStateWithDependency = function useStateWithDependency(defaultValue) {
3456
+ var dependencies = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [defaultValue];
3457
+ var _useState = useState(defaultValue),
3458
+ _useState2 = _slicedToArray(_useState, 2),
3459
+ value = _useState2[0],
3460
+ setValue = _useState2[1];
3461
+ useEffect(function () {
3462
+ setValue(defaultValue);
3463
+ }, dependencies);
3464
+ return [value, setValue];
3465
+ };
3466
+
3445
3467
  function _typeof$1(obj) {
3446
3468
  "@babel/helpers - typeof";
3447
3469
 
@@ -4668,5 +4690,5 @@ var withImmutableActions = function withImmutableActions(config) {
4668
4690
  };
4669
4691
  };
4670
4692
 
4671
- export { HoneybadgerErrorBoundary, PrivateRoute, destroyBrowserSubscription, handleMetaClick, isMetaKeyPressed, registerBrowserNotifications, useDebounce, useDisplayErrorPage, useErrorDisplayStore, useFuncDebounce, useHotKeys, useIsElementVisibleInDom, useKeyboardShortcutsPaneState, useLocalStorage, useOnClickOutside, usePrevious, useTimer, useUpdateEffect, withImmutableActions, withTitle };
4693
+ export { HoneybadgerErrorBoundary, PrivateRoute, destroyBrowserSubscription, handleMetaClick, isMetaKeyPressed, registerBrowserNotifications, useDebounce, useDisplayErrorPage, useErrorDisplayStore, useFuncDebounce, useHotKeys, useIsElementVisibleInDom, useKeyboardShortcutsPaneState, useLocalStorage, useOnClickOutside, usePrevious, useStateWithDependency, useTimer, useUpdateEffect, withImmutableActions, withTitle };
4672
4694
  //# sourceMappingURL=react-utils.js.map