From 6baf04d2ec5041a53ff887416c33629309f3fe49 Mon Sep 17 00:00:00 2001 From: adityachandelgit <> Date: Mon, 21 Apr 2025 22:55:40 -0600 Subject: [PATCH] Sync url params for view mode and sort order (grid/table, sorting, direction) --- .../book-browser/book-browser.component.html | 4 +- .../book-browser/book-browser.component.ts | 139 ++++++++++++++---- 2 files changed, 116 insertions(+), 27 deletions(-) diff --git a/booklore-ui/src/app/book/components/book-browser/book-browser.component.html b/booklore-ui/src/app/book/components/book-browser/book-browser.component.html index acb39171d..4c4418696 100644 --- a/booklore-ui/src/app/book/components/book-browser/book-browser.component.html +++ b/booklore-ui/src/app/book/components/book-browser/book-browser.component.html @@ -106,9 +106,9 @@
- + - +
(null); + protected resetFilterSubject = new Subject(); protected userService = inject(UserService); private activatedRoute = inject(ActivatedRoute); @@ -92,11 +107,9 @@ export class BookBrowserComponent implements OnInit, AfterViewInit { private shelfService = inject(ShelfService); private dialogService = inject(DialogService); private sortService = inject(SortService); + private router = inject(Router); private libraryShelfMenuService = inject(LibraryShelfMenuService); - protected resetFilterSubject = new Subject(); - - sortOptions: any[] = [ {label: 'Title', icon: '', field: 'title', command: () => this.sortBooks('title')}, {label: 'Author', icon: '', field: 'author', command: () => this.sortBooks('author')}, @@ -112,15 +125,64 @@ export class BookBrowserComponent implements OnInit, AfterViewInit { {label: 'Pages', icon: '', field: 'pageCount', command: () => this.sortBooks('pageCount')} ]; - selectedSort: SortOption = { - label: 'Added On', - field: 'addedOn', - direction: SortDirection.ASCENDING, - }; + selectedSort: SortOption | undefined = undefined; + currentViewMode: string | undefined = undefined; + lastAppliedSort: SortOption | null = null; + ngOnInit(): void { this.bookService.loadBooks(); - this.sortBooks(this.selectedSort.field); + + this.activatedRoute.queryParamMap.subscribe(paramMap => { + const viewParam = paramMap.get(QUERY_PARAMS.VIEW); + const sortParam = paramMap.get(QUERY_PARAMS.SORT); + const directionParam = paramMap.get(QUERY_PARAMS.DIRECTION); + + this.currentViewMode = viewParam === VIEW_MODES.TABLE || viewParam === VIEW_MODES.GRID ? viewParam : VIEW_MODES.GRID; + + const matchingSort = this.sortOptions.find(opt => opt.field === sortParam); + const direction = directionParam === SORT_DIRECTION.DESCENDING ? SortDirection.DESCENDING : SortDirection.ASCENDING; + + if (matchingSort) { + this.selectedSort = { + label: matchingSort.label, + field: matchingSort.field, + direction + }; + } else { + this.selectedSort = { + label: 'Added On', + field: 'addedOn', + direction: SortDirection.ASCENDING + }; + } + + if (this.lastAppliedSort?.field !== this.selectedSort.field || + this.lastAppliedSort?.direction !== this.selectedSort.direction) { + this.lastAppliedSort = { ...this.selectedSort }; + this.applySortOption(this.selectedSort); + } + + const queryParams: any = { + [QUERY_PARAMS.VIEW]: this.currentViewMode, + [QUERY_PARAMS.SORT]: this.selectedSort.field, + [QUERY_PARAMS.DIRECTION]: this.selectedSort.direction === SortDirection.ASCENDING ? SORT_DIRECTION.ASCENDING : SORT_DIRECTION.DESCENDING + }; + + const currentParams = this.activatedRoute.snapshot.queryParams; + + if ( + currentParams[QUERY_PARAMS.VIEW] !== queryParams[QUERY_PARAMS.VIEW] || + currentParams[QUERY_PARAMS.SORT] !== queryParams[QUERY_PARAMS.SORT] || + currentParams[QUERY_PARAMS.DIRECTION] !== queryParams[QUERY_PARAMS.DIRECTION] + ) { + this.router.navigate([], { + queryParams, + replaceUrl: true + }); + } + }); + const isAllBooksRoute = this.activatedRoute.snapshot.routeConfig?.path === 'all-books'; if (isAllBooksRoute) { @@ -171,12 +233,17 @@ export class BookBrowserComponent implements OnInit, AfterViewInit { return (entity as Library).paths !== undefined; } - toggleTableGrid() { - this.gridOrTable = this.gridOrTable === 'grid' ? 'table' : 'grid'; + toggleTableGrid(): void { + this.currentViewMode = this.currentViewMode === VIEW_MODES.GRID ? VIEW_MODES.TABLE : VIEW_MODES.GRID; + this.router.navigate([], { + queryParams: { view: this.currentViewMode }, + queryParamsHandling: 'merge', + replaceUrl: true + }); } get viewIcon(): string { - return this.gridOrTable === 'grid' ? 'pi pi-objects-column' : 'pi pi-table'; + return this.currentViewMode === VIEW_MODES.GRID ? 'pi pi-objects-column' : 'pi pi-table'; } private getEntityInfoFromRoute(): Observable<{ entityId: number; entityType: EntityType }> { @@ -300,7 +367,7 @@ export class BookBrowserComponent implements OnInit, AfterViewInit { map(bookState => { if (bookState.loaded && !bookState.error) { const filteredBooks = bookState.books?.filter(bookFilter) || []; - const sortedBooks = this.sortService.applySort(filteredBooks, this.selectedSort); + const sortedBooks = this.sortService.applySort(filteredBooks, this.selectedSort!); return {...bookState, books: sortedBooks}; } return bookState; @@ -312,7 +379,7 @@ export class BookBrowserComponent implements OnInit, AfterViewInit { private processBookState(bookState: BookState): BookState { if (bookState.loaded && !bookState.error) { - const sortedBooks = this.sortService.applySort(bookState.books || [], this.selectedSort); + const sortedBooks = this.sortService.applySort(bookState.books || [], this.selectedSort!); return {...bookState, books: sortedBooks}; } return bookState; @@ -452,22 +519,44 @@ export class BookBrowserComponent implements OnInit, AfterViewInit { this.clearSearch(); } - sortBooks(field: string) { + sortBooks(field: string): void { + const existingSort = this.sortOptions.find(opt => opt.field === field); + + if (!existingSort) return; + if (this.selectedSort?.field === field) { - this.selectedSort.direction = this.selectedSort.direction === SortDirection.ASCENDING ? SortDirection.DESCENDING : SortDirection.ASCENDING; + this.selectedSort = { + ...this.selectedSort, + direction: this.selectedSort.direction === SortDirection.ASCENDING + ? SortDirection.DESCENDING + : SortDirection.ASCENDING + }; } else { - this.selectedSort.field = field; - this.selectedSort.direction = SortDirection.ASCENDING; + this.selectedSort = { + label: existingSort.label, + field: existingSort.field, + direction: SortDirection.ASCENDING + }; } + 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() { - const directionIcon = this.selectedSort.direction === SortDirection.ASCENDING ? 'pi pi-arrow-up' : 'pi pi-arrow-down'; + const directionIcon = this.selectedSort!.direction === SortDirection.ASCENDING ? 'pi pi-arrow-up' : 'pi pi-arrow-down'; this.sortOptions = this.sortOptions.map((option) => ({ ...option, - icon: option.field === this.selectedSort.field ? directionIcon : '', + icon: option.field === this.selectedSort!.field ? directionIcon : '', })); }