Skip to content

Commit

Permalink
portalicious: store selected columns
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterSmallenbroek committed Jan 18, 2025
1 parent 58940be commit 80ad37c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class QueryTableColumnManagementComponent<
> {
columns = input.required<QueryTableColumn<TData>[]>();
visibleColumns = model.required<QueryTableColumn<TData>[]>();
selectedColumnsStateKey = input.required<string>();
readonly resetColumnVisibility = output();

formVisible = model<boolean>(false);
Expand All @@ -60,6 +61,10 @@ export class QueryTableColumnManagementComponent<
mutationFn: () =>
Promise.resolve(this.formGroup.getRawValue().selectedColumns),
onSuccess: (selectedColumns) => {
localStorage.setItem(
this.selectedColumnsStateKey(),
JSON.stringify(selectedColumns),
);
this.visibleColumns.set(selectedColumns);
this.formVisible.set(false);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@
<app-query-table-column-management
[columns]="columns()"
[(visibleColumns)]="visibleColumns"
(resetColumnVisibility)="resetColumnVisibility()"
(resetColumnVisibility)="resetColumnVisibility(true)"
[selectedColumnsStateKey]="selectedColumnsStateKey()"
/>
}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ export class QueryTableComponent<TData extends { id: PropertyKey }, TContext> {
readonly contextMenu = viewChild<Menu>('contextMenu');
readonly extraOptionsMenu = viewChild<Menu>('extraOptionsMenu');

selectedColumnsStateKey = computed(() => {
const key = this.localStorageKey();
return key ? `${key}-selected-columns` : 'selected-columns';
});

/**
* DISPLAY
*/
Expand Down Expand Up @@ -484,10 +489,31 @@ export class QueryTableComponent<TData extends { id: PropertyKey }, TContext> {
*/
visibleColumns = model<QueryTableColumn<TData>[]>([]);

resetColumnVisibility() {
this.visibleColumns.set(
this.columns().filter((column) => !column.defaultHidden),
resetColumnVisibility(removeFromLocalStorage = false) {
if (removeFromLocalStorage) {
localStorage.removeItem(this.selectedColumnsStateKey());
}

const storedSelectedColumns = localStorage.getItem(
this.selectedColumnsStateKey(),
);
if (storedSelectedColumns) {
const interpretedColumns = JSON.parse(
storedSelectedColumns,
) as QueryTableColumn<TData>[];
console.log(
'🚀 ~ QueryTableComponent<TData ~ resetColumnVisibility ~ interpretedColumns:',
interpretedColumns,
);
const matchedColumns = interpretedColumns
.map((column) => this.columns().find((c) => c.field === column.field))
.filter(Boolean) as QueryTableColumn<TData>[];
this.visibleColumns.set(matchedColumns);
} else {
this.visibleColumns.set(
this.columns().filter((column) => !column.defaultHidden),
);
}
}

columnVisibilityEffect = effect(() => {
Expand Down

0 comments on commit 80ad37c

Please sign in to comment.