From b4f98296820f1de37010c79dcedced5671a59320 Mon Sep 17 00:00:00 2001 From: "aditya.chandel" <> Date: Wed, 25 Dec 2024 16:25:37 -0700 Subject: [PATCH] Fix sorting --- .../books-browser.component.html | 17 +- .../books-browser/books-browser.component.ts | 162 +++++++++++++----- .../src/app/book/model/library.model.ts | 7 - .../src/app/book/model/sort-option.model.ts | 4 + .../src/app/book/service/sort.service.ts | 25 +++ 5 files changed, 162 insertions(+), 53 deletions(-) create mode 100644 booklore-ui/src/app/book/model/sort-option.model.ts create mode 100644 booklore-ui/src/app/book/service/sort.service.ts diff --git a/booklore-ui/src/app/book/component/books-browser/books-browser.component.html b/booklore-ui/src/app/book/component/books-browser/books-browser.component.html index f87faee0d..32e6ad407 100644 --- a/booklore-ui/src/app/book/component/books-browser/books-browser.component.html +++ b/booklore-ui/src/app/book/component/books-browser/books-browser.component.html @@ -13,18 +13,21 @@
- - - + + +
- + +
diff --git a/booklore-ui/src/app/book/component/books-browser/books-browser.component.ts b/booklore-ui/src/app/book/component/books-browser/books-browser.component.ts index 108b2c198..80050dbc3 100644 --- a/booklore-ui/src/app/book/component/books-browser/books-browser.component.ts +++ b/booklore-ui/src/app/book/component/books-browser/books-browser.component.ts @@ -4,13 +4,15 @@ import {ConfirmationService, MenuItem, MessageService} from 'primeng/api'; import {LibraryService} from '../../service/library.service'; import {BookService} from '../../service/book.service'; import {map, switchMap} from 'rxjs/operators'; -import {Observable, of, Subject} from 'rxjs'; +import {Observable, of} from 'rxjs'; import {Book} from '../../model/book.model'; -import {Library} from '../../model/library.model'; import {Shelf} from '../../model/book.model'; import {ShelfService} from '../../service/shelf.service'; import {ShelfAssignerComponent} from '../shelf-assigner/shelf-assigner.component'; import {DialogService} from 'primeng/dynamicdialog'; +import {SortOption} from '../../model/sort-option.model'; +import {SortService} from '../../service/sort.service'; +import {Library} from '../../model/library.model'; @Component({ selector: 'app-books-browser', @@ -22,9 +24,18 @@ export class BooksBrowserComponent implements OnInit { books$: Observable | undefined; entity$: Observable | undefined; entityType$: Observable | undefined; + items: MenuItem[] | undefined; selectedBooks: Set = new Set(); entity: Library | Shelf | null = null; + entityType: string = ''; + selectedSort: SortOption | null = null; + sortOptions: SortOption[] = [ + {label: '↑ Title', value: 'ascendingTitle'}, + {label: '↓ Title', value: 'descendingTitle'}, + {label: '↑ Published Date', value: 'ascendingDate'}, + {label: '↓ Published Date', value: 'descendingDate'} + ]; constructor( private activatedRoute: ActivatedRoute, @@ -34,7 +45,8 @@ export class BooksBrowserComponent implements OnInit { private libraryService: LibraryService, private bookService: BookService, private shelfService: ShelfService, - private dialogService: DialogService + private dialogService: DialogService, + private sortService: SortService ) { } @@ -48,53 +60,88 @@ export class BooksBrowserComponent implements OnInit { ); this.books$ = routeParam$.pipe( - switchMap(({libraryId, shelfId}) => { - if (!isNaN(libraryId) && libraryId !== 0) { - return this.bookService.books$.pipe( - map(books => books.filter(book => book.libraryId === libraryId)) - ); - } else if (!isNaN(shelfId) && shelfId !== 0) { - return this.bookService.books$.pipe( - map(books => books.filter(book => book.shelves?.some(shelf => shelf.id === shelfId))) - ); - } else { - return of([]); - } - }) + switchMap(({libraryId, shelfId}) => this.fetchBooks(libraryId, shelfId)) ); this.entity$ = routeParam$.pipe( - switchMap(({libraryId, shelfId}) => { - if (!isNaN(libraryId) && libraryId !== 0) { - return this.libraryService.libraries$.pipe( - map(libraries => libraries.find(lib => lib.id === libraryId) || null) - ); - } else if (!isNaN(shelfId) && shelfId !== 0) { - return this.shelfService.shelves$.pipe( - map(shelves => shelves.find(shelf => shelf.id === shelfId) || null) - ); - } - return of(null); - }) + switchMap(({libraryId, shelfId}) => this.fetchEntity(libraryId, shelfId)) ); this.entityType$ = routeParam$.pipe( - switchMap(({libraryId, shelfId}) => { - if (!isNaN(libraryId) && libraryId !== 0) { - return of('Library'); - } else if (!isNaN(shelfId) && shelfId !== 0) { - return of('Shelf'); - } - return of(null); - }) + switchMap(({libraryId, shelfId}) => this.fetchEntityType(libraryId, shelfId)) ); this.entity$.subscribe(entity => { this.entity = entity; }); - this.setupMenu(); + routeParam$.subscribe(({ libraryId, shelfId }) => { + this.handleSortingState(libraryId, shelfId); + }); + this.setupMenu(); + } + + private handleSortingState(libraryId: number, shelfId: number): void { + let entityType: string | null = null; + let entityId: number | null = null; + + if (libraryId) { + entityType = 'Library'; + entityId = libraryId; + } else if (shelfId) { + entityType = 'Shelf'; + entityId = shelfId; + } + + if (entityType && entityId !== null) { + const key = this.sortService.generateKey(entityType, entityId); + if (key) { + const storedSort = this.sortService.getSortOption(key); + if (storedSort) { + this.selectedSort = storedSort; + } else { + this.selectedSort = null; + } + } + } + } + + private fetchEntityType(libraryId: number, shelfId: number): Observable { + if (!isNaN(libraryId) && libraryId !== 0) { + this.entityType = 'Library'; + return of('Library'); + } else if (!isNaN(shelfId) && shelfId !== 0) { + this.entityType = 'Shelf'; + return of('Shelf'); + } + return of(''); + } + + private fetchBooks(libraryId: number, shelfId: number): Observable { + if (libraryId) { + return this.bookService.books$.pipe( + map(books => books.filter(book => book.libraryId === libraryId)) + ); + } else if (shelfId) { + return this.bookService.books$.pipe( + map(books => books.filter(book => book.shelves?.some(shelf => shelf.id === shelfId))) + ); + } + return of([]); + } + + private fetchEntity(libraryId: number, shelfId: number): Observable { + if (libraryId) { + return this.libraryService.libraries$.pipe( + map(libraries => libraries.find(lib => lib.id === libraryId) || null) + ); + } else if (shelfId) { + return this.shelfService.shelves$.pipe( + map(shelves => shelves.find(shelf => shelf.id === shelfId) || null) + ); + } + return of(null); } handleBookSelect(bookId: number, selected: boolean): void { @@ -120,6 +167,34 @@ export class BooksBrowserComponent implements OnInit { ]; } + sortBooks(): void { + if (this.books$ && this.selectedSort) { + this.books$ = this.books$.pipe( + map(books => { + return books.sort((a, b) => { + const titleA = a.metadata?.title?.toLowerCase() || ''; + const titleB = b.metadata?.title?.toLowerCase() || ''; + const dateA = new Date(a.metadata?.publishedDate || 0); + const dateB = new Date(b.metadata?.publishedDate || 0); + + switch (this.selectedSort?.value) { + case 'ascendingTitle': + return titleA.localeCompare(titleB); + case 'descendingTitle': + return titleB.localeCompare(titleA); + case 'ascendingDate': + return dateA.getTime() - dateB.getTime(); + case 'descendingDate': + return dateB.getTime() - dateA.getTime(); + default: + return 0; + } + }); + }) + ); + } + } + deselectAllBooks(): void { this.selectedBooks.clear(); } @@ -147,14 +222,23 @@ export class BooksBrowserComponent implements OnInit { if (this.entity) { this.bookService.updateBookShelves(this.selectedBooks, new Set(), new Set([this.entity.id])).subscribe( () => { - this.messageService.add({severity: 'info', summary: 'Success', detail: 'Book\'s shelves updated'}); + this.messageService.add({severity: 'info', summary: 'Success', detail: 'Books shelves updated'}); this.selectedBooks = new Set(); }, (error) => { - this.messageService.add({severity: 'error', summary: 'Error', detail: 'Failed to update book\'s shelves'}); + this.messageService.add({severity: 'error', summary: 'Error', detail: 'Failed to update books shelves'}); } ); } } + updateSortOption(sortOption: SortOption): void { + const key = this.sortService.generateKey(this.entityType, this.entity?.id!); + if (key) { + this.sortService.setSortOption(key, sortOption); + this.selectedSort = sortOption; + this.sortBooks(); + } + } + } diff --git a/booklore-ui/src/app/book/model/library.model.ts b/booklore-ui/src/app/book/model/library.model.ts index db7322233..23d2e8891 100644 --- a/booklore-ui/src/app/book/model/library.model.ts +++ b/booklore-ui/src/app/book/model/library.model.ts @@ -3,10 +3,3 @@ export interface Library { name: string; paths: string[]; } - -export interface LibraryApiResponse { - content: Library[]; - totalElements: number; - totalPages: number; - numberOfElements: number; -} diff --git a/booklore-ui/src/app/book/model/sort-option.model.ts b/booklore-ui/src/app/book/model/sort-option.model.ts new file mode 100644 index 000000000..2c1dbeaf4 --- /dev/null +++ b/booklore-ui/src/app/book/model/sort-option.model.ts @@ -0,0 +1,4 @@ +export interface SortOption { + label: string; + value: string; +} diff --git a/booklore-ui/src/app/book/service/sort.service.ts b/booklore-ui/src/app/book/service/sort.service.ts new file mode 100644 index 000000000..6fbbc53f7 --- /dev/null +++ b/booklore-ui/src/app/book/service/sort.service.ts @@ -0,0 +1,25 @@ +import { Injectable } from '@angular/core'; +import { SortOption } from '../model/sort-option.model'; + +@Injectable({ + providedIn: 'root', +}) +export class SortService { + private selectedSorts: { [key: string]: SortOption | null } = {}; + + generateKey(entityType: string, entityId: number): string { + if (entityType && entityId) { + return `${entityType.toLowerCase()}-${entityId}`; + } + return ''; + } + + getSortOption(key: string): SortOption | null { + return this.selectedSorts[key] || null; + } + + setSortOption(key: string, sortOption: SortOption): void { + this.selectedSorts[key] = sortOption; + } +} +