From 96f30d1b311be9eba3045203c77de5e9a7b501a5 Mon Sep 17 00:00:00 2001 From: hashicc <191911133+hashicc@users.noreply.github.com> Date: Wed, 28 May 2025 18:20:43 -0400 Subject: [PATCH] Add sorting to Groups in boundary admin (#2846) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- .../controllers/scopes/scope/groups/index.js | 17 ++- .../app/routes/scopes/scope/groups/index.js | 23 +++- .../templates/scopes/scope/groups/index.hbs | 73 ++++++++----- ui/admin/tests/acceptance/groups/list-test.js | 103 +++++++++++++++++- 4 files changed, 184 insertions(+), 32 deletions(-) diff --git a/ui/admin/app/controllers/scopes/scope/groups/index.js b/ui/admin/app/controllers/scopes/scope/groups/index.js index 2d570c9cc..955f0e8a8 100644 --- a/ui/admin/app/controllers/scopes/scope/groups/index.js +++ b/ui/admin/app/controllers/scopes/scope/groups/index.js @@ -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; + } } diff --git a/ui/admin/app/routes/scopes/scope/groups/index.js b/ui/admin/app/routes/scopes/scope/groups/index.js index 2859ffb0a..ee098eff1 100644 --- a/ui/admin/app/routes/scopes/scope/groups/index.js +++ b/ui/admin/app/routes/scopes/scope/groups/index.js @@ -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, }); diff --git a/ui/admin/app/templates/scopes/scope/groups/index.hbs b/ui/admin/app/templates/scopes/scope/groups/index.hbs index 5a381a7cc..fd2ce17a4 100644 --- a/ui/admin/app/templates/scopes/scope/groups/index.hbs +++ b/ui/admin/app/templates/scopes/scope/groups/index.hbs @@ -47,37 +47,54 @@ {{#if @model.groups}} + <:head as |H|> + + + {{t 'form.name.label'}} + + + {{t 'form.id.label'}} + + + + <:body as |B|> - - - {{#if (can 'read group' B.data)}} - - {{B.data.displayName}} - - {{else}} - {{B.data.displayName}} - {{/if}} - - {{B.data.description}} - - - - - - + {{#each @model.groups as |data|}} + + + {{#if (can 'read group' data)}} + + {{data.displayName}} + + {{else}} + {{data.displayName}} + {{/if}} + + {{data.description}} + + + + + + + {{/each}} { + 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); + }); + }, + ); });