WIP: Improve sorting

This commit is contained in:
aditya.chandel
2024-12-25 17:01:07 -07:00
parent 2734c9bd4a
commit affdcb95c6
18 changed files with 107 additions and 38 deletions
@@ -6,13 +6,13 @@ import {BookService} from '../../service/book.service';
import {map, switchMap} from 'rxjs/operators';
import {Observable, of} from 'rxjs';
import {Book} from '../../model/book.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';
import {Shelf} from '../../model/shelf.model';
@Component({
selector: 'app-books-browser',
@@ -31,10 +31,10 @@ export class BooksBrowserComponent implements OnInit {
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'}
{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(
@@ -75,7 +75,7 @@ export class BooksBrowserComponent implements OnInit {
this.entity = entity;
});
routeParam$.subscribe(({ libraryId, shelfId }) => {
routeParam$.subscribe(({libraryId, shelfId}) => {
this.handleSortingState(libraryId, shelfId);
});
@@ -172,22 +172,21 @@ export class BooksBrowserComponent implements OnInit {
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;
const field = this.selectedSort?.field;
const direction = this.selectedSort?.direction;
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;
}
});
})
@@ -1,11 +1,12 @@
import {Component, OnInit} from '@angular/core';
import {DynamicDialogConfig, DynamicDialogRef} from 'primeng/dynamicdialog';
import {Book, Shelf} from '../../model/book.model';
import {Book} from '../../model/book.model';
import {MessageService} from 'primeng/api';
import {ShelfService} from '../../service/shelf.service';
import {Observable} from 'rxjs';
import {BookService} from '../../service/book.service';
import {map, tap} from 'rxjs/operators';
import {Shelf} from '../../model/shelf.model';
@Component({
selector: 'app-shelf-assigner',
+2 -4
View File
@@ -1,3 +1,5 @@
import {Shelf} from './shelf.model';
export interface Book {
id: number;
libraryId: number;
@@ -32,10 +34,6 @@ export interface Category {
name: string;
}
export interface Shelf {
id?: number;
name: string;
}
export interface BookWithNeighborsDTO {
currentBook: Book;
@@ -1,5 +1,8 @@
import {Sort} from './sort.model';
export interface Library {
id?: number;
name: string;
sort?: Sort;
paths: string[];
}
@@ -0,0 +1,7 @@
import {Sort} from './sort.model';
export interface Shelf {
id?: number;
name: string;
sort?: Sort;
}
@@ -1,4 +1,5 @@
export interface SortOption {
label: string;
value: string;
field: string;
direction: string;
}
@@ -0,0 +1,4 @@
export interface Sort {
name: string;
direction: string;
}
@@ -1,9 +1,8 @@
import {Injectable} from '@angular/core';
import {BehaviorSubject, Observable, of} from 'rxjs';
import {Shelf} from '../model/book.model';
import {catchError, map} from 'rxjs/operators';
import {HttpClient} from '@angular/common/http';
import {Library} from '../model/library.model';
import {Shelf} from '../model/shelf.model';
@Injectable({
providedIn: 'root'