Improve sorting and bottom shelf assigner popup

This commit is contained in:
aditya.chandel
2025-01-09 00:01:57 -07:00
parent 6b541c77af
commit 99cce53cfc
11 changed files with 170 additions and 81 deletions
@@ -59,7 +59,6 @@ export class BookMetadataCenterComponent implements OnInit {
reviewCount: metadata?.reviewCount || null,
rating: metadata?.rating || null,
language: metadata?.language || '',
googleBookId: metadata?.googleBookId || '',
};
}
}
@@ -1,5 +1,5 @@
import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
import {FetchedMetadata} from '../../book/model/book.model';
import {BookMetadata, FetchedMetadata} from '../../book/model/book.model';
import {BookService} from '../../book/service/book.service';
import {MessageService} from 'primeng/api';
import {Button} from 'primeng/button';
@@ -116,7 +116,8 @@ export class MetadataPickerComponent implements OnInit {
};
this.bookService.updateMetadata(this.currentBookId, updatedBookMetadata).subscribe({
next: () => {
next: (bookMetadata) => {
console.log(bookMetadata)
Object.keys(this.copiedFields).forEach((field) => {
if (this.copiedFields[field]) {
this.savedFields[field] = true;
@@ -17,8 +17,9 @@
<div class="header-right-items">
<input type="text" pInputText placeholder="Search by title..." [(ngModel)]="bookTitle" (ngModelChange)="onBookTitleChange($event)"/>
<p-dropdown [options]="sortOptions" optionLabel="label" placeholder="Select Sorting" [(ngModel)]="selectedSort" (onChange)="updateSortOption($event.value)"></p-dropdown>
<div class="state-options">
<p-radioButton *ngFor="let option of stateOptions" [value]="option.value" [(ngModel)]="value" (onClick)="viewChanged($event)"></p-radioButton>
<div *ngFor="let option of stateOptions" class="field-checkbox">
<p-radioButton [inputId]="option.key" [value]="option.value" [(ngModel)]="value" (onClick)="viewChanged($event)"></p-radioButton>
<label [for]="option.key" class="ml-2">{{ option.label }}</label>
</div>
</div>
</div>
@@ -41,7 +42,9 @@
</div>
<div class="book-card-container" *ngIf="bookState?.books as books">
<app-book-table *ngIf="value === 'table'" [books]="books" (selectedBooksChange)="onSelectedBooksChange($event)"></app-book-table>
<app-book-table *ngIf="value === 'table'&& books.length > 0" [books]="books" (selectedBooksChange)="onSelectedBooksChange($event)"
[sortOption]="selectedSort"></app-book-table>
<virtual-scroller *ngIf="value === 'grid'" class="virtual-scroller" #scroll [items]="books">
<div class="grid grid-cols-12 gap-4" #container>
@@ -57,7 +60,7 @@
</virtual-scroller>
</div>
<div class="book-browser-footer" *ngIf="selectedBooks.size > 0">
<div class="book-browser-footer" *ngIf="selectedBooks.size > 0" [@slideInOut]>
<div class="footer-items-container">
<ng-container *ngIf="entityType$ | async as entityType">
<p-button *ngIf="entityType === EntityType.LIBRARY || entityType === EntityType.ALL_BOOKS"
@@ -69,12 +69,36 @@
.book-browser-footer {
position: absolute;
bottom: 0;
left: 37%;
right: 37%;
display: flex;
justify-content: center;
padding: 1.1rem;
box-shadow: 0px -2px 10px rgba(0, 0, 0, 0.1);
z-index: 1;
border-radius: 10px 10px 0 0;
}
.book-browser-footer::before {
content: "";
position: absolute;
top: 0;
left: 0;
right: 0;
background-color: var(--card-background);
padding: 1rem;
box-shadow: 0px -2px 10px rgba(0, 0, 0, 0.1);
display: flex;
bottom: 0;
background-color: rgba(0, 0, 0, 0.85);
z-index: -1;
border-radius: 10px 10px 0 0;
border: 1px solid rgba(255, 255, 255, 0.5);
border-width: 1px 1px 0px 1px;
}
.book-browser-footer > * {
pointer-events: auto;
}
.book-browser-footer:hover::before {
pointer-events: none;
}
.footer-items-container {
@@ -11,13 +11,13 @@ import {DialogService, DynamicDialogRef} from 'primeng/dynamicdialog';
import {Library} from '../../model/library.model';
import {Shelf} from '../../model/shelf.model';
import {SortService} from '../../service/sort.service';
import {SortOptionsHelper} from '../../service/sort-options.helper';
import {SortOption} from '../../model/sort.model';
import {BookState} from '../../model/state/book-state.model';
import {Book} from '../../model/book.model';
import {LibraryShelfMenuService} from '../../service/library-shelf-menu.service';
import {SelectButtonChangeEvent} from 'primeng/selectbutton';
import {BookTableComponent} from '../book-table/book-table.component';
import {animate, state, style, transition, trigger} from '@angular/animations';
export enum EntityType {
LIBRARY = 'Library',
@@ -29,7 +29,23 @@ export enum EntityType {
selector: 'app-book-browser',
standalone: false,
templateUrl: './book-browser.component.html',
styleUrls: ['./book-browser.component.scss']
styleUrls: ['./book-browser.component.scss'],
animations: [
trigger('slideInOut', [
state('void', style({
transform: 'translateY(100%)'
})),
state('*', style({
transform: 'translateY(0)'
})),
transition(':enter', [
animate('0.1s ease-in')
]),
transition(':leave', [
animate('0.1s ease-out')
])
])
]
})
export class BookBrowserComponent implements OnInit {
bookState$: Observable<BookState> | undefined;
@@ -48,7 +64,7 @@ export class BookBrowserComponent implements OnInit {
dynamicDialogRef: DynamicDialogRef | undefined;
EntityType = EntityType;
stateOptions: any[] = [{ label: 'Grid', value: 'grid' },{ label: 'Table', value: 'table' }];
stateOptions: any[] = [{label: 'Grid', value: 'grid'}, {label: 'Table', value: 'table'}];
value: string = 'table';
@ViewChild(BookTableComponent) bookTableComponent!: BookTableComponent;
@@ -65,7 +81,7 @@ export class BookBrowserComponent implements OnInit {
}
ngOnInit(): void {
this.sortOptions = SortOptionsHelper.generateSortOptions();
this.sortOptions = SortService.generateSortOptions();
const isAllBooksRoute = this.activatedRoute.snapshot.routeConfig?.path === 'all-books';
if (isAllBooksRoute) {
@@ -85,7 +101,7 @@ export class BookBrowserComponent implements OnInit {
);
this.entityType$ = routeEntityInfo$.pipe(
map(({ entityType }) => entityType)
map(({entityType}) => entityType)
);
this.entity$.subscribe(entity => {
@@ -107,11 +123,11 @@ export class BookBrowserComponent implements OnInit {
const libraryId = Number(params.get('libraryId') || NaN);
const shelfId = Number(params.get('shelfId') || NaN);
if (!isNaN(libraryId)) {
return { entityId: libraryId, entityType: EntityType.LIBRARY };
return {entityId: libraryId, entityType: EntityType.LIBRARY};
} else if (!isNaN(shelfId)) {
return { entityId: shelfId, entityType: EntityType.SHELF };
return {entityId: shelfId, entityType: EntityType.SHELF};
} else {
return { entityId: NaN, entityType: EntityType.ALL_BOOKS };
return {entityId: NaN, entityType: EntityType.ALL_BOOKS};
}
})
);
@@ -160,7 +176,7 @@ export class BookBrowserComponent implements OnInit {
if (bookState.loaded && !bookState.error) {
const filteredBooks = bookState.books?.filter(bookFilter) || [];
const sortedBooks = this.sortService.applySort(filteredBooks, this.selectedSort);
return { ...bookState, books: sortedBooks };
return {...bookState, books: sortedBooks};
}
return bookState;
}),
@@ -171,7 +187,7 @@ export class BookBrowserComponent implements OnInit {
private processBookState(bookState: BookState): BookState {
if (bookState.loaded && !bookState.error) {
const sortedBooks = this.sortService.applySort(bookState.books || [], this.selectedSort);
return { ...bookState, books: sortedBooks };
return {...bookState, books: sortedBooks};
}
return bookState;
}
@@ -183,7 +199,7 @@ export class BookBrowserComponent implements OnInit {
const filteredBooks = bookState.books?.filter(book =>
book.metadata?.title?.toLowerCase().includes(title.toLowerCase())
) || null;
return { ...bookState, books: filteredBooks };
return {...bookState, books: filteredBooks};
}
return bookState;
})
@@ -232,7 +248,7 @@ export class BookBrowserComponent implements OnInit {
this.bookTableComponent.clearSelectedBooks();
}
viewChanged(event : SelectButtonChangeEvent) {
viewChanged(event: SelectButtonChangeEvent) {
this.selectedBooks.clear();
this.isDrawerVisible = false;
}
@@ -1,33 +1,45 @@
<p-table [value]="books" [resizableColumns]="true" showGridlines stripedRows [scrollable]="true" [scrollHeight]="'calc(100vh - 160px)'"
[virtualScroll]="true" [virtualScrollItemSize]="46" [(selection)]="selectedBooks" (onHeaderCheckboxToggle)="onHeaderCheckboxToggle($event)"
(onRowSelect)="onRowSelect($event)" (onRowUnselect)="onRowUnselect($event)">
<p-table
[value]="books"
[resizableColumns]="true"
showGridlines
stripedRows
[scrollable]="true"
[scrollHeight]="'calc(100vh - 160px)'"
[virtualScroll]="true"
[virtualScrollItemSize]="46"
[(selection)]="selectedBooks"
(onHeaderCheckboxToggle)="onHeaderCheckboxToggle($event)"
(onRowSelect)="onRowSelect($event)"
(onRowUnselect)="onRowUnselect($event)">
<ng-template #header>
<tr>
<th class="max-w-16">Cover</th>
<th class="w-1/2 max-w-52 overflow-hidden truncate" pResizableColumn>Title</th>
<th class="w-1/6 max-w-48 overflow-hidden truncate">Publisher</th>
<th class="w-1/8 max-w-44 overflow-hidden truncate" pResizableColumn>Subtitle</th>
<th class="w-1/8 max-w-36 overflow-hidden truncate">ISBN</th>
<th class="w-1/8 max-w-32 overflow-hidden truncate">Language</th>
<th class="w-1/8 max-w-36 overflow-hidden truncate">Rating</th>
<th class="max-w-16">
<p-tableHeaderCheckbox></p-tableHeaderCheckbox>
</th>
<th pResizableColumn>Title</th>
<th pResizableColumn>Publisher</th>
<th>Published</th>
<th>ISBN</th>
<th>Language</th>
<th>Rating</th>
<th>Pages</th>
<th>Reviews</th>
<th><p-tableHeaderCheckbox/></th>
</tr>
</ng-template>
<ng-template #body let-book>
<tr style="height:46px">
<td (click)="openMetadataCenter(book.id)" class="w-6 h-6"><img [src]="getBookCoverUrl(book.id)" alt="Book Cover" class="size-7"/></td>
<td (click)="openMetadataCenter(book.id)" class="w-1/2 max-w-52 overflow-hidden truncate" [title]="book.metadata?.title">{{ book.metadata?.title }}</td>
<td class="w-1/6 max-w-48 overflow-hidden truncate">{{ book.metadata?.publisher }}</td>
<td class="w-1/8 max-w-44 overflow-hidden truncate">{{ book.metadata?.subtitle }}</td>
<td class="w-1/8 max-w-36 overflow-hidden truncate">{{ book.metadata?.isbn10 }}</td>
<td class="w-1/8 max-w-32 overflow-hidden truncate">{{ book.metadata?.language }}</td>
<td class="w-1/8 max-w-36 overflow-hidden truncate">
<td (click)="openMetadataCenter(book.id)"><img [src]="getBookCoverUrl(book.id)" alt="Book Cover" class="size-7"/></td>
<td (click)="openMetadataCenter(book.id)" class="w-1/2 max-w-96 overflow-hidden truncate" [title]="book.metadata?.id">{{ book.metadata?.title }}</td>
<td class="w-1/6 max-w-40 overflow-hidden truncate">{{ book.metadata?.publisher }}</td>
<td class="w-1/12 max-w-30 overflow-hidden truncate">{{ book.metadata?.publishedDate }}</td>
<td class="w-1/12 max-w-32 overflow-hidden truncate">{{ book.metadata?.isbn10 }}</td>
<td class="w-1/12 max-w-28 overflow-hidden truncate">{{ book.metadata?.language }}</td>
<td class="w-1/12 max-w-30 overflow-hidden truncate">
<ng-container *ngIf="book.metadata">
<p-rating [(ngModel)]="book.metadata.rating" readonly stars="5"></p-rating>
</ng-container>
</td>
<td class="w-1/12 max-w-32 overflow-hidden truncate">{{ book.metadata?.pageCount }}</td>
<td class="w-1/12 max-w-32 overflow-hidden truncate">{{ book.metadata?.reviewCount }}</td>
<td class="max-w-16">
<p-tableCheckbox [value]="book"></p-tableCheckbox>
</td>
@@ -1,4 +1,4 @@
import {ChangeDetectorRef, Component, EventEmitter, Input, NgZone, OnChanges, Output, SimpleChanges} from '@angular/core';
import {AfterContentChecked, AfterViewChecked, AfterViewInit, Component, EventEmitter, Input, NgZone, OnChanges, Output} from '@angular/core';
import {TableModule} from 'primeng/table';
import {NgIf} from '@angular/common';
import {BookService} from '../../service/book.service';
@@ -7,6 +7,7 @@ import {FormsModule} from '@angular/forms';
import {BookMetadataCenterComponent} from '../../../book-metadata-center/book-metadata-center.component';
import {DialogService} from 'primeng/dynamicdialog';
import {Book} from '../../model/book.model';
import {SortDirection, SortOption} from '../../model/sort.model';
@Component({
selector: 'app-book-table',
@@ -20,14 +21,23 @@ import {Book} from '../../model/book.model';
],
styleUrl: './book-table.component.scss'
})
export class BookTableComponent {
export class BookTableComponent implements OnChanges {
selectedBooks: Book[] = [];
selectedBookIds: Set<number> = new Set();
@Output() selectedBooksChange = new EventEmitter<Set<number>>();
@Input() books: Book[] = [];
@Input() sortOption: SortOption | null = null;
constructor(private bookService: BookService, private dialogService: DialogService, private zone: NgZone) {
constructor(private bookService: BookService, private dialogService: DialogService) {
}
// Hack to set virtual-scroller height
ngOnChanges() {
let wrapperElements: HTMLCollection = document.getElementsByClassName('p-virtualscroller');
Array.prototype.forEach.call(wrapperElements, function (wrapperElement) {
wrapperElement.style["height"] = 'calc(100vh - 160px)';
});
}
clearSelectedBooks(): void {
@@ -86,4 +96,11 @@ export class BookTableComponent {
}
});
}
testzzz($event: any) {
console.log($event)
}
protected readonly SortDirection = SortDirection;
}
+8 -9
View File
@@ -1,5 +1,4 @@
import {Shelf} from './shelf.model';
import {Provider} from './provider.model';
export interface Book {
id: number;
@@ -13,25 +12,25 @@ export interface Book {
export interface BookMetadata {
title: string;
subtitle?: string;
authors: Author[];
categories: Category[];
publisher: string;
publishedDate: string;
isbn10: string;
isbn13: string;
description?: string;
isbn13: string;
isbn10: string;
asin?: string;
pageCount: number;
language: string;
rating: number;
reviewCount: number;
language: string;
googleBookId: string;
authors: Author[];
categories: Category[];
[key: string]: any;
}
export interface FetchedMetadata {
bookId: number | null;
provider: string,
providerBookId: string,
provider: string;
providerBookId: string;
title: string | null;
subtitle?: string | null;
publisher: string | null;
@@ -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}
];
}
}