zustand-querystring 0.0.5 → 0.0.7
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/lib/middleware.js +13 -11
- package/package.json +1 -1
package/lib/middleware.js
CHANGED
|
@@ -47,9 +47,9 @@ const queryStringImpl = (fn, options) => (set, get, api) => {
|
|
|
47
47
|
};
|
|
48
48
|
const url = defaultedOptions.url;
|
|
49
49
|
const initialState = get() ?? fn(set, get, api);
|
|
50
|
-
const getSelectedState = state => {
|
|
50
|
+
const getSelectedState = (state, pathname) => {
|
|
51
51
|
if (defaultedOptions.select) {
|
|
52
|
-
const selection = defaultedOptions.select(
|
|
52
|
+
const selection = defaultedOptions.select(pathname);
|
|
53
53
|
// translate the selection to state
|
|
54
54
|
const selectedState = translateSelectionToState(selection, state);
|
|
55
55
|
return selectedState;
|
|
@@ -59,7 +59,9 @@ const queryStringImpl = (fn, options) => (set, get, api) => {
|
|
|
59
59
|
const initialize = (url, _set = set) => {
|
|
60
60
|
const fallback = () => fn(_set, get, api);
|
|
61
61
|
try {
|
|
62
|
-
const
|
|
62
|
+
const splitUrl = url.split('?');
|
|
63
|
+
const queryString = splitUrl[1];
|
|
64
|
+
const pathname = splitUrl[0];
|
|
63
65
|
if (!queryString) {
|
|
64
66
|
return fallback();
|
|
65
67
|
}
|
|
@@ -71,10 +73,10 @@ const queryStringImpl = (fn, options) => (set, get, api) => {
|
|
|
71
73
|
if (!toParse) {
|
|
72
74
|
return fallback();
|
|
73
75
|
}
|
|
74
|
-
console.log('toParse', toParse);
|
|
76
|
+
// console.log('toParse', toParse);
|
|
75
77
|
const parsed = parse(toParse);
|
|
76
78
|
const currentValue = get() ?? fn(_set, get, api);
|
|
77
|
-
const merged = mergeWith(currentValue, getSelectedState(parsed));
|
|
79
|
+
const merged = mergeWith(currentValue, getSelectedState(parsed, pathname));
|
|
78
80
|
set(merged, true);
|
|
79
81
|
return merged;
|
|
80
82
|
}
|
|
@@ -88,7 +90,7 @@ const queryStringImpl = (fn, options) => (set, get, api) => {
|
|
|
88
90
|
};
|
|
89
91
|
if (typeof window !== 'undefined') {
|
|
90
92
|
const setQuery = () => {
|
|
91
|
-
const selectedState = getSelectedState(get());
|
|
93
|
+
const selectedState = getSelectedState(get(), window.location.pathname);
|
|
92
94
|
if (!selectedState) {
|
|
93
95
|
return;
|
|
94
96
|
}
|
|
@@ -116,15 +118,15 @@ const queryStringImpl = (fn, options) => (set, get, api) => {
|
|
|
116
118
|
.map(key => `${key}=${variables[key]}`)
|
|
117
119
|
// join all pairs with &
|
|
118
120
|
.join('&');
|
|
119
|
-
console.log('newQueryString', newQueryString);
|
|
121
|
+
// console.log('newQueryString', newQueryString);
|
|
120
122
|
window.history.replaceState(null, '', newQueryString ? `?${newQueryString}` : window.location.pathname);
|
|
121
123
|
}
|
|
122
124
|
};
|
|
123
125
|
//TODO: find a better way to do this
|
|
124
|
-
let
|
|
126
|
+
let previousPathname = '';
|
|
125
127
|
setInterval(() => {
|
|
126
|
-
if (window.location.
|
|
127
|
-
|
|
128
|
+
if (window.location.pathname !== previousPathname) {
|
|
129
|
+
previousPathname = window.location.pathname;
|
|
128
130
|
setQuery();
|
|
129
131
|
}
|
|
130
132
|
}, 50);
|
|
@@ -133,7 +135,7 @@ const queryStringImpl = (fn, options) => (set, get, api) => {
|
|
|
133
135
|
originalSetState(...args);
|
|
134
136
|
setQuery();
|
|
135
137
|
};
|
|
136
|
-
return initialize(window.location.
|
|
138
|
+
return initialize(window.location.pathname + window.location.search, (...args) => {
|
|
137
139
|
set(...args);
|
|
138
140
|
setQuery();
|
|
139
141
|
});
|