Files
threshold-ui/ui/admin/app/components/form/target/add-host-sets/index.js
T

93 lines
2.3 KiB
JavaScript
Raw Normal View History

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
2020-08-27 16:38:49 -04:00
import Component from '@glimmer/component';
import { computed, action } from '@ember/object';
import { A } from '@ember/array';
import { inject as service } from '@ember/service';
import { loading } from 'ember-loading';
import { notifyError } from 'core/decorators/notify';
2020-08-27 16:38:49 -04:00
export default class FormTargetAddHostSetsComponent extends Component {
// =services
@service confirm;
@service intl;
2020-08-27 16:38:49 -04:00
// =properties
/**
* Array of selected host set IDs.
* @type {EmberArray}
*/
selectedHostSetIDs = A();
2020-09-25 21:21:27 -04:00
/**
* Checks for unassigned hostsets.
* @param {[HostSetModel]} filteredHostSets
* @type {boolean}
*/
2020-10-06 09:53:58 -04:00
@computed('filteredHostSets.length')
2020-09-25 21:21:27 -04:00
get hasAvailableHostSets() {
return this.filteredHostSets.length > 0;
}
2020-08-27 16:38:49 -04:00
/**
2020-08-27 16:52:28 -04:00
* Host sets not already added to the target.
* @type {[HostSetModel]}
2020-08-27 16:38:49 -04:00
*/
2021-09-22 19:15:09 -04:00
@computed('args.{hostSets.[],model.host_sources.[]}')
2020-08-27 16:52:28 -04:00
get filteredHostSets() {
2020-08-27 16:38:49 -04:00
// Get IDs for host sets already added to the current target
2021-09-22 19:15:09 -04:00
const alreadyAddedHostSetIDs = this.args.model.host_sources.map(
2021-09-21 16:58:25 -04:00
({ host_source_id }) => host_source_id
2020-09-16 14:33:04 -04:00
);
const notAddedHostSets = this.args.hostSets.filter(
({ id }) => !alreadyAddedHostSetIDs.includes(id)
);
2020-08-27 16:52:28 -04:00
return notAddedHostSets;
2020-08-27 16:38:49 -04:00
}
// =actions
@action
toggleHostSet(hostSet) {
if (!this.selectedHostSetIDs.includes(hostSet.id)) {
this.selectedHostSetIDs.addObject(hostSet.id);
} else {
this.selectedHostSetIDs.removeObject(hostSet.id);
}
}
@action
@loading
@notifyError(({ message }) => message)
async submit() {
const target = this.args.model;
if (target.address && this.selectedHostSetIDs.length) {
try {
await this.confirm.confirm(
this.intl.t(
'resources.target.host-source.questions.delete-address.message'
),
{
title:
'resources.target.host-source.questions.delete-address.title',
confirm: 'resources.target.actions.remove-address',
}
);
} catch (e) {
// if the user denies, do nothing and return
return;
}
target.address = null;
await target.save();
}
await this.args.submit(this.selectedHostSetIDs);
2020-08-27 16:38:49 -04:00
}
}