@promoboxx/use-filter 1.11.2 → 2.0.1
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/dist/cjs/_virtual/_rolldown/runtime.js +29 -0
- package/dist/cjs/lib/buildDefaultFilterInfo.d.ts +5 -0
- package/dist/cjs/lib/buildDefaultFilterInfo.js +27 -0
- package/dist/{lib → cjs/lib}/getOffsetFromPage.d.ts +2 -1
- package/dist/cjs/lib/getOffsetFromPage.js +8 -0
- package/dist/{lib → cjs/lib}/getPageFromOffset.d.ts +2 -1
- package/dist/cjs/lib/getPageFromOffset.js +8 -0
- package/dist/{lib → cjs/lib}/shallowEqual.d.ts +2 -1
- package/dist/cjs/lib/shallowEqual.js +24 -0
- package/dist/cjs/store/index.d.ts +14 -0
- package/dist/cjs/store/index.js +18 -0
- package/dist/cjs/store/localStorageStore.d.ts +5 -0
- package/dist/cjs/store/localStorageStore.js +33 -0
- package/dist/cjs/store/memoryStore.d.ts +5 -0
- package/dist/cjs/store/memoryStore.js +25 -0
- package/dist/cjs/store/reduxHelpers/createActions.d.ts +16 -0
- package/dist/cjs/store/reduxHelpers/createActions.js +16 -0
- package/dist/cjs/store/reduxHelpers/createReducer.d.ts +10 -0
- package/dist/cjs/store/reduxHelpers/createReducer.js +25 -0
- package/dist/cjs/store/reduxStore.d.ts +18 -0
- package/dist/cjs/store/reduxStore.js +63 -0
- package/dist/cjs/store/urlParamStore.d.ts +8 -0
- package/dist/cjs/store/urlParamStore.js +68 -0
- package/dist/cjs/useFilter.d.ts +106 -0
- package/dist/cjs/useFilter.js +251 -0
- package/dist/cjs/useSimpleFilter.d.ts +89 -0
- package/dist/cjs/useSimpleFilter.js +198 -0
- package/dist/esm/lib/buildDefaultFilterInfo.d.mts +6 -0
- package/dist/esm/lib/buildDefaultFilterInfo.mjs +27 -0
- package/dist/esm/lib/getOffsetFromPage.d.mts +4 -0
- package/dist/esm/lib/getOffsetFromPage.mjs +7 -0
- package/dist/esm/lib/getPageFromOffset.d.mts +4 -0
- package/dist/esm/lib/getPageFromOffset.mjs +7 -0
- package/dist/esm/lib/shallowEqual.d.mts +4 -0
- package/dist/esm/lib/shallowEqual.mjs +23 -0
- package/dist/esm/store/index.d.mts +14 -0
- package/dist/esm/store/index.mjs +15 -0
- package/dist/esm/store/localStorageStore.d.mts +6 -0
- package/dist/esm/store/localStorageStore.mjs +32 -0
- package/dist/esm/store/memoryStore.d.mts +6 -0
- package/dist/esm/store/memoryStore.mjs +24 -0
- package/dist/esm/store/reduxHelpers/createActions.d.mts +16 -0
- package/dist/esm/store/reduxHelpers/createActions.mjs +15 -0
- package/dist/esm/store/reduxHelpers/createReducer.d.mts +11 -0
- package/dist/esm/store/reduxHelpers/createReducer.mjs +24 -0
- package/dist/esm/store/reduxStore.d.mts +18 -0
- package/dist/esm/store/reduxStore.mjs +61 -0
- package/dist/esm/store/urlParamStore.d.mts +8 -0
- package/dist/esm/store/urlParamStore.mjs +63 -0
- package/dist/esm/useFilter.d.mts +106 -0
- package/dist/esm/useFilter.mjs +251 -0
- package/dist/esm/useSimpleFilter.d.mts +89 -0
- package/dist/esm/useSimpleFilter.mjs +198 -0
- package/package.json +33 -43
- package/dist/lib/buildDefaultFilterInfo.d.ts +0 -3
- package/dist/lib/buildDefaultFilterInfo.js +0 -35
- package/dist/lib/getOffsetFromPage.js +0 -6
- package/dist/lib/getPageFromOffset.js +0 -6
- package/dist/lib/shallowEqual.js +0 -23
- package/dist/store/index.d.ts +0 -10
- package/dist/store/index.js +0 -16
- package/dist/store/localStorageStore.d.ts +0 -3
- package/dist/store/localStorageStore.js +0 -31
- package/dist/store/memoryStore.d.ts +0 -3
- package/dist/store/memoryStore.js +0 -23
- package/dist/store/reduxHelpers/createActions.d.ts +0 -16
- package/dist/store/reduxHelpers/createActions.js +0 -27
- package/dist/store/reduxHelpers/createReducer.d.ts +0 -8
- package/dist/store/reduxHelpers/createReducer.js +0 -26
- package/dist/store/reduxStore.d.ts +0 -15
- package/dist/store/reduxStore.js +0 -67
- package/dist/store/urlParamStore.d.ts +0 -4
- package/dist/store/urlParamStore.js +0 -91
- package/dist/useFilter.d.ts +0 -103
- package/dist/useFilter.js +0 -254
- package/dist/useSimpleFilter.d.ts +0 -86
- package/dist/useSimpleFilter.js +0 -173
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
//#region \0rolldown/runtime.js
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
10
|
+
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
+
key = keys[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) {
|
|
13
|
+
__defProp(to, key, {
|
|
14
|
+
get: ((k) => from[k]).bind(null, key),
|
|
15
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return to;
|
|
21
|
+
};
|
|
22
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
23
|
+
value: mod,
|
|
24
|
+
enumerable: true
|
|
25
|
+
}) : target, mod));
|
|
26
|
+
|
|
27
|
+
//#endregion
|
|
28
|
+
|
|
29
|
+
exports.__toESM = __toESM;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
const require_lib_getOffsetFromPage = require('./getOffsetFromPage.js');
|
|
2
|
+
const require_lib_getPageFromOffset = require('./getPageFromOffset.js');
|
|
3
|
+
|
|
4
|
+
//#region src/lib/buildDefaultFilterInfo.ts
|
|
5
|
+
function buildDefaultFilterInfo(filterInfo = {}) {
|
|
6
|
+
const merged = {
|
|
7
|
+
filter: {},
|
|
8
|
+
sort: undefined,
|
|
9
|
+
pageSize: 20,
|
|
10
|
+
lastRefreshAt: new Date().getTime(),
|
|
11
|
+
totalResults: 1,
|
|
12
|
+
totalPages: 1,
|
|
13
|
+
offset: 0,
|
|
14
|
+
page: 1,
|
|
15
|
+
shouldRunImmediately: false,
|
|
16
|
+
...filterInfo
|
|
17
|
+
};
|
|
18
|
+
if (filterInfo.page != null && filterInfo.offset == null) {
|
|
19
|
+
merged.offset = require_lib_getOffsetFromPage(filterInfo.page, merged.pageSize);
|
|
20
|
+
} else if (filterInfo.page == null && filterInfo.offset != null) {
|
|
21
|
+
merged.page = require_lib_getPageFromOffset(filterInfo.offset, merged.pageSize);
|
|
22
|
+
}
|
|
23
|
+
return merged;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
//#endregion
|
|
27
|
+
module.exports = buildDefaultFilterInfo;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/lib/shallowEqual.ts
|
|
3
|
+
const shallowEqual = (objA, objB) => {
|
|
4
|
+
if (Object.is(objA, objB)) {
|
|
5
|
+
return true;
|
|
6
|
+
}
|
|
7
|
+
if (typeof objA !== "object" || !objA || typeof objB !== "object" || !objB) {
|
|
8
|
+
return false;
|
|
9
|
+
}
|
|
10
|
+
const keysA = Object.keys(objA);
|
|
11
|
+
const keysB = Object.keys(objB);
|
|
12
|
+
if (keysA.length !== keysB.length) {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
for (const key of keysA) {
|
|
16
|
+
if (!Object.is(objA[key], objB[key])) {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return true;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
//#endregion
|
|
24
|
+
module.exports = shallowEqual;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { FilterInfo } from "../useFilter.js";
|
|
2
|
+
|
|
3
|
+
//#region src/store/index.d.ts
|
|
4
|
+
interface FilterStore {
|
|
5
|
+
getFilter<TFilter extends Record<string, unknown>>(namespace: string): FilterInfo<TFilter> | null | undefined;
|
|
6
|
+
saveFilter<TFilter extends Record<string, unknown>>(namespace: string, filter: FilterInfo<TFilter>): void;
|
|
7
|
+
getData<TResult = any>(namespace: string): TResult | null | undefined;
|
|
8
|
+
saveData<TResult = any>(namespace: string, data: TResult): void;
|
|
9
|
+
clear(): void;
|
|
10
|
+
}
|
|
11
|
+
declare function setFilterStore(newStore: FilterStore): void;
|
|
12
|
+
declare function getFilterStore(optionalStore?: FilterStore): FilterStore;
|
|
13
|
+
//#endregion
|
|
14
|
+
export { FilterStore, getFilterStore, setFilterStore };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
|
|
3
|
+
//#region src/store/index.ts
|
|
4
|
+
let globalStore;
|
|
5
|
+
function setFilterStore(newStore) {
|
|
6
|
+
globalStore = newStore;
|
|
7
|
+
}
|
|
8
|
+
function getFilterStore(optionalStore) {
|
|
9
|
+
const resolvedStore = optionalStore || globalStore;
|
|
10
|
+
if (!resolvedStore) {
|
|
11
|
+
throw new Error("A store must be set with setFilterStore");
|
|
12
|
+
}
|
|
13
|
+
return resolvedStore;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
//#endregion
|
|
17
|
+
exports.getFilterStore = getFilterStore;
|
|
18
|
+
exports.setFilterStore = setFilterStore;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/store/localStorageStore.ts
|
|
3
|
+
const prefix = "useFilter";
|
|
4
|
+
const localStorageStore = {
|
|
5
|
+
getFilter(namespace) {
|
|
6
|
+
const item = localStorage.getItem(`${prefix}/${namespace}/filter`);
|
|
7
|
+
if (item) {
|
|
8
|
+
return JSON.parse(item);
|
|
9
|
+
}
|
|
10
|
+
},
|
|
11
|
+
saveFilter(namespace, filter) {
|
|
12
|
+
localStorage.setItem(`${prefix}/${namespace}/filter`, JSON.stringify(filter));
|
|
13
|
+
},
|
|
14
|
+
getData(namespace) {
|
|
15
|
+
const item = localStorage.getItem(`${prefix}/${namespace}/data`);
|
|
16
|
+
if (item) {
|
|
17
|
+
return JSON.parse(item);
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
saveData(namespace, data) {
|
|
21
|
+
localStorage.setItem(`${prefix}/${namespace}/data`, JSON.stringify(data));
|
|
22
|
+
},
|
|
23
|
+
clear() {
|
|
24
|
+
for (const key in localStorage) {
|
|
25
|
+
if (key.startsWith(prefix)) {
|
|
26
|
+
localStorage.removeItem(key);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
//#endregion
|
|
33
|
+
module.exports = localStorageStore;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/store/memoryStore.ts
|
|
3
|
+
let cachedFilters = {};
|
|
4
|
+
let cachedData = {};
|
|
5
|
+
const memoryStore = {
|
|
6
|
+
getFilter(namespace) {
|
|
7
|
+
return cachedFilters[namespace];
|
|
8
|
+
},
|
|
9
|
+
saveFilter(namespace, filter) {
|
|
10
|
+
cachedFilters[namespace] = filter;
|
|
11
|
+
},
|
|
12
|
+
getData(namespace) {
|
|
13
|
+
return cachedData[namespace];
|
|
14
|
+
},
|
|
15
|
+
saveData(namespace, data) {
|
|
16
|
+
cachedData[namespace] = data;
|
|
17
|
+
},
|
|
18
|
+
clear() {
|
|
19
|
+
cachedFilters = {};
|
|
20
|
+
cachedData = {};
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
//#endregion
|
|
25
|
+
module.exports = memoryStore;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
//#region src/store/reduxHelpers/createActions.d.ts
|
|
2
|
+
interface CreateActions {
|
|
3
|
+
<Namespace extends string, PayloadCreatorMap extends ReduxPayloadCreatorMap>(namespace: Namespace, actions: PayloadCreatorMap): ReduxActionCreatorMap<Namespace, PayloadCreatorMap>;
|
|
4
|
+
}
|
|
5
|
+
type ReduxPayloadCreatorMap = Record<string, (...args: any[]) => any>;
|
|
6
|
+
type ReduxActionCreatorMap<Namespace extends string, PayloadCreatorMap extends ReduxPayloadCreatorMap> = { [Type in keyof PayloadCreatorMap]: Type extends string ? ReduxActionCreator<Namespace, Type, PayloadCreatorMap[Type]> : undefined };
|
|
7
|
+
interface ReduxActionCreator<Namespace extends string = string, Type extends string = string, Fn extends (...args: any[]) => any = (...args: any[]) => any> {
|
|
8
|
+
(...args: Parameters<Fn>): {
|
|
9
|
+
type: `${Namespace}/${Type}`;
|
|
10
|
+
payload: ReturnType<Fn>;
|
|
11
|
+
};
|
|
12
|
+
actionType: `${Namespace}/${Type}`;
|
|
13
|
+
}
|
|
14
|
+
declare const createActions: CreateActions;
|
|
15
|
+
//#endregion
|
|
16
|
+
export { ReduxActionCreator, ReduxActionCreatorMap, createActions as default };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/store/reduxHelpers/createActions.ts
|
|
3
|
+
const createActions = (namespace, payloadCreatorMap) => {
|
|
4
|
+
const actionCreators = {};
|
|
5
|
+
for (const key in payloadCreatorMap) {
|
|
6
|
+
actionCreators[key] = ((...args) => ({
|
|
7
|
+
type: `${namespace}/${key}`,
|
|
8
|
+
payload: payloadCreatorMap[key](...args)
|
|
9
|
+
}));
|
|
10
|
+
actionCreators[key].actionType = `${namespace}/${key}`;
|
|
11
|
+
}
|
|
12
|
+
return actionCreators;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
//#endregion
|
|
16
|
+
module.exports = createActions;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ReduxActionCreator } from "./createActions.js";
|
|
2
|
+
import { Action } from "redux";
|
|
3
|
+
|
|
4
|
+
//#region src/store/reduxHelpers/createReducer.d.ts
|
|
5
|
+
interface ReduxReducerBuilder<State> {
|
|
6
|
+
addHandler: <ActionCreator extends ReduxActionCreator>(actionCreator: ActionCreator, handler: (state: State, action: ReturnType<ActionCreator>) => State) => ReduxReducerBuilder<State>;
|
|
7
|
+
addCase: <HandlerAction extends Action>(actionType: string, handler: (state: State, action: HandlerAction) => State) => ReduxReducerBuilder<State>;
|
|
8
|
+
}
|
|
9
|
+
declare function createReducer<State>(initialState: State, builderFn: (builder: ReduxReducerBuilder<State>) => void): (state: State | undefined, action: Action) => State;
|
|
10
|
+
export = createReducer;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/store/reduxHelpers/createReducer.ts
|
|
3
|
+
function createReducer(initialState, builderFn) {
|
|
4
|
+
const handlers = {};
|
|
5
|
+
const builder = {
|
|
6
|
+
addHandler: (actionCreator, handler) => {
|
|
7
|
+
handlers[actionCreator.actionType] = handler;
|
|
8
|
+
return builder;
|
|
9
|
+
},
|
|
10
|
+
addCase: (actionType, handler) => {
|
|
11
|
+
handlers[actionType] = handler;
|
|
12
|
+
return builder;
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
builderFn(builder);
|
|
16
|
+
return (state = initialState, action) => {
|
|
17
|
+
if (handlers[action.type]) {
|
|
18
|
+
return handlers[action.type](state, action);
|
|
19
|
+
}
|
|
20
|
+
return state;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
//#endregion
|
|
25
|
+
module.exports = createReducer;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { FilterStore } from "./index.js";
|
|
2
|
+
import { FilterInfo } from "../useFilter.js";
|
|
3
|
+
import { Store } from "redux";
|
|
4
|
+
|
|
5
|
+
//#region src/store/reduxStore.d.ts
|
|
6
|
+
interface CreateReduxStoreConfig {
|
|
7
|
+
store: Store<{
|
|
8
|
+
useFilter: UseFilterReduxState;
|
|
9
|
+
}>;
|
|
10
|
+
}
|
|
11
|
+
declare function createReduxStore(config: CreateReduxStoreConfig): FilterStore;
|
|
12
|
+
interface UseFilterReduxState {
|
|
13
|
+
data: Record<string, any>;
|
|
14
|
+
filters: Record<string, FilterInfo<any>>;
|
|
15
|
+
}
|
|
16
|
+
declare const useFilterReduxReducer: (state: UseFilterReduxState | undefined, action: import("redux").Action) => UseFilterReduxState;
|
|
17
|
+
//#endregion
|
|
18
|
+
export { createReduxStore, useFilterReduxReducer };
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
const require_store_reduxHelpers_createActions = require('./reduxHelpers/createActions.js');
|
|
3
|
+
const require_store_reduxHelpers_createReducer = require('./reduxHelpers/createReducer.js');
|
|
4
|
+
|
|
5
|
+
//#region src/store/reduxStore.ts
|
|
6
|
+
function createReduxStore(config) {
|
|
7
|
+
const reduxStoreStore = {
|
|
8
|
+
getFilter: (namespace) => {
|
|
9
|
+
return config.store.getState().useFilter.filters[namespace];
|
|
10
|
+
},
|
|
11
|
+
saveFilter: (namespace, filter) => {
|
|
12
|
+
config.store.dispatch(actions.saveFilter(namespace, filter));
|
|
13
|
+
},
|
|
14
|
+
getData: (namespace) => {
|
|
15
|
+
return config.store.getState().useFilter.data[namespace];
|
|
16
|
+
},
|
|
17
|
+
saveData: (namespace, data) => {
|
|
18
|
+
config.store.dispatch(actions.saveData(namespace, data));
|
|
19
|
+
},
|
|
20
|
+
clear: () => {
|
|
21
|
+
config.store.dispatch(actions.clear());
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
return reduxStoreStore;
|
|
25
|
+
}
|
|
26
|
+
const actions = require_store_reduxHelpers_createActions("useFilter", {
|
|
27
|
+
saveData: (namespace, data) => ({
|
|
28
|
+
namespace,
|
|
29
|
+
data
|
|
30
|
+
}),
|
|
31
|
+
saveFilter: (namespace, filter) => ({
|
|
32
|
+
namespace,
|
|
33
|
+
filter
|
|
34
|
+
}),
|
|
35
|
+
clear: () => undefined
|
|
36
|
+
});
|
|
37
|
+
const initialState = {
|
|
38
|
+
data: {},
|
|
39
|
+
filters: {}
|
|
40
|
+
};
|
|
41
|
+
const useFilterReduxReducer = require_store_reduxHelpers_createReducer(initialState, (builder) => {
|
|
42
|
+
builder.addHandler(actions.saveFilter, (state, action) => ({
|
|
43
|
+
...state,
|
|
44
|
+
filters: {
|
|
45
|
+
...state.filters,
|
|
46
|
+
[action.payload.namespace]: action.payload.filter
|
|
47
|
+
}
|
|
48
|
+
})).addHandler(actions.saveData, (state, action) => ({
|
|
49
|
+
...state,
|
|
50
|
+
data: {
|
|
51
|
+
...state.data,
|
|
52
|
+
[action.payload.namespace]: action.payload.data
|
|
53
|
+
}
|
|
54
|
+
})).addHandler(actions.clear, (state) => ({
|
|
55
|
+
...state,
|
|
56
|
+
filters: {},
|
|
57
|
+
data: {}
|
|
58
|
+
}));
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
//#endregion
|
|
62
|
+
exports.createReduxStore = createReduxStore;
|
|
63
|
+
exports.useFilterReduxReducer = useFilterReduxReducer;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { FilterStore } from "./index.js";
|
|
2
|
+
|
|
3
|
+
//#region src/store/urlParamStore.d.ts
|
|
4
|
+
declare const createUrlParamStore: () => FilterStore;
|
|
5
|
+
declare function replaceQueryParams(newParams: string): void;
|
|
6
|
+
declare function naivelyParseExistingParams(): any;
|
|
7
|
+
//#endregion
|
|
8
|
+
export { createUrlParamStore, naivelyParseExistingParams, replaceQueryParams };
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
const require_runtime = require('../_virtual/_rolldown/runtime.js');
|
|
3
|
+
let qs = require("qs");
|
|
4
|
+
qs = require_runtime.__toESM(qs);
|
|
5
|
+
|
|
6
|
+
//#region src/store/urlParamStore.ts
|
|
7
|
+
const createUrlParamStore = () => {
|
|
8
|
+
const urlParamStore = {
|
|
9
|
+
getFilter(namespace) {
|
|
10
|
+
const parsed = naivelyParseExistingParams();
|
|
11
|
+
const parsedInfo = parsed[`info.${namespace}`];
|
|
12
|
+
const parsedFilter = parsed[`filter.${namespace}`];
|
|
13
|
+
if (parsedInfo || parsedFilter) {
|
|
14
|
+
const { page, offset, pageSize, ...rest } = parsedInfo || {};
|
|
15
|
+
return {
|
|
16
|
+
...{
|
|
17
|
+
page: 1 / page ? +page : undefined,
|
|
18
|
+
pageSize: 1 / pageSize ? +pageSize : undefined,
|
|
19
|
+
offset: 1 / offset ? +offset : undefined,
|
|
20
|
+
...rest
|
|
21
|
+
},
|
|
22
|
+
filter: parsedFilter
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
saveFilter(namespace, filterInfo) {
|
|
27
|
+
const parsed = naivelyParseExistingParams();
|
|
28
|
+
const clonedInfo = { ...filterInfo };
|
|
29
|
+
const clonedFilter = filterInfo.filter;
|
|
30
|
+
delete clonedInfo.filter;
|
|
31
|
+
delete clonedInfo.lastRefreshAt;
|
|
32
|
+
delete clonedInfo.shouldRunImmediately;
|
|
33
|
+
delete clonedInfo.totalResults;
|
|
34
|
+
delete clonedInfo.totalPages;
|
|
35
|
+
delete clonedInfo.nextCursor;
|
|
36
|
+
parsed[`filter.${namespace}`] = clonedFilter;
|
|
37
|
+
parsed[`info.${namespace}`] = clonedInfo;
|
|
38
|
+
replaceQueryParams(qs.default.stringify(parsed));
|
|
39
|
+
},
|
|
40
|
+
clear() {
|
|
41
|
+
const parsed = naivelyParseExistingParams();
|
|
42
|
+
for (const key in parsed) {
|
|
43
|
+
if (key.startsWith("filter.") || key.startsWith("info.")) {
|
|
44
|
+
delete parsed[key];
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
replaceQueryParams(qs.default.stringify(parsed));
|
|
48
|
+
},
|
|
49
|
+
getData() {
|
|
50
|
+
return undefined;
|
|
51
|
+
},
|
|
52
|
+
saveData() {}
|
|
53
|
+
};
|
|
54
|
+
return urlParamStore;
|
|
55
|
+
};
|
|
56
|
+
function replaceQueryParams(newParams) {
|
|
57
|
+
const nextUrl = new URL(window.location.toString());
|
|
58
|
+
nextUrl.search = newParams;
|
|
59
|
+
window.history.replaceState(undefined, "", nextUrl.toString());
|
|
60
|
+
}
|
|
61
|
+
function naivelyParseExistingParams() {
|
|
62
|
+
return qs.default.parse(window.location.search.substring(1));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
//#endregion
|
|
66
|
+
exports.createUrlParamStore = createUrlParamStore;
|
|
67
|
+
exports.naivelyParseExistingParams = naivelyParseExistingParams;
|
|
68
|
+
exports.replaceQueryParams = replaceQueryParams;
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { FilterStore } from "./store/index.js";
|
|
2
|
+
|
|
3
|
+
//#region src/useFilter.d.ts
|
|
4
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
5
|
+
interface UseFilterOptions<TFilter extends Record<string, unknown>, TResult> {
|
|
6
|
+
/**
|
|
7
|
+
* Default values for your filter. When calling `.reset` your filter will be
|
|
8
|
+
* set to this.
|
|
9
|
+
* Changing these values does not cause a call to happen.
|
|
10
|
+
*/
|
|
11
|
+
defaultFilterInfo?: Partial<FilterInfo<TFilter>>;
|
|
12
|
+
/**
|
|
13
|
+
* Enable this if you are not using the data caching of this hook.
|
|
14
|
+
*/
|
|
15
|
+
shouldForceRunOnMount?: boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Called whenever the filter changes.
|
|
18
|
+
*/
|
|
19
|
+
onChange: (filterInfo: FilterInfo<TFilter>, reason: UseFilterUpdateReason) => MaybePromise<UseFilterOnChangeResult<TFilter, TResult>>;
|
|
20
|
+
/**
|
|
21
|
+
* In case you want to change the default debounce duration.
|
|
22
|
+
*/
|
|
23
|
+
debounceDuration?: number;
|
|
24
|
+
onBeforeSaveFilter?: (filterInfo: FilterInfo<TFilter>) => FilterInfo<TFilter>;
|
|
25
|
+
store?: FilterStore;
|
|
26
|
+
}
|
|
27
|
+
interface FilterApi<TFilter extends Record<string, unknown>, TResult> {
|
|
28
|
+
/**
|
|
29
|
+
* Whether the system is debouncing or waiting for your `onChange` to finish.
|
|
30
|
+
*/
|
|
31
|
+
isLoading: boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Any cached data for this filter.
|
|
34
|
+
*/
|
|
35
|
+
data: TResult | null | undefined;
|
|
36
|
+
/**
|
|
37
|
+
* Access to the filter and its metadata.
|
|
38
|
+
*/
|
|
39
|
+
filterInfo: FilterInfo<TFilter>;
|
|
40
|
+
/**
|
|
41
|
+
* Whether the filter existed in the system when the component mounted.
|
|
42
|
+
* @deprecated
|
|
43
|
+
*/
|
|
44
|
+
doesFilterExist: boolean;
|
|
45
|
+
/**
|
|
46
|
+
* Why the last update happened.
|
|
47
|
+
*/
|
|
48
|
+
updateReason: UseFilterUpdateReason;
|
|
49
|
+
/**
|
|
50
|
+
* Update one or more values in your filter.
|
|
51
|
+
*/
|
|
52
|
+
updateFilter: (filter: Partial<TFilter>, shouldRunImmediately?: boolean) => void;
|
|
53
|
+
/**
|
|
54
|
+
* Resets the filter back to `defaultFilterInfo`.
|
|
55
|
+
*/
|
|
56
|
+
resetFilter: (shouldRunImmediately?: boolean) => void;
|
|
57
|
+
/**
|
|
58
|
+
* Changes the offset. Will update `page`.
|
|
59
|
+
*/
|
|
60
|
+
setOffset: (offset: number | string, shouldRunImmediately?: boolean) => void;
|
|
61
|
+
/**
|
|
62
|
+
* Changes the page. Will update `offset`.
|
|
63
|
+
*/
|
|
64
|
+
setPage: (page: number | string, shouldRunImmediately?: boolean) => void;
|
|
65
|
+
/**
|
|
66
|
+
* Changes the page size.
|
|
67
|
+
*/
|
|
68
|
+
setPageSize: (pageSize: number | string, shouldRunImmediately?: boolean) => void;
|
|
69
|
+
/**
|
|
70
|
+
* Change the sort method.
|
|
71
|
+
*/
|
|
72
|
+
setSort: (sort: string | undefined, shouldRunImmediately?: boolean) => void;
|
|
73
|
+
/**
|
|
74
|
+
* Changes the cursor.
|
|
75
|
+
*/
|
|
76
|
+
setCursor: (cursor: string | null | undefined, shouldRunImmediately?: boolean) => void;
|
|
77
|
+
/**
|
|
78
|
+
* Forces a refresh of the filter.
|
|
79
|
+
*/
|
|
80
|
+
forceRefresh: (shouldRunImmediately?: boolean) => void;
|
|
81
|
+
}
|
|
82
|
+
type UseFilterUpdateReason = 'initial' | 'filter' | 'pagination';
|
|
83
|
+
declare function useFilter<TFilter extends Record<string, unknown>, TResult>(namespace: string, options: UseFilterOptions<TFilter, TResult>): FilterApi<TFilter, TResult>;
|
|
84
|
+
interface FilterInfo<TFilter extends Record<string, unknown>> {
|
|
85
|
+
filter: TFilter;
|
|
86
|
+
offset: number;
|
|
87
|
+
page: number;
|
|
88
|
+
sort?: string;
|
|
89
|
+
pageSize: number;
|
|
90
|
+
lastRefreshAt: number;
|
|
91
|
+
totalResults: number;
|
|
92
|
+
totalPages: number;
|
|
93
|
+
shouldRunImmediately: boolean;
|
|
94
|
+
cursor?: string | null;
|
|
95
|
+
nextCursor?: string | null;
|
|
96
|
+
}
|
|
97
|
+
type UseFilterOnChangeResult<TFilter extends Record<string, unknown>, TResult> = void | {
|
|
98
|
+
filterInfo?: Partial<Omit<FilterInfo<TFilter>, 'totalResults' | 'offset' | 'pageSize'> & {
|
|
99
|
+
totalResults: string | number;
|
|
100
|
+
offset: string | number;
|
|
101
|
+
pageSize: string | number;
|
|
102
|
+
}>;
|
|
103
|
+
data?: TResult;
|
|
104
|
+
};
|
|
105
|
+
//#endregion
|
|
106
|
+
export { FilterApi, FilterInfo, UseFilterUpdateReason, useFilter as default };
|