diff --git a/booklore-ui/.editorconfig b/booklore-ui/.editorconfig index f166060d..91536513 100644 --- a/booklore-ui/.editorconfig +++ b/booklore-ui/.editorconfig @@ -1,17 +1,17 @@ -# Editor configuration, see https://editorconfig.org root = true [*] charset = utf-8 indent_style = space indent_size = 2 +end_of_line = lf insert_final_newline = true trim_trailing_whitespace = true [*.ts] -quote_type = single ij_typescript_use_double_quotes = false [*.md] max_line_length = off -trim_trailing_whitespace = false +insert_final_newline = false +trim_trailing_whitespace = false \ No newline at end of file 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 6be52b23..dad31858 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 @@ -18,74 +18,57 @@
- - - - +
+ + + + - - - - + + + + + +
+ {{ item.label }} + +
+
+
- - - - + + + + + +
- - + +
- - - - - - (); - selectedSort: SortOption | null = null; - sortOptions: SortOption[] = []; isDrawerVisible: boolean = false; dynamicDialogRef: DynamicDialogRef | undefined; EntityType = EntityType; @@ -95,9 +92,24 @@ export class BookBrowserComponent implements OnInit, AfterViewInit { private sortService = inject(SortService); private libraryShelfMenuService = inject(LibraryShelfMenuService); + sortOptions: any[] = [ + {label: 'Title', icon: '', field: 'title', command: () => this.sortBooks('title')}, + {label: 'Publisher', icon: '', field: 'publisher', command: () => this.sortBooks('publisher')}, + {label: 'Published', icon: '', field: 'publishedDate', command: () => this.sortBooks('publishedDate')}, + {label: 'Pages', icon: '', field: 'pageCount', command: () => this.sortBooks('pageCount')}, + {label: 'Rating', icon: '', field: 'rating', command: () => this.sortBooks('rating')}, + {label: 'Reviews', icon: '', field: 'reviewCount', command: () => this.sortBooks('reviewCount')}, + ]; + + selectedSort: SortOption = { + label: 'Title', + field: 'title', + direction: SortDirection.DESCENDING, + }; + ngOnInit(): void { this.bookService.loadBooks(); - this.sortOptions = SortService.generateSortOptions(); + this.sortBooks(this.selectedSort.field); const isAllBooksRoute = this.activatedRoute.snapshot.routeConfig?.path === 'all-books'; if (isAllBooksRoute) { @@ -324,8 +336,7 @@ export class BookBrowserComponent implements OnInit, AfterViewInit { } } - updateSortOption(sortOption: SortOption): void { - this.selectedSort = sortOption; + applySortOption(sortOption: SortOption): void { if (this.entityType === EntityType.ALL_BOOKS) { this.bookState$ = this.fetchAllBooks(); } else { @@ -346,7 +357,11 @@ export class BookBrowserComponent implements OnInit, AfterViewInit { const {field, direction} = entity.sort; this.selectedSort = this.sortOptions.find(option => option.field === field && option.direction === direction) || null; } else { - this.selectedSort = null; + this.selectedSort = { + label: 'Title', + field: 'title', + direction: SortDirection.ASCENDING, + }; } } @@ -412,6 +427,25 @@ export class BookBrowserComponent implements OnInit, AfterViewInit { this.clearSearch(); } + sortBooks(field: string) { + if (this.selectedSort?.field === field) { + this.selectedSort.direction = this.selectedSort.direction === SortDirection.ASCENDING ? SortDirection.DESCENDING : SortDirection.ASCENDING; + } else { + this.selectedSort.field = field; + this.selectedSort.direction = SortDirection.ASCENDING; + } + this.updateSortOptions(); + this.applySortOption(this.selectedSort) + } + + updateSortOptions() { + 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 : '', + })); + } + ngAfterViewInit() { this.bookFilterComponent.authorSelected.subscribe((authorId: number) => { this.selectedAuthor.next(authorId); diff --git a/booklore-ui/src/app/book/service/sort.service.ts b/booklore-ui/src/app/book/service/sort.service.ts index b7b6fe3e..289712ce 100644 --- a/booklore-ui/src/app/book/service/sort.service.ts +++ b/booklore-ui/src/app/book/service/sort.service.ts @@ -38,21 +38,4 @@ export class SortService { } }); } - - static generateSortOptions(): SortOption[] { - return [ - {label: '↑ Title', field: 'title', direction: SortDirection.ASCENDING}, - {label: '↓ Title', field: 'title', direction: SortDirection.DESCENDING}, - {label: '↑ Published', field: 'publishedDate', direction: SortDirection.ASCENDING}, - {label: '↓ Published', field: 'publishedDate', direction: SortDirection.DESCENDING}, - {label: '↑ Pages', field: 'pageCount', direction: SortDirection.ASCENDING}, - {label: '↓ Pages', field: 'pageCount', direction: SortDirection.DESCENDING}, - {label: '↑ Rating', field: 'rating', direction: SortDirection.ASCENDING}, - {label: '↓ Rating', field: 'rating', direction: SortDirection.DESCENDING}, - {label: '↑ Reviews', field: 'reviewCount', direction: SortDirection.ASCENDING}, - {label: '↓ Reviews', field: 'reviewCount', direction: SortDirection.DESCENDING}, - {label: '↑ Publisher', field: 'publisher', direction: SortDirection.ASCENDING}, - {label: '↓ Publisher', field: 'publisher', direction: SortDirection.DESCENDING} - ]; - } }