Add sorting to Groups in boundary admin (#2846)
* feat: 🎸 wire up sorting for groups/index route ✅ Closes: https://hashicorp.atlassian.net/browse/ICU-16783 * refactor: 💡 use underlying hds components directly for sorting This step refactors the table to use the components directly. With this setup a sorting refactor can be done more easily. ✅ Closes: https://hashicorp.atlassian.net/browse/ICU-16783 * feat: 🎸 wire up sorting from controller to template ✅ Closes: https://hashicorp.atlassian.net/browse/ICU-16783 * test: 💍 add sorting tests for groups table ✅ Closes: https://hashicorp.atlassian.net/browse/ICU-16783
This commit is contained in:
1 parent
f4a48467e3
commit
96f30d1b31
4 files changed
+184
-32
No files matched your search
@@ -20,11 +20,19 @@ export default class ScopesScopeGroupsIndexController extends Controller {
|
||||
|
||||
// =attributes
|
||||
|
||||
queryParams = ['search', 'page', 'pageSize'];
|
||||
queryParams = [
|
||||
'search',
|
||||
'page',
|
||||
'pageSize',
|
||||
'sortAttribute',
|
||||
'sortDirection',
|
||||
];
|
||||
|
||||
@tracked search;
|
||||
@tracked page = 1;
|
||||
@tracked pageSize = 10;
|
||||
@tracked sortAttribute;
|
||||
@tracked sortDirection;
|
||||
|
||||
/**
|
||||
* If can list (at least): return default welcome message.
|
||||
@@ -124,4 +132,11 @@ export default class ScopesScopeGroupsIndexController extends Controller {
|
||||
this.search = value;
|
||||
this.page = 1;
|
||||
}
|
||||
|
||||
@action
|
||||
sortBy(attribute, direction) {
|
||||
this.sortAttribute = attribute;
|
||||
this.sortDirection = direction;
|
||||
this.page = 1;
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,14 @@ export default class ScopesScopeGroupsIndexRoute extends Route {
|
||||
pageSize: {
|
||||
refreshModel: true,
|
||||
},
|
||||
sortAttribute: {
|
||||
refreshModel: true,
|
||||
replace: true,
|
||||
},
|
||||
sortDirection: {
|
||||
refreshModel: true,
|
||||
replace: true,
|
||||
},
|
||||
};
|
||||
|
||||
// =methods
|
||||
@@ -41,7 +49,14 @@ export default class ScopesScopeGroupsIndexRoute extends Route {
|
||||
}
|
||||
|
||||
retrieveData = restartableTask(
|
||||
async ({ search, page, pageSize, useDebounce }) => {
|
||||
async ({
|
||||
search,
|
||||
page,
|
||||
pageSize,
|
||||
sortAttribute,
|
||||
sortDirection,
|
||||
useDebounce,
|
||||
}) => {
|
||||
if (useDebounce) {
|
||||
await timeout(250);
|
||||
}
|
||||
@@ -52,11 +67,15 @@ export default class ScopesScopeGroupsIndexRoute extends Route {
|
||||
let totalItems = 0;
|
||||
let doGroupsExist = false;
|
||||
const filters = { scope_id: [{ equals: scope_id }] };
|
||||
const sort = {
|
||||
attribute: sortAttribute,
|
||||
direction: sortDirection,
|
||||
};
|
||||
|
||||
if (this.can.can('list model', scope, { collection: 'groups' })) {
|
||||
groups = await this.store.query('group', {
|
||||
scope_id,
|
||||
query: { filters, search },
|
||||
query: { filters, search, sort },
|
||||
page,
|
||||
pageSize,
|
||||
});
|
||||
|
||||
@@ -47,37 +47,54 @@
|
||||
{{#if @model.groups}}
|
||||
|
||||
<Hds::Table
|
||||
@model={{@model.groups}}
|
||||
@columns={{array
|
||||
(hash label=(t 'form.name.label'))
|
||||
(hash label=(t 'form.id.label'))
|
||||
}}
|
||||
@valign='middle'
|
||||
@sortBy={{this.sortAttribute}}
|
||||
@sortOrder={{this.sortDirection}}
|
||||
@onSort={{this.sortBy}}
|
||||
>
|
||||
<:head as |H|>
|
||||
<H.Tr>
|
||||
<H.ThSort
|
||||
@onClickSort={{fn H.setSortBy 'name'}}
|
||||
@sortOrder={{if (eq 'name' H.sortBy) H.sortOrder}}
|
||||
>
|
||||
{{t 'form.name.label'}}
|
||||
</H.ThSort>
|
||||
<H.ThSort
|
||||
@onClickSort={{fn H.setSortBy 'id'}}
|
||||
@sortOrder={{if (eq 'id' H.sortBy) H.sortOrder}}
|
||||
>
|
||||
{{t 'form.id.label'}}
|
||||
</H.ThSort>
|
||||
</H.Tr>
|
||||
</:head>
|
||||
|
||||
<:body as |B|>
|
||||
<B.Tr>
|
||||
<B.Td>
|
||||
{{#if (can 'read group' B.data)}}
|
||||
<LinkTo
|
||||
@route='scopes.scope.groups.group'
|
||||
@model={{B.data.id}}
|
||||
>
|
||||
{{B.data.displayName}}
|
||||
</LinkTo>
|
||||
{{else}}
|
||||
{{B.data.displayName}}
|
||||
{{/if}}
|
||||
<Hds::Text::Body @tag='p'>
|
||||
{{B.data.description}}
|
||||
</Hds::Text::Body>
|
||||
</B.Td>
|
||||
<B.Td>
|
||||
<Hds::Copy::Snippet
|
||||
@textToCopy={{B.data.id}}
|
||||
@color='secondary'
|
||||
/>
|
||||
</B.Td>
|
||||
</B.Tr>
|
||||
{{#each @model.groups as |data|}}
|
||||
<B.Tr>
|
||||
<B.Td>
|
||||
{{#if (can 'read group' data)}}
|
||||
<LinkTo
|
||||
@route='scopes.scope.groups.group'
|
||||
@model={{data.id}}
|
||||
>
|
||||
{{data.displayName}}
|
||||
</LinkTo>
|
||||
{{else}}
|
||||
{{data.displayName}}
|
||||
{{/if}}
|
||||
<Hds::Text::Body @tag='p'>
|
||||
{{data.description}}
|
||||
</Hds::Text::Body>
|
||||
</B.Td>
|
||||
<B.Td>
|
||||
<Hds::Copy::Snippet
|
||||
@textToCopy={{data.id}}
|
||||
@color='secondary'
|
||||
/>
|
||||
</B.Td>
|
||||
</B.Tr>
|
||||
{{/each}}
|
||||
</:body>
|
||||
</Hds::Table>
|
||||
<Rose::Pagination
|
||||
|
||||
@@ -4,12 +4,20 @@
|
||||
*/
|
||||
|
||||
import { module, test } from 'qunit';
|
||||
import { visit, click, fillIn, waitUntil, findAll } from '@ember/test-helpers';
|
||||
import {
|
||||
visit,
|
||||
click,
|
||||
fillIn,
|
||||
waitUntil,
|
||||
findAll,
|
||||
currentURL,
|
||||
} from '@ember/test-helpers';
|
||||
import { setupApplicationTest } from 'admin/tests/helpers';
|
||||
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
|
||||
import { setupIndexedDb } from 'api/test-support/helpers/indexed-db';
|
||||
import { authenticateSession } from 'ember-simple-auth/test-support';
|
||||
import * as commonSelectors from 'admin/tests/helpers/selectors';
|
||||
import { faker } from '@faker-js/faker';
|
||||
|
||||
module('Acceptance | groups | list', function (hooks) {
|
||||
setupApplicationTest(hooks);
|
||||
@@ -122,4 +130,97 @@ module('Acceptance | groups | list', function (hooks) {
|
||||
assert.dom(commonSelectors.HREF(urls.group2)).doesNotExist();
|
||||
assert.dom(NO_RESULTS_MSG_SELECTOR).includesText('No results found');
|
||||
});
|
||||
|
||||
test('groups table is sorted by created_time descending by default', async function (assert) {
|
||||
this.server.schema.groups.all().destroy();
|
||||
|
||||
const years = ['2026', '2025', '2024', '2023'];
|
||||
faker.helpers.shuffle(years).map((year) => {
|
||||
return this.server.create('group', {
|
||||
scope: instances.scopes.org,
|
||||
id: `g_${year}`,
|
||||
name: `Group ${year}`,
|
||||
created_time: `${year}-01-01T00:00:00Z`,
|
||||
});
|
||||
});
|
||||
|
||||
await visit(urls.groups);
|
||||
|
||||
assert.dom(commonSelectors.TABLE_ROWS).exists({ count: years.length });
|
||||
assert.dom(commonSelectors.TABLE_ROW(1)).containsText('Group 2026');
|
||||
assert.dom(commonSelectors.TABLE_ROW(2)).containsText('Group 2025');
|
||||
assert.dom(commonSelectors.TABLE_ROW(3)).containsText('Group 2024');
|
||||
assert.dom(commonSelectors.TABLE_ROW(4)).containsText('Group 2023');
|
||||
});
|
||||
|
||||
test.each(
|
||||
'sorting',
|
||||
{
|
||||
'on name': {
|
||||
attribute: {
|
||||
key: 'name',
|
||||
values: ['Alpha', 'Beta', 'Delta', 'Gamma', 'Epsilon'],
|
||||
},
|
||||
expectedAscendingSort: ['Alpha', 'Beta', 'Delta', 'Epsilon', 'Gamma'],
|
||||
column: 1,
|
||||
},
|
||||
'on id': {
|
||||
attribute: {
|
||||
key: 'id',
|
||||
values: ['g_0001', 'g_0010', 'g_0100', 'g_1000', 'g_10000'],
|
||||
},
|
||||
expectedAscendingSort: [
|
||||
'g_0001',
|
||||
'g_0010',
|
||||
'g_0100',
|
||||
'g_1000',
|
||||
'g_10000',
|
||||
],
|
||||
column: 2,
|
||||
},
|
||||
},
|
||||
async function (assert, input) {
|
||||
this.server.schema.groups.all().destroy();
|
||||
faker.helpers.shuffle(input.attribute.values).forEach((value) => {
|
||||
this.server.create('group', {
|
||||
[input.attribute.key]: value,
|
||||
scope: instances.scopes.org,
|
||||
});
|
||||
});
|
||||
|
||||
await visit(urls.groups);
|
||||
|
||||
// click the sort button again to sort in ascending order
|
||||
await click(commonSelectors.TABLE_SORT_BTN(input.column));
|
||||
assert.true(currentURL().includes('sortDirection=asc'));
|
||||
assert.true(
|
||||
currentURL().includes(`sortAttribute=${input.attribute.key}`),
|
||||
);
|
||||
assert
|
||||
.dom(commonSelectors.TABLE_SORT_BTN_ARROW_UP(input.column))
|
||||
.isVisible();
|
||||
assert
|
||||
.dom(commonSelectors.TABLE_ROWS)
|
||||
.isVisible({ count: input.attribute.values.length });
|
||||
|
||||
input.expectedAscendingSort.forEach((expected, index) => {
|
||||
// nth-child index starts at 1
|
||||
assert.dom(commonSelectors.TABLE_ROW(index + 1)).containsText(expected);
|
||||
});
|
||||
|
||||
// click the sort button again to sort in descending order
|
||||
await click(commonSelectors.TABLE_SORT_BTN(input.column));
|
||||
assert.true(currentURL().includes('sortDirection=desc'));
|
||||
assert.true(
|
||||
currentURL().includes(`sortAttribute=${input.attribute.key}`),
|
||||
);
|
||||
assert
|
||||
.dom(commonSelectors.TABLE_SORT_BTN_ARROW_DOWN(input.column))
|
||||
.isVisible();
|
||||
input.expectedAscendingSort.toReversed().forEach((expected, index) => {
|
||||
// nth-child index starts at 1
|
||||
assert.dom(commonSelectors.TABLE_ROW(index + 1)).containsText(expected);
|
||||
});
|
||||
},
|
||||
);
|
||||
});
|
||||
Reference in new issue
Block a user