feat: 🎸 reintroduce sessions UX

This commit is contained in:
Randall Morey committed 2020-10-28 11:14:58 -04:00
1 parent 9c7158f930
commit 369bff9402
9 files changed
+341

No files matched your search

+1
View File
@@ -56,6 +56,7 @@ Router.map(function () {
this.route('scopes', function() {});
this.route('edit');
this.route('new');
this.route('sessions', function() {});
});
});
+103
View File
@@ -0,0 +1,103 @@
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
import { action } from '@ember/object';
import { all, hash } from 'rsvp';
import { task, timeout } from 'ember-concurrency';
import { A } from '@ember/array';
import { notifySuccess, notifyError } from '../../../decorators/notify';
import config from '../../../config/environment';
const POLL_TIMEOUT_SECONDS = config.sessionPollingTimeoutSeconds;
export default class ScopesScopeSessionsRoute extends Route {
// =services
@service intl;
@service notify;
// =attributes
/**
* A simple Ember Concurrency-based polling task that refreshes the route
* every POLL_TIMEOUT_SECONDS seconds. This is necessary to display changes
* to session `status` that may occur.
*
* NOTE: tasks are sort of attributes and sort of methods, but they are not
* language-level constructs. Thus we annotate this task as if it
* is an attribute.
* @type {Task}
*/
@task(function * () {
while(true) {
yield timeout(POLL_TIMEOUT_SECONDS * 1000);
yield this.refresh();
}
}).drop() poller;
// =methods
/**
* Loads all sessions under the current scope and encapsulates them into
* an array of objects containing their associated users and targets.
* @return {Promise{[{session: SessionModel, user: UserModel, target: TargetModel}]}}
*/
async model() {
const { id: scope_id } = this.modelFor('scopes.scope');
const sessions = await this.store.query('session', { scope_id });
const sessionAggregates = await all(
sessions.map(session => hash({
session,
user: session.user_id
? (
this.store.peekRecord('user', session.user_id) ||
this.store.findRecord('user', session.user_id)
)
: null,
target: session.target_id
? (
this.store.peekRecord('target', session.target_id) ||
this.store.findRecord('target', session.target_id)
)
: null,
}))
);
// Sort sessions by time created...
let sortedSessionAggregates =
A(sessionAggregates).sortBy('session.created_time').reverse();
// Then move active sessions to the top...
sortedSessionAggregates = [
...sortedSessionAggregates.filter((aggregate) => aggregate.session.status === 'active'),
...sortedSessionAggregates.filter((aggregate) => aggregate.session.status !== 'active'),
];
return sortedSessionAggregates;
}
/**
* When this route is activated (entered), begin polling for changes.
*/
activate() {
this.poller.perform();
}
/**
* When this route is deactivated (exited), stop polling for changes.
*/
deactivate() {
this.poller.cancelAll();
}
// =actions
/**
* Cancels the specified session and notifies user of success or error.
* @param {SessionModel}
*/
@action
@notifyError(({ message }) => message, { catch: true })
@notifySuccess('notifications.canceled-success')
async cancelSession(session) {
await session.cancelSession();
}
}
@@ -0,0 +1,4 @@
import Route from '@ember/routing/route';
export default class ScopesScopeSessionsIndexRoute extends Route {
}
+22
View File
@@ -68,6 +68,28 @@
</nav.link>
</Rose::Nav::Sidebar>
--}}
<Rose::Nav::Sidebar @title={{t "titles.resources"}} as |nav|>
<nav.link @route="scopes.scope.sessions">
<Rose::Icon
@name="app-icons/action-session"
@size="medium" />
{{t "resources.session.title_plural"}}
</nav.link>
{{!--
<nav.link @route="scopes.scope.projects.project.targets">
<Rose::Icon
@name="app-icons/target"
@size="medium" />
{{t "resources.target.title_plural"}}
</nav.link>
<nav.link @route="scopes.scope.projects.project.host-catalogs">
<Rose::Icon
@name="app-icons/host-catalogs"
@size="medium" />
{{t "resources.host-catalog.title_plural"}}
</nav.link>
--}}
</Rose::Nav::Sidebar>
{{/if}}
{{#unless @model.isProject}}
@@ -0,0 +1,3 @@
{{title (t "resources.session.title_plural")}}
{{outlet}}
@@ -0,0 +1,82 @@
<Rose::Layout::Page as |page|>
<page.breadcrumbs>
<BreadCrumbs />
</page.breadcrumbs>
<page.header>
<h2>
{{t "resources.session.title_plural"}}
<DocLink @doc="session" @iconSize="large" />
</h2>
<p>{{t "resources.session.description"}}</p>
</page.header>
<page.body>
{{#if @model}}
<Rose::Table as |table|>
<table.header as |header|>
<header.row as |row|>
<row.headerCell>{{t "form.id.label"}}</row.headerCell>
<row.headerCell>{{t "resources.user.title"}}</row.headerCell>
<row.headerCell>{{t "resources.target.title"}}</row.headerCell>
<row.headerCell>{{t "form.started.label"}}</row.headerCell>
<row.headerCell>{{t "form.status.label"}}</row.headerCell>
<row.headerCell>{{t "titles.actions"}}</row.headerCell>
</header.row>
</table.header>
<table.body as |body|>
{{#each @model as |sessionAggregate|}}
<body.row as |row|>
<row.headerCell class="nowrap">
<Rose::Icon
class={{concat "session-status-" sessionAggregate.session.status}}
@name="app-icons/action-session"
@size="medium" />
<Copyable
@text={{sessionAggregate.session.id}}
@buttonText={{t "actions.copy-to-clipboard"}}
@acknowledgeText={{t "states.copied"}}
>
<code>{{sessionAggregate.session.id}}</code>
</Copyable>
</row.headerCell>
<row.cell>{{sessionAggregate.user.displayName}}</row.cell>
<row.cell>{{sessionAggregate.target.displayName}}</row.cell>
<row.cell>
<time datetime={{format-date-iso sessionAggregate.session.created_time}}>
{{format-date-iso-human sessionAggregate.session.created_time}}
</time>
</row.cell>
<row.cell>
<Rose::Badge>{{sessionAggregate.session.status}}</Rose::Badge>
</row.cell>
<row.cell>
{{#if sessionAggregate.session.isCancelable}}
<Rose::Button @style="secondary" {{on "click" (route-action "cancelSession" sessionAggregate.session)}}>
{{t "actions.cancel"}}
</Rose::Button>
{{/if}}
</row.cell>
</body.row>
{{/each}}
</table.body>
</Rose::Table>
{{else}}
<Rose::Layout::Centered>
<Rose::Message @title={{t "resources.session.messages.none.title"}} as |message|>
<message.description>
{{t "resources.session.messages.none.description"}}
</message.description>
</Rose::Message>
</Rose::Layout::Centered>
{{/if}}
</page.body>
</Rose::Layout::Page>
+104
View File
@@ -0,0 +1,104 @@
import { module, test } from 'qunit';
import { visit, currentURL, findAll, find, click } from '@ember/test-helpers';
import { setupApplicationTest } from 'ember-qunit';
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
import a11yAudit from 'ember-a11y-testing/test-support/audit';
import { run, later } from '@ember/runloop';
import { Response } from 'miragejs';
import {
authenticateSession,
// These are left here intentionally for future reference.
//currentSession,
//invalidateSession,
} from 'ember-simple-auth/test-support';
module('Acceptance | sessions', function (hooks) {
setupApplicationTest(hooks);
setupMirage(hooks);
const instances = {
scopes: {
global: null,
org: null,
},
sessions: null,
};
const urls = {
orgScope: null,
sessions: null,
};
hooks.beforeEach(function () {
authenticateSession({});
instances.scopes.global = this.server.create('scope', { id: 'global' });
instances.scopes.org = this.server.create('scope', {
type: 'org',
scope: { id: 'global', type: 'global' },
});
instances.scopes.project = this.server.create('scope', {
type: 'project',
scope: { id: instances.scopes.org.id, type: instances.scopes.org.type },
});
this.server.createList('group', 1, { scope: instances.scopes.org }, 'withMembers');
this.server.createList('host-catalog', 1, { scope: instances.scopes.project }, 'withChildren');
this.server.createList('target', 1, { scope: instances.scopes.project }, 'withRandomHostSets');
instances.sessions = this.server.createList('session', 3, {
scope: instances.scopes.project,
status: 'active'
}, 'withAssociations');
urls.sessions = `/scopes/${instances.scopes.project.id}/sessions`;
});
test('visiting sessions', async function (assert) {
assert.expect(2);
authenticateSession({});
later(async () => {
run.cancelTimers();
await a11yAudit();
assert.equal(currentURL(), urls.sessions);
assert.equal(findAll('tbody tr').length, instances.sessions.length);
}, 750);
await visit(urls.sessions);
});
test('visiting sessions without users or targets is OK', async function (assert) {
assert.expect(2);
instances.sessions[0].update({
userId: null,
targetId: null
});
authenticateSession({});
later(async () => {
run.cancelTimers();
await a11yAudit();
assert.equal(currentURL(), urls.sessions);
assert.equal(findAll('tbody tr').length, instances.sessions.length);
}, 750);
await visit(urls.sessions);
});
test('cancelling a session', async function (assert) {
assert.expect(2);
authenticateSession({});
later(async () => {
run.cancelTimers();
await click('tbody tr:first-child td:last-child button');
assert.ok(find('[role="alert"].is-success'));
}, 750);
await visit(urls.sessions);
assert.equal(currentURL(), urls.sessions);
});
test('cancelling a session with error shows notification', async function (assert) {
assert.expect(2);
this.server.post('/sessions/:id_method', () => new Response(400));
authenticateSession({});
later(async () => {
run.cancelTimers();
await click('tbody tr:first-child td:last-child button');
assert.ok(find('[role="alert"].is-error'));
}, 750);
await visit(urls.sessions);
assert.equal(currentURL(), urls.sessions);
});
});
@@ -0,0 +1,11 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
module('Unit | Route | scopes/scope/sessions', function(hooks) {
setupTest(hooks);
test('it exists', function(assert) {
let route = this.owner.lookup('route:scopes/scope/sessions');
assert.ok(route);
});
});
@@ -0,0 +1,11 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
module('Unit | Route | scopes/scope/sessions/index', function(hooks) {
setupTest(hooks);
test('it exists', function(assert) {
let route = this.owner.lookup('route:scopes/scope/sessions/index');
assert.ok(route);
});
});