open-consul/ui-v2/app/helpers/href-mut.js
John Cowen 40e632a955
ui: Enable recovery from an unreachable datacenter (500 error) (#7404)
For URL maintenance reasons we store the last visited DC in
localStorage incase you come back to a page (for example settings) that
doesn't have a dc in the URL.

A problem arises here if the last DC you tried to visit is unreachable.

The first fix here clears out the last visited DC from localStorage if
the API has errored out.

Secondly, our `href-mut` helper which mutates the current current and
replaces 'parts' in the URL rather than the whole thing functioned by
detecting the current route/URL you are on an 'mutating' that. A problem
arose here as even though you might be on the `/ui/dc-1/services` URL the
actual route is the 'error' route which does not have a URL that can be
changed properly.

The second fix here uses route.currentRoute.name over route.currentRouteName.

The latter is equal to error when an error occurs whereas the former gives you the name of the route before the error happened, which is actually what we want/the intent here.

ie. when `router.currentRouteName === 'error'` then
`router.currentRoute.name === Name Of Route Before It Errored` it seems
2020-03-09 09:10:47 +00:00

40 lines
1.4 KiB
JavaScript

import Helper from '@ember/component/helper';
import { inject as service } from '@ember/service';
import { hrefTo } from 'consul-ui/helpers/href-to';
const getRouteParams = function(route, params = {}) {
return route.paramNames.map(function(item) {
if (typeof params[item] !== 'undefined') {
return params[item];
}
return route.params[item];
});
};
export default Helper.extend({
router: service('router'),
compute([params], hash) {
let current = this.router.currentRoute;
let parent;
let atts = getRouteParams(current, params);
// walk up the entire route/s replacing any instances
// of the specified params with the values specified
while ((parent = current.parent)) {
atts = atts.concat(getRouteParams(parent, params));
current = parent;
}
let route = this.router.currentRoute.name;
// TODO: this is specific to consul/nspaces
// 'ideally' we could try and do this elsewhere
// not super important though.
// This will turn an URL that has no nspace (/ui/dc-1/nodes) into one
// that does have a namespace (/ui/~nspace/dc-1/nodes) if you href-mut with
// a nspace parameter
if (typeof params.nspace !== 'undefined' && route.startsWith('dc.')) {
route = `nspace.${route}`;
atts.push(params.nspace);
}
//
return hrefTo(this, this.router, [route, ...atts.reverse()], hash);
},
});