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) {
|
|
|
|
return {
|
|
|
|
type: LOCATION_CHANGED,
|
|
|
|
route,
|
|
|
|
params,
|
|
|
|
location
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function push(path) {
|
|
|
|
return {
|
|
|
|
type: PUSH,
|
|
|
|
path
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function replace(path) {
|
|
|
|
return {
|
|
|
|
type: REPLACE,
|
|
|
|
path
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function routeReducer(state = {}, action) {
|
|
|
|
if (action.type === LOCATION_CHANGED) {
|
|
|
|
return {
|
|
|
|
route: action.route,
|
|
|
|
params: action.params,
|
|
|
|
location: action.location
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function routeMiddleware() {
|
|
|
|
return next => action => {
|
|
|
|
switch (action.type) {
|
|
|
|
case PUSH:
|
|
|
|
history.push(action.path);
|
|
|
|
break;
|
|
|
|
case REPLACE:
|
|
|
|
history.replace(action.path);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return next(action);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-05-21 06:29:59 +00:00
|
|
|
function decode(location) {
|
|
|
|
location.pathname = decodeURIComponent(location.pathname);
|
|
|
|
return location;
|
|
|
|
}
|
|
|
|
|
2017-05-07 20:19:15 +00:00
|
|
|
function match(routes, location) {
|
|
|
|
let params;
|
|
|
|
for (let i = 0; i < routes.length; i++) {
|
|
|
|
params = routes[i].pattern.match(location.pathname);
|
|
|
|
if (params !== null) {
|
2017-05-21 06:29:59 +00:00
|
|
|
const keys = Object.keys(params);
|
|
|
|
for (let j = 0; j < keys.length; j++) {
|
|
|
|
params[keys[j]] = decodeURIComponent(params[keys[j]]);
|
|
|
|
}
|
2020-04-30 11:44:36 +00:00
|
|
|
return locationChanged(routes[i].name, params, decode({ ...location }));
|
2017-05-07 20:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
} else {
|
|
|
|
matched = { location: {} };
|
|
|
|
}
|
|
|
|
|
2020-04-30 05:54:30 +00:00
|
|
|
history.listen(({ location }) => {
|
2017-05-21 06:29:59 +00:00
|
|
|
const nextMatch = match(patterns, location);
|
2018-04-05 23:46:22 +00:00
|
|
|
if (
|
|
|
|
nextMatch &&
|
|
|
|
nextMatch.location.pathname !== matched.location.pathname
|
|
|
|
) {
|
2017-05-07 20:19:15 +00:00
|
|
|
matched = nextMatch;
|
|
|
|
store.dispatch(matched);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|