Files
threshold-ui/ui/desktop/app/controllers/scopes/scope/projects/sessions/index.js
T
Cameron PereraandZhihe Li b86ce41e68 fix: 🐛 use filter query param to filter by org scope id (#2103)
* fix: 🐛 use filter query param to filter by org scope id

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

* refactor: 💡 remove duplicate call for scopes.scope model

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

* refactor: 💡 add filter to allTargets and sessions query

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

* refactor: 💡 reset query params for targets and sessions

on org scope change

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

* refactor: 💡 remove open/close parens from filter string

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

* refactor: 💡 Update logic on when to reset query parameters

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

* refactor: 💡 filter sessions query by parent scope isOrg

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

* refactor: 💡 update to & from transition references

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

* refactor: 💡 update to & from transition references for sessions

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

---------

Co-authored-by: Zhihe Li <zhihe.li@hashicorp.com>
2024-01-24 10:18:28 -06:00

125 lines
2.9 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 {
STATUS_SESSION_ACTIVE,
STATUS_SESSION_PENDING,
STATUS_SESSION_CANCELING,
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 = [
STATUS_SESSION_ACTIVE,
STATUS_SESSION_PENDING,
STATUS_SESSION_CANCELING,
];
@tracked scopes = [];
@tracked page = 1;
@tracked pageSize = 10;
// =methods
/**
* A list of sessions sorted by created time then sorted by availability
* @type {[SessionModel]}
*/
get sortedSessions() {
return orderBy(
this.model.sessions,
['isAvailable', 'created_time'],
['desc', '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 {
allFilters: {
targets: this.availableTargets,
status: this.sessionStatusOptions,
scopes: this.availableScopes,
},
selectedFilters: {
targets: this.targets,
status: this.status,
scopes: this.scopes,
},
};
}
/**
* 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;
}
}