Skip to content

Commit

Permalink
fix(admin-ui): Preset filters preserve query parameters (#3176)
Browse files Browse the repository at this point in the history
  • Loading branch information
taxilian authored Nov 29, 2024
1 parent 8545267 commit 7a25bef
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
</div>
<a
[routerLink]="['./']"
[queryParams]="preset.value === serializedActiveFilters ? {} : { filters: preset.value, page: 1 }"
[queryParams]="getQueryParamsForPreset(preset.value, serializedActiveFilters)"
>
<div>{{ preset.name }}</div>
</a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,28 @@ export class DataTableFilterPresetsComponent implements OnInit, OnDestroy {
this.filterPresets$ = this.filterPresetService.presetChanges$.pipe(
startWith(this.filterPresetService.getFilterPresets(this.dataTableId)),
);
// When any query param changes, we want to trigger change detection
// so that the links for each preset are updated.
this.route.queryParamMap
.pipe(takeUntil(this.destroy$))
.subscribe(() => this.changeDetectorRef.markForCheck());
}

getQueryParamsForPreset(preset: string, serializedActiveFilters: string): Record<string, string> {
// Clone the current query params to avoid mutating them directly
const currentParams = { ...this.route.snapshot.queryParams };

if (preset === serializedActiveFilters) {
// Toggling off: remove 'filters' and 'page' params
delete currentParams['filters'];
delete currentParams['page'];
} else {
// Toggling on: set 'filters' and 'page' params
currentParams['filters'] = preset;
currentParams['page'] = 1;
}

return currentParams;
}

ngOnDestroy() {
Expand Down

0 comments on commit 7a25bef

Please sign in to comment.