Files
threshold-ui/ui/desktop/app/controllers/scopes/scope/projects/sessions/index.js
T
Lisbet Alvarez a1fbfdc1ad Icu 11804 filter targets by type (#2063)
* feat: 🎸 filter targets by type

✅ Closes: https://hashicorp.atlassian.net/browse/ICU-11804

* refactor: 💡 remove console.log

✅ Closes: https://hashicorp.atlassian.net/browse/ICU-11804

* docs: ✏️ fix comment typo

✅ Closes: https://hashicorp.atlassian.net/browse/ICU-11804
2024-01-16 10:17:46 -05:00

105 lines
2.5 KiB
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import Controller from '@ember/controller';
import { inject as service } from '@ember/service';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
import orderBy from 'lodash/orderBy';
import { statusTypes } from 'api/models/session';
export default class ScopesScopeProjectsSessionsIndexController extends Controller {
// =services
@service intl;
// =attributes
queryParams = [
{ targets: { type: 'array' } },
{ status: { type: 'array' } },
{ scopes: { type: 'array' } },
'page',
'pageSize',
];
@tracked targets = [];
@tracked status = [];
@tracked scopes = [];
@tracked page = 1;
@tracked pageSize = 10;
// =methods
/**
* A list of sessions sorted by created time
* @type {[SessionModel]}
*/
get sortedSessions() {
return orderBy(this.model.sessions, 'created_time', 'desc');
}
/**
* Returns targets that are associated will all sessions the user has access to
* @returns {[TargetModel]}
*/
get availableTargets() {
const uniqueSessionTargetIds = new Set(
this.model.allSessions.map((session) => session.target_id),
);
return this.model.allTargets.filter((target) =>
uniqueSessionTargetIds.has(target.id),
);
}
/**
* Returns all status types for sessions
* @returns {[object]}
*/
get sessionStatusOptions() {
return statusTypes.map((status) => ({
id: status,
name: this.intl.t(`resources.session.status.${status}`),
}));
}
/**
* Returns scopes that are associated with all sessions the user has access to
* @returns {[ScopeModel]}
*/
get availableScopes() {
const uniqueSessionScopeIds = new Set(
this.model.allSessions.map((session) => session.scope.id),
);
return this.model.projects.filter((project) =>
uniqueSessionScopeIds.has(project.id),
);
}
/**
* Returns object of filters to be used for displaying selected filters
* @returns {object}
*/
get filters() {
return {
targets: this.availableTargets,
status: this.sessionStatusOptions,
scopes: this.availableScopes,
};
}
/**
* Sets the query params to value of selectedItems
* to trigger a query and closes the dropdown
* @param {string} filter
* @param {object} selectedItems
*/
@action
applyFilter(filter, selectedItems) {
this[filter] = [...selectedItems];
this.page = 1;
}
}