@bigbinary/neeto-commons-frontend 2.0.133 → 2.0.135

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
@@ -688,6 +688,33 @@ export function useRegisterNavigationCheckpoint(key: string, path: string): void
688
688
  export function useNavigationCheckpoints(...keys: string[]): {
689
689
  [key: string]: string;
690
690
  };
691
+ /**
692
+ *
693
+ * Similar to useDebounce but this hook accepts a function as an argument. That
694
+ *
695
+ * function will be executed only when the dependencies stops changing for a while.
696
+ *
697
+ * The debounced function comes with a cancel method which can be used to cancel
698
+ *
699
+ * the delayed function invocation.
700
+ *
701
+ * @example
702
+ *
703
+ * const searchForProducts = useFuncDebounce(async key => {
704
+ * // this function will be triggered once after user stops typing for 300ms
705
+ * const products = await productsApi.fetch(key);
706
+ * // do something with the products
707
+ * }, 300);
708
+ *
709
+ * // component
710
+ * <Input onChange={e => searchForProducts(e.target.value)} />;
711
+ * <Button onClick={() => searchForProducts.cancel()}>Cancel search</Button>;
712
+ * @endexample
713
+ */
714
+ export const useFuncDebounce: {
715
+ <F extends Function>(func: F, delay?: number): F;
716
+ cancel: () => void;
717
+ };
691
718
  /**
692
719
  *
693
720
  * usePersistedQuery is an wrapper function around useQuery hook from
@@ -714,6 +741,10 @@ export function useNavigationCheckpoints(...keys: string[]): {
714
741
  *
715
742
  * undefined) until isFreshLoading turns to false.
716
743
  *
744
+ * It also provides a method getCache to get the cached data from localStorage
745
+ *
746
+ * by providing the queryKey.
747
+ *
717
748
  * @example
718
749
  *
719
750
  * const useFetchUsers = options =>
@@ -721,10 +752,16 @@ export function useNavigationCheckpoints(...keys: string[]): {
721
752
  *
722
753
  * // Now we can use useFetchUsers as:
723
754
  * const { data, isLoading, isFreshLoading } = useFetchUsers();
755
+ *
756
+ * // To get the cached data from localStorage
757
+ * const cachedUsersData = usePersistedQuery.getCache(QUERY_KEYS.FETCH_USERS);
724
758
  * @endexample
725
759
  */
726
- export function usePersistedQuery<T>(queryKey: string | unknown[], fetch: () => Promise<T>, options?: UseQueryOptions): UseQueryResult<T> & {
727
- isFreshLoading: boolean;
760
+ export const usePersistedQuery: {
761
+ <T>(queryKey: string | unknown[], fetch: () => Promise<T>, options?: UseQueryOptions): UseQueryResult<T> & {
762
+ isFreshLoading: boolean;
763
+ };
764
+ getCache: <T>(queryKey: string | unknown[]) => T | null;
728
765
  };
729
766
  /**
730
767
  *
@@ -734,7 +771,11 @@ export function usePersistedQuery<T>(queryKey: string | unknown[], fetch: () =>
734
771
  *
735
772
  * This hook uses usePersistedQuery under the hood. So data will be immediately
736
773
  *
737
- * available except for the first page load.
774
+ * available except for the first page load. Additionally if the data is available
775
+ *
776
+ * in localStorage, it will be served from there until an explicit call to
777
+ *
778
+ * refetch is made.
738
779
  *
739
780
  * @example
740
781
  *
package/react-utils.js CHANGED
@@ -6,7 +6,10 @@ import { isNil, includes, __, prop, toPairs, mergeLeft, curry, keys, isEmpty, me
6
6
  import { useTranslation, Trans, withTranslation } from 'react-i18next';
7
7
  import ErrorPage from '@bigbinary/neeto-molecules/ErrorPage';
8
8
  import { Route, Redirect } from 'react-router-dom';
9
+ import { isNotPresent } from '@bigbinary/neeto-commons-frontend/pure';
10
+ import { usePersistedQuery as usePersistedQuery$1 } from '@bigbinary/neeto-commons-frontend/react-utils';
9
11
  import axios from 'axios';
12
+ import { getFromLocalStorage } from '@bigbinary/neeto-commons-frontend/utils';
10
13
  import { useQuery, useQueryClient, useMutation } from 'react-query';
11
14
  import { Helmet } from 'react-helmet';
12
15
 
@@ -114,7 +117,8 @@ var HoneybadgerErrorBoundary = function HoneybadgerErrorBoundary(_ref) {
114
117
  enableUncaught: true,
115
118
  async: true,
116
119
  breadcrumbsEnabled: true,
117
- projectRoot: "webpack:///./"
120
+ projectRoot: "webpack:///./",
121
+ enableUnhandledRejection: false
118
122
  });
119
123
  return /*#__PURE__*/React__default.createElement(HoneybadgerErrorBoundary$1, {
120
124
  ErrorComponent: ErrorComponent,
@@ -239,19 +243,31 @@ var NEETO_APPS_LIST_STALE_TIME = 30 * 24 * 60 * 60 * 1000; // 30 days
239
243
 
240
244
  function ownKeys$2(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
241
245
  function _objectSpread$2(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys$2(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys$2(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
246
+ var useFetchNeetoApps = function useFetchNeetoApps(options) {
247
+ var _globalProps$user;
248
+ var scopedQueryKey = "".concat(QUERY_KEYS.NEETO_APPS_LIST, "-").concat((_globalProps$user = globalProps.user) === null || _globalProps$user === void 0 ? void 0 : _globalProps$user.id);
249
+ var neetoApps = usePersistedQuery$1.getCache(scopedQueryKey);
250
+ return usePersistedQuery$1(scopedQueryKey, neetoAppsApi.fetch, _objectSpread$2({
251
+ staleTime: NEETO_APPS_LIST_STALE_TIME,
252
+ enabled: isNotPresent(neetoApps === null || neetoApps === void 0 ? void 0 : neetoApps.data)
253
+ }, options));
254
+ };
255
+
256
+ function ownKeys$1(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
257
+ function _objectSpread$1(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys$1(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys$1(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
242
258
  var localStorageQueryCache = {
243
259
  set: function set(key, data) {
244
260
  var cache = localStorageQueryCache.getAll();
245
- var newCache = _objectSpread$2(_objectSpread$2({}, cache), {}, _defineProperty({}, key, {
261
+ var newCache = _objectSpread$1(_objectSpread$1({}, cache), {}, _defineProperty({}, key, {
246
262
  data: data,
247
263
  modifiedAt: Date.now()
248
264
  }));
249
265
  localStorage.setItem(QUERY_CACHE_NAME_SPACE, JSON.stringify(newCache));
250
266
  },
251
267
  getAll: function getAll() {
252
- var cache = localStorage.getItem(QUERY_CACHE_NAME_SPACE);
268
+ var cache = getFromLocalStorage(QUERY_CACHE_NAME_SPACE);
253
269
  if (!cache) return {};
254
- return JSON.parse(cache);
270
+ return cache;
255
271
  },
256
272
  get: function get(key) {
257
273
  return localStorageQueryCache.getAll()[key];
@@ -270,23 +286,16 @@ var usePersistedQuery = function usePersistedQuery(queryKey, fetch, options) {
270
286
  localStorageQueryCache.set(queryKey, queryResult.data);
271
287
  }, [queryKey, queryResult.data, queryResult.isSuccess]);
272
288
  if (isOutdated(localCache, options === null || options === void 0 ? void 0 : options.staleTime)) {
273
- return _objectSpread$2(_objectSpread$2({}, queryResult), {}, {
289
+ return _objectSpread$1(_objectSpread$1({}, queryResult), {}, {
274
290
  isFreshLoading: queryResult.isLoading
275
291
  });
276
292
  }
277
- return _objectSpread$2(_objectSpread$2({}, queryResult), {}, {
293
+ return _objectSpread$1(_objectSpread$1({}, queryResult), {}, {
278
294
  data: queryResult.data || localCache.data,
279
295
  isFreshLoading: false
280
296
  });
281
297
  };
282
-
283
- function ownKeys$1(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
284
- function _objectSpread$1(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys$1(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys$1(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
285
- var useFetchNeetoApps = function useFetchNeetoApps(options) {
286
- return usePersistedQuery(QUERY_KEYS.NEETO_APPS_LIST, neetoAppsApi.fetch, _objectSpread$1({
287
- staleTime: NEETO_APPS_LIST_STALE_TIME
288
- }, options));
289
- };
298
+ usePersistedQuery.getCache = localStorageQueryCache.get;
290
299
 
291
300
  function _arrayWithHoles(arr) {
292
301
  if (Array.isArray(arr)) return arr;