Skip to content

Commit 3c44bef

Browse files
table view: save sorting settings on server and remove ui settings using
1 parent 25659b4 commit 3c44bef

3 files changed

Lines changed: 74 additions & 22 deletions

File tree

frontend/src/app/components/dashboard/db-table-view/db-table-view.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,8 @@ <h2 class="mat-h2 table-name">{{ displayName }}</h2>
271271
<mat-divider></mat-divider>
272272
<button mat-menu-item (click)="toggleDefaultSort(column); $event.stopPropagation()"
273273
class="sort-menu__item"
274-
[disabled]="sort.active !== column && !isDefaultSort(column)"
275-
[matTooltip]="sort.active !== column && !isDefaultSort(column) ? 'Select sort order first' : ''"
274+
[disabled]="(sort.active !== column || !sort.direction) && !isDefaultSort(column)"
275+
[matTooltip]="(sort.active !== column || !sort.direction) && !isDefaultSort(column) ? 'Select sort order first' : ''"
276276
matTooltipPosition="left">
277277
<mat-icon class="sort-menu__lock-icon">{{ isDefaultSort(column) ? 'lock' : 'lock_open' }}</mat-icon>
278278
<span>{{ isDefaultSort(column) ? 'Remove default' : 'Set as default' }}</span>

frontend/src/app/components/dashboard/db-table-view/db-table-view.component.ts

Lines changed: 57 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import { getTableTypes } from 'src/app/lib/setup-table-row-structure';
3636
import {
3737
CustomAction,
3838
TableForeignKey,
39+
TableOrdering,
3940
TablePermissions,
4041
TableProperties,
4142
TableRow,
@@ -44,7 +45,6 @@ import {
4445
import { NotificationsService } from 'src/app/services/notifications.service';
4546
import { TableRowService } from 'src/app/services/table-row.service';
4647
import { TableStateService } from 'src/app/services/table-state.service';
47-
import { UiSettingsService } from 'src/app/services/ui-settings.service';
4848
import { tableDisplayTypes, UIwidgets } from '../../../consts/table-display-types';
4949
import { normalizeTableName } from '../../../lib/normalize';
5050
import { PlaceholderTableDataComponent } from '../../skeletons/placeholder-table-data/placeholder-table-data.component';
@@ -164,7 +164,6 @@ export class DbTableViewComponent implements OnInit {
164164
private _notifications: NotificationsService,
165165
private _tableRow: TableRowService,
166166
private _connections: ConnectionsService,
167-
private _uiSettings: UiSettingsService,
168167
private _tables: TablesService,
169168
private route: ActivatedRoute,
170169
public router: Router,
@@ -175,16 +174,42 @@ export class DbTableViewComponent implements OnInit {
175174
ngAfterViewInit() {
176175
this.tableData.paginator = this.paginator;
177176

178-
this.tableData.sort = this.sort;
177+
// Check if sort params exist in URL
178+
const urlSortActive = this.route.snapshot.queryParams.sort_active;
179+
const urlSortDirection = this.route.snapshot.queryParams.sort_direction;
179180

180-
// Load default sort from settings
181-
this.loadDefaultSort();
181+
let sortInitialized = false;
182182

183-
// Initialize sort - default sort takes priority
184-
if (this.defaultSort) {
185-
this.sort.active = this.defaultSort.column;
186-
this.sort.direction = this.defaultSort.direction;
187-
}
183+
// Subscribe to loading state to initialize default sort after data is loaded
184+
this.tableData.loading$.subscribe((loading: boolean) => {
185+
console.log('loading$ changed:', loading, 'sort.active:', this.sort?.active, 'sort.direction:', this.sort?.direction);
186+
187+
if (!loading && this.tableData.defaultSort !== undefined) {
188+
// Update defaultSort reference whenever data loads
189+
this.defaultSort = this.tableData.defaultSort;
190+
191+
console.log('DbTableViewComponent tableData loaded:', this.tableData);
192+
console.log('DbTableViewComponent tableData.defaultSort loaded:', this.tableData.defaultSort);
193+
194+
// Only initialize sort on first load
195+
if (!sortInitialized) {
196+
sortInitialized = true;
197+
198+
// Initialize sort based on priority: URL params > default sort
199+
if (urlSortActive && urlSortDirection) {
200+
// Use sort from URL
201+
this.sort.active = urlSortActive;
202+
this.sort.direction = urlSortDirection.toLowerCase() as 'asc' | 'desc';
203+
} else if (this.defaultSort) {
204+
// Use default sort if no URL params
205+
this.sort.active = this.defaultSort.column;
206+
this.sort.direction = this.defaultSort.direction;
207+
}
208+
}
209+
210+
console.log('After loading complete - sort.active:', this.sort?.active, 'sort.direction:', this.sort?.direction);
211+
}
212+
});
188213

189214
merge(this.sort.sortChange, this.paginator.page)
190215
.pipe(
@@ -272,7 +297,6 @@ export class DbTableViewComponent implements OnInit {
272297
// If this column was the default, remove the default too
273298
if (this.defaultSort?.column === column) {
274299
this.defaultSort = null;
275-
this._uiSettings.updateTableSetting(this.connectionID, this.name, 'defaultSort', null);
276300
}
277301
// Clear sort
278302
this.sort.active = '';
@@ -285,23 +309,36 @@ export class DbTableViewComponent implements OnInit {
285309
}
286310
}
287311

288-
loadDefaultSort() {
289-
const tableSettings = this._uiSettings.settings?.connections?.[this.connectionID]?.tables?.[this.name];
290-
if (tableSettings?.defaultSort) {
291-
this.defaultSort = tableSettings.defaultSort;
292-
}
293-
}
294-
295312
toggleDefaultSort(column: string) {
296313
if (this.isDefaultSort(column)) {
297314
// Remove default sort
298315
this.defaultSort = null;
299-
this._uiSettings.updateTableSetting(this.connectionID, this.name, 'defaultSort', null);
316+
this._tables.updatePersonalTableViewSettings(this.connectionID, this.name, {
317+
ordering: null,
318+
ordering_field: null,
319+
}).subscribe({
320+
next: () => {
321+
console.log('Personal table view settings updated - default sort removed');
322+
},
323+
error: (error) => {
324+
console.error('Error updating personal table view settings:', error);
325+
}
326+
});
300327
} else {
301328
// Set current sort as default
302329
const direction = this.sort.active === column ? this.sort.direction : 'asc';
303330
this.defaultSort = { column, direction: direction as 'asc' | 'desc' };
304-
this._uiSettings.updateTableSetting(this.connectionID, this.name, 'defaultSort', this.defaultSort);
331+
this._tables.updatePersonalTableViewSettings(this.connectionID, this.name, {
332+
ordering: this.sort.direction === 'asc' ? TableOrdering.Ascending : TableOrdering.Descending,
333+
ordering_field: column,
334+
}).subscribe({
335+
next: () => {
336+
console.log('Personal table view settings updated - default sort removed');
337+
},
338+
error: (error) => {
339+
console.error('Error updating personal table view settings:', error);
340+
}
341+
});
305342
}
306343
}
307344

frontend/src/app/components/dashboard/db-tables-data-source.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { filter } from "lodash";
1616
import { formatFieldValue } from 'src/app/lib/format-field-value';
1717
import { getTableTypes } from 'src/app/lib/setup-table-row-structure';
1818
import { normalizeFieldName } from 'src/app/lib/normalize';
19+
// import { MatSort } from '@angular/material/sort';
1920

2021
interface Column {
2122
title: string,
@@ -53,6 +54,10 @@ export class TablesDataSource implements DataSource<Object> {
5354
public displayedColumns: string[];
5455
public displayedDataColumns: string[];
5556
public sortByColumns: string[];
57+
public defaultSort: {
58+
column: string;
59+
direction: 'asc' | 'desc';
60+
} | null;
5661
public foreignKeysList: string[] = [];
5762
public foreignKeys: TableForeignKey[] = [];
5863
public widgetsList: string[];
@@ -272,6 +277,16 @@ export class TablesDataSource implements DataSource<Object> {
272277

273278
this.sortByColumns = res.sortable_by;
274279

280+
// Only set defaultSort if both ordering_field and ordering are present
281+
if (res.table_settings.ordering_field && res.table_settings.ordering) {
282+
this.defaultSort = {
283+
column: res.table_settings.ordering_field,
284+
direction: res.table_settings.ordering.toLowerCase() as 'asc' | 'desc'
285+
};
286+
} else {
287+
this.defaultSort = null;
288+
}
289+
275290
const widgetsConfigured = res.widgets?.length;
276291
if (!res.configured && !widgetsConfigured
277292
&& this._connections.connectionAccessLevel !== AccessLevel.None

0 commit comments

Comments
 (0)