Refactor BooksBrowserComponent
This commit is contained in:
@@ -9,10 +9,11 @@ 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 { 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,29 +41,14 @@ 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))
|
||||
@@ -79,7 +59,7 @@ export class BooksBrowserComponent implements OnInit {
|
||||
);
|
||||
|
||||
this.entityType$ = routeParam$.pipe(
|
||||
switchMap(({libraryId, shelfId}) => this.fetchEntityType(libraryId, shelfId))
|
||||
map(({ libraryId, shelfId }) => libraryId ? 'Library' : shelfId ? 'Shelf' : null)
|
||||
);
|
||||
|
||||
this.entity$.subscribe(entity => {
|
||||
@@ -90,43 +70,61 @@ 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) {
|
||||
this.entityType = 'Library';
|
||||
return this.fetchLibrary(libraryId);
|
||||
} else if (shelfId) {
|
||||
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)
|
||||
);
|
||||
} else if (shelfId) {
|
||||
}
|
||||
|
||||
private fetchShelf(shelfId: number): Observable<Shelf | null> {
|
||||
return this.shelfService.shelves$.pipe(
|
||||
map(shelves => shelves.find(shelf => shelf.id === shelfId) || null)
|
||||
);
|
||||
}
|
||||
return of(null);
|
||||
}
|
||||
|
||||
handleBookSelect(bookId: number, selected: boolean): void {
|
||||
if (selected) {
|
||||
@@ -136,43 +134,6 @@ export class BooksBrowserComponent implements OnInit {
|
||||
}
|
||||
}
|
||||
|
||||
setupMenu(): void {
|
||||
this.items = [
|
||||
{
|
||||
label: 'Options',
|
||||
items: [
|
||||
{
|
||||
label: 'Edit shelf',
|
||||
icon: 'pi pi-folder',
|
||||
command: () => this.openShelfDialog(),
|
||||
}
|
||||
],
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
@@ -181,21 +142,6 @@ export class BooksBrowserComponent implements OnInit {
|
||||
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(
|
||||
@@ -212,12 +158,8 @@ export class BooksBrowserComponent implements OnInit {
|
||||
|
||||
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};
|
||||
}),
|
||||
const routeParam$ = this.getRouteParams();
|
||||
this.books$ = routeParam$.pipe(
|
||||
switchMap(({ libraryId, shelfId }) => this.fetchBooks(libraryId, shelfId))
|
||||
);
|
||||
if (this.entityType === 'Library') {
|
||||
@@ -227,4 +169,41 @@ export class BooksBrowserComponent implements OnInit {
|
||||
}
|
||||
}
|
||||
|
||||
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 = [
|
||||
{
|
||||
label: 'Options',
|
||||
items: [
|
||||
{
|
||||
label: 'Edit shelf',
|
||||
icon: 'pi pi-folder',
|
||||
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
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
],
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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[];
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
export interface SortOption {
|
||||
label: string;
|
||||
field: string;
|
||||
direction: string;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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<Library> {
|
||||
updateSort(libraryId: number, sort: SortOption): Observable<Library> {
|
||||
return this.http.put<Library>(`${this.url}/${libraryId}/sort`, sort).pipe(
|
||||
map(updatedLibrary => {
|
||||
const updatedShelves = this.libraries.value.map(library =>
|
||||
|
||||
@@ -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<Shelf> {
|
||||
updateSort(shelfId: number, sort: SortOption): Observable<Shelf> {
|
||||
return this.http.put<Shelf>(`${this.url}/${shelfId}/sort`, { shelfId, sort }).pipe(
|
||||
map(updatedShelf => {
|
||||
const updatedShelves = this.shelves.value.map(shelf =>
|
||||
|
||||
@@ -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}
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,12 +1,34 @@
|
||||
import {Injectable} from '@angular/core';
|
||||
import { SortOption } from '../model/sort-option.model';
|
||||
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;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user