@bigbinary/neeto-commons-frontend 2.0.133 → 2.0.134

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
 
@@ -239,19 +242,31 @@ var NEETO_APPS_LIST_STALE_TIME = 30 * 24 * 60 * 60 * 1000; // 30 days
239
242
 
240
243
  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
244
  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; }
245
+ var useFetchNeetoApps = function useFetchNeetoApps(options) {
246
+ var _globalProps$user;
247
+ var scopedQueryKey = "".concat(QUERY_KEYS.NEETO_APPS_LIST, "-").concat((_globalProps$user = globalProps.user) === null || _globalProps$user === void 0 ? void 0 : _globalProps$user.id);
248
+ var neetoApps = usePersistedQuery$1.getCache(scopedQueryKey);
249
+ return usePersistedQuery$1(scopedQueryKey, neetoAppsApi.fetch, _objectSpread$2({
250
+ staleTime: NEETO_APPS_LIST_STALE_TIME,
251
+ enabled: isNotPresent(neetoApps === null || neetoApps === void 0 ? void 0 : neetoApps.data)
252
+ }, options));
253
+ };
254
+
255
+ 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; }
256
+ 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
257
  var localStorageQueryCache = {
243
258
  set: function set(key, data) {
244
259
  var cache = localStorageQueryCache.getAll();
245
- var newCache = _objectSpread$2(_objectSpread$2({}, cache), {}, _defineProperty({}, key, {
260
+ var newCache = _objectSpread$1(_objectSpread$1({}, cache), {}, _defineProperty({}, key, {
246
261
  data: data,
247
262
  modifiedAt: Date.now()
248
263
  }));
249
264
  localStorage.setItem(QUERY_CACHE_NAME_SPACE, JSON.stringify(newCache));
250
265
  },
251
266
  getAll: function getAll() {
252
- var cache = localStorage.getItem(QUERY_CACHE_NAME_SPACE);
267
+ var cache = getFromLocalStorage(QUERY_CACHE_NAME_SPACE);
253
268
  if (!cache) return {};
254
- return JSON.parse(cache);
269
+ return cache;
255
270
  },
256
271
  get: function get(key) {
257
272
  return localStorageQueryCache.getAll()[key];
@@ -270,23 +285,16 @@ var usePersistedQuery = function usePersistedQuery(queryKey, fetch, options) {
270
285
  localStorageQueryCache.set(queryKey, queryResult.data);
271
286
  }, [queryKey, queryResult.data, queryResult.isSuccess]);
272
287
  if (isOutdated(localCache, options === null || options === void 0 ? void 0 : options.staleTime)) {
273
- return _objectSpread$2(_objectSpread$2({}, queryResult), {}, {
288
+ return _objectSpread$1(_objectSpread$1({}, queryResult), {}, {
274
289
  isFreshLoading: queryResult.isLoading
275
290
  });
276
291
  }
277
- return _objectSpread$2(_objectSpread$2({}, queryResult), {}, {
292
+ return _objectSpread$1(_objectSpread$1({}, queryResult), {}, {
278
293
  data: queryResult.data || localCache.data,
279
294
  isFreshLoading: false
280
295
  });
281
296
  };
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
- };
297
+ usePersistedQuery.getCache = localStorageQueryCache.get;
290
298
 
291
299
  function _arrayWithHoles(arr) {
292
300
  if (Array.isArray(arr)) return arr;