@promoboxx/use-filter 1.9.0 → 1.10.0

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.
@@ -0,0 +1,2 @@
1
+ import { FilterStore } from './index';
2
+ export declare const createUrlParamStore: () => FilterStore;
@@ -0,0 +1,86 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __importDefault = (this && this.__importDefault) || function (mod) {
14
+ return (mod && mod.__esModule) ? mod : { "default": mod };
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.createUrlParamStore = void 0;
18
+ var qs_1 = __importDefault(require("qs"));
19
+ var createUrlParamStore = function () {
20
+ var urlParamStore = {
21
+ getFilter: function (namespace) {
22
+ var parsed = naivelyParseExistingParams();
23
+ var parsedInfo = parsed["info." + namespace];
24
+ var parsedFilter = parsed["filter." + namespace];
25
+ // useFilter doesn't really support returning a partial FilterInfo, so both
26
+ // 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
+ }
39
+ },
40
+ saveFilter: function (namespace, filterInfo) {
41
+ var parsed = naivelyParseExistingParams();
42
+ // Setting the params to the equivalent of { [namespace]: filterInfo }
43
+ // takes up more room. The idea is that this ...
44
+ // 'filter.filterName%5BfilterValueKey%5D=filterValueValue'
45
+ // ... is smaller than ...
46
+ // filterName%5Bfilter%5D%5BfilterValueKey%5D=filterValueValue
47
+ // ... by 5 characters per value in a filter. So excuse us for a second ...
48
+ var clonedInfo = __assign({}, filterInfo);
49
+ var clonedFilter = filterInfo.filter;
50
+ delete clonedInfo.filter;
51
+ delete clonedInfo.lastRefreshAt;
52
+ delete clonedInfo.shouldRunImmediately;
53
+ delete clonedInfo.totalResults;
54
+ delete clonedInfo.totalPages;
55
+ delete clonedInfo.nextCursor;
56
+ parsed["filter." + namespace] = clonedFilter;
57
+ parsed["info." + namespace] = clonedInfo;
58
+ replaceQueryParams(qs_1.default.stringify(parsed));
59
+ },
60
+ clear: function () {
61
+ var parsed = naivelyParseExistingParams();
62
+ for (var key in parsed) {
63
+ if (key.startsWith('filter.') || key.startsWith('info.')) {
64
+ delete parsed[key];
65
+ }
66
+ }
67
+ replaceQueryParams(qs_1.default.stringify(parsed));
68
+ },
69
+ getData: function () {
70
+ return undefined;
71
+ },
72
+ saveData: function () { },
73
+ };
74
+ return urlParamStore;
75
+ };
76
+ exports.createUrlParamStore = createUrlParamStore;
77
+ function replaceQueryParams(newParams) {
78
+ var nextUrl = new URL(window.location.toString());
79
+ nextUrl.search = newParams;
80
+ window.history.replaceState(undefined, '', nextUrl.toString());
81
+ }
82
+ function naivelyParseExistingParams() {
83
+ // Don't really want to do any casting, especially not to `any`, but the type
84
+ // of `qs.parse` is so wide we'd have to litter checks all over the place.
85
+ return qs_1.default.parse(window.location.search.substring(1));
86
+ }
@@ -69,7 +69,7 @@ export interface FilterApi<TFilter, TResult> {
69
69
  /**
70
70
  * Changes the cursor.
71
71
  */
72
- setCursor: (cursor: string | null | undefined) => void;
72
+ setCursor: (cursor: string | null | undefined, shouldRunImmediately?: boolean) => void;
73
73
  /**
74
74
  * Forces a refresh of the filter.
75
75
  */
package/dist/useFilter.js CHANGED
@@ -114,11 +114,17 @@ function useFilter(namespace, options) {
114
114
  extra.offset = offsetNumber;
115
115
  extra.page = getPageFromOffset_1.default(offsetNumber, pageSizeNumber);
116
116
  }
117
- if (totalResults) {
118
- extra.totalResults = Number(totalResults);
117
+ // `null` / `undefined` are valid here, it means there is zero
118
+ // results, so we can't just check if it's truthy, we need to check
119
+ // for presence.
120
+ if ('totalResults' in response.filterInfo) {
121
+ extra.totalResults = Number(totalResults || 0);
119
122
  extra.totalPages = Math.ceil(extra.totalResults / pageSizeNumber);
120
123
  }
121
- if (nextCursor) {
124
+ // `null` / `undefined` are valid here, it means there is no next
125
+ // cursor, so we can't just check if it's truthy, we need to check
126
+ // for presence.
127
+ if ('nextCursor' in response.filterInfo) {
122
128
  extra.nextCursor = nextCursor;
123
129
  }
124
130
  return __assign(__assign(__assign({}, previous), filterInfoFromResponse), extra);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@promoboxx/use-filter",
3
- "version": "1.9.0",
3
+ "version": "1.10.0",
4
4
  "description": "",
5
5
  "main": "dist/useFilter.js",
6
6
  "keywords": [],
@@ -10,6 +10,7 @@
10
10
  "@promoboxx/eslint-config": "^2.3.0",
11
11
  "@testing-library/react": "^12.0.0",
12
12
  "@types/jest": "^26.0.24",
13
+ "@types/qs": "^6.9.7",
13
14
  "@types/react": "^17.0.14",
14
15
  "@types/redux": "^3.6.0",
15
16
  "eslint": "^7.30.0",
@@ -43,7 +44,12 @@
43
44
  "dist/store/index.d.ts",
44
45
  "dist/store/index.js",
45
46
  "dist/store/localStorageStore.js",
47
+ "dist/store/urlParamStore.d.ts",
48
+ "dist/store/urlParamStore.js",
46
49
  "dist/store/memoryStore.js",
47
50
  "dist/store/reduxStore.js"
48
- ]
51
+ ],
52
+ "dependencies": {
53
+ "qs": "^6.11.2"
54
+ }
49
55
  }