refactor: 💡 simplify host set list filtering
This commit is contained in:
3 files changed
+34
-33
No files matched your search
@@ -1,5 +1,6 @@
|
||||
import GeneratedHostSetModel from '../generated/models/host-set';
|
||||
import { fragmentArray } from 'ember-data-model-fragments/attributes';
|
||||
//import { computed } from '@ember/object';
|
||||
|
||||
export default class HostSetModel extends GeneratedHostSetModel {
|
||||
|
||||
@@ -11,6 +12,18 @@ export default class HostSetModel extends GeneratedHostSetModel {
|
||||
*/
|
||||
@fragmentArray('fragment-string', {readOnly: true}) host_ids;
|
||||
|
||||
// TODO this is here in case we want it in the future,
|
||||
// especially for the target / add host sets workflow
|
||||
/**
|
||||
* Returns the host catalog instance associated with this host set if
|
||||
* it is already loaded into the store.
|
||||
* @type {?HostCatalogModel}
|
||||
*/
|
||||
// @computed('host_catalog_id')
|
||||
// get hostCatalog() {
|
||||
// return this.store.peekRecord('host-catalog', this.host_catalog_id);
|
||||
// }
|
||||
|
||||
// =methods
|
||||
|
||||
/**
|
||||
|
||||
@@ -21,24 +21,22 @@
|
||||
</header.row>
|
||||
</table.header>
|
||||
<table.body as |body|>
|
||||
{{#each this.hostSetGroups as |hostSetGroup|}}
|
||||
{{#each hostSetGroup.hostSets as |hostSet|}}
|
||||
<body.row as |row|>
|
||||
<row.cell>
|
||||
<form.checkbox
|
||||
@label={{hostSet.id}}
|
||||
@description={{hostSet.description}}
|
||||
onChange={{fn this.toggleHostSet hostSet}} />
|
||||
</row.cell>
|
||||
<row.cell>{{hostSet.name}}</row.cell>
|
||||
<row.cell>
|
||||
<Rose::Badge @style="outline">{{hostSet.type}}</Rose::Badge>
|
||||
</row.cell>
|
||||
<row.cell>
|
||||
<code>{{hostSetGroup.hostCatalog.id}}</code>
|
||||
</row.cell>
|
||||
</body.row>
|
||||
{{/each}}
|
||||
{{#each this.filteredHostSets as |hostSet|}}
|
||||
<body.row as |row|>
|
||||
<row.cell>
|
||||
<form.checkbox
|
||||
@label={{hostSet.id}}
|
||||
@description={{hostSet.description}}
|
||||
onChange={{fn this.toggleHostSet hostSet}} />
|
||||
</row.cell>
|
||||
<row.cell>{{hostSet.name}}</row.cell>
|
||||
<row.cell>
|
||||
<Rose::Badge @style="outline">{{hostSet.type}}</Rose::Badge>
|
||||
</row.cell>
|
||||
<row.cell>
|
||||
<code>{{hostSet.host_catalog_id}}</code>
|
||||
</row.cell>
|
||||
</body.row>
|
||||
{{/each}}
|
||||
</table.body>
|
||||
</Rose::Table>
|
||||
|
||||
@@ -13,27 +13,17 @@ export default class FormTargetAddHostSetsComponent extends Component {
|
||||
selectedHostSetIDs = A();
|
||||
|
||||
/**
|
||||
* Groups host sets not already added to the target by host catalog.
|
||||
* @type {[{hostCatalog: HostCatalogModel, hostSets: [{HostSetModel}]}]}
|
||||
* Host sets not already added to the target.
|
||||
* @type {[HostSetModel]}
|
||||
*/
|
||||
@computed('args.{hostCatalogs.[],hostSets.[],model.host_sets.[]}')
|
||||
get hostSetGroups() {
|
||||
@computed('args.{hostSets.[],model.host_sets.[]}')
|
||||
get filteredHostSets() {
|
||||
// Get IDs for host sets already added to the current target
|
||||
const alreadyAddedHostSetIDs = this.args.model.host_sets
|
||||
.map(({ host_set_id }) => host_set_id);
|
||||
const notAddedHostSets = this.args.hostSets
|
||||
.filter(({ id }) => !alreadyAddedHostSetIDs.includes(id));
|
||||
// Group host sets by host catalog
|
||||
const hostSetGroupsObject = notAddedHostSets.reduce((obj, hostSet) => {
|
||||
if (!obj[hostSet.host_catalog_id]) {
|
||||
const hostCatalog = this.args.hostCatalogs.findBy('id', hostSet.host_catalog_id);
|
||||
obj[hostSet.host_catalog_id] = { hostCatalog, hostSets: [] };
|
||||
}
|
||||
obj[hostSet.host_catalog_id].hostSets.push(hostSet);
|
||||
return obj;
|
||||
}, {});
|
||||
const hostSetGroups = Object.values(hostSetGroupsObject);
|
||||
return hostSetGroups;
|
||||
return notAddedHostSets;
|
||||
}
|
||||
|
||||
// =actions
|
||||
|
||||
Reference in new issue
Block a user