Sync url params for view mode and sort order (grid/table, sorting, direction)
This commit is contained in:
committed by
Aditya Chandel
parent
9f9b84e650
commit
6baf04d2ec
@@ -106,9 +106,9 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="book-card-container" *ngIf="bookState?.books as books">
|
<div class="book-card-container" *ngIf="bookState?.books as books">
|
||||||
<app-book-table *ngIf="gridOrTable === 'table' && books.length > 0" [books]="books" (selectedBooksChange)="onSelectedBooksChange($event)" [sortOption]="selectedSort"></app-book-table>
|
<app-book-table *ngIf="currentViewMode === 'table' && books.length > 0" [books]="books" (selectedBooksChange)="onSelectedBooksChange($event)" [sortOption]="selectedSort!"></app-book-table>
|
||||||
|
|
||||||
<virtual-scroller *ngIf="gridOrTable === 'grid'" class="virtual-scroller" #scroll [items]="books">
|
<virtual-scroller *ngIf="currentViewMode === 'grid'" class="virtual-scroller" #scroll [items]="books">
|
||||||
<div class="grid grid-cols-12 gap-4" #container>
|
<div class="grid grid-cols-12 gap-4" #container>
|
||||||
<div class="virtual-scroller-item" *ngFor="let book of scroll.viewPortItems">
|
<div class="virtual-scroller-item" *ngFor="let book of scroll.viewPortItems">
|
||||||
<app-book-card
|
<app-book-card
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import {AfterViewInit, Component, inject, OnInit, ViewChild} from '@angular/core';
|
import {AfterViewInit, Component, inject, OnInit, ViewChild} from '@angular/core';
|
||||||
import {ActivatedRoute} from '@angular/router';
|
import {ActivatedRoute, Router} from '@angular/router';
|
||||||
import {ConfirmationService, MenuItem, MessageService, PrimeTemplate} from 'primeng/api';
|
import {MenuItem, MessageService, PrimeTemplate} from 'primeng/api';
|
||||||
import {LibraryService} from '../../service/library.service';
|
import {LibraryService} from '../../service/library.service';
|
||||||
import {BookService} from '../../service/book.service';
|
import {BookService} from '../../service/book.service';
|
||||||
import {map, switchMap} from 'rxjs/operators';
|
import {map, switchMap} from 'rxjs/operators';
|
||||||
@@ -39,6 +39,22 @@ export enum EntityType {
|
|||||||
ALL_BOOKS = 'All Books'
|
ALL_BOOKS = 'All Books'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const QUERY_PARAMS = {
|
||||||
|
VIEW: 'view',
|
||||||
|
SORT: 'sort',
|
||||||
|
DIRECTION: 'direction',
|
||||||
|
};
|
||||||
|
|
||||||
|
const VIEW_MODES = {
|
||||||
|
GRID: 'grid',
|
||||||
|
TABLE: 'table',
|
||||||
|
};
|
||||||
|
|
||||||
|
const SORT_DIRECTION = {
|
||||||
|
ASCENDING: 'asc',
|
||||||
|
DESCENDING: 'desc',
|
||||||
|
};
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-book-browser',
|
selector: 'app-book-browser',
|
||||||
standalone: true,
|
standalone: true,
|
||||||
@@ -77,12 +93,11 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
|||||||
dynamicDialogRef: DynamicDialogRef | undefined;
|
dynamicDialogRef: DynamicDialogRef | undefined;
|
||||||
EntityType = EntityType;
|
EntityType = EntityType;
|
||||||
|
|
||||||
gridOrTable: string = 'grid';
|
|
||||||
|
|
||||||
@ViewChild(BookTableComponent) bookTableComponent!: BookTableComponent;
|
@ViewChild(BookTableComponent) bookTableComponent!: BookTableComponent;
|
||||||
@ViewChild(BookFilterComponent) bookFilterComponent!: BookFilterComponent;
|
@ViewChild(BookFilterComponent) bookFilterComponent!: BookFilterComponent;
|
||||||
|
|
||||||
selectedFilter = new BehaviorSubject<{ type: string; value: any } | null>(null);
|
selectedFilter = new BehaviorSubject<{ type: string; value: any } | null>(null);
|
||||||
|
protected resetFilterSubject = new Subject<void>();
|
||||||
|
|
||||||
protected userService = inject(UserService);
|
protected userService = inject(UserService);
|
||||||
private activatedRoute = inject(ActivatedRoute);
|
private activatedRoute = inject(ActivatedRoute);
|
||||||
@@ -92,11 +107,9 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
|||||||
private shelfService = inject(ShelfService);
|
private shelfService = inject(ShelfService);
|
||||||
private dialogService = inject(DialogService);
|
private dialogService = inject(DialogService);
|
||||||
private sortService = inject(SortService);
|
private sortService = inject(SortService);
|
||||||
|
private router = inject(Router);
|
||||||
private libraryShelfMenuService = inject(LibraryShelfMenuService);
|
private libraryShelfMenuService = inject(LibraryShelfMenuService);
|
||||||
|
|
||||||
protected resetFilterSubject = new Subject<void>();
|
|
||||||
|
|
||||||
|
|
||||||
sortOptions: any[] = [
|
sortOptions: any[] = [
|
||||||
{label: 'Title', icon: '', field: 'title', command: () => this.sortBooks('title')},
|
{label: 'Title', icon: '', field: 'title', command: () => this.sortBooks('title')},
|
||||||
{label: 'Author', icon: '', field: 'author', command: () => this.sortBooks('author')},
|
{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')}
|
{label: 'Pages', icon: '', field: 'pageCount', command: () => this.sortBooks('pageCount')}
|
||||||
];
|
];
|
||||||
|
|
||||||
selectedSort: SortOption = {
|
selectedSort: SortOption | undefined = undefined;
|
||||||
label: 'Added On',
|
currentViewMode: string | undefined = undefined;
|
||||||
field: 'addedOn',
|
lastAppliedSort: SortOption | null = null;
|
||||||
direction: SortDirection.ASCENDING,
|
|
||||||
};
|
|
||||||
|
|
||||||
ngOnInit(): void {
|
ngOnInit(): void {
|
||||||
this.bookService.loadBooks();
|
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';
|
const isAllBooksRoute = this.activatedRoute.snapshot.routeConfig?.path === 'all-books';
|
||||||
|
|
||||||
if (isAllBooksRoute) {
|
if (isAllBooksRoute) {
|
||||||
@@ -171,12 +233,17 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
|||||||
return (entity as Library).paths !== undefined;
|
return (entity as Library).paths !== undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
toggleTableGrid() {
|
toggleTableGrid(): void {
|
||||||
this.gridOrTable = this.gridOrTable === 'grid' ? 'table' : 'grid';
|
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 {
|
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 }> {
|
private getEntityInfoFromRoute(): Observable<{ entityId: number; entityType: EntityType }> {
|
||||||
@@ -300,7 +367,7 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
|||||||
map(bookState => {
|
map(bookState => {
|
||||||
if (bookState.loaded && !bookState.error) {
|
if (bookState.loaded && !bookState.error) {
|
||||||
const filteredBooks = bookState.books?.filter(bookFilter) || [];
|
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, books: sortedBooks};
|
||||||
}
|
}
|
||||||
return bookState;
|
return bookState;
|
||||||
@@ -312,7 +379,7 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
|||||||
|
|
||||||
private processBookState(bookState: BookState): BookState {
|
private processBookState(bookState: BookState): BookState {
|
||||||
if (bookState.loaded && !bookState.error) {
|
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, books: sortedBooks};
|
||||||
}
|
}
|
||||||
return bookState;
|
return bookState;
|
||||||
@@ -452,22 +519,44 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
|||||||
this.clearSearch();
|
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) {
|
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 {
|
} else {
|
||||||
this.selectedSort.field = field;
|
this.selectedSort = {
|
||||||
this.selectedSort.direction = SortDirection.ASCENDING;
|
label: existingSort.label,
|
||||||
|
field: existingSort.field,
|
||||||
|
direction: SortDirection.ASCENDING
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
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() {
|
||||||
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) => ({
|
this.sortOptions = this.sortOptions.map((option) => ({
|
||||||
...option,
|
...option,
|
||||||
icon: option.field === this.selectedSort.field ? directionIcon : '',
|
icon: option.field === this.selectedSort!.field ? directionIcon : '',
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user