Refactor BooksBrowserComponent
This commit is contained in:
@@ -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 {Injectable} from '@angular/core';
|
||||
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