2020-04-30 05:54:30 +00:00
|
|
|
import history from 'history/browser';
|
2017-05-07 20:19:15 +00:00
|
|
|
import UrlPattern from 'url-pattern';
|
|
|
|
|
|
|
|
export const LOCATION_CHANGED = 'ROUTER_LOCATION_CHANGED';
|
|
|
|
export const PUSH = 'ROUTER_PUSH';
|
|
|
|
export const REPLACE = 'ROUTER_REPLACE';
|
|
|
|
|
|
|
|
export function locationChanged(route, params, location) {
|
2020-05-08 08:12:21 +00:00
|
|
|
Object.keys(params).forEach(key => {
|
|
|
|
params[key] = decodeURIComponent(params[key]);
|
|
|
|
});
|
|
|
|
|
|
|
|
const query = {};
|
|
|
|
new URLSearchParams(location.search).forEach((value, key) => {
|
|
|
|
query[key] = value;
|
|
|
|
});
|
|
|
|
|
2017-05-07 20:19:15 +00:00
|
|
|
return {
|
|
|
|
type: LOCATION_CHANGED,
|
|
|
|
route,
|
|
|
|
params,
|
2020-05-08 08:12:21 +00:00
|
|
|
query,
|
|
|
|
path: decodeURIComponent(location.pathname)
|
2017-05-07 20:19:15 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function push(path) {
|
|
|
|
return {
|
|
|
|
type: PUSH,
|
|
|
|
path
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function replace(path) {
|
|
|
|
return {
|
|
|
|
type: REPLACE,
|
|
|
|
path
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-05-08 08:12:21 +00:00
|
|
|
export function routeReducer(state = {}, { type, ...action }) {
|
|
|
|
if (type === LOCATION_CHANGED) {
|
|
|
|
return action;
|
2017-05-07 20:19:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function routeMiddleware() {
|
|
|
|
return next => action => {
|
|
|
|
switch (action.type) {
|
|
|
|
case PUSH:
|
2020-05-08 08:12:21 +00:00
|
|
|
history.push(`${action.path}`);
|
2017-05-07 20:19:15 +00:00
|
|
|
break;
|
|
|
|
case REPLACE:
|
|
|
|
history.replace(action.path);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return next(action);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function match(routes, location) {
|
|
|
|
for (let i = 0; i < routes.length; i++) {
|
2020-05-08 08:12:21 +00:00
|
|
|
const params = routes[i].pattern.match(location.pathname);
|
2017-05-07 20:19:15 +00:00
|
|
|
if (params !== null) {
|
2020-05-08 08:12:21 +00:00
|
|
|
return locationChanged(routes[i].name, params, location);
|
2017-05-07 20:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function initRouter(routes, store) {
|
|
|
|
const patterns = [];
|
|
|
|
const opts = {
|
2017-05-21 20:59:35 +00:00
|
|
|
segmentValueCharset: 'a-zA-Z0-9-_.%'
|
2017-05-07 20:19:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Object.keys(routes).forEach(name =>
|
|
|
|
patterns.push({
|
|
|
|
name,
|
|
|
|
pattern: new UrlPattern(routes[name], opts)
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2017-05-21 06:29:59 +00:00
|
|
|
let matched = match(patterns, history.location);
|
2017-05-07 20:19:15 +00:00
|
|
|
if (matched) {
|
|
|
|
store.dispatch(matched);
|
|
|
|
}
|
|
|
|
|
2020-04-30 05:54:30 +00:00
|
|
|
history.listen(({ location }) => {
|
2017-05-21 06:29:59 +00:00
|
|
|
const nextMatch = match(patterns, location);
|
2020-05-08 08:12:21 +00:00
|
|
|
if (nextMatch && nextMatch.path !== matched?.path) {
|
2017-05-07 20:19:15 +00:00
|
|
|
matched = nextMatch;
|
|
|
|
store.dispatch(matched);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|