@promoboxx/use-filter 1.11.0 → 1.11.2

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,2 +1,4 @@
1
1
  import { FilterStore } from './index';
2
2
  export declare const createUrlParamStore: () => FilterStore;
3
+ export declare function replaceQueryParams(newParams: string): void;
4
+ export declare function naivelyParseExistingParams(): any;
@@ -10,11 +10,22 @@ var __assign = (this && this.__assign) || function () {
10
10
  };
11
11
  return __assign.apply(this, arguments);
12
12
  };
13
+ var __rest = (this && this.__rest) || function (s, e) {
14
+ var t = {};
15
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
16
+ t[p] = s[p];
17
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
18
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
19
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
20
+ t[p[i]] = s[p[i]];
21
+ }
22
+ return t;
23
+ };
13
24
  var __importDefault = (this && this.__importDefault) || function (mod) {
14
25
  return (mod && mod.__esModule) ? mod : { "default": mod };
15
26
  };
16
27
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.createUrlParamStore = void 0;
28
+ exports.naivelyParseExistingParams = exports.replaceQueryParams = exports.createUrlParamStore = void 0;
18
29
  var qs_1 = __importDefault(require("qs"));
19
30
  var createUrlParamStore = function () {
20
31
  var urlParamStore = {
@@ -24,17 +35,9 @@ var createUrlParamStore = function () {
24
35
  var parsedFilter = parsed["filter." + namespace];
25
36
  // useFilter doesn't really support returning a partial FilterInfo, so both
26
37
  // need to be present,
27
- if (parsedInfo && parsedFilter) {
28
- if (parsedInfo.page != null) {
29
- parsedInfo.page = Number(parsedInfo.page);
30
- }
31
- if (parsedInfo.pageSize != null) {
32
- parsedInfo.pageSize = Number(parsedInfo.pageSize);
33
- }
34
- if (parsedInfo.offset != null) {
35
- parsedInfo.offset = Number(parsedInfo.offset);
36
- }
37
- return __assign(__assign({}, parsedInfo), { filter: parsedFilter });
38
+ if (parsedInfo || parsedFilter) {
39
+ var _a = parsedInfo || {}, page = _a.page, offset = _a.offset, pageSize = _a.pageSize, rest = __rest(_a, ["page", "offset", "pageSize"]);
40
+ return __assign(__assign({}, __assign({ page: 1 / page ? +page : undefined, pageSize: 1 / pageSize ? +pageSize : undefined, offset: 1 / offset ? +offset : undefined }, rest)), { filter: parsedFilter });
38
41
  }
39
42
  },
40
43
  saveFilter: function (namespace, filterInfo) {
@@ -79,8 +82,10 @@ function replaceQueryParams(newParams) {
79
82
  nextUrl.search = newParams;
80
83
  window.history.replaceState(undefined, '', nextUrl.toString());
81
84
  }
85
+ exports.replaceQueryParams = replaceQueryParams;
82
86
  function naivelyParseExistingParams() {
83
87
  // Don't really want to do any casting, especially not to `any`, but the type
84
88
  // of `qs.parse` is so wide we'd have to litter checks all over the place.
85
89
  return qs_1.default.parse(window.location.search.substring(1));
86
90
  }
91
+ exports.naivelyParseExistingParams = naivelyParseExistingParams;
package/dist/useFilter.js CHANGED
@@ -47,10 +47,15 @@ function useFilter(namespace, options) {
47
47
  ctxRef.current = ctxRefValue;
48
48
  });
49
49
  var _a = react_1.useState(function () {
50
- var existing = store_1.getFilterStore(ctxRef.current.store).getFilter(namespace);
51
- var initialValue = existing
52
- ? __assign(__assign({}, existing), { shouldRunImmediately: true }) : undefined;
53
- return initialValue;
50
+ var _a;
51
+ var fromStore = store_1.getFilterStore(ctxRef.current.store).getFilter(namespace);
52
+ // If the filter is already in the store, we have to make sure it ...
53
+ // - has both the filter and filterInfo
54
+ // - has all the fields the user passed to the hook
55
+ if (fromStore) {
56
+ var filterInfo_1 = buildDefaultFilterInfo_1.default(__assign(__assign({}, fromStore), { shouldRunImmediately: true, filter: __assign(__assign({}, (_a = options.defaultFilterInfo) === null || _a === void 0 ? void 0 : _a.filter), fromStore.filter) }));
57
+ return filterInfo_1;
58
+ }
54
59
  }), filterInfo = _a[0], setFilterInfoState = _a[1];
55
60
  var _b = react_1.useState(function () {
56
61
  return store_1.getFilterStore(ctxRef.current.store).getData(namespace);
@@ -34,8 +34,20 @@ function useSimpleFilter(namespace, options) {
34
34
  ctxRef.current = ctxRefValue;
35
35
  });
36
36
  var _a = react_1.useState(false), isLoading = _a[0], setIsLoading = _a[1];
37
- var _b = react_1.useState(function () { return (__assign(__assign({}, (store_1.getFilterStore(ctxRef.current.store).getFilter(namespace) ||
38
- buildDefaultFilterInfo(options.defaultFilterInfo))), { shouldRunImmediately: true })); }), filterInfo = _b[0], setFilterInfoState = _b[1];
37
+ var _b = react_1.useState(function () {
38
+ var _a;
39
+ var fromStore = store_1.getFilterStore(ctxRef.current.store).getFilter(namespace);
40
+ // If the filter is already in the store, we have to make sure it ...
41
+ // - has both the filter and filterInfo
42
+ // - has all the fields the user passed to the hook
43
+ if (fromStore) {
44
+ var filterInfo_1 = buildDefaultFilterInfo(__assign(__assign({}, fromStore), { shouldRunImmediately: true, filter: __assign(__assign({}, (_a = options.defaultFilterInfo) === null || _a === void 0 ? void 0 : _a.filter), fromStore.filter) }));
45
+ return filterInfo_1;
46
+ }
47
+ else {
48
+ return buildDefaultFilterInfo(__assign(__assign({}, options.defaultFilterInfo), { shouldRunImmediately: true }));
49
+ }
50
+ }), filterInfo = _b[0], setFilterInfoState = _b[1];
39
51
  var _c = react_1.useState(filterInfo), debouncedFilterInfo = _c[0], setDebouncedFilterInfo = _c[1];
40
52
  // Not sure this is needed in simple mode?
41
53
  var lastRefreshAtRef = react_1.useRef(-1);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@promoboxx/use-filter",
3
- "version": "1.11.0",
3
+ "version": "1.11.2",
4
4
  "description": "",
5
5
  "main": "dist/useFilter.js",
6
6
  "keywords": [],