From 7345f3f0ee6e810796d27910f62be65c7def53a5 Mon Sep 17 00:00:00 2001 From: "aditya.chandel" <> Date: Wed, 25 Dec 2024 22:01:23 -0700 Subject: [PATCH] Refactor BooksBrowserComponent --- .../books-browser/books-browser.component.ts | 283 ++++++++---------- .../src/app/book/model/library.model.ts | 4 +- booklore-ui/src/app/book/model/shelf.model.ts | 4 +- .../src/app/book/model/sort-option.model.ts | 5 - booklore-ui/src/app/book/model/sort.model.ts | 10 +- .../src/app/book/service/library.service.ts | 4 +- .../src/app/book/service/shelf.service.ts | 4 +- .../app/book/service/sort-options.helper.ts | 14 + .../src/app/book/service/sort.service.ts | 28 +- 9 files changed, 186 insertions(+), 170 deletions(-) delete mode 100644 booklore-ui/src/app/book/model/sort-option.model.ts create mode 100644 booklore-ui/src/app/book/service/sort-options.helper.ts 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 4c5f8d488..9062886c0 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 @@ -1,18 +1,19 @@ -import {Component, OnInit} from '@angular/core'; -import {ActivatedRoute, Router} from '@angular/router'; -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} from 'rxjs'; -import {Book} 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'; -import {Shelf} from '../../model/shelf.model'; +import { Component, OnInit } from '@angular/core'; +import { ActivatedRoute, Router } from '@angular/router'; +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 } from 'rxjs'; +import { Book } from '../../model/book.model'; +import { ShelfService } from '../../service/shelf.service'; +import { ShelfAssignerComponent } from '../shelf-assigner/shelf-assigner.component'; +import { DialogService } from 'primeng/dynamicdialog'; +import { Library } from '../../model/library.model'; +import { Shelf } from '../../model/shelf.model'; +import { SortService } from '../../service/sort.service'; +import {SortOptionsHelper} from '../../service/sort-options.helper'; +import {SortOption} from '../../model/sort.model'; @Component({ selector: 'app-books-browser', @@ -25,18 +26,12 @@ export class BooksBrowserComponent implements OnInit { entity$: Observable | undefined; entityType$: Observable | undefined; - items: MenuItem[] | undefined; - selectedBooks: Set = new Set(); entity: Library | Shelf | null = null; entityType: string = ''; + items: MenuItem[] | undefined; + selectedBooks: Set = new Set(); selectedSort: SortOption | null = null; - - sortOptions: SortOption[] = [ - {label: '↑ Title', field: 'title', direction: 'ASCENDING'}, - {label: '↓ Title', field: 'title', direction: 'DESCENDING'}, - {label: '↑ Published Date', field: 'publishedDate', direction: 'ASCENDING'}, - {label: '↓ Published Date', field: 'publishedDate', direction: 'DESCENDING'} - ]; + sortOptions: SortOption[] = []; constructor( private activatedRoute: ActivatedRoute, @@ -46,40 +41,25 @@ export class BooksBrowserComponent implements OnInit { private libraryService: LibraryService, private bookService: BookService, private shelfService: ShelfService, - private dialogService: DialogService - ) { - } - - private setSelectedSortFromEntity(entity: Library | Shelf | null): void { - if (entity?.sort) { - const {field, direction} = entity.sort; - this.selectedSort = this.sortOptions.find( - option => option.field === field && option.direction === direction - ) || null; - } else { - this.selectedSort = null; - } - } + private dialogService: DialogService, + private sortService: SortService + ) {} ngOnInit(): void { - const routeParam$ = this.activatedRoute.paramMap.pipe( - map(params => { - const libraryId = Number(params.get('libraryId')); - const shelfId = Number(params.get('shelfId')); - return {libraryId, shelfId}; - }) - ); + this.sortOptions = SortOptionsHelper.generateSortOptions(); + + const routeParam$ = this.getRouteParams(); this.books$ = routeParam$.pipe( - switchMap(({libraryId, shelfId}) => this.fetchBooks(libraryId, shelfId)) + switchMap(({ libraryId, shelfId }) => this.fetchBooks(libraryId, shelfId)) ); this.entity$ = routeParam$.pipe( - switchMap(({libraryId, shelfId}) => this.fetchEntity(libraryId, shelfId)) + switchMap(({ libraryId, shelfId }) => this.fetchEntity(libraryId, shelfId)) ); this.entityType$ = routeParam$.pipe( - switchMap(({libraryId, shelfId}) => this.fetchEntityType(libraryId, shelfId)) + map(({ libraryId, shelfId }) => libraryId ? 'Library' : shelfId ? 'Shelf' : null) ); this.entity$.subscribe(entity => { @@ -90,44 +70,62 @@ export class BooksBrowserComponent implements OnInit { this.setupMenu(); } - 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 { - return this.bookService.books$.pipe( - map(books => { - let filteredBooks = books; - if (libraryId) { - filteredBooks = books.filter(book => book.libraryId === libraryId); - } else if (shelfId) { - filteredBooks = books.filter(book => book.shelves?.some(shelf => shelf.id === shelfId)); - } - return this.applySort(filteredBooks); + private getRouteParams(): Observable<{ libraryId: number; shelfId: number }> { + return this.activatedRoute.paramMap.pipe( + map(params => { + const libraryId = Number(params.get('libraryId')); + const shelfId = Number(params.get('shelfId')); + return { libraryId, shelfId }; }) ); } + private fetchBooks(libraryId: number, shelfId: number): Observable { + if (libraryId) { + return this.fetchBooksByLibrary(libraryId); + } else if (shelfId) { + return this.fetchBooksByShelf(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) - ); + this.entityType = 'Library'; + return this.fetchLibrary(libraryId); } else if (shelfId) { - return this.shelfService.shelves$.pipe( - map(shelves => shelves.find(shelf => shelf.id === shelfId) || null) - ); + this.entityType = 'Shelf'; + return this.fetchShelf(shelfId); } return of(null); } + private fetchBooksByLibrary(libraryId: number): Observable { + return this.bookService.books$.pipe( + map(books => books.filter(book => book.libraryId === libraryId)), + map(books => this.sortService.applySort(books, this.selectedSort)) + ); + } + + private fetchBooksByShelf(shelfId: number): Observable { + return this.bookService.books$.pipe( + map(books => books.filter(book => book.shelves?.some(shelf => shelf.id === shelfId))), + map(books => this.sortService.applySort(books, this.selectedSort)) + ); + } + + private fetchLibrary(libraryId: number): Observable { + return this.libraryService.libraries$.pipe( + map(libraries => libraries.find(lib => lib.id === libraryId) || null) + ); + } + + private fetchShelf(shelfId: number): Observable { + return this.shelfService.shelves$.pipe( + map(shelves => shelves.find(shelf => shelf.id === shelfId) || null) + ); + } + handleBookSelect(bookId: number, selected: boolean): void { if (selected) { this.selectedBooks.add(bookId); @@ -136,6 +134,50 @@ export class BooksBrowserComponent implements OnInit { } } + deselectAllBooks(): void { + this.selectedBooks.clear(); + } + + isAnyBookSelected(): boolean { + return this.selectedBooks.size > 0; + } + + unshelfBooks() { + if (this.entity) { + this.bookService.updateBookShelves(this.selectedBooks, new Set(), new Set([this.entity.id])).subscribe( + () => { + 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 books shelves' }); + } + ); + } + } + + updateSortOption(sortOption: SortOption): void { + this.selectedSort = sortOption; + const routeParam$ = this.getRouteParams(); + this.books$ = routeParam$.pipe( + switchMap(({ libraryId, shelfId }) => this.fetchBooks(libraryId, shelfId)) + ); + if (this.entityType === 'Library') { + this.libraryService.updateSort(this.entity?.id!, sortOption).subscribe(); + } else if (this.entityType === 'Shelf') { + this.shelfService.updateSort(this.entity?.id!, sortOption).subscribe(); + } + } + + private setSelectedSortFromEntity(entity: Library | Shelf | null): void { + if (entity?.sort) { + const { field, direction } = entity.sort; + this.selectedSort = this.sortOptions.find(option => option.field === field && option.direction === direction) || null; + } else { + this.selectedSort = null; + } + } + setupMenu(): void { this.items = [ { @@ -144,87 +186,24 @@ export class BooksBrowserComponent implements OnInit { { label: 'Edit shelf', icon: 'pi pi-folder', - command: () => this.openShelfDialog(), + command: () => { + this.dialogService.open(ShelfAssignerComponent, { + header: `Update Books Shelves`, + modal: true, + width: '30%', + height: '70%', + contentStyle: { overflow: 'auto' }, + baseZIndex: 10, + data: { + isMultiBooks: true, + bookIds: this.selectedBooks + }, + }); + } } ], }, ]; } - private applySort(books: Book[]): Book[] { - if (!this.selectedSort) return books; - - const {field, direction} = this.selectedSort; - return books.sort((a, b) => { - let valueA: any, valueB: any; - if (field === 'title') { - valueA = a.metadata?.title?.toLowerCase() || ''; - valueB = b.metadata?.title?.toLowerCase() || ''; - } else if (field === 'publishedDate') { - valueA = new Date(a.metadata?.publishedDate || 0).getTime(); - valueB = new Date(b.metadata?.publishedDate || 0).getTime(); - } - if (valueA === undefined || valueB === undefined) return 0; - if (direction === 'ASCENDING') { - return valueA < valueB ? -1 : valueA > valueB ? 1 : 0; - } else { - return valueA > valueB ? -1 : valueA < valueB ? 1 : 0; - } - }); - } - - deselectAllBooks(): void { - this.selectedBooks.clear(); - } - - isAnyBookSelected(): boolean { - return this.selectedBooks.size > 0; - } - - openShelfDialog(): void { - this.dialogService.open(ShelfAssignerComponent, { - header: `Update Books Shelves`, - modal: true, - width: '30%', - height: '70%', - contentStyle: {overflow: 'auto'}, - baseZIndex: 10, - data: { - isMultiBooks: true, - bookIds: this.selectedBooks - }, - }); - } - - unshelfBooks() { - if (this.entity) { - this.bookService.updateBookShelves(this.selectedBooks, new Set(), new Set([this.entity.id])).subscribe( - () => { - 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 books shelves'}); - } - ); - } - } - - updateSortOption(sortOption: SortOption): void { - this.selectedSort = sortOption; - this.books$ = this.activatedRoute.paramMap.pipe( - map(params => { - const libraryId = Number(params.get('libraryId')); - const shelfId = Number(params.get('shelfId')); - return {libraryId, shelfId}; - }), - switchMap(({libraryId, shelfId}) => this.fetchBooks(libraryId, shelfId)) - ); - if (this.entityType === 'Library') { - this.libraryService.updateSort(this.entity?.id!, sortOption).subscribe(); - } else if (this.entityType === 'Shelf') { - this.shelfService.updateSort(this.entity?.id!, sortOption).subscribe(); - } - } - } diff --git a/booklore-ui/src/app/book/model/library.model.ts b/booklore-ui/src/app/book/model/library.model.ts index 852c7c508..bd1810caf 100644 --- a/booklore-ui/src/app/book/model/library.model.ts +++ b/booklore-ui/src/app/book/model/library.model.ts @@ -1,8 +1,8 @@ -import {Sort} from './sort.model'; +import {SortOption} from './sort.model'; export interface Library { id?: number; name: string; - sort?: Sort; + sort?: SortOption; paths: string[]; } diff --git a/booklore-ui/src/app/book/model/shelf.model.ts b/booklore-ui/src/app/book/model/shelf.model.ts index 01da8012a..8b284f860 100644 --- a/booklore-ui/src/app/book/model/shelf.model.ts +++ b/booklore-ui/src/app/book/model/shelf.model.ts @@ -1,7 +1,7 @@ -import {Sort} from './sort.model'; +import {SortOption} from './sort.model'; export interface Shelf { id?: number; name: string; - sort?: Sort; + sort?: SortOption; } diff --git a/booklore-ui/src/app/book/model/sort-option.model.ts b/booklore-ui/src/app/book/model/sort-option.model.ts deleted file mode 100644 index 405782ed4..000000000 --- a/booklore-ui/src/app/book/model/sort-option.model.ts +++ /dev/null @@ -1,5 +0,0 @@ -export interface SortOption { - label: string; - field: string; - direction: string; -} diff --git a/booklore-ui/src/app/book/model/sort.model.ts b/booklore-ui/src/app/book/model/sort.model.ts index 96569cf42..8d8babc22 100644 --- a/booklore-ui/src/app/book/model/sort.model.ts +++ b/booklore-ui/src/app/book/model/sort.model.ts @@ -1,4 +1,10 @@ -export interface Sort { +export enum SortDirection { + ASCENDING = 'ASCENDING', + DESCENDING = 'DESCENDING' +} + +export interface SortOption { + label: string; field: string; - direction: string; + direction: SortDirection; } diff --git a/booklore-ui/src/app/book/service/library.service.ts b/booklore-ui/src/app/book/service/library.service.ts index 68244eadf..eaa307ca3 100644 --- a/booklore-ui/src/app/book/service/library.service.ts +++ b/booklore-ui/src/app/book/service/library.service.ts @@ -5,7 +5,7 @@ import {catchError, map} from 'rxjs/operators'; import {Library} from '../model/library.model'; import {BookService} from './book.service'; import {BookWithNeighborsDTO} from '../model/book.model'; -import {Sort} from '../model/sort.model'; +import {SortOption} from '../model/sort.model'; @Injectable({ providedIn: 'root' @@ -52,7 +52,7 @@ export class LibraryService { ); } - updateSort(libraryId: number, sort: Sort): Observable { + updateSort(libraryId: number, sort: SortOption): Observable { return this.http.put(`${this.url}/${libraryId}/sort`, sort).pipe( map(updatedLibrary => { const updatedShelves = this.libraries.value.map(library => diff --git a/booklore-ui/src/app/book/service/shelf.service.ts b/booklore-ui/src/app/book/service/shelf.service.ts index 878f57679..b801f789e 100644 --- a/booklore-ui/src/app/book/service/shelf.service.ts +++ b/booklore-ui/src/app/book/service/shelf.service.ts @@ -3,7 +3,7 @@ import {BehaviorSubject, Observable, of} from 'rxjs'; import {catchError, map} from 'rxjs/operators'; import {HttpClient} from '@angular/common/http'; import {Shelf} from '../model/shelf.model'; -import {Sort} from '../model/sort.model'; +import {SortOption} from '../model/sort.model'; @Injectable({ providedIn: 'root' @@ -37,7 +37,7 @@ export class ShelfService { ); } - updateSort(shelfId: number, sort: Sort): Observable { + updateSort(shelfId: number, sort: SortOption): Observable { return this.http.put(`${this.url}/${shelfId}/sort`, { shelfId, sort }).pipe( map(updatedShelf => { const updatedShelves = this.shelves.value.map(shelf => diff --git a/booklore-ui/src/app/book/service/sort-options.helper.ts b/booklore-ui/src/app/book/service/sort-options.helper.ts new file mode 100644 index 000000000..d1f7dedd4 --- /dev/null +++ b/booklore-ui/src/app/book/service/sort-options.helper.ts @@ -0,0 +1,14 @@ +import {SortDirection} from '../model/sort.model'; + +export class SortOptionsHelper { + + static generateSortOptions(): { label: string, field: string, direction: SortDirection }[] { + return [ + {label: '↑ Title', field: 'title', direction: SortDirection.ASCENDING}, + {label: '↓ Title', field: 'title', direction: SortDirection.DESCENDING}, + {label: '↑ Published Date', field: 'publishedDate', direction: SortDirection.ASCENDING}, + {label: '↓ Published Date', field: 'publishedDate', direction: SortDirection.DESCENDING} + ]; + } + +} diff --git a/booklore-ui/src/app/book/service/sort.service.ts b/booklore-ui/src/app/book/service/sort.service.ts index 650660494..a8fcb4f16 100644 --- a/booklore-ui/src/app/book/service/sort.service.ts +++ b/booklore-ui/src/app/book/service/sort.service.ts @@ -1,12 +1,34 @@ -import { Injectable } from '@angular/core'; -import { SortOption } from '../model/sort-option.model'; +import {Injectable} from '@angular/core'; +import {Book} from '../model/book.model'; +import {SortOption} from "../model/sort.model"; @Injectable({ providedIn: 'root', }) export class SortService { + applySort(books: Book[], selectedSort: SortOption | null): Book[] { + if (!selectedSort) return books; + const { field, direction } = selectedSort; + return books.sort((a, b) => { + let valueA: any, valueB: any; + if (field === 'title') { + valueA = a.metadata?.title?.toLowerCase() || ''; + valueB = b.metadata?.title?.toLowerCase() || ''; + } else if (field === 'publishedDate') { + valueA = new Date(a.metadata?.publishedDate || 0).getTime(); + valueB = new Date(b.metadata?.publishedDate || 0).getTime(); + } + + if (valueA === undefined || valueB === undefined) return 0; + + if (direction === 'ASCENDING') { + return valueA < valueB ? -1 : valueA > valueB ? 1 : 0; + } else { + return valueA > valueB ? -1 : valueA < valueB ? 1 : 0; + } + }); + } } -