Refactor BooksBrowserComponent

This commit is contained in:
aditya.chandel
2024-12-25 22:01:23 -07:00
parent 383a3189bb
commit 7345f3f0ee
9 changed files with 186 additions and 170 deletions
@@ -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<Library | Shelf | null> | undefined;
entityType$: Observable<string | null> | undefined;
items: MenuItem[] | undefined;
selectedBooks: Set<number> = new Set();
entity: Library | Shelf | null = null;
entityType: string = '';
items: MenuItem[] | undefined;
selectedBooks: Set<number> = 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<string> {
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<Book[]> {
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<Book[]> {
if (libraryId) {
return this.fetchBooksByLibrary(libraryId);
} else if (shelfId) {
return this.fetchBooksByShelf(shelfId);
}
return of([]);
}
private fetchEntity(libraryId: number, shelfId: number): Observable<Library | Shelf | null> {
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<Book[]> {
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<Book[]> {
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<Library | null> {
return this.libraryService.libraries$.pipe(
map(libraries => libraries.find(lib => lib.id === libraryId) || null)
);
}
private fetchShelf(shelfId: number): Observable<Shelf | null> {
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<number>();
},
(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<number>();
},
(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();
}
}
}