open-consul/ui-v2/app/controllers/dc/intentions/index.js
John Cowen 651c0b2c33 ui: Moves intentions listing and form into components (#7549)
Whilst we tried to do this with the smallest amount of changes possible,
our acceptance tests for trying to submit a blank form started failing
due to usage of `destroyRecord`, its seems that the correct way to
achieve the same thing is to use `rollbackAttributes` instead. We
changed that here and the tests pass once again. Furture work related to
this will involve change the rest of the UI where we use `destroyRecord`
to achieve the same thing, to use `rollbackAttributes` instead
2020-05-12 17:14:21 +00:00

56 lines
1.6 KiB
JavaScript

import Controller from '@ember/controller';
import { computed, get } from '@ember/object';
import WithFiltering from 'consul-ui/mixins/with-filtering';
import WithSearching from 'consul-ui/mixins/with-searching';
import WithEventSource from 'consul-ui/mixins/with-event-source';
import ucfirst from 'consul-ui/utils/ucfirst';
// TODO: DRY out in acls at least
const createCounter = function(prop) {
return function(items, val) {
return val === '' ? get(items, 'length') : items.filterBy(prop, val).length;
};
};
const countAction = createCounter('Action');
export default Controller.extend(WithSearching, WithFiltering, WithEventSource, {
queryParams: {
currentFilter: {
as: 'action',
},
s: {
as: 'filter',
replace: true,
},
},
init: function() {
this.searchParams = {
intention: 's',
};
this._super(...arguments);
},
searchable: computed('filtered', function() {
return get(this, 'searchables.intention')
.add(get(this, 'filtered'))
.search(get(this, this.searchParams.intention));
}),
actionFilters: computed('items', function() {
const items = get(this, 'items');
return ['', 'allow', 'deny'].map(function(item) {
return {
label: `${item === '' ? 'All' : ucfirst(item)} (${countAction(
items,
item
).toLocaleString()})`,
value: item,
};
});
}),
filter: function(item, { s = '', currentFilter = '' }) {
return currentFilter === '' || get(item, 'Action') === currentFilter;
},
actions: {
route: function() {
this.send(...arguments);
},
},
});