WIP: Rearrange code
This commit is contained in:
@@ -0,0 +1,337 @@
|
||||
import {Component, OnInit, ViewChild} from '@angular/core';
|
||||
import {ActivatedRoute} from '@angular/router';
|
||||
import {MenuItem, MessageService} from 'primeng/api';
|
||||
import {LibraryService} from '../../service/library.service';
|
||||
import {BookService} from '../../service/book.service';
|
||||
import {map, switchMap} from 'rxjs/operators';
|
||||
import {BehaviorSubject, Observable, of} from 'rxjs';
|
||||
import {ShelfService} from '../../service/shelf.service';
|
||||
import {ShelfAssignerComponent} from '../shelf-assigner/shelf-assigner.component';
|
||||
import {DialogService, DynamicDialogRef} from 'primeng/dynamicdialog';
|
||||
import {Library} from '../../model/library.model';
|
||||
import {Shelf} from '../../model/shelf.model';
|
||||
import {SortService} from '../../service/sort.service';
|
||||
import {SortOption} from '../../model/sort.model';
|
||||
import {BookState} from '../../model/state/book-state.model';
|
||||
import {Book} from '../../model/book.model';
|
||||
import {LibraryShelfMenuService} from '../../service/library-shelf-menu.service';
|
||||
import {SelectButtonChangeEvent} from 'primeng/selectbutton';
|
||||
import {BookTableComponent} from './book-table/book-table.component';
|
||||
import {animate, state, style, transition, trigger} from '@angular/animations';
|
||||
import {MetadataFetchOptionsComponent} from '../../../metadata/metadata-options-dialog/metadata-fetch-options/metadata-fetch-options.component';
|
||||
import {MetadataRefreshType} from '../../../metadata/model/request/metadata-refresh-type.enum';
|
||||
|
||||
export enum EntityType {
|
||||
LIBRARY = 'Library',
|
||||
SHELF = 'Shelf',
|
||||
ALL_BOOKS = 'All Books'
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'app-book-browser',
|
||||
standalone: false,
|
||||
templateUrl: './book-browser.component.html',
|
||||
styleUrls: ['./book-browser.component.scss'],
|
||||
animations: [
|
||||
trigger('slideInOut', [
|
||||
state('void', style({
|
||||
transform: 'translateY(100%)'
|
||||
})),
|
||||
state('*', style({
|
||||
transform: 'translateY(0)'
|
||||
})),
|
||||
transition(':enter', [
|
||||
animate('0.1s ease-in')
|
||||
]),
|
||||
transition(':leave', [
|
||||
animate('0.1s ease-out')
|
||||
])
|
||||
])
|
||||
]
|
||||
})
|
||||
export class BookBrowserComponent implements OnInit {
|
||||
bookState$: Observable<BookState> | undefined;
|
||||
entity$: Observable<Library | Shelf | null> | undefined;
|
||||
entityType$: Observable<EntityType> | undefined;
|
||||
bookTitle$ = new BehaviorSubject<string>('');
|
||||
|
||||
entity: Library | Shelf | null = null;
|
||||
entityType: EntityType | undefined;
|
||||
bookTitle: string = '';
|
||||
entityOptions: MenuItem[] | undefined;
|
||||
selectedBooks: Set<number> = new Set();
|
||||
selectedSort: SortOption | null = null;
|
||||
sortOptions: SortOption[] = [];
|
||||
isDrawerVisible: boolean = false;
|
||||
dynamicDialogRef: DynamicDialogRef | undefined;
|
||||
EntityType = EntityType;
|
||||
|
||||
stateOptions: any[] = [{label: 'Grid', value: 'grid'}, {label: 'Table', value: 'table'}];
|
||||
value: string = 'table';
|
||||
|
||||
@ViewChild(BookTableComponent) bookTableComponent!: BookTableComponent;
|
||||
|
||||
constructor(
|
||||
private activatedRoute: ActivatedRoute,
|
||||
private messageService: MessageService,
|
||||
private libraryService: LibraryService,
|
||||
private bookService: BookService,
|
||||
private shelfService: ShelfService,
|
||||
private dialogService: DialogService,
|
||||
private sortService: SortService,
|
||||
private libraryShelfMenuService: LibraryShelfMenuService) {
|
||||
}
|
||||
|
||||
ngOnInit(): void {
|
||||
this.sortOptions = SortService.generateSortOptions();
|
||||
const isAllBooksRoute = this.activatedRoute.snapshot.routeConfig?.path === 'all-books';
|
||||
|
||||
if (isAllBooksRoute) {
|
||||
this.entityType = EntityType.ALL_BOOKS;
|
||||
this.entityType$ = of(EntityType.ALL_BOOKS);
|
||||
this.bookState$ = this.fetchAllBooks();
|
||||
} else {
|
||||
|
||||
const routeEntityInfo$ = this.getEntityInfoFromRoute();
|
||||
|
||||
this.bookState$ = routeEntityInfo$.pipe(
|
||||
switchMap(({entityId, entityType}) => this.fetchBooksByEntity(entityId, entityType)),
|
||||
);
|
||||
|
||||
this.entity$ = routeEntityInfo$.pipe(
|
||||
switchMap(({entityId, entityType}) => this.fetchEntity(entityId, entityType))
|
||||
);
|
||||
|
||||
this.entityType$ = routeEntityInfo$.pipe(
|
||||
map(({entityType}) => entityType)
|
||||
);
|
||||
|
||||
this.entity$.subscribe(entity => {
|
||||
this.entity = entity;
|
||||
this.entityOptions = this.libraryShelfMenuService.initializeLibraryMenuItems(entity);
|
||||
this.setSelectedSortFromEntity(entity);
|
||||
});
|
||||
}
|
||||
|
||||
this.activatedRoute.paramMap.subscribe(() => {
|
||||
this.bookTitle$.next('');
|
||||
this.bookTitle = '';
|
||||
});
|
||||
}
|
||||
|
||||
private getEntityInfoFromRoute(): Observable<{ entityId: number; entityType: EntityType }> {
|
||||
return this.activatedRoute.paramMap.pipe(
|
||||
map(params => {
|
||||
const libraryId = Number(params.get('libraryId') || NaN);
|
||||
const shelfId = Number(params.get('shelfId') || NaN);
|
||||
if (!isNaN(libraryId)) {
|
||||
return {entityId: libraryId, entityType: EntityType.LIBRARY};
|
||||
} else if (!isNaN(shelfId)) {
|
||||
return {entityId: shelfId, entityType: EntityType.SHELF};
|
||||
} else {
|
||||
return {entityId: NaN, entityType: EntityType.ALL_BOOKS};
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
private fetchEntity(entityId: number, entityType: EntityType): Observable<Library | Shelf | null> {
|
||||
if (entityType == EntityType.LIBRARY) {
|
||||
return this.fetchLibrary(entityId);
|
||||
} else if (EntityType.SHELF) {
|
||||
this.libraryShelfMenuService.initializeLibraryMenuItems(this.entity);
|
||||
return this.fetchShelf(entityId);
|
||||
}
|
||||
return of(null);
|
||||
}
|
||||
|
||||
private fetchBooksByEntity(entityId: number, entityType: EntityType): Observable<BookState> {
|
||||
if (entityType == EntityType.LIBRARY) {
|
||||
return this.fetchBooksByLibrary(entityId);
|
||||
} else if (entityType == EntityType.SHELF) {
|
||||
return this.fetchBooksByShelf(entityId);
|
||||
} else {
|
||||
return this.fetchAllBooks();
|
||||
}
|
||||
}
|
||||
|
||||
private fetchBooksByLibrary(libraryId: number): Observable<BookState> {
|
||||
return this.fetchBooks(book => book.libraryId === libraryId);
|
||||
}
|
||||
|
||||
private fetchBooksByShelf(shelfId: number): Observable<BookState> {
|
||||
return this.fetchBooks(book => {
|
||||
return book.shelves?.some(shelf => shelf.id === shelfId) ?? false;
|
||||
});
|
||||
}
|
||||
|
||||
private fetchAllBooks(): Observable<BookState> {
|
||||
return this.bookService.bookState$.pipe(
|
||||
map(bookState => this.processBookState(bookState)),
|
||||
switchMap(bookState => this.filterBooksByTitle(bookState))
|
||||
);
|
||||
}
|
||||
|
||||
private fetchBooks(bookFilter: (book: Book) => boolean): Observable<BookState> {
|
||||
return this.bookService.bookState$.pipe(
|
||||
map(bookState => {
|
||||
if (bookState.loaded && !bookState.error) {
|
||||
const filteredBooks = bookState.books?.filter(bookFilter) || [];
|
||||
const sortedBooks = this.sortService.applySort(filteredBooks, this.selectedSort);
|
||||
return {...bookState, books: sortedBooks};
|
||||
}
|
||||
return bookState;
|
||||
}),
|
||||
switchMap(bookState => this.filterBooksByTitle(bookState))
|
||||
);
|
||||
}
|
||||
|
||||
private processBookState(bookState: BookState): BookState {
|
||||
if (bookState.loaded && !bookState.error) {
|
||||
const sortedBooks = this.sortService.applySort(bookState.books || [], this.selectedSort);
|
||||
return {...bookState, books: sortedBooks};
|
||||
}
|
||||
return bookState;
|
||||
}
|
||||
|
||||
private filterBooksByTitle(bookState: BookState): Observable<BookState> {
|
||||
return this.bookTitle$.pipe(
|
||||
map(title => {
|
||||
if (title && title.trim() !== '') {
|
||||
const filteredBooks = bookState.books?.filter(book =>
|
||||
book.metadata?.title?.toLowerCase().includes(title.toLowerCase())
|
||||
) || null;
|
||||
return {...bookState, books: filteredBooks};
|
||||
}
|
||||
return bookState;
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
private fetchLibrary(libraryId: number): Observable<Library | null> {
|
||||
return this.libraryService.libraryState$.pipe(
|
||||
map(libraryState => {
|
||||
if (libraryState.libraries) {
|
||||
return libraryState.libraries.find(lib => lib.id === libraryId) || null;
|
||||
}
|
||||
return null;
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
private fetchShelf(shelfId: number): Observable<Shelf | null> {
|
||||
return this.shelfService.shelfState$.pipe(
|
||||
map(shelfState => {
|
||||
if (shelfState.shelves) {
|
||||
return shelfState.shelves.find(shelf => shelf.id === shelfId) || null;
|
||||
}
|
||||
return null;
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
handleBookSelect(bookId: number, selected: boolean): void {
|
||||
if (selected) {
|
||||
this.selectedBooks.add(bookId);
|
||||
} else {
|
||||
this.selectedBooks.delete(bookId);
|
||||
}
|
||||
this.isDrawerVisible = this.selectedBooks.size > 0;
|
||||
}
|
||||
|
||||
onSelectedBooksChange(selectedBookIds: Set<number>): void {
|
||||
this.selectedBooks = new Set(selectedBookIds);
|
||||
this.isDrawerVisible = this.selectedBooks.size > 0;
|
||||
}
|
||||
|
||||
deselectAllBooks(): void {
|
||||
this.selectedBooks.clear();
|
||||
this.isDrawerVisible = false;
|
||||
this.bookTableComponent.clearSelectedBooks();
|
||||
}
|
||||
|
||||
viewChanged(event: SelectButtonChangeEvent) {
|
||||
this.selectedBooks.clear();
|
||||
this.isDrawerVisible = false;
|
||||
}
|
||||
|
||||
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;
|
||||
if (this.entityType === EntityType.ALL_BOOKS) {
|
||||
this.bookState$ = this.fetchAllBooks();
|
||||
} else {
|
||||
const routeParam$ = this.getEntityInfoFromRoute();
|
||||
this.bookState$ = routeParam$.pipe(
|
||||
switchMap(({entityId, entityType}) => this.fetchBooksByEntity(entityId, entityType))
|
||||
);
|
||||
}
|
||||
if (this.entityType === EntityType.LIBRARY) {
|
||||
this.libraryService.updateSort(this.entity?.id!, sortOption).subscribe();
|
||||
} else if (this.entityType === 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;
|
||||
}
|
||||
}
|
||||
|
||||
onBookTitleChange(newTitle: string): void {
|
||||
this.bookTitle$.next(newTitle);
|
||||
}
|
||||
|
||||
openShelfAssigner() {
|
||||
this.dynamicDialogRef = this.dialogService.open(ShelfAssignerComponent, {
|
||||
header: `Update Books Shelves`,
|
||||
modal: true,
|
||||
closable: true,
|
||||
width: '30%',
|
||||
height: '60%',
|
||||
contentStyle: {overflow: 'auto'},
|
||||
baseZIndex: 10,
|
||||
style: {
|
||||
position: 'absolute',
|
||||
top: '15%',
|
||||
},
|
||||
data: {
|
||||
isMultiBooks: true,
|
||||
bookIds: this.selectedBooks
|
||||
},
|
||||
});
|
||||
this.dynamicDialogRef.onClose.subscribe(() => {
|
||||
this.selectedBooks.clear();
|
||||
});
|
||||
}
|
||||
|
||||
updateMetadata() {
|
||||
this.dialogService.open(MetadataFetchOptionsComponent, {
|
||||
header: 'Metadata Refresh Options',
|
||||
modal: true,
|
||||
closable: true,
|
||||
data: {
|
||||
bookIds: this.selectedBooks,
|
||||
metadataRefreshType: MetadataRefreshType.BOOKS
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user