Refactor BooksBrowserComponent
This commit is contained in:
@@ -1,18 +1,19 @@
|
|||||||
import {Component, OnInit} from '@angular/core';
|
import { Component, OnInit } from '@angular/core';
|
||||||
import {ActivatedRoute, Router} from '@angular/router';
|
import { ActivatedRoute, Router } from '@angular/router';
|
||||||
import {ConfirmationService, MenuItem, MessageService} from 'primeng/api';
|
import { ConfirmationService, MenuItem, MessageService } from 'primeng/api';
|
||||||
import {LibraryService} from '../../service/library.service';
|
import { LibraryService } from '../../service/library.service';
|
||||||
import {BookService} from '../../service/book.service';
|
import { BookService } from '../../service/book.service';
|
||||||
import {map, switchMap} from 'rxjs/operators';
|
import { map, switchMap } from 'rxjs/operators';
|
||||||
import {Observable, of} from 'rxjs';
|
import { Observable, of } from 'rxjs';
|
||||||
import {Book} from '../../model/book.model';
|
import { Book } from '../../model/book.model';
|
||||||
import {ShelfService} from '../../service/shelf.service';
|
import { ShelfService } from '../../service/shelf.service';
|
||||||
import {ShelfAssignerComponent} from '../shelf-assigner/shelf-assigner.component';
|
import { ShelfAssignerComponent } from '../shelf-assigner/shelf-assigner.component';
|
||||||
import {DialogService} from 'primeng/dynamicdialog';
|
import { DialogService } from 'primeng/dynamicdialog';
|
||||||
import {SortOption} from '../../model/sort-option.model';
|
import { Library } from '../../model/library.model';
|
||||||
import {SortService} from '../../service/sort.service';
|
import { Shelf } from '../../model/shelf.model';
|
||||||
import {Library} from '../../model/library.model';
|
import { SortService } from '../../service/sort.service';
|
||||||
import {Shelf} from '../../model/shelf.model';
|
import {SortOptionsHelper} from '../../service/sort-options.helper';
|
||||||
|
import {SortOption} from '../../model/sort.model';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-books-browser',
|
selector: 'app-books-browser',
|
||||||
@@ -25,18 +26,12 @@ export class BooksBrowserComponent implements OnInit {
|
|||||||
entity$: Observable<Library | Shelf | null> | undefined;
|
entity$: Observable<Library | Shelf | null> | undefined;
|
||||||
entityType$: Observable<string | null> | undefined;
|
entityType$: Observable<string | null> | undefined;
|
||||||
|
|
||||||
items: MenuItem[] | undefined;
|
|
||||||
selectedBooks: Set<number> = new Set();
|
|
||||||
entity: Library | Shelf | null = null;
|
entity: Library | Shelf | null = null;
|
||||||
entityType: string = '';
|
entityType: string = '';
|
||||||
|
items: MenuItem[] | undefined;
|
||||||
|
selectedBooks: Set<number> = new Set();
|
||||||
selectedSort: SortOption | null = null;
|
selectedSort: SortOption | null = null;
|
||||||
|
sortOptions: SortOption[] = [];
|
||||||
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'}
|
|
||||||
];
|
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private activatedRoute: ActivatedRoute,
|
private activatedRoute: ActivatedRoute,
|
||||||
@@ -46,40 +41,25 @@ export class BooksBrowserComponent implements OnInit {
|
|||||||
private libraryService: LibraryService,
|
private libraryService: LibraryService,
|
||||||
private bookService: BookService,
|
private bookService: BookService,
|
||||||
private shelfService: ShelfService,
|
private shelfService: ShelfService,
|
||||||
private dialogService: DialogService
|
private dialogService: DialogService,
|
||||||
) {
|
private sortService: SortService
|
||||||
}
|
) {}
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ngOnInit(): void {
|
ngOnInit(): void {
|
||||||
const routeParam$ = this.activatedRoute.paramMap.pipe(
|
this.sortOptions = SortOptionsHelper.generateSortOptions();
|
||||||
map(params => {
|
|
||||||
const libraryId = Number(params.get('libraryId'));
|
const routeParam$ = this.getRouteParams();
|
||||||
const shelfId = Number(params.get('shelfId'));
|
|
||||||
return {libraryId, shelfId};
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
this.books$ = routeParam$.pipe(
|
this.books$ = routeParam$.pipe(
|
||||||
switchMap(({libraryId, shelfId}) => this.fetchBooks(libraryId, shelfId))
|
switchMap(({ libraryId, shelfId }) => this.fetchBooks(libraryId, shelfId))
|
||||||
);
|
);
|
||||||
|
|
||||||
this.entity$ = routeParam$.pipe(
|
this.entity$ = routeParam$.pipe(
|
||||||
switchMap(({libraryId, shelfId}) => this.fetchEntity(libraryId, shelfId))
|
switchMap(({ libraryId, shelfId }) => this.fetchEntity(libraryId, shelfId))
|
||||||
);
|
);
|
||||||
|
|
||||||
this.entityType$ = routeParam$.pipe(
|
this.entityType$ = routeParam$.pipe(
|
||||||
switchMap(({libraryId, shelfId}) => this.fetchEntityType(libraryId, shelfId))
|
map(({ libraryId, shelfId }) => libraryId ? 'Library' : shelfId ? 'Shelf' : null)
|
||||||
);
|
);
|
||||||
|
|
||||||
this.entity$.subscribe(entity => {
|
this.entity$.subscribe(entity => {
|
||||||
@@ -90,44 +70,62 @@ export class BooksBrowserComponent implements OnInit {
|
|||||||
this.setupMenu();
|
this.setupMenu();
|
||||||
}
|
}
|
||||||
|
|
||||||
private fetchEntityType(libraryId: number, shelfId: number): Observable<string> {
|
private getRouteParams(): Observable<{ libraryId: number; shelfId: number }> {
|
||||||
if (!isNaN(libraryId) && libraryId !== 0) {
|
return this.activatedRoute.paramMap.pipe(
|
||||||
this.entityType = 'Library';
|
map(params => {
|
||||||
return of('Library');
|
const libraryId = Number(params.get('libraryId'));
|
||||||
} else if (!isNaN(shelfId) && shelfId !== 0) {
|
const shelfId = Number(params.get('shelfId'));
|
||||||
this.entityType = 'Shelf';
|
return { libraryId, shelfId };
|
||||||
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 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> {
|
private fetchEntity(libraryId: number, shelfId: number): Observable<Library | Shelf | null> {
|
||||||
if (libraryId) {
|
if (libraryId) {
|
||||||
return this.libraryService.libraries$.pipe(
|
this.entityType = 'Library';
|
||||||
map(libraries => libraries.find(lib => lib.id === libraryId) || null)
|
return this.fetchLibrary(libraryId);
|
||||||
);
|
|
||||||
} else if (shelfId) {
|
} else if (shelfId) {
|
||||||
return this.shelfService.shelves$.pipe(
|
this.entityType = 'Shelf';
|
||||||
map(shelves => shelves.find(shelf => shelf.id === shelfId) || null)
|
return this.fetchShelf(shelfId);
|
||||||
);
|
|
||||||
}
|
}
|
||||||
return of(null);
|
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 {
|
handleBookSelect(bookId: number, selected: boolean): void {
|
||||||
if (selected) {
|
if (selected) {
|
||||||
this.selectedBooks.add(bookId);
|
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 {
|
setupMenu(): void {
|
||||||
this.items = [
|
this.items = [
|
||||||
{
|
{
|
||||||
@@ -144,87 +186,24 @@ export class BooksBrowserComponent implements OnInit {
|
|||||||
{
|
{
|
||||||
label: 'Edit shelf',
|
label: 'Edit shelf',
|
||||||
icon: 'pi pi-folder',
|
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import {Sort} from './sort.model';
|
import {SortOption} from './sort.model';
|
||||||
|
|
||||||
export interface Library {
|
export interface Library {
|
||||||
id?: number;
|
id?: number;
|
||||||
name: string;
|
name: string;
|
||||||
sort?: Sort;
|
sort?: SortOption;
|
||||||
paths: string[];
|
paths: string[];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import {Sort} from './sort.model';
|
import {SortOption} from './sort.model';
|
||||||
|
|
||||||
export interface Shelf {
|
export interface Shelf {
|
||||||
id?: number;
|
id?: number;
|
||||||
name: string;
|
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;
|
field: string;
|
||||||
direction: string;
|
direction: SortDirection;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import {catchError, map} from 'rxjs/operators';
|
|||||||
import {Library} from '../model/library.model';
|
import {Library} from '../model/library.model';
|
||||||
import {BookService} from './book.service';
|
import {BookService} from './book.service';
|
||||||
import {BookWithNeighborsDTO} from '../model/book.model';
|
import {BookWithNeighborsDTO} from '../model/book.model';
|
||||||
import {Sort} from '../model/sort.model';
|
import {SortOption} from '../model/sort.model';
|
||||||
|
|
||||||
@Injectable({
|
@Injectable({
|
||||||
providedIn: 'root'
|
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(
|
return this.http.put<Library>(`${this.url}/${libraryId}/sort`, sort).pipe(
|
||||||
map(updatedLibrary => {
|
map(updatedLibrary => {
|
||||||
const updatedShelves = this.libraries.value.map(library =>
|
const updatedShelves = this.libraries.value.map(library =>
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import {BehaviorSubject, Observable, of} from 'rxjs';
|
|||||||
import {catchError, map} from 'rxjs/operators';
|
import {catchError, map} from 'rxjs/operators';
|
||||||
import {HttpClient} from '@angular/common/http';
|
import {HttpClient} from '@angular/common/http';
|
||||||
import {Shelf} from '../model/shelf.model';
|
import {Shelf} from '../model/shelf.model';
|
||||||
import {Sort} from '../model/sort.model';
|
import {SortOption} from '../model/sort.model';
|
||||||
|
|
||||||
@Injectable({
|
@Injectable({
|
||||||
providedIn: 'root'
|
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(
|
return this.http.put<Shelf>(`${this.url}/${shelfId}/sort`, { shelfId, sort }).pipe(
|
||||||
map(updatedShelf => {
|
map(updatedShelf => {
|
||||||
const updatedShelves = this.shelves.value.map(shelf =>
|
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 {Injectable} from '@angular/core';
|
||||||
import { SortOption } from '../model/sort-option.model';
|
import {Book} from '../model/book.model';
|
||||||
|
import {SortOption} from "../model/sort.model";
|
||||||
|
|
||||||
@Injectable({
|
@Injectable({
|
||||||
providedIn: 'root',
|
providedIn: 'root',
|
||||||
})
|
})
|
||||||
export class SortService {
|
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