Fix sorting
This commit is contained in:
@@ -13,18 +13,21 @@
|
||||
|
||||
<div *ngIf="isAnyBookSelected()">
|
||||
<div class="button-container">
|
||||
<p-button *ngIf="(entityType$ | async) === 'Library'" icon="pi pi-ellipsis-v" label="Options" severity="primary"
|
||||
[rounded]="true" [outlined]="true" (click)="menu.toggle($event)"></p-button>
|
||||
<p-button *ngIf="(entityType$ | async) === 'Shelf'" icon="pi pi-minus-circle" label="Unshelf" severity="primary"
|
||||
[rounded]="true" [outlined]="true" (click)="unshelfBooks()"></p-button>
|
||||
<p-button icon="pi pi-times" label="Deselect" severity="warning" [rounded]="true" [outlined]="true"
|
||||
(click)="deselectAllBooks()"></p-button>
|
||||
<p-button *ngIf="(entityType$ | async) === 'Library'" icon="pi pi-ellipsis-v" label="Options" severity="primary" [rounded]="true" [outlined]="true" (click)="menu.toggle($event)"></p-button>
|
||||
<p-button *ngIf="(entityType$ | async) === 'Shelf'" icon="pi pi-minus-circle" label="Unshelf" severity="primary" [rounded]="true" [outlined]="true" (click)="unshelfBooks()"></p-button>
|
||||
<p-button icon="pi pi-times" label="Deselect" severity="warning" [rounded]="true" [outlined]="true" (click)="deselectAllBooks()"></p-button>
|
||||
</div>
|
||||
<p-menu #menu [model]="items" [popup]="true" appendTo="body"></p-menu>
|
||||
</div>
|
||||
|
||||
<div class="library-header-right">
|
||||
<p-dropdown optionLabel="name" placeholder="Select Sorting"/>
|
||||
<p-dropdown
|
||||
[options]="sortOptions"
|
||||
optionLabel="label"
|
||||
placeholder="Select Sorting"
|
||||
[(ngModel)]="selectedSort"
|
||||
(onChange)="updateSortOption($event.value)">
|
||||
</p-dropdown>
|
||||
<div class="zoom-controls">
|
||||
<p-button icon="pi pi-minus" [rounded]="true" [outlined]="true" severity="info"></p-button>
|
||||
<p-button icon="pi pi-plus" [rounded]="true" [outlined]="true" severity="info"></p-button>
|
||||
|
||||
@@ -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<Book[]> | undefined;
|
||||
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 = '';
|
||||
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<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[]> {
|
||||
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<Library | Shelf | null> {
|
||||
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<number>();
|
||||
},
|
||||
(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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,10 +3,3 @@ export interface Library {
|
||||
name: string;
|
||||
paths: string[];
|
||||
}
|
||||
|
||||
export interface LibraryApiResponse {
|
||||
content: Library[];
|
||||
totalElements: number;
|
||||
totalPages: number;
|
||||
numberOfElements: number;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
export interface SortOption {
|
||||
label: string;
|
||||
value: string;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user