open-consul/ui-v2/app/routes/application.js
John Cowen 25f90fbcdd Make sure the dc menu is as useful as possible when things go wrong
1. Check the dc's model for both dcs list and the requested dc.
2. If the dc model doesn't exist use and empty array for dcs and a fake
dc with the Name 'Error' as we can't even trust what is in the
`paramsFor`
2018-06-22 17:23:00 +01:00

79 lines
2.2 KiB
JavaScript

import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
import { hash } from 'rsvp';
import { get } from '@ember/object';
import { next } from '@ember/runloop';
const $html = document.documentElement;
const removeLoading = function() {
return $html.classList.remove('ember-loading');
};
export default Route.extend({
init: function() {
this._super(...arguments);
},
repo: service('dc'),
actions: {
loading: function(transition, originRoute) {
let dc = null;
if (originRoute.routeName !== 'dc') {
const model = this.modelFor('dc') || { dcs: null, dc: { Name: null } };
dc = get(this, 'repo').getActive(model.dc.Name, model.dcs);
}
hash({
loading: !$html.classList.contains('ember-loading'),
dc: dc,
}).then(model => {
next(() => {
const controller = this.controllerFor('application');
controller.setProperties(model);
transition.promise.finally(function() {
removeLoading();
controller.setProperties({
loading: false,
dc: model.dc,
});
});
});
});
return true;
},
error: function(e, transition) {
let error = {
status: e.code || '',
message: e.message || e.detail || 'Error',
};
if (e.errors && e.errors[0]) {
error = e.errors[0];
error.message = error.title || error.detail || 'Error';
}
if (error.status === '') {
error.message = 'Error';
}
const model = this.modelFor('dc');
hash({
error: error,
dc:
error.status.toString().indexOf('5') !== 0
? get(this, 'repo').getActive()
: model && model.dc
? model.dc
: { Name: 'Error' },
dcs: model && model.dcs ? model.dcs : [],
})
.then(model => {
removeLoading();
next(() => {
this.controllerFor('error').setProperties(model);
});
})
.catch(e => {
removeLoading();
next(() => {
this.controllerFor('error').setProperties({ error: error });
});
});
return true;
},
},
});