From 2827dd3de345e90c2b15d5d58d881951acc083ad Mon Sep 17 00:00:00 2001 From: Ivan Kljun Date: Mon, 19 Feb 2024 15:12:37 +0100 Subject: [PATCH] Refactor fetching/applying table state [SCI-10163] --- app/javascript/vue/shared/datatable/table.vue | 88 ++++++++++++------- app/javascript/vue/shared/select_dropdown.vue | 3 + app/services/lists/repositories_service.rb | 6 +- 3 files changed, 61 insertions(+), 36 deletions(-) diff --git a/app/javascript/vue/shared/datatable/table.vue b/app/javascript/vue/shared/datatable/table.vue index 16a155b50..7bbb6a2c2 100644 --- a/app/javascript/vue/shared/datatable/table.vue +++ b/app/javascript/vue/shared/datatable/table.vue @@ -48,7 +48,7 @@ @first-data-rendered="onFirstDataRendered" @sortChanged="setOrder" @columnResized="saveTableState" - @columnMoved="saveTableState" + @columnMoved="onColumnMoved" @bodyScroll="handleScroll" @columnPinned="handlePin" @columnVisible="handleVisibility" @@ -66,7 +66,7 @@ :params="actionsParams" @toolbar:action="emitAction" /> -
+
{ if (response.data.data) { - const { currentViewRender, columnsState, perPage, order } = response.data.data; - this.tableState = response.data.data; - this.currentViewRender = currentViewRender; - this.columnsState = columnsState; - this.perPage = perPage; - this.order = order; - - if (this.order) { - this.tableState.columnsState.forEach((column) => { - const updatedColumn = column; - updatedColumn.sort = this.order.column === column.colId ? this.order.dir : null; - return updatedColumn; - }); + this.fetchedTableState = response.data.data; + if (this.gridReady && this.fetchedTableState) { + this.applyTableState(this.fetchedTableState); } - this.columnApi.applyColumnState({ - state: this.tableState.columnsState, - applyOrder: true - }); } else { + this.initializing = false; this.saveTableState(); } - setTimeout(() => { - this.initializing = false; - }, 200); }); }, + applyTableState(state) { + const { currentViewRender, columnsState, perPage, order } = state; + this.tableState = state; + this.currentViewRender = currentViewRender; + this.columnsState = columnsState; + this.perPage = perPage; + this.order = order; + + if (this.order) { + this.tableState.columnsState.forEach((column) => { + const updatedColumn = column; + updatedColumn.sort = this.order.column === column.colId ? this.order.dir : null; + return updatedColumn; + }); + } + this.columnApi.applyColumnState({ + state: this.tableState.columnsState, + applyOrder: true + }); + setTimeout(() => { + this.initializing = false; + }, 200); + }, getRowClass() { if (this.currentViewMode === 'archived') { return '!bg-sn-super-light-grey'; @@ -435,8 +447,10 @@ export default { onGridReady(params) { this.gridApi = params.api; this.columnApi = params.columnApi; - - this.fetchAndApplyTableState(); + this.gridReady = true; + if (this.fetchedTableState) { + this.applyTableState(this.fetchedTableState); + } }, onFirstDataRendered() { this.resize(); @@ -459,6 +473,9 @@ export default { this.reloadTable(false); }, saveTableState() { + if (this.initializing) { + return; + } const columnsState = this.columnApi ? this.columnApi.getColumnState() : this.tableState?.columnsState || []; const tableState = { columnsState, @@ -551,6 +568,11 @@ export default { }; this.saveTableState(); this.reloadTable(false); + }, + onColumnMoved(event) { + if (event.finished) { + this.saveTableState(); + } } } }; diff --git a/app/javascript/vue/shared/select_dropdown.vue b/app/javascript/vue/shared/select_dropdown.vue index 6fe3bebf6..b056e1c97 100644 --- a/app/javascript/vue/shared/select_dropdown.vue +++ b/app/javascript/vue/shared/select_dropdown.vue @@ -228,6 +228,9 @@ export default { this.fetchOptions(); }, watch: { + value(newValue) { + this.newValue = newValue; + }, isOpen() { if (this.isOpen) { this.$nextTick(() => { diff --git a/app/services/lists/repositories_service.rb b/app/services/lists/repositories_service.rb index 6c08d256b..69eecf495 100644 --- a/app/services/lists/repositories_service.rb +++ b/app/services/lists/repositories_service.rb @@ -45,11 +45,11 @@ module Lists def sortable_columns @sortable_columns ||= { name: 'repositories.name', - team: 'teams.name', - created_by: 'creators.full_name', + team: 'team_name', + created_by: 'created_by_user', created_at: 'repositories.created_at', archived_on: 'repositories.archived_on', - archived_by: 'archivers.full_name', + archived_by: 'archived_by_user', nr_of_rows: 'row_count' } end