feat: 🎸 add initial hosts selection UI for host sets

This commit is contained in:
Randall Morey committed 2020-08-21 11:26:55 -04:00
1 parent 4197fe483a
commit 8196de1dd3
11 files changed
+222 -20

No files matched your search

@@ -0,0 +1,51 @@
<Rose::Form
class="full-width"
@onSubmit={{@submit}}
@cancel={{@cancel}}
@disabled={{@model.isSaving}}
@error={{@model.errors.base}}
as |form|
>
<form.actions
@disabled={{if @model.cannotSave @model.cannotSave}}
@submitText={{t "actions.save"}}
@cancelText={{t "actions.cancel"}} />
{{!--
{{#if @model.errors.host_ids}}
<input.errors as |errors|>
{{#each @model.errors.host_ids as |error|}}
<errors.message>{{error.message}}</errors.message>
{{/each}}
</input.errors>
{{/if}}
--}}
<Rose::Table as |table|>
<table.header as |header|>
<header.row as |row|>
<row.headerCell>{{t 'form.label.id'}}</row.headerCell>
<row.headerCell>{{t 'form.label.name'}}</row.headerCell>
<row.headerCell>{{t 'form.label.address'}}</row.headerCell>
</header.row>
</table.header>
<table.body as |body|>
{{#each this.options as |option|}}
<body.row as |row|>
<row.headerCell>
<form.checkbox
@label={{option.host.id}}
@checked={{option.selected}}
onChange={{fn @toggleHost option.host}}
/>
<p>{{option.host.description}}</p>
</row.headerCell>
<row.cell>{{option.host.name}}</row.cell>
<row.cell>{{option.host.address}}</row.cell>
</body.row>
{{/each}}
</table.body>
</Rose::Table>
</Rose::Form>
@@ -0,0 +1,23 @@
import Component from '@glimmer/component';
import { computed } from '@ember/object';
export default class FormHostSetHostsComponent extends Component {
// =attributes
/**
* An array of wrapped `HostModel` instances where the wrapper includes
* a `selected` boolean field indicating if the host is represented
* in `model.host_ids`.
* @type {[{host: HostModel, selected: boolean}]}
*/
@computed('args.{hosts.[],model.host_ids.[]}')
get options() {
const { hosts, model: hostSet } = this.args;
return hosts.map(host => ({
host,
selected: Boolean(hostSet.host_ids.findBy('value', host.id))
}));
}
}
+3 -1
View File
@@ -24,7 +24,9 @@ Router.map(function () {
this.route('new');
this.route('host-catalog', { path: ':host_catalog_id' }, function() {
this.route('host-sets', function() {
this.route('host-set', { path: ':host_set_id' }, function() {});
this.route('host-set', { path: ':host_set_id' }, function() {
this.route('hosts');
});
this.route('new');
});
});
@@ -8,6 +8,19 @@ export default class ScopesScopeProjectsProjectHostCatalogsHostCatalogHostSetsRo
@service intl;
@service notify;
// =attributes
/**
* Configure adapter options using current host catalog and
* the current project scope.
* @return {object}
*/
get adapterOptions() {
const scopeID = this.modelFor('scopes.scope.projects.project').id;
const hostCatalogID = this.modelFor('scopes.scope.projects.project.host-catalogs.host-catalog').id;
return { adapterOptions : { scopeID, hostCatalogID } };
}
// =methods
/**
@@ -15,9 +28,7 @@ export default class ScopesScopeProjectsProjectHostCatalogsHostCatalogHostSetsRo
* @return {Promise{[HostSetModel]}}
*/
async model() {
const scopeID = this.modelFor('scopes.scope.projects.project').id;
const hostCatalogID = this.modelFor('scopes.scope.projects.project.host-catalogs.host-catalog').id;
return this.store.findAll('host-set', { adapterOptions: { scopeID, hostCatalogID } });
return this.store.findAll('host-set', this.adapterOptions);
}
// =actions
@@ -42,8 +53,7 @@ export default class ScopesScopeProjectsProjectHostCatalogsHostCatalogHostSetsRo
async save(hostSet) {
try {
const { isNew } = hostSet;
await hostSet.save(this.adapterOptions());
this.refresh();
await hostSet.save(this.adapterOptions);
this.notify.success(
this.intl.t(isNew ? 'notify.create-success' : 'notify.save-success')
);
@@ -61,8 +71,7 @@ export default class ScopesScopeProjectsProjectHostCatalogsHostCatalogHostSetsRo
@action
async delete(hostSet) {
try {
await hostSet.destroyRecord(this.adapterOptions());
this.refresh();
await hostSet.destroyRecord(this.adapterOptions);
this.notify.success(this.intl.t('notify.delete-success'));
this.transitionTo('scopes.scope.projects.project.host-catalogs.host-catalog.host-sets');
} catch (error) {
@@ -70,14 +79,4 @@ export default class ScopesScopeProjectsProjectHostCatalogsHostCatalogHostSetsRo
this.notify.error(error.message, { closeAfter: null });
}
}
/**
* Configure adapter options using current host catalog scope and it's parent project scope.
* @return {object}
*/
adapterOptions() {
const scopeID = this.modelFor('scopes.scope.projects.project').id;
const hostCatalogID = this.modelFor('scopes.scope.projects.project.host-catalogs.host-catalog').id;
return { adapterOptions : { scopeID, hostCatalogID } };
}
}
@@ -0,0 +1,70 @@
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
import { action } from '@ember/object';
import { hash } from 'rsvp';
export default class ScopesScopeProjectsProjectHostCatalogsHostCatalogHostSetsHostSetHostsRoute extends Route {
// =services
@service intl;
@service notify;
// =methods
/**
* Loads all hosts under the current host catalog.
* @return {Promise{[HostModel]}}
*/
async model() {
const scopeID = this.modelFor('scopes.scope.projects.project').id;
const hostCatalogID = this.modelFor('scopes.scope.projects.project.host-catalogs.host-catalog').id;
const hostSet = this.modelFor('scopes.scope.projects.project.host-catalogs.host-catalog.host-sets.host-set');
return hash({
hostSet,
hosts: this.store.findAll('host', { adapterOptions: { scopeID, hostCatalogID } })
});
}
// =actions
/**
* Toggles the presence of the passed host ID in the passed host set's
* `host_ids` field.
* @param {HostSetModel} hostSet
* @param {HostModel} host
*/
@action
toggleHost(hostSet, host) {
const isHostSelected = Boolean(hostSet.host_ids.findBy('value', host.id));
const fragment = hostSet.host_ids.findBy('value', host.id);
if (isHostSelected) {
// if host is already selected, filter it out
hostSet.host_ids.removeObject(fragment);
} else {
// if host is not selected, add it
hostSet.host_ids.addObject({ value: host.id });
}
}
/**
* Saves host IDs on the host set.
* @param {HostSetModel} hostSet
*/
@action
async save(hostSet) {
try {
const scopeID = this.modelFor('scopes.scope.projects.project').id;
const hostCatalogID = this.modelFor('scopes.scope.projects.project.host-catalogs.host-catalog').id;
const options = { adapterOptions : { scopeID, hostCatalogID } };
await hostSet.saveHostIDs(options);
this.notify.success(
this.intl.t('notify.save-success')
);
} catch (error) {
// TODO: replace with translated strings
this.notify.error(error.message, { closeAfter: null });
}
}
}
+9 -1
View File
@@ -13,7 +13,9 @@
}
.rose-form {
width: 66%;
&:not(.full-width) {
width: 66%;
}
.rose-form-input {
width: 100%;
@@ -23,3 +25,9 @@
}
}
}
.rose-table {
p {
margin-bottom: 0;
}
}
@@ -2,4 +2,7 @@
<nav.link @route='scopes.scope.projects.project.host-catalogs.host-catalog.host-sets.host-set.index'>
{{t 'titles.details'}}
</nav.link>
</Rose::Nav::Tabs>
<nav.link @route='scopes.scope.projects.project.host-catalogs.host-catalog.host-sets.host-set.hosts'>
{{t 'resources.hosts'}}
</nav.link>
</Rose::Nav::Tabs>
@@ -0,0 +1,7 @@
<Form::HostSet::Hosts
@model={{@model.hostSet}}
@hosts={{@model.hosts}}
@submit={{route-action "save" @model.hostSet}}
@cancel={{route-action "cancel" @model.hostSet}}
@toggleHost={{route-action "toggleHost" @model.hostSet}}
/>
@@ -0,0 +1,25 @@
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render, find } from '@ember/test-helpers';
import { hbs } from 'ember-cli-htmlbars';
module('Integration | Component | form/host-set/hosts', function(hooks) {
setupRenderingTest(hooks);
test('it renders', async function (assert) {
this.submit = () => {};
this.cancel = () => {};
this.model = { host_ids: [] };
this.hosts = [];
await render(hbs`
<Form::HostSet::Hosts
@model={{this.model}}
@hosts={{this.hosts}}
@submit={{this.submit}}
@cancel={{this.cancel}}
/>`);
assert.ok(find('form'));
});
});
@@ -0,0 +1,11 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
module('Unit | Route | scopes/scope/projects/project/host-catalogs/host-catalog/host-sets/host-set/hosts', function(hooks) {
setupTest(hooks);
test('it exists', function(assert) {
let route = this.owner.lookup('route:scopes/scope/projects/project/host-catalogs/host-catalog/host-sets/host-set/hosts');
assert.ok(route);
});
});
+3
View File
@@ -48,6 +48,8 @@ resources:
auth-methods: Auth Methods
host-set: Host Set
host-sets: Host Sets
host: Host
hosts: Hosts
titles:
iam: IAM
new: New
@@ -95,6 +97,7 @@ form:
manage: Manage
grant: Grant
principal: Principal
address: Address
errors:
titles:
unauthenticated-error: You are not signed in