Improve sorting and bottom shelf assigner popup
This commit is contained in:
@@ -195,6 +195,14 @@ export class BookService {
|
||||
}
|
||||
|
||||
updateMetadata(bookId: number, bookMetadata: BookMetadataBI): Observable<BookMetadata> {
|
||||
return this.http.put<BookMetadata>(`${this.metadataUrl}/${bookId}`, bookMetadata);
|
||||
return this.http.put<BookMetadata>(`${this.metadataUrl}/${bookId}`, bookMetadata).pipe(
|
||||
tap(updatedMetadata => {
|
||||
const currentState = this.bookStateSubject.value;
|
||||
const updatedBooks = (currentState.books || []).map(book =>
|
||||
book.id === bookId ? {...book, metadata: updatedMetadata} : book
|
||||
);
|
||||
this.bookStateSubject.next({...currentState, books: updatedBooks});
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
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,34 +1,58 @@
|
||||
import {Injectable} from '@angular/core';
|
||||
import {Book} from '../model/book.model';
|
||||
import {SortOption} from "../model/sort.model";
|
||||
import {SortDirection, SortOption} from "../model/sort.model";
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class SortService {
|
||||
|
||||
private readonly fieldExtractors: Record<string, (book: Book) => any> = {
|
||||
title: (book) => book.metadata?.title?.toLowerCase() || null,
|
||||
publishedDate: (book) =>book.metadata?.publishedDate === null ? null : new Date(book.metadata?.publishedDate!).getTime(),
|
||||
publisher: (book) => book.metadata?.publisher || null,
|
||||
pageCount: (book) => book.metadata?.pageCount || null,
|
||||
rating: (book) => book.metadata?.rating || null,
|
||||
reviewCount: (book) => book.metadata?.reviewCount || null,
|
||||
};
|
||||
|
||||
applySort(books: Book[], selectedSort: SortOption | null): Book[] {
|
||||
if (!selectedSort) return books;
|
||||
|
||||
const { field, direction } = selectedSort;
|
||||
const {field, direction} = selectedSort;
|
||||
const extractor = this.fieldExtractors[field];
|
||||
|
||||
if (!extractor) return books;
|
||||
|
||||
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();
|
||||
}
|
||||
const valueA = extractor(a);
|
||||
const valueB = extractor(b);
|
||||
|
||||
if (valueA === undefined || valueB === undefined) return 0;
|
||||
if (valueA === null || valueA === undefined) return 1;
|
||||
if (valueB === null || valueB === undefined) return -1;
|
||||
|
||||
if (direction === 'ASCENDING') {
|
||||
if (direction === SortDirection.ASCENDING) {
|
||||
return valueA < valueB ? -1 : valueA > valueB ? 1 : 0;
|
||||
} else {
|
||||
return valueA > valueB ? -1 : valueA < valueB ? 1 : 0;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
static generateSortOptions(): SortOption[] {
|
||||
return [
|
||||
{label: '↑ Title', field: 'title', direction: SortDirection.ASCENDING},
|
||||
{label: '↓ Title', field: 'title', direction: SortDirection.DESCENDING},
|
||||
{label: '↑ Published', field: 'publishedDate', direction: SortDirection.ASCENDING},
|
||||
{label: '↓ Published', field: 'publishedDate', direction: SortDirection.DESCENDING},
|
||||
{label: '↑ Pages', field: 'pageCount', direction: SortDirection.ASCENDING},
|
||||
{label: '↓ Pages', field: 'pageCount', direction: SortDirection.DESCENDING},
|
||||
{label: '↑ Rating', field: 'rating', direction: SortDirection.ASCENDING},
|
||||
{label: '↓ Rating', field: 'rating', direction: SortDirection.DESCENDING},
|
||||
{label: '↑ Reviews', field: 'reviewCount', direction: SortDirection.ASCENDING},
|
||||
{label: '↓ Reviews', field: 'reviewCount', direction: SortDirection.DESCENDING},
|
||||
{label: '↑ Publisher', field: 'publisher', direction: SortDirection.ASCENDING},
|
||||
{label: '↓ Publisher', field: 'publisher', direction: SortDirection.DESCENDING}
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user