This blog article will illustrate how the Session API has been integrated into the admin users route Open Event Frontend, as well as how it’s now possible to filter active and deleted users using the new filters implemented.
To make the sessions buttons on the users table functional a new sub route is added to the app’s user route as follows:
this.route('users', function() { this.route('view', { path: '/:user_id' }, function() { this.route('sessions', function() { this.route('list', { path: '/:session_status' }); }); }); this.route('list', { path: '/:users_status' });
The newly added route further contains a dynamic sub route called list. This nested route fulfills the requirement of filtering the various sessions of a given user according to their states. Interestingly, the routes admin/users/view and admin/users/list are both dynamic and expect a parameter after /users/ hence, the app cannot distinguish between them on it’s own, thus explicit handling of the dynamic parameter of the routes was implemented, differentiating them on the basis of the route’s state as follows:
beforeModel(transition) { this._super(...arguments); const userState = transition.params[transition.targetName].users_status; if (!['all', 'deleted', 'active'].includes(userState)) { this.replaceWith('admin.users.view', userState); } }
Thus if the dynamic portion of the route doesn’t contain the parameters all, deleted or active, then it must be referring to a user’s sessions and the route needs to be replaced with the desired sessions route accordingly. Also, the template admin/users.hbs needs to be changed to display the navigation bar only when required. It is efficiently handled by an IF condition as follows:
{{#if (and (not-includes session.currentRouteName ‘admin.users.user’) (not-includes session.currentRouteName ‘admin.users.view.sessions.list’))}} |
The server is queried to fetch the sessions of a given user by making use of the hasMany relationship a user has with his sessions. They are loaded in the route admin/users/view/sessions/list.js
model() { const userDetails = this.modelFor('admin.users.view'); return this.store.findRecord('user', userDetails.id, { include: 'sessions' });
After fetching the the sessions from the server, the existing session-card component is reused in the route’s template to display the sessions.
{{#if model.sessions}} {{#each model.sessions as |session|}} {{session-card session=session}} {{/each}} {{t ‘No session proposals found for the events’}}
{{/if}} |
Also, in the admin/users route the filtering of deleted users was not functional. Thus the property deleted-at of the users model which stores the timestamp of the deletion of a user was utilised. deleted-at is null for a user which is active. Hence the active and deleted users can be filtered as :
if (params.users_status === 'active') { filterOptions = [ { name : 'deleted-at', op : 'eq', val : null } ]; } else if (params.users_status === 'deleted') { filterOptions = [ { name : 'deleted-at', op : 'ne', val : null } ]; } return this.get('store').query('user', { get_trashed : true, filter : filterOptions, 'page[size]' : 10 });
It’s important to pass the get_trashed parameter as true in the query as the the deleted user records are actually soft deleted records and will be fetched only when explicitly queried for.