From acd71e1f54a29e0ad6227c806340641dcf9702e4 Mon Sep 17 00:00:00 2001 From: Patrick Deuley Date: Fri, 12 Sep 2025 13:10:30 -0500 Subject: [PATCH] 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 --- .../book-browser/book-browser.component.ts | 34 ++++++++++++++----- .../book-browser/sorting/BookSorter.ts | 12 ------- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/booklore-ui/src/app/book/components/book-browser/book-browser.component.ts b/booklore-ui/src/app/book/components/book-browser/book-browser.component.ts index 42baa0663..96b7ae332 100644 --- a/booklore-ui/src/app/book/components/book-browser/book-browser.component.ts +++ b/booklore-ui/src/app/book/components/book-browser/book-browser.component.ts @@ -147,7 +147,9 @@ export class BookBrowserComponent implements OnInit { private sideBarFilter = new SideBarFilter(this.selectedFilter, this.selectedFilterMode); private headerFilter = new HeaderFilter(this.searchTerm$); - protected bookSorter = new BookSorter(selectedSort => this.applySortOption(selectedSort)); + protected bookSorter = new BookSorter( + selectedSort => this.onManualSortChange(selectedSort) + ); @ViewChild(BookTableComponent) bookTableComponent!: BookTableComponent; @@ -215,7 +217,6 @@ export class BookBrowserComponent implements OnInit { ); this.tieredMenuItems = this.bookMenuService.getTieredMenuItems(this.selectedBooks); - // --- NEW: Subscribe to query params + user changes for reactive updates --- combineLatest([ this.activatedRoute.paramMap, this.activatedRoute.queryParamMap, @@ -290,16 +291,17 @@ export class BookBrowserComponent implements OnInit { ? SortDirection.DESCENDING : 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 ? { label: matchedSort.label, field: matchedSort.field, - direction: userSortDir ?? ( - directionParam?.toUpperCase() === SORT_DIRECTION.DESCENDING - ? SortDirection.DESCENDING - : SortDirection.ASCENDING - ) + direction: effectiveSortDir } : { label: 'Added On', field: 'addedOn', @@ -453,6 +455,22 @@ export class BookBrowserComponent implements OnInit { 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 { if (this.entityType === EntityType.ALL_BOOKS) { this.bookState$ = this.fetchAllBooks(); diff --git a/booklore-ui/src/app/book/components/book-browser/sorting/BookSorter.ts b/booklore-ui/src/app/book/components/book-browser/sorting/BookSorter.ts index ec7b432c6..91588009b 100644 --- a/booklore-ui/src/app/book/components/book-browser/sorting/BookSorter.ts +++ b/booklore-ui/src/app/book/components/book-browser/sorting/BookSorter.ts @@ -1,6 +1,5 @@ import {SortDirection, SortOption} from '../../../model/sort.model'; - export class BookSorter { selectedSort: SortOption | undefined = undefined; @@ -46,17 +45,6 @@ export class BookSorter { this.updateSortOptions(); 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() {