-
Notifications
You must be signed in to change notification settings - Fork 2
/
history.js
27 lines (21 loc) · 953 Bytes
/
history.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import { HistoryPop, HistoryPush } from "./web_modules/hyperapp-fx.js";
import qs from "./web_modules/qs.js";
import { update, updateWithChart } from "./update.js";
const settings = ({ report, ...rest }) => rest;
export const stateToUrl = state => "?" + qs.stringify(settings(state));
const isInteger = value => /^-{0,1}\d+$/.test(value);
const decoder = (s, defaultDecoder) =>
isInteger(s) ? Number(s) : defaultDecoder(s);
export const urlToState = url =>
qs.parse(url.substring(1), { arrayLimit: 1000, decoder });
const SetState = stateFromUrl => state => ({ ...state, ...stateFromUrl });
export const ReadStateFromUrl = [
dispatch => {
dispatch(SetState(qs.parse(urlToState(window.location.search))));
}
];
export const UpdateHistory = state =>
HistoryPush({ state: settings(state), url: stateToUrl(state) });
export const HistoryListen = HistoryPop({
action: (state, event) => updateWithChart({ ...state, ...event.state })
});