Files
threshold-ui/ui/admin/app/components/form/user/add-accounts/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

59 lines
1.4 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';
export default class FormUserAddAccountsComponent extends Component {
// =properties
/**
* Array of selected account IDs.
* @type {EmberArray}
*/
selectedAccountIDs = A();
/**
* Checks for unassigned accounts.
* @param {[AccountModel]} filteredAccounts
* @type {boolean}
*/
@computed('filteredAccounts.length')
get hasAvailableAccounts() {
return this.filteredAccounts.length > 0;
}
/**
* Accounts not already added to the user.
* @type {[AccountModel]}
*/
@computed('args.{accounts.[],model.account_ids.[]}')
get filteredAccounts() {
// Get IDs for accounts already added to the current user
const alreadyAddedAccountIDs = this.args.model.account_ids;
const notAddedAccounts = this.args.accounts.filter(
({ id }) => !alreadyAddedAccountIDs.includes(id)
);
return notAddedAccounts;
}
// =actions
@action
toggleAccount(account) {
if (!this.selectedAccountIDs.includes(account.id)) {
this.selectedAccountIDs.addObject(account.id);
} else {
this.selectedAccountIDs.removeObject(account.id);
}
}
@action
submit(fn) {
fn(this.selectedAccountIDs);
}
}