fix: sort direction URL parameter persistence (#1134)

- Fix URL parameters not updating when sort direction changes
- Prioritize URL parameters over user preferences for sort state
- Fix case sensitivity bug in direction parameter comparison
- Ensure both sort field and direction persist on page refresh
This commit is contained in:
Patrick Deuley
2025-09-12 12:10:30 -06:00
committed by GitHub
parent 6ea2eb58bc
commit acd71e1f54
2 changed files with 26 additions and 20 deletions
@@ -147,7 +147,9 @@ export class BookBrowserComponent implements OnInit {
private sideBarFilter = new SideBarFilter(this.selectedFilter, this.selectedFilterMode); private sideBarFilter = new SideBarFilter(this.selectedFilter, this.selectedFilterMode);
private headerFilter = new HeaderFilter(this.searchTerm$); private headerFilter = new HeaderFilter(this.searchTerm$);
protected bookSorter = new BookSorter(selectedSort => this.applySortOption(selectedSort)); protected bookSorter = new BookSorter(
selectedSort => this.onManualSortChange(selectedSort)
);
@ViewChild(BookTableComponent) @ViewChild(BookTableComponent)
bookTableComponent!: BookTableComponent; bookTableComponent!: BookTableComponent;
@@ -215,7 +217,6 @@ export class BookBrowserComponent implements OnInit {
); );
this.tieredMenuItems = this.bookMenuService.getTieredMenuItems(this.selectedBooks); this.tieredMenuItems = this.bookMenuService.getTieredMenuItems(this.selectedBooks);
// --- NEW: Subscribe to query params + user changes for reactive updates ---
combineLatest([ combineLatest([
this.activatedRoute.paramMap, this.activatedRoute.paramMap,
this.activatedRoute.queryParamMap, this.activatedRoute.queryParamMap,
@@ -290,16 +291,17 @@ export class BookBrowserComponent implements OnInit {
? SortDirection.DESCENDING ? SortDirection.DESCENDING
: SortDirection.ASCENDING; : SortDirection.ASCENDING;
const matchedSort = this.bookSorter.sortOptions.find(opt => opt.field === userSortKey) || this.bookSorter.sortOptions.find(opt => opt.field === sortParam); const effectiveSortKey = sortParam || userSortKey;
const effectiveSortDir = directionParam
? (directionParam.toLowerCase() === SORT_DIRECTION.DESCENDING ? SortDirection.DESCENDING : SortDirection.ASCENDING)
: userSortDir;
const matchedSort = this.bookSorter.sortOptions.find(opt => opt.field === effectiveSortKey);
this.bookSorter.selectedSort = matchedSort ? { this.bookSorter.selectedSort = matchedSort ? {
label: matchedSort.label, label: matchedSort.label,
field: matchedSort.field, field: matchedSort.field,
direction: userSortDir ?? ( direction: effectiveSortDir
directionParam?.toUpperCase() === SORT_DIRECTION.DESCENDING
? SortDirection.DESCENDING
: SortDirection.ASCENDING
)
} : { } : {
label: 'Added On', label: 'Added On',
field: 'addedOn', field: 'addedOn',
@@ -453,6 +455,22 @@ export class BookBrowserComponent implements OnInit {
this.seriesCollapseFilter.setCollapsed(value); this.seriesCollapseFilter.setCollapsed(value);
} }
onManualSortChange(sortOption: SortOption): void {
this.applySortOption(sortOption);
const currentParams = this.activatedRoute.snapshot.queryParams;
const newParams = {
...currentParams,
sort: sortOption.field,
direction: sortOption.direction === SortDirection.ASCENDING ? SORT_DIRECTION.ASCENDING : SORT_DIRECTION.DESCENDING
};
this.router.navigate([], {
queryParams: newParams,
replaceUrl: true
});
}
applySortOption(sortOption: SortOption): void { applySortOption(sortOption: SortOption): void {
if (this.entityType === EntityType.ALL_BOOKS) { if (this.entityType === EntityType.ALL_BOOKS) {
this.bookState$ = this.fetchAllBooks(); this.bookState$ = this.fetchAllBooks();
@@ -1,6 +1,5 @@
import {SortDirection, SortOption} from '../../../model/sort.model'; import {SortDirection, SortOption} from '../../../model/sort.model';
export class BookSorter { export class BookSorter {
selectedSort: SortOption | undefined = undefined; selectedSort: SortOption | undefined = undefined;
@@ -46,17 +45,6 @@ export class BookSorter {
this.updateSortOptions(); this.updateSortOptions();
this.applySortOption(this.selectedSort); this.applySortOption(this.selectedSort);
/*this.router.navigate([], {
queryParams: {
sort: this.selectedSort.field,
direction: this.selectedSort.direction === SortDirection.ASCENDING
? SORT_DIRECTION.ASCENDING
: SORT_DIRECTION.DESCENDING
},
queryParamsHandling: 'merge',
replaceUrl: true
});*/
} }
updateSortOptions() { updateSortOptions() {