open-consul/ui-v2/app/routes/dc.js
John Cowen 6e8a8a2bc0
ui: Various amends for 1.7beta (#6965)
* Remove empty init

* Actually make the disco chain endpoint send the nspace, note:

The backend doesn't support this as yet.

* Tweak the font size of flash-messages ever so slightly

* Make sure the nspace menu is kept up to date when creating a new one

* Move comment to the correct place

* Only refresh the namespace menu if you specifically created a nspace

* Change FIXMEs to TODOs as we are happy for these to wait until later
2019-12-18 18:27:54 +00:00

91 lines
2.8 KiB
JavaScript

import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
import { hash } from 'rsvp';
import { get } from '@ember/object';
// TODO: We should potentially move all these nspace related things
// up a level to application.js
const findActiveNspace = function(nspaces, nspace) {
let found = nspaces.find(function(item) {
return item.Name === nspace.Name;
});
if (typeof found === 'undefined') {
// if we can't find the nspace that was saved
// try default
found = nspaces.find(function(item) {
return item.Name === 'default';
});
// if there is no default just choose the first
if (typeof found === 'undefined') {
found = nspaces.firstObject;
}
}
return found;
};
export default Route.extend({
repo: service('repository/dc'),
nspacesRepo: service('repository/nspace/disabled'),
settingsRepo: service('settings'),
model: function(params) {
const repo = this.repo;
const nspacesRepo = this.nspacesRepo;
const settingsRepo = this.settingsRepo;
return hash({
dcs: repo.findAll(),
nspaces: nspacesRepo.findAll(),
nspace: nspacesRepo.getActive(),
token: settingsRepo.findBySlug('token'),
})
.then(function(model) {
return hash({
...model,
...{
dc: repo.findBySlug(params.dc, model.dcs),
// if there is only 1 namespace then use that
// otherwise find the namespace object that corresponds
// to the active one
nspace:
model.nspaces.length > 1
? findActiveNspace(model.nspaces, model.nspace)
: model.nspaces.firstObject,
},
});
})
.then(function(model) {
if (get(model, 'token.SecretID')) {
return hash({
...model,
...{
// When disabled nspaces is [], so nspace is undefined
permissions: nspacesRepo.authorize(params.dc, get(model, 'nspace.Name')),
},
});
} else {
return model;
}
});
},
setupController: function(controller, model) {
controller.setProperties(model);
},
actions: {
// TODO: This will eventually be deprecated please see
// https://deprecations.emberjs.com/v3.x/#toc_deprecate-router-events
willTransition: function(transition) {
this._super(...arguments);
if (typeof transition !== 'undefined' && transition.from.name.endsWith('nspaces.create')) {
// Only when we create, reload the nspaces in the main menu to update them
// as we don't block for those
this.nspacesRepo.findAll().then(items => {
if (typeof this.controller !== 'undefined') {
this.controller.setProperties({
nspaces: items,
});
}
});
}
},
},
});