@@ -8,18 +8,22 @@ import { service } from '@ember/service';
88import { tracked } from '@glimmer/tracking' ;
99import {
1010 STATUS_SESSION_ACTIVE ,
11- STATUS_SESSION_PENDING ,
1211 STATUS_SESSION_CANCELING ,
12+ STATUS_SESSION_PENDING ,
1313 statusTypes ,
1414} from 'api/models/session' ;
1515import { action } from '@ember/object' ;
16- import { notifySuccess , notifyError } from 'core/decorators/notify' ;
16+ import { notifyError , notifySuccess } from 'core/decorators/notify' ;
17+ import { restartableTask } from 'ember-concurrency' ;
18+ import { assert } from '@ember/debug' ;
19+ import FilterOptions from 'admin/utils/filter-options' ;
1720
1821export default class ScopesScopeSessionsIndexController extends Controller {
1922 // =services
2023
2124 @service store ;
2225 @service intl ;
26+ @service db ;
2327
2428 // =attributes
2529
@@ -34,6 +38,9 @@ export default class ScopesScopeSessionsIndexController extends Controller {
3438 'sortDirection' ,
3539 ] ;
3640
41+ userFilters = new FilterOptions ( ) ;
42+ targetFilters = new FilterOptions ( ) ;
43+
3744 @tracked search ;
3845 @tracked users = [ ] ;
3946 @tracked targets = [ ] ;
@@ -54,8 +61,8 @@ export default class ScopesScopeSessionsIndexController extends Controller {
5461 get filters ( ) {
5562 return {
5663 allFilters : {
57- users : this . model . associatedUsers ,
58- targets : this . model . associatedTargets ,
64+ users : this . userFilters . allOptions ,
65+ targets : this . targetFilters . allOptions ,
5966 status : this . sessionStatusOptions ,
6067 } ,
6168 selectedFilters : {
@@ -77,6 +84,74 @@ export default class ScopesScopeSessionsIndexController extends Controller {
7784 } ) ) ;
7885 }
7986
87+ /**
88+ * Configurations for different filter options
89+ * @returns {[object] }
90+ */
91+ get filterConfigs ( ) {
92+ return {
93+ userFilters : {
94+ resource : 'user' ,
95+ select : [
96+ { field : 'id' , isDistinct : true } ,
97+ { field : 'name' , isDistinct : true } ,
98+ ] ,
99+ searchFields : [ 'id' , 'name' ] ,
100+ filters : {
101+ joins : [
102+ {
103+ resource : 'session' ,
104+ joinOn : 'user_id' ,
105+ } ,
106+ ] ,
107+ } ,
108+ } ,
109+ targetFilters : {
110+ resource : 'target' ,
111+ select : [
112+ { field : 'id' , isDistinct : true } ,
113+ { field : 'name' , isDistinct : true } ,
114+ ] ,
115+ searchFields : [ 'id' , 'name' ] ,
116+ filters : {
117+ joins : [
118+ {
119+ resource : 'session' ,
120+ joinOn : 'target_id' ,
121+ } ,
122+ ] ,
123+ } ,
124+ } ,
125+ } ;
126+ }
127+
128+ /**
129+ * Generic retrieve function for session filter options
130+ * @param {string } type - The type of options to retrieve (userFilters, targetFilters)
131+ * @param {string } search - Search term
132+ * @returns {Promise<Array> }
133+ */
134+ async retrieveFilterOptions ( type , search ) {
135+ const config = this . filterConfigs [ type ] ;
136+ assert ( `Unknown filter type: ${ type } ` , config ) ;
137+
138+ return this . db . query ( config . resource , {
139+ select : config . select ,
140+ query : {
141+ search : { text : search , fields : config . searchFields } ,
142+ filters : config . filters ,
143+ } ,
144+ page : 1 ,
145+ pageSize : 250 ,
146+ } ) ;
147+ }
148+
149+ loadItems = restartableTask ( async ( ) => {
150+ this . userFilters . options = await this . retrieveFilterOptions ( 'userFilters' ) ;
151+ this . targetFilters . options =
152+ await this . retrieveFilterOptions ( 'targetFilters' ) ;
153+ } ) ;
154+
80155 // =actions
81156
82157 /**
@@ -106,7 +181,7 @@ export default class ScopesScopeSessionsIndexController extends Controller {
106181 */
107182 @action
108183 refresh ( ) {
109- this . send ( 'refreshAll ') ;
184+ this . router . refresh ( 'scopes.scope.sessions ') ;
110185 }
111186
112187 /**
@@ -131,4 +206,9 @@ export default class ScopesScopeSessionsIndexController extends Controller {
131206 this . sortDirection = sortOrder ;
132207 this . page = 1 ;
133208 }
209+
210+ onFilterSearch = restartableTask ( async ( filter , value ) => {
211+ this [ filter ] . search = value ;
212+ this [ filter ] . options = await this . retrieveFilterOptions ( filter , value ) ;
213+ } ) ;
134214}
0 commit comments