consul/ui-v2/app/routes/dc/kv/index.js
John Cowen 14aa90e309
ui: Move repo services to repository/ folder and standardize naming (#4694)
Repositories are a class of services to help with CRUD actions, most of
the functionality is reused across various Models. This creates a new
repository service that centralizes all this reused functionality.
Inheritance via ember `Service.extend` is used as opposed to
decorating via Mixins.

1. Move all repository services (and their tests) to a
services/repository folder
2. Standardize on a singular name format 'node vs nodes'
3. Create a new 'repository' service to centralize functionality. This
should be extended by 'repository' services
2018-10-26 17:36:15 +01:00

62 lines
1.7 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 isFolder from 'consul-ui/utils/isFolder';
import WithKvActions from 'consul-ui/mixins/kv/with-actions';
export default Route.extend(WithKvActions, {
queryParams: {
s: {
as: 'filter',
replace: true,
},
},
repo: service('repository/kv'),
beforeModel: function() {
// we are index or folder, so if the key doesn't have a trailing slash
// add one to force a fake findBySlug
const params = this.paramsFor(this.routeName);
const key = params.key || '/';
if (!isFolder(key)) {
return this.replaceWith(this.routeName, key + '/');
}
},
model: function(params) {
let key = params.key || '/';
const dc = this.modelFor('dc').dc.Name;
const repo = get(this, 'repo');
return hash({
isLoading: false,
parent: repo.findBySlug(key, dc),
}).then(model => {
return hash({
...model,
...{
items: repo.findAllBySlug(get(model.parent, 'Key'), dc).catch(e => {
const status = get(e, 'errors.firstObject.status');
switch (status) {
case '403':
return this.transitionTo('dc.acls.tokens');
default:
return this.transitionTo('dc.kv.index');
}
}),
},
});
});
},
actions: {
error: function(e) {
if (e.errors && e.errors[0] && e.errors[0].status == '404') {
return this.transitionTo('dc.kv.index');
}
throw e;
},
},
setupController: function(controller, model) {
this._super(...arguments);
controller.setProperties(model);
},
});