@bigbinary/neeto-commons-frontend 3.5.3 → 3.6.0-beta1
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/cjs/react-utils/useMutationWithInvalidation/useMutationWithInvalidation.js +19 -4
- package/cjs/react-utils/useMutationWithInvalidation/useMutationWithInvalidation.js.map +1 -1
- package/cjs/react-utils/usePersistedQuery/usePersistedQuery.js +5 -2
- package/cjs/react-utils/usePersistedQuery/usePersistedQuery.js.map +1 -1
- package/package.json +3 -3
- package/react-utils/useMutationWithInvalidation/useMutationWithInvalidation.js +19 -4
- package/react-utils/useMutationWithInvalidation/useMutationWithInvalidation.js.map +1 -1
- package/react-utils/usePersistedQuery/usePersistedQuery.js +5 -2
- package/react-utils/usePersistedQuery/usePersistedQuery.js.map +1 -1
|
@@ -5,9 +5,10 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
value: true
|
|
6
6
|
});
|
|
7
7
|
exports["default"] = void 0;
|
|
8
|
+
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
|
|
8
9
|
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
9
10
|
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
|
|
10
|
-
var _reactQuery = require("react-query");
|
|
11
|
+
var _reactQuery = require("@tanstack/react-query");
|
|
11
12
|
var _excluded = ["keysToInvalidate"],
|
|
12
13
|
_excluded2 = ["onSuccess"];
|
|
13
14
|
function ownKeys(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; }
|
|
@@ -18,11 +19,25 @@ var useMutationWithInvalidation = function useMutationWithInvalidation(mutationF
|
|
|
18
19
|
var queryClient = (0, _reactQuery.useQueryClient)();
|
|
19
20
|
var _onSuccess = options.onSuccess,
|
|
20
21
|
otherOptions = (0, _objectWithoutProperties2["default"])(options, _excluded2);
|
|
21
|
-
return (0, _reactQuery.useMutation)(
|
|
22
|
+
return (0, _reactQuery.useMutation)(_objectSpread({
|
|
23
|
+
mutationFn: mutationFn,
|
|
22
24
|
onSuccess: function onSuccess(data, variables, context) {
|
|
23
25
|
keysToInvalidate.forEach(function (key) {
|
|
24
|
-
var
|
|
25
|
-
|
|
26
|
+
var result = key;
|
|
27
|
+
if (typeof key === "function") {
|
|
28
|
+
result = key(data, variables, context);
|
|
29
|
+
}
|
|
30
|
+
if ((0, _typeof2["default"])(result) === "object") {
|
|
31
|
+
queryClient.invalidateQueries(result);
|
|
32
|
+
}
|
|
33
|
+
if (typeof result === "string") {
|
|
34
|
+
queryClient.invalidateQueries({
|
|
35
|
+
queryKey: [key]
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
queryClient.invalidateQueries({
|
|
39
|
+
queryKey: key
|
|
40
|
+
});
|
|
26
41
|
});
|
|
27
42
|
_onSuccess === null || _onSuccess === void 0 ? void 0 : _onSuccess(data, variables, context);
|
|
28
43
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useMutationWithInvalidation.js","names":["_reactQuery","require","_excluded","_excluded2","ownKeys","object","enumerableOnly","keys","Object","getOwnPropertySymbols","symbols","filter","sym","getOwnPropertyDescriptor","enumerable","push","apply","_objectSpread","target","i","arguments","length","source","forEach","key","_defineProperty2","getOwnPropertyDescriptors","defineProperties","defineProperty","useMutationWithInvalidation","mutationFn","_ref","keysToInvalidate","options","_objectWithoutProperties2","queryClient","useQueryClient","onSuccess","otherOptions","useMutation","data","variables","context","
|
|
1
|
+
{"version":3,"file":"useMutationWithInvalidation.js","names":["_reactQuery","require","_excluded","_excluded2","ownKeys","object","enumerableOnly","keys","Object","getOwnPropertySymbols","symbols","filter","sym","getOwnPropertyDescriptor","enumerable","push","apply","_objectSpread","target","i","arguments","length","source","forEach","key","_defineProperty2","getOwnPropertyDescriptors","defineProperties","defineProperty","useMutationWithInvalidation","mutationFn","_ref","keysToInvalidate","options","_objectWithoutProperties2","queryClient","useQueryClient","onSuccess","otherOptions","useMutation","data","variables","context","result","_typeof2","invalidateQueries","queryKey","_default","exports"],"sources":["../../../../src/react-utils/useMutationWithInvalidation/useMutationWithInvalidation.js"],"sourcesContent":["import { useQueryClient, useMutation } from \"@tanstack/react-query\";\n\nconst useMutationWithInvalidation = (\n mutationFn,\n { keysToInvalidate, ...options }\n) => {\n const queryClient = useQueryClient();\n const { onSuccess, ...otherOptions } = options;\n\n return useMutation({\n mutationFn,\n onSuccess: (data, variables, context) => {\n keysToInvalidate.forEach(key => {\n let result = key;\n if (typeof key === \"function\") {\n result = key(data, variables, context);\n }\n\n if (typeof result === \"object\") {\n queryClient.invalidateQueries(result);\n }\n\n if (typeof result === \"string\") {\n queryClient.invalidateQueries({ queryKey: [key] });\n }\n queryClient.invalidateQueries({ queryKey: key });\n });\n onSuccess?.(data, variables, context);\n },\n ...otherOptions,\n });\n};\n\nexport default useMutationWithInvalidation;\n"],"mappings":";;;;;;;;;;AAAA,IAAAA,WAAA,GAAAC,OAAA;AAAoE,IAAAC,SAAA;EAAAC,UAAA;AAAA,SAAAC,QAAAC,MAAA,EAAAC,cAAA,QAAAC,IAAA,GAAAC,MAAA,CAAAD,IAAA,CAAAF,MAAA,OAAAG,MAAA,CAAAC,qBAAA,QAAAC,OAAA,GAAAF,MAAA,CAAAC,qBAAA,CAAAJ,MAAA,GAAAC,cAAA,KAAAI,OAAA,GAAAA,OAAA,CAAAC,MAAA,WAAAC,GAAA,WAAAJ,MAAA,CAAAK,wBAAA,CAAAR,MAAA,EAAAO,GAAA,EAAAE,UAAA,OAAAP,IAAA,CAAAQ,IAAA,CAAAC,KAAA,CAAAT,IAAA,EAAAG,OAAA,YAAAH,IAAA;AAAA,SAAAU,cAAAC,MAAA,aAAAC,CAAA,MAAAA,CA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,EAAEC,OAAO,CAAC;QACxC;QAEA,IAAI,IAAAE,QAAA,aAAOD,MAAM,MAAK,QAAQ,EAAE;UAC9BR,WAAW,CAACU,iBAAiB,CAACF,MAAM,CAAC;QACvC;QAEA,IAAI,OAAOA,MAAM,KAAK,QAAQ,EAAE;UAC9BR,WAAW,CAACU,iBAAiB,CAAC;YAAEC,QAAQ,EAAE,CAACtB,GAAG;UAAE,CAAC,CAAC;QACpD;QACAW,WAAW,CAACU,iBAAiB,CAAC;UAAEC,QAAQ,EAAEtB;QAAI,CAAC,CAAC;MAClD,CAAC,CAAC;MACFa,UAAS,aAATA,UAAS,uBAATA,UAAS,CAAGG,IAAI,EAAEC,SAAS,EAAEC,OAAO,CAAC;IACvC;EAAC,GACEJ,YAAY,EACf;AACJ,CAAC;AAAC,IAAAS,QAAA,GAEalB,2BAA2B;AAAAmB,OAAA,cAAAD,QAAA"}
|
|
@@ -7,8 +7,8 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
7
7
|
exports["default"] = void 0;
|
|
8
8
|
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
9
9
|
var _react = require("react");
|
|
10
|
+
var _reactQuery = require("@tanstack/react-query");
|
|
10
11
|
var _ramda = require("ramda");
|
|
11
|
-
var _reactQuery = require("react-query");
|
|
12
12
|
var _constants = require("../constants");
|
|
13
13
|
var _general = require("../../utils/general");
|
|
14
14
|
function ownKeys(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; }
|
|
@@ -37,7 +37,10 @@ var isOutdated = function isOutdated(localCache, staleTime) {
|
|
|
37
37
|
return localCache.modifiedAt <= Date.now() - staleTime;
|
|
38
38
|
};
|
|
39
39
|
var usePersistedQuery = function usePersistedQuery(queryKey, fetch, options) {
|
|
40
|
-
var queryResult = (0, _reactQuery.useQuery)(
|
|
40
|
+
var queryResult = (0, _reactQuery.useQuery)(_objectSpread({
|
|
41
|
+
queryKey: queryKey,
|
|
42
|
+
queryFn: fetch
|
|
43
|
+
}, options));
|
|
41
44
|
var localCache = localStorageQueryCache.get(queryKey);
|
|
42
45
|
(0, _react.useEffect)(function () {
|
|
43
46
|
if (!queryResult.isSuccess) return;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"usePersistedQuery.js","names":["_react","require","
|
|
1
|
+
{"version":3,"file":"usePersistedQuery.js","names":["_react","require","_reactQuery","_ramda","_constants","_general","ownKeys","object","enumerableOnly","keys","Object","getOwnPropertySymbols","symbols","filter","sym","getOwnPropertyDescriptor","enumerable","push","apply","_objectSpread","target","i","arguments","length","source","forEach","key","_defineProperty2","getOwnPropertyDescriptors","defineProperties","defineProperty","localStorageQueryCache","set","data","cache","getAll","newCache","modifiedAt","Date","now","setToLocalStorage","QUERY_CACHE_NAME_SPACE","getFromLocalStorage","get","isOutdated","localCache","staleTime","isNil","usePersistedQuery","queryKey","fetch","options","queryResult","useQuery","queryFn","useEffect","isSuccess","isFreshLoading","isLoading","getCache","_default","exports"],"sources":["../../../../src/react-utils/usePersistedQuery/usePersistedQuery.js"],"sourcesContent":["import { useEffect } from \"react\";\n\nimport { useQuery } from \"@tanstack/react-query\";\nimport { isNil } from \"ramda\";\nimport { QUERY_CACHE_NAME_SPACE } from \"react-utils/constants\";\nimport { getFromLocalStorage, setToLocalStorage } from \"utils/general\";\n\nconst localStorageQueryCache = {\n set: (key, data) => {\n const cache = localStorageQueryCache.getAll();\n const newCache = { ...cache, [key]: { data, modifiedAt: Date.now() } };\n setToLocalStorage(QUERY_CACHE_NAME_SPACE, newCache);\n },\n getAll: () => {\n const cache = getFromLocalStorage(QUERY_CACHE_NAME_SPACE);\n if (!cache) return {};\n\n return cache;\n },\n get: key => localStorageQueryCache.getAll()[key],\n};\n\nconst isOutdated = (localCache, staleTime) => {\n if (isNil(localCache)) return true;\n\n if (isNil(staleTime)) return false;\n\n return localCache.modifiedAt <= Date.now() - staleTime;\n};\n\nconst usePersistedQuery = (queryKey, fetch, options) => {\n const queryResult = useQuery({\n queryKey,\n queryFn: fetch,\n ...options,\n });\n const localCache = localStorageQueryCache.get(queryKey);\n\n useEffect(() => {\n if (!queryResult.isSuccess) return;\n localStorageQueryCache.set(queryKey, queryResult.data);\n }, [queryKey, queryResult.data, queryResult.isSuccess]);\n\n if (isOutdated(localCache, options?.staleTime)) {\n return { ...queryResult, isFreshLoading: queryResult.isLoading };\n }\n\n return {\n ...queryResult,\n data: queryResult.data || localCache.data,\n isFreshLoading: false,\n };\n};\n\nusePersistedQuery.getCache = localStorageQueryCache.get;\n\nexport default usePersistedQuery;\n"],"mappings":";;;;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AAEA,IAAAC,WAAA,GAAAD,OAAA;AACA,IAAAE,MAAA,GAAAF,OAAA;AACA,IAAAG,UAAA,GAAAH,OAAA;AACA,IAAAI,QAAA,GAAAJ,OAAA;AAAuE,SAAAK,QAAAC,MAAA,EAAAC,cAAA,QAAAC,IAAA,GAAAC,MAAA,CAAAD,IAAA,CAAAF,MAAA,OAAAG,MAAA,CAAAC,qBAAA,QAAAC,OAAA,GAAAF,MAAA,CAAAC,qBAAA,CAAAJ,MAAA,GAAAC,cAAA,KAAAI,OAAA,GAAAA,OAAA,CAAAC,MAAA,WAAAC,GAAA,WAAAJ,MAAA,CAAAK,wBAAA,CAAAR,MAAA,EAAAO,GAAA,EAAAE,UAAA,OAAAP,IAAA,CAAAQ,IAAA,CAAAC,KAAA,CAAAT,IAAA,EAAAG,OAAA,YAAAH,IAAA;AAAA,SAAAU,cAAAC,MAAA,aAAAC,CAAA,MAAAA,CAAA,GAAAC,SAAA,CAAAC,MAAA,EAAAF,CAAA,UAAAG,MAAA,WAAAF,SAAA,CAAAD,CAAA,IAAAC,SAAA,CAAAD,CAAA,QAAAA,CAAA,OAAAf,OAAA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
2
|
"name": "@bigbinary/neeto-commons-frontend",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.6.0-beta1",
|
|
4
4
|
"description": "A package encapsulating common code across neeto projects including initializers, utility functions, common components and hooks and so on.",
|
|
5
5
|
"repository": "git@github.com:bigbinary/neeto-commons-frontend.git",
|
|
6
6
|
"author": "Amaljith K <amaljith.k@bigbinary.com>",
|
|
@@ -81,6 +81,7 @@
|
|
|
81
81
|
"@faker-js/faker": "^8.2.0",
|
|
82
82
|
"@honeybadger-io/js": "^6.4.1",
|
|
83
83
|
"@honeybadger-io/react": "^6.1.3",
|
|
84
|
+
"@tanstack/react-query": "5.40.0",
|
|
84
85
|
"antd": "4.18.7",
|
|
85
86
|
"autoprefixer": "^10.4.13",
|
|
86
87
|
"axios": "1.6.2",
|
|
@@ -95,7 +96,6 @@
|
|
|
95
96
|
"react": "18.2.0",
|
|
96
97
|
"react-helmet": "^6.1.0",
|
|
97
98
|
"react-i18next": "^12.3.1",
|
|
98
|
-
"react-query": "^3.39.2",
|
|
99
99
|
"react-router-dom": "5.3.3",
|
|
100
100
|
"react-toastify": "8.0.2",
|
|
101
101
|
"shakapacker": "^6.5.5",
|
|
@@ -103,4 +103,4 @@
|
|
|
103
103
|
"webpack": "^5.75.0",
|
|
104
104
|
"yup": "^1.3.3"
|
|
105
105
|
}
|
|
106
|
-
}
|
|
106
|
+
}
|
|
@@ -1,21 +1,36 @@
|
|
|
1
|
+
import _typeof from "@babel/runtime/helpers/typeof";
|
|
1
2
|
import _defineProperty from "@babel/runtime/helpers/defineProperty";
|
|
2
3
|
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
|
|
3
4
|
var _excluded = ["keysToInvalidate"],
|
|
4
5
|
_excluded2 = ["onSuccess"];
|
|
5
6
|
function ownKeys(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; }
|
|
6
7
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
7
|
-
import { useQueryClient, useMutation } from "react-query";
|
|
8
|
+
import { useQueryClient, useMutation } from "@tanstack/react-query";
|
|
8
9
|
var useMutationWithInvalidation = function useMutationWithInvalidation(mutationFn, _ref) {
|
|
9
10
|
var keysToInvalidate = _ref.keysToInvalidate,
|
|
10
11
|
options = _objectWithoutProperties(_ref, _excluded);
|
|
11
12
|
var queryClient = useQueryClient();
|
|
12
13
|
var _onSuccess = options.onSuccess,
|
|
13
14
|
otherOptions = _objectWithoutProperties(options, _excluded2);
|
|
14
|
-
return useMutation(
|
|
15
|
+
return useMutation(_objectSpread({
|
|
16
|
+
mutationFn: mutationFn,
|
|
15
17
|
onSuccess: function onSuccess(data, variables, context) {
|
|
16
18
|
keysToInvalidate.forEach(function (key) {
|
|
17
|
-
var
|
|
18
|
-
|
|
19
|
+
var result = key;
|
|
20
|
+
if (typeof key === "function") {
|
|
21
|
+
result = key(data, variables, context);
|
|
22
|
+
}
|
|
23
|
+
if (_typeof(result) === "object") {
|
|
24
|
+
queryClient.invalidateQueries(result);
|
|
25
|
+
}
|
|
26
|
+
if (typeof result === "string") {
|
|
27
|
+
queryClient.invalidateQueries({
|
|
28
|
+
queryKey: [key]
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
queryClient.invalidateQueries({
|
|
32
|
+
queryKey: key
|
|
33
|
+
});
|
|
19
34
|
});
|
|
20
35
|
_onSuccess === null || _onSuccess === void 0 ? void 0 : _onSuccess(data, variables, context);
|
|
21
36
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useMutationWithInvalidation.js","names":["useQueryClient","useMutation","useMutationWithInvalidation","mutationFn","_ref","keysToInvalidate","options","_objectWithoutProperties","_excluded","queryClient","onSuccess","otherOptions","_excluded2","_objectSpread","data","variables","context","forEach","key","
|
|
1
|
+
{"version":3,"file":"useMutationWithInvalidation.js","names":["useQueryClient","useMutation","useMutationWithInvalidation","mutationFn","_ref","keysToInvalidate","options","_objectWithoutProperties","_excluded","queryClient","onSuccess","otherOptions","_excluded2","_objectSpread","data","variables","context","forEach","key","result","_typeof","invalidateQueries","queryKey"],"sources":["../../../src/react-utils/useMutationWithInvalidation/useMutationWithInvalidation.js"],"sourcesContent":["import { useQueryClient, useMutation } from \"@tanstack/react-query\";\n\nconst useMutationWithInvalidation = (\n mutationFn,\n { keysToInvalidate, ...options }\n) => {\n const queryClient = useQueryClient();\n const { onSuccess, ...otherOptions } = options;\n\n return useMutation({\n mutationFn,\n onSuccess: (data, variables, context) => {\n keysToInvalidate.forEach(key => {\n let result = key;\n if (typeof key === \"function\") {\n result = key(data, variables, context);\n }\n\n if (typeof result === \"object\") {\n queryClient.invalidateQueries(result);\n }\n\n if (typeof result === \"string\") {\n queryClient.invalidateQueries({ queryKey: [key] });\n }\n queryClient.invalidateQueries({ queryKey: key });\n });\n onSuccess?.(data, variables, context);\n },\n ...otherOptions,\n });\n};\n\nexport default useMutationWithInvalidation;\n"],"mappings":";;;;;;;AAAA,SAASA,cAAc,EAAEC,WAAW,QAAQ,uBAAuB;AAEnE,IAAMC,2BAA2B,GAAG,SAA9BA,2BAA2BA,CAC/BC,UAAU,EAAAC,IAAA,EAEP;EAAA,IADDC,gBAAgB,GAAAD,IAAA,CAAhBC,gBAAgB;IAAKC,OAAO,GAAAC,wBAAA,CAAAH,IAAA,EAAAI,SAAA;EAE9B,IAAMC,WAAW,GAAGT,cAAc,EAAE;EACpC,IAAQU,UAAS,GAAsBJ,OAAO,CAAtCI,SAAS;IAAKC,YAAY,GAAAJ,wBAAA,CAAKD,OAAO,EAAAM,UAAA;EAE9C,OAAOX,WAAW,CAAAY,aAAA;IAChBV,UAAU,EAAVA,UAAU;IACVO,SAAS,EAAE,SAAAA,UAACI,IAAI,EAAEC,SAAS,EAAEC,OAAO,EAAK;MACvCX,gBAAgB,CAACY,OAAO,CAAC,UAAAC,GAAG,EAAI;QAC9B,IAAIC,MAAM,GAAGD,GAAG;QAChB,IAAI,OAAOA,GAAG,KAAK,UAAU,EAAE;UAC7BC,MAAM,GAAGD,GAAG,CAACJ,IAAI,EAAEC,SAAS,EAAEC,OAAO,CAAC;QACxC;QAEA,IAAII,OAAA,CAAOD,MAAM,MAAK,QAAQ,EAAE;UAC9BV,WAAW,CAACY,iBAAiB,CAACF,MAAM,CAAC;QACvC;QAEA,IAAI,OAAOA,MAAM,KAAK,QAAQ,EAAE;UAC9BV,WAAW,CAACY,iBAAiB,CAAC;YAAEC,QAAQ,EAAE,CAACJ,GAAG;UAAE,CAAC,CAAC;QACpD;QACAT,WAAW,CAACY,iBAAiB,CAAC;UAAEC,QAAQ,EAAEJ;QAAI,CAAC,CAAC;MAClD,CAAC,CAAC;MACFR,UAAS,aAATA,UAAS,uBAATA,UAAS,CAAGI,IAAI,EAAEC,SAAS,EAAEC,OAAO,CAAC;IACvC;EAAC,GACEL,YAAY,EACf;AACJ,CAAC;AAED,eAAeT,2BAA2B"}
|
|
@@ -2,8 +2,8 @@ import _defineProperty from "@babel/runtime/helpers/defineProperty";
|
|
|
2
2
|
function ownKeys(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; }
|
|
3
3
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
4
4
|
import { useEffect } from "react";
|
|
5
|
+
import { useQuery } from "@tanstack/react-query";
|
|
5
6
|
import { isNil } from "ramda";
|
|
6
|
-
import { useQuery } from "react-query";
|
|
7
7
|
import { QUERY_CACHE_NAME_SPACE } from "../constants";
|
|
8
8
|
import { getFromLocalStorage, setToLocalStorage } from "../../utils/general";
|
|
9
9
|
var localStorageQueryCache = {
|
|
@@ -30,7 +30,10 @@ var isOutdated = function isOutdated(localCache, staleTime) {
|
|
|
30
30
|
return localCache.modifiedAt <= Date.now() - staleTime;
|
|
31
31
|
};
|
|
32
32
|
var usePersistedQuery = function usePersistedQuery(queryKey, fetch, options) {
|
|
33
|
-
var queryResult = useQuery(
|
|
33
|
+
var queryResult = useQuery(_objectSpread({
|
|
34
|
+
queryKey: queryKey,
|
|
35
|
+
queryFn: fetch
|
|
36
|
+
}, options));
|
|
34
37
|
var localCache = localStorageQueryCache.get(queryKey);
|
|
35
38
|
useEffect(function () {
|
|
36
39
|
if (!queryResult.isSuccess) return;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"usePersistedQuery.js","names":["useEffect","
|
|
1
|
+
{"version":3,"file":"usePersistedQuery.js","names":["useEffect","useQuery","isNil","QUERY_CACHE_NAME_SPACE","getFromLocalStorage","setToLocalStorage","localStorageQueryCache","set","key","data","cache","getAll","newCache","_objectSpread","_defineProperty","modifiedAt","Date","now","get","isOutdated","localCache","staleTime","usePersistedQuery","queryKey","fetch","options","queryResult","queryFn","isSuccess","isFreshLoading","isLoading","getCache"],"sources":["../../../src/react-utils/usePersistedQuery/usePersistedQuery.js"],"sourcesContent":["import { useEffect } from \"react\";\n\nimport { useQuery } from \"@tanstack/react-query\";\nimport { isNil } from \"ramda\";\nimport { QUERY_CACHE_NAME_SPACE } from \"react-utils/constants\";\nimport { getFromLocalStorage, setToLocalStorage } from \"utils/general\";\n\nconst localStorageQueryCache = {\n set: (key, data) => {\n const cache = localStorageQueryCache.getAll();\n const newCache = { ...cache, [key]: { data, modifiedAt: Date.now() } };\n setToLocalStorage(QUERY_CACHE_NAME_SPACE, newCache);\n },\n getAll: () => {\n const cache = getFromLocalStorage(QUERY_CACHE_NAME_SPACE);\n if (!cache) return {};\n\n return cache;\n },\n get: key => localStorageQueryCache.getAll()[key],\n};\n\nconst isOutdated = (localCache, staleTime) => {\n if (isNil(localCache)) return true;\n\n if (isNil(staleTime)) return false;\n\n return localCache.modifiedAt <= Date.now() - staleTime;\n};\n\nconst usePersistedQuery = (queryKey, fetch, options) => {\n const queryResult = useQuery({\n queryKey,\n queryFn: fetch,\n ...options,\n });\n const localCache = localStorageQueryCache.get(queryKey);\n\n useEffect(() => {\n if (!queryResult.isSuccess) return;\n localStorageQueryCache.set(queryKey, queryResult.data);\n }, [queryKey, queryResult.data, queryResult.isSuccess]);\n\n if (isOutdated(localCache, options?.staleTime)) {\n return { ...queryResult, isFreshLoading: queryResult.isLoading };\n }\n\n return {\n ...queryResult,\n data: queryResult.data || localCache.data,\n isFreshLoading: false,\n };\n};\n\nusePersistedQuery.getCache = localStorageQueryCache.get;\n\nexport default usePersistedQuery;\n"],"mappings":";;;AAAA,SAASA,SAAS,QAAQ,OAAO;AAEjC,SAASC,QAAQ,QAAQ,uBAAuB;AAChD,SAASC,KAAK,QAAQ,OAAO;AAC7B,SAASC,sBAAsB;AAC/B,SAASC,mBAAmB,EAAEC,iBAAiB;AAE/C,IAAMC,sBAAsB,GAAG;EAC7BC,GAAG,EAAE,SAAAA,IAACC,GAAG,EAAEC,IAAI,EAAK;IAClB,IAAMC,KAAK,GAAGJ,sBAAsB,CAACK,MAAM,EAAE;IAC7C,IAAMC,QAAQ,GAAAC,aAAA,CAAAA,aAAA,K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