Files
threshold-ui/ui/admin/app/components/form/target/add-host-sets/index.js
T
hashicorp-copywrite[bot]andCameron Perera 3d610427d0 [COMPLIANCE] Add Copyright and License Headers (#1617)
* [COMPLIANCE] Add Copyright and License Headers

* chore: 🤖 update Copyright headers to satisfy our lint rules

---------

Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
Co-authored-by: Cameron Perera <cameron.perera@hashicorp.com>
2023-03-01 09:39:41 -06:00

94 lines
2.3 KiB
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
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';
export default class FormTargetAddHostSetsComponent extends Component {
// =services
@service confirm;
@service intl;
// =properties
/**
* Array of selected host set IDs.
* @type {EmberArray}
*/
selectedHostSetIDs = A();
/**
* Checks for unassigned hostsets.
* @param {[HostSetModel]} filteredHostSets
* @type {boolean}
*/
@computed('filteredHostSets.length')
get hasAvailableHostSets() {
return this.filteredHostSets.length > 0;
}
/**
* Host sets not already added to the target.
* @type {[HostSetModel]}
*/
@computed('args.{hostSets.[],model.host_sources.[]}')
get filteredHostSets() {
// Get IDs for host sets already added to the current target
const alreadyAddedHostSetIDs = this.args.model.host_sources.map(
({ host_source_id }) => host_source_id
);
const notAddedHostSets = this.args.hostSets.filter(
({ id }) => !alreadyAddedHostSetIDs.includes(id)
);
return notAddedHostSets;
}
// =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);
}
}