Keep track of applied filters in URL query string (#1654)
This commit is contained in:
+27
-10
@@ -1,4 +1,4 @@
|
|||||||
import {Component, inject, OnInit, ViewChild} from '@angular/core';
|
import {Component, inject, OnInit, ViewChild, AfterViewInit} from '@angular/core';
|
||||||
import {ActivatedRoute, Router} from '@angular/router';
|
import {ActivatedRoute, Router} from '@angular/router';
|
||||||
import {ConfirmationService, MenuItem, MessageService, PrimeTemplate} from 'primeng/api';
|
import {ConfirmationService, MenuItem, MessageService, PrimeTemplate} from 'primeng/api';
|
||||||
import {PageTitleService} from "../../../../shared/service/page-title.service";
|
import {PageTitleService} from "../../../../shared/service/page-title.service";
|
||||||
@@ -96,7 +96,7 @@ const SORT_DIRECTION = {
|
|||||||
])
|
])
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
export class BookBrowserComponent implements OnInit {
|
export class BookBrowserComponent implements OnInit, AfterViewInit {
|
||||||
protected userService = inject(UserService);
|
protected userService = inject(UserService);
|
||||||
protected coverScalePreferenceService = inject(CoverScalePreferenceService);
|
protected coverScalePreferenceService = inject(CoverScalePreferenceService);
|
||||||
protected columnPreferenceService = inject(TableColumnPreferenceService);
|
protected columnPreferenceService = inject(TableColumnPreferenceService);
|
||||||
@@ -123,6 +123,7 @@ export class BookBrowserComponent implements OnInit {
|
|||||||
entity$: Observable<Library | Shelf | MagicShelf | null> | undefined;
|
entity$: Observable<Library | Shelf | MagicShelf | null> | undefined;
|
||||||
entityType$: Observable<EntityType> | undefined;
|
entityType$: Observable<EntityType> | undefined;
|
||||||
searchTerm$ = new BehaviorSubject<string>('');
|
searchTerm$ = new BehaviorSubject<string>('');
|
||||||
|
parsedFilters: Record<string, string[]> = {};
|
||||||
selectedFilter = new BehaviorSubject<Record<string, any> | null>(null);
|
selectedFilter = new BehaviorSubject<Record<string, any> | null>(null);
|
||||||
selectedFilterMode = new BehaviorSubject<'and' | 'or'>('and');
|
selectedFilterMode = new BehaviorSubject<'and' | 'or'>('and');
|
||||||
protected resetFilterSubject = new Subject<void>();
|
protected resetFilterSubject = new Subject<void>();
|
||||||
@@ -244,6 +245,8 @@ export class BookBrowserComponent implements OnInit {
|
|||||||
|
|
||||||
const parsedFilters: Record<string, string[]> = {};
|
const parsedFilters: Record<string, string[]> = {};
|
||||||
|
|
||||||
|
this.currentFilterLabel = 'All Books';
|
||||||
|
|
||||||
if (filterParams) {
|
if (filterParams) {
|
||||||
this.settingFiltersFromUrl = true;
|
this.settingFiltersFromUrl = true;
|
||||||
|
|
||||||
@@ -261,22 +264,19 @@ export class BookBrowserComponent implements OnInit {
|
|||||||
this.bookFilterComponent.onFiltersChanged?.();
|
this.bookFilterComponent.onFiltersChanged?.();
|
||||||
}
|
}
|
||||||
|
|
||||||
const firstFilter = filterParams.split(',')[0];
|
if (Object.keys(parsedFilters).length > 0) {
|
||||||
const [key, ...values] = firstFilter.split(':');
|
this.currentFilterLabel = 'All Books (Filtered)';
|
||||||
const firstValue = values.join(':').split('|')[0];
|
|
||||||
if (key && firstValue) {
|
|
||||||
this.currentFilterLabel = this.capitalize(key) + ': ' + firstValue;
|
|
||||||
} else {
|
|
||||||
this.currentFilterLabel = 'All Books';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.rawFilterParamFromUrl = filterParams;
|
this.rawFilterParamFromUrl = filterParams;
|
||||||
this.settingFiltersFromUrl = false;
|
this.settingFiltersFromUrl = false;
|
||||||
} else {
|
} else {
|
||||||
|
this.clearFilter();
|
||||||
this.rawFilterParamFromUrl = null;
|
this.rawFilterParamFromUrl = null;
|
||||||
this.currentFilterLabel = 'All Books';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.parsedFilters = parsedFilters;
|
||||||
|
|
||||||
this.entityViewPreferences = user.user?.userSettings?.entityViewPreferences;
|
this.entityViewPreferences = user.user?.userSettings?.entityViewPreferences;
|
||||||
const globalPrefs = this.entityViewPreferences?.global;
|
const globalPrefs = this.entityViewPreferences?.global;
|
||||||
const currentEntityTypeStr = this.entityType ? this.entityType.toString().toUpperCase() : undefined;
|
const currentEntityTypeStr = this.entityType ? this.entityType.toString().toUpperCase() : undefined;
|
||||||
@@ -357,6 +357,13 @@ export class BookBrowserComponent implements OnInit {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ngAfterViewInit(){
|
||||||
|
if (this.bookFilterComponent) {
|
||||||
|
this.bookFilterComponent.setFilters?.(this.parsedFilters);
|
||||||
|
this.bookFilterComponent.onFiltersChanged?.();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
onFilterSelected(filters: Record<string, any> | null): void {
|
onFilterSelected(filters: Record<string, any> | null): void {
|
||||||
if (this.settingFiltersFromUrl) return;
|
if (this.settingFiltersFromUrl) return;
|
||||||
|
|
||||||
@@ -365,6 +372,16 @@ export class BookBrowserComponent implements OnInit {
|
|||||||
|
|
||||||
const hasSidebarFilters = !!filters && Object.keys(filters).length > 0;
|
const hasSidebarFilters = !!filters && Object.keys(filters).length > 0;
|
||||||
this.currentFilterLabel = hasSidebarFilters ? 'All Books (Filtered)' : 'All Books';
|
this.currentFilterLabel = hasSidebarFilters ? 'All Books (Filtered)' : 'All Books';
|
||||||
|
|
||||||
|
const queryParam = hasSidebarFilters ? Object.entries(filters).map(([k, v]) => `${k}:${v.join('|')}`).join(',') : null;
|
||||||
|
if (queryParam !== this.activatedRoute.snapshot.queryParamMap.get(QUERY_PARAMS.FILTER)) {
|
||||||
|
this.router.navigate([], {
|
||||||
|
relativeTo: this.activatedRoute,
|
||||||
|
queryParams: {[QUERY_PARAMS.FILTER]: queryParam},
|
||||||
|
queryParamsHandling: 'merge',
|
||||||
|
replaceUrl: false
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onFilterModeChanged(mode: 'and' | 'or'): void {
|
onFilterModeChanged(mode: 'and' | 'or'): void {
|
||||||
|
|||||||
Reference in New Issue
Block a user