Skip to content

Commit

Permalink
chore: DRY up navigation code
Browse files Browse the repository at this point in the history
  • Loading branch information
w33ble committed Sep 20, 2018
1 parent 46f6cb6 commit 2f3e050
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions x-pack/plugins/canvas/public/lib/router_provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ export function routerProvider(routes) {
return state || history.getLocation().state;
};

const updateLocation = (name, params, state, replace = false) => {
const currentState = getState(name, params, state);
const method = replace ? 'replace' : 'push';

// given a path, go there directly
if (isPath(name)) return history[method](currentState, name);
history[method](currentState, baseRouter.create(name, params));
};

// our router is an extended version of the imported router
// which mixes in history methods for navigation
router = {
Expand All @@ -36,16 +45,10 @@ export function routerProvider(routes) {
getPath: history.getPath,
getFullPath: history.getFullPath,
navigateTo(name, params, state) {
const currentState = getState(name, params, state);
// given a path, go there directly
if (isPath(name)) return history.push(currentState, name);
history.push(currentState, this.create(name, params));
updateLocation(name, params, state);
},
redirectTo(name, params, state) {
const currentState = getState(name, params, state);
// given a path, go there directly, assuming params is state
if (isPath(name)) return history.replace(currentState, name);
history.replace(currentState, this.create(name, params));
updateLocation(name, params, state, true);
},
onPathChange(fn) {
if (componentListener != null)
Expand Down

0 comments on commit 2f3e050

Please sign in to comment.