open-nomad/ui/app/routes/application.js
Michael Lange 9f3a37f1c2 Simplify the control flow around changing namespaces and regions
The UI will no longer try to redirect to the appropriate namespace or
region if one is found in localStorage. Instead, it will assume that
the lack of query param means the default namespaces or region is
desired.
2018-08-09 18:22:39 -07:00

84 lines
2.2 KiB
JavaScript

import { inject as service } from '@ember/service';
import { next } from '@ember/runloop';
import Route from '@ember/routing/route';
import { AbortError } from 'ember-data/adapters/errors';
import RSVP from 'rsvp';
export default Route.extend({
config: service(),
system: service(),
store: service(),
queryParams: {
region: {
refreshModel: true,
},
},
resetController(controller, isExiting) {
if (isExiting) {
controller.set('error', null);
}
},
beforeModel(transition) {
return RSVP.all([this.get('system.regions'), this.get('system.defaultRegion')]).then(
promises => {
if (!this.get('system.shouldShowRegions')) return promises;
const queryParam = transition.queryParams.region;
const defaultRegion = this.get('system.defaultRegion.region');
const currentRegion = this.get('system.activeRegion') || defaultRegion;
// Only reset the store if the region actually changed
if (
(queryParam && queryParam !== currentRegion) ||
(!queryParam && currentRegion !== defaultRegion)
) {
this.get('system').reset();
this.get('store').unloadAll();
}
this.set('system.activeRegion', queryParam || defaultRegion);
return promises;
}
);
},
// setupController doesn't refire when the model hook refires as part of
// a query param change
afterModel(model, transition) {
const queryParam = transition.queryParams.region;
const controller = this.controllerFor('application');
// The default region shouldn't show up as a query param since
// it's superfluous.
if (queryParam === this.get('system.defaultRegion.region')) {
next(() => {
controller.set('region', null);
});
}
return this._super(...arguments);
},
actions: {
didTransition() {
if (!this.get('config.isTest')) {
window.scrollTo(0, 0);
}
},
willTransition() {
this.controllerFor('application').set('error', null);
},
error(error) {
if (!(error instanceof AbortError)) {
this.controllerFor('application').set('error', error);
}
},
},
});