Enhance the sidebar with additional filtering options
This commit is contained in:
committed by
Aditya Chandel
parent
7a4a401e87
commit
006cd8981b
@@ -28,7 +28,7 @@ import {ProgressSpinner} from 'primeng/progressspinner';
|
|||||||
import {Menu} from 'primeng/menu';
|
import {Menu} from 'primeng/menu';
|
||||||
import {InputText} from 'primeng/inputtext';
|
import {InputText} from 'primeng/inputtext';
|
||||||
import {FormsModule} from '@angular/forms';
|
import {FormsModule} from '@angular/forms';
|
||||||
import {BookFilterComponent} from './book-filter/book-filter.component';
|
import {BookFilterComponent, fileSizeRanges, isFileSizeInRange, isPageCountInRange, isRatingInRange, pageCountRanges, ratingRanges} from './book-filter/book-filter.component';
|
||||||
import {Tooltip} from 'primeng/tooltip';
|
import {Tooltip} from 'primeng/tooltip';
|
||||||
import {Fluid} from 'primeng/fluid';
|
import {Fluid} from 'primeng/fluid';
|
||||||
import {UserService} from '../../../settings/user-management/user.service';
|
import {UserService} from '../../../settings/user-management/user.service';
|
||||||
@@ -283,7 +283,6 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
|||||||
if (!activeFilters) {
|
if (!activeFilters) {
|
||||||
return bookState;
|
return bookState;
|
||||||
}
|
}
|
||||||
|
|
||||||
const filteredBooks = (bookState.books || []).filter(book => {
|
const filteredBooks = (bookState.books || []).filter(book => {
|
||||||
return Object.entries(activeFilters).every(([filterType, filterValues]) => {
|
return Object.entries(activeFilters).every(([filterType, filterValues]) => {
|
||||||
if (!Array.isArray(filterValues) || filterValues.length === 0) return true;
|
if (!Array.isArray(filterValues) || filterValues.length === 0) return true;
|
||||||
@@ -297,13 +296,29 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
|||||||
return filterValues.every(val => book.metadata?.publisher === val);
|
return filterValues.every(val => book.metadata?.publisher === val);
|
||||||
case 'series':
|
case 'series':
|
||||||
return filterValues.every(val => book.metadata?.seriesName === val);
|
return filterValues.every(val => book.metadata?.seriesName === val);
|
||||||
|
case 'amazonRating':
|
||||||
|
return filterValues.some(range => isRatingInRange(book.metadata?.amazonRating, range));
|
||||||
|
case 'goodreadsRating':
|
||||||
|
return filterValues.some(range => isRatingInRange(book.metadata?.goodreadsRating, range));
|
||||||
|
case 'hardcoverRating':
|
||||||
|
return filterValues.some(range => isRatingInRange(book.metadata?.hardcoverRating, range));
|
||||||
|
case 'publishedDate':
|
||||||
|
return filterValues.includes(new Date(book.metadata?.publishedDate || '').getFullYear());
|
||||||
|
case 'fileSize':
|
||||||
|
return filterValues.some(range => isFileSizeInRange(book.fileSizeKb, range));
|
||||||
|
case 'shelfStatus':
|
||||||
|
const shelved = book.shelves && book.shelves.length > 0 ? 'shelved' : 'unshelved';
|
||||||
|
return filterValues.includes(shelved);
|
||||||
|
case 'pageCount':
|
||||||
|
return filterValues.some(range => isPageCountInRange(book.metadata?.pageCount!, range));
|
||||||
|
case 'language':
|
||||||
|
return filterValues.includes(book.metadata?.language);
|
||||||
default:
|
default:
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
return { ...bookState, books: filteredBooks };
|
||||||
return {...bookState, books: filteredBooks};
|
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
+15
-6
@@ -1,27 +1,36 @@
|
|||||||
<div *ngIf="showFilters" class="w-[225px] ml-4 h-[calc(100vh-6.5rem)] rounded-xl bg-[var(--card-background)] overflow-hidden">
|
<div *ngIf="showFilters" class="w-[225px] ml-4 h-[calc(100vh-6.5rem)] rounded-xl bg-[var(--card-background)] overflow-hidden">
|
||||||
|
<p class="text-lg p-4">Filters</p>
|
||||||
<p-accordion [value]="0">
|
<p-accordion [value]="0">
|
||||||
<p-accordion-panel *ngFor="let filterType of filterTypes; let i = index" [value]="i">
|
<p-accordion-panel *ngFor="let filterType of filterTypes; let i = index" [value]="i">
|
||||||
<p-accordion-header>
|
<p-accordion-header>
|
||||||
<span class="inline-flex items-center gap-1">
|
<span class="inline-flex items-center gap-1">
|
||||||
{{ filterType | titlecase }}
|
{{ filterLabels[filterType] || (filterType | titlecase) }}
|
||||||
<span *ngIf="activeFilters[filterType]?.length" class="text-sm text-[var(--primary-color)]">
|
<span *ngIf="activeFilters[filterType]?.length" class="text-sm text-[var(--primary-color)]">
|
||||||
({{ activeFilters[filterType].length }})
|
({{ activeFilters[filterType].length }})
|
||||||
</span>
|
</span>
|
||||||
</span>
|
</span>
|
||||||
</p-accordion-header>
|
</p-accordion-header>
|
||||||
<p-accordion-content class="h-[40rem] !overflow-y-auto">
|
<p-accordion-content class="h-[31rem] !overflow-y-auto">
|
||||||
<div *ngIf="filterStreams[filterType] | async as filters">
|
<div *ngIf="filterStreams[filterType] | async as filters">
|
||||||
<div *ngFor="let filter of filters">
|
<div *ngFor="let filter of filters">
|
||||||
<p
|
<div
|
||||||
class="cursor-pointer transition-colors duration-200 ease-in-out pb-1"
|
class="cursor-pointer transition-colors duration-200 ease-in-out pb-1 flex items-center gap-2"
|
||||||
[ngClass]="{
|
[ngClass]="{
|
||||||
'text-[var(--primary-color)]':
|
'text-[var(--primary-color)]':
|
||||||
activeFilters[filterType]?.includes(filter.value?.id || filter.value)
|
activeFilters[filterType]?.includes(filter.value?.id || filter.value)
|
||||||
}"
|
}"
|
||||||
(click)="handleFilterClick(filterType, filter.value?.id || filter.value)">
|
(click)="handleFilterClick(filterType, filter.value?.id || filter.value)">
|
||||||
|
|
||||||
<p-badge [value]="filter.bookCount"></p-badge>
|
<p-badge [value]="filter.bookCount"></p-badge>
|
||||||
{{ filter.value.name || filter.value }}
|
<ng-container *ngIf="filterType === 'amazonRating'; else defaultLabel">
|
||||||
</p>
|
<ng-container *ngIf="filterType === 'amazonRating'; else defaultLabel">
|
||||||
|
<span>{{ filter.value.name }}</span>
|
||||||
|
</ng-container>
|
||||||
|
</ng-container>
|
||||||
|
<ng-template #defaultLabel>
|
||||||
|
{{ filter.value.name || filter.value }}
|
||||||
|
</ng-template>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</p-accordion-content>
|
</p-accordion-content>
|
||||||
|
|||||||
+3
@@ -0,0 +1,3 @@
|
|||||||
|
:host ::ng-deep p-accordion-header {
|
||||||
|
--p-accordion-header-padding: 0.6rem 1rem;
|
||||||
|
}
|
||||||
|
|||||||
+143
-22
@@ -9,10 +9,97 @@ import {Book} from '../../../model/book.model';
|
|||||||
import {Accordion, AccordionContent, AccordionHeader, AccordionPanel} from 'primeng/accordion';
|
import {Accordion, AccordionContent, AccordionHeader, AccordionPanel} from 'primeng/accordion';
|
||||||
import {AsyncPipe, NgClass, NgForOf, NgIf, TitleCasePipe} from '@angular/common';
|
import {AsyncPipe, NgClass, NgForOf, NgIf, TitleCasePipe} from '@angular/common';
|
||||||
import {Badge} from 'primeng/badge';
|
import {Badge} from 'primeng/badge';
|
||||||
|
import {FormsModule} from '@angular/forms';
|
||||||
|
import {Divider} from 'primeng/divider';
|
||||||
|
|
||||||
type Filter<T> = { value: T; bookCount: number };
|
type Filter<T> = { value: T; bookCount: number };
|
||||||
|
|
||||||
|
export const ratingRanges = [
|
||||||
|
{id: '0to1', label: '0 to 1', min: 0, max: 1},
|
||||||
|
{id: '1to2', label: '1 to 2', min: 1, max: 2},
|
||||||
|
{id: '2to3', label: '2 to 3', min: 2, max: 3},
|
||||||
|
{id: '3to4', label: '3 to 4', min: 3, max: 4},
|
||||||
|
{id: '4to4.5', label: '4 to 4.5', min: 4, max: 4.5},
|
||||||
|
{id: '4.5plus', label: '4.5+', min: 4.5, max: Infinity}
|
||||||
|
];
|
||||||
|
|
||||||
|
export const fileSizeRanges = [
|
||||||
|
{id: '<1mb', label: '< 1 MB', min: 0, max: 1024},
|
||||||
|
{id: '1to10mb', label: '1–10 MB', min: 1024, max: 10240},
|
||||||
|
{id: '10to50mb', label: '10–50 MB', min: 10240, max: 51200},
|
||||||
|
{id: '50to100mb', label: '50–100 MB', min: 51200, max: 102400},
|
||||||
|
{id: '250to500mb', label: '250–500 MB', min: 256000, max: 512000},
|
||||||
|
{id: '500mbto1gb', label: '500 MB – 1 GB', min: 512000, max: 1048576},
|
||||||
|
{id: '1to2gb', label: '1–2 GB', min: 1048576, max: 2097152},
|
||||||
|
{id: '5plusgb', label: '5+ GB', min: 5242880, max: Infinity}
|
||||||
|
];
|
||||||
|
|
||||||
|
export const pageCountRanges = [
|
||||||
|
{id: '<50', label: '< 50 pages', min: 0, max: 50},
|
||||||
|
{id: '50to100', label: '50–100 pages', min: 50, max: 100},
|
||||||
|
{id: '100to200', label: '100–200 pages', min: 100, max: 200},
|
||||||
|
{id: '200to400', label: '200–400 pages', min: 200, max: 400},
|
||||||
|
{id: '400to600', label: '400–600 pages', min: 400, max: 600},
|
||||||
|
{id: '600to1000', label: '600–1000 pages', min: 600, max: 1000},
|
||||||
|
{id: '1000plus', label: '1000+ pages', min: 1000, max: Infinity}
|
||||||
|
];
|
||||||
|
|
||||||
|
export function isRatingInRange(rating: number | undefined | null, rangeId: string): boolean {
|
||||||
|
if (rating == null) return false;
|
||||||
|
const range = ratingRanges.find(r => r.id === rangeId);
|
||||||
|
if (!range) return false;
|
||||||
|
return rating >= range.min && rating < range.max;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function isFileSizeInRange(fileSizeKb: number | undefined, rangeId: string): boolean {
|
||||||
|
if (fileSizeKb == null) return false;
|
||||||
|
const range = fileSizeRanges.find(r => r.id === rangeId);
|
||||||
|
if (!range) return false;
|
||||||
|
return fileSizeKb >= range.min && fileSizeKb < range.max;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function isPageCountInRange(pageCount: number | undefined, rangeId: string): boolean {
|
||||||
|
if (pageCount == null) return false;
|
||||||
|
const range = pageCountRanges.find(r => r.id === rangeId);
|
||||||
|
if (!range) return false;
|
||||||
|
return pageCount >= range.min && pageCount < range.max;
|
||||||
|
}
|
||||||
|
|
||||||
|
const getLanguageFilter = (book: Book) => {
|
||||||
|
const lang = book.metadata?.language;
|
||||||
|
return lang ? [{id: lang, name: lang}] : [];
|
||||||
|
};
|
||||||
|
|
||||||
|
function getFileSizeRangeFilters(sizeKb?: number) {
|
||||||
|
if (sizeKb == null) return [];
|
||||||
|
const match = fileSizeRanges.find(r => sizeKb >= r.min && sizeKb < r.max);
|
||||||
|
return match ? [{id: match.id, name: match.label}] : [];
|
||||||
|
}
|
||||||
|
|
||||||
|
function getRatingRangeFilters(rating?: number) {
|
||||||
|
if (rating == null) return [];
|
||||||
|
const match = ratingRanges.find(r => rating >= r.min && rating < r.max);
|
||||||
|
return match ? [{id: match.id, name: match.label}] : [];
|
||||||
|
}
|
||||||
|
|
||||||
|
function extractPublishedYearFilter(book: Book): { id: number; name: string }[] {
|
||||||
|
const date = book.metadata?.publishedDate;
|
||||||
|
if (!date) return [];
|
||||||
|
const year = new Date(date).getFullYear();
|
||||||
|
return [{id: year, name: year.toString()}];
|
||||||
|
}
|
||||||
|
|
||||||
|
function getShelfStatusFilter(book: Book): { id: string; name: string }[] {
|
||||||
|
const isShelved = book.shelves && book.shelves.length > 0;
|
||||||
|
return [{id: isShelved ? 'shelved' : 'unshelved', name: isShelved ? 'Shelved' : 'Unshelved'}];
|
||||||
|
}
|
||||||
|
|
||||||
|
function getPageCountRangeFilters(pageCount?: number) {
|
||||||
|
if (pageCount == null) return [];
|
||||||
|
const match = pageCountRanges.find(r => pageCount >= r.min && pageCount < r.max);
|
||||||
|
return match ? [{id: match.id, name: match.label}] : [];
|
||||||
|
}
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-book-filter',
|
selector: 'app-book-filter',
|
||||||
templateUrl: './book-filter.component.html',
|
templateUrl: './book-filter.component.html',
|
||||||
@@ -28,7 +115,9 @@ type Filter<T> = { value: T; bookCount: number };
|
|||||||
NgClass,
|
NgClass,
|
||||||
Badge,
|
Badge,
|
||||||
AsyncPipe,
|
AsyncPipe,
|
||||||
TitleCasePipe
|
TitleCasePipe,
|
||||||
|
FormsModule,
|
||||||
|
Divider
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
export class BookFilterComponent implements OnInit {
|
export class BookFilterComponent implements OnInit {
|
||||||
@@ -43,41 +132,72 @@ export class BookFilterComponent implements OnInit {
|
|||||||
filterStreams: Record<string, Observable<Filter<any>[]>> = {};
|
filterStreams: Record<string, Observable<Filter<any>[]>> = {};
|
||||||
filterTypes: string[] = [];
|
filterTypes: string[] = [];
|
||||||
|
|
||||||
|
readonly filterLabels: Record<string, string> = {
|
||||||
|
author: 'Author',
|
||||||
|
category: 'Category',
|
||||||
|
series: 'Series',
|
||||||
|
publisher: 'Publisher',
|
||||||
|
amazonRating: 'Amazon Rating',
|
||||||
|
goodreadsRating: 'Goodreads Rating',
|
||||||
|
hardcoverRating: 'Hardcover Rating',
|
||||||
|
publishedDate: 'Published Year',
|
||||||
|
fileSize: 'File Size',
|
||||||
|
shelfStatus: 'Shelf Status',
|
||||||
|
pageCount: 'Page Count',
|
||||||
|
language: 'Language',
|
||||||
|
};
|
||||||
|
|
||||||
bookService = inject(BookService);
|
bookService = inject(BookService);
|
||||||
|
|
||||||
ngOnInit(): void {
|
ngOnInit(): void {
|
||||||
if (this.entity$ && this.entityType$) {
|
if (this.entity$ && this.entityType$) {
|
||||||
this.filterStreams = {
|
this.filterStreams = {
|
||||||
author: this.getFilterStream((book: Book) => book.metadata?.authors.map((name) => ({id: name, name})) || [], 'id', 'name'),
|
author: this.getFilterStream((book: Book) => book.metadata?.authors.map(name => ({id: name, name})) || [], 'id', 'name'),
|
||||||
category: this.getFilterStream((book: Book) => book.metadata?.categories.map((name) => ({id: name, name})) || [], 'id', 'name'),
|
category: this.getFilterStream((book: Book) => book.metadata?.categories.map(name => ({id: name, name})) || [], 'id', 'name'),
|
||||||
series: this.getFilterStream((book) => (book.metadata?.seriesName ? [{id: book.metadata.seriesName, name: book.metadata.seriesName}] : []), 'id', 'name'),
|
series: this.getFilterStream((book) => (book.metadata?.seriesName ? [{id: book.metadata.seriesName, name: book.metadata.seriesName}] : []), 'id', 'name'),
|
||||||
publisher: this.getFilterStream((book) => (book.metadata?.publisher ? [{id: book.metadata.publisher, name: book.metadata.publisher}] : []), 'id', 'name'),
|
publisher: this.getFilterStream((book) => (book.metadata?.publisher ? [{id: book.metadata.publisher, name: book.metadata.publisher}] : []), 'id', 'name'),
|
||||||
|
amazonRating: this.getFilterStream((book: Book) => getRatingRangeFilters(book.metadata?.amazonRating!), 'id', 'name'),
|
||||||
|
goodreadsRating: this.getFilterStream((book: Book) => getRatingRangeFilters(book.metadata?.goodreadsRating!), 'id', 'name'),
|
||||||
|
hardcoverRating: this.getFilterStream((book: Book) => getRatingRangeFilters(book.metadata?.hardcoverRating!), 'id', 'name'),
|
||||||
|
publishedDate: this.getFilterStream(extractPublishedYearFilter, 'id', 'name'),
|
||||||
|
fileSize: this.getFilterStream((book: Book) => getFileSizeRangeFilters(book.fileSizeKb), 'id', 'name'),
|
||||||
|
pageCount: this.getFilterStream((book: Book) => getPageCountRangeFilters(book.metadata?.pageCount!), 'id', 'name'),
|
||||||
|
language: this.getFilterStream(getLanguageFilter, 'id', 'name'),
|
||||||
|
shelfStatus: this.getFilterStream(getShelfStatusFilter, 'id', 'name')
|
||||||
};
|
};
|
||||||
this.filterTypes = Object.keys(this.filterStreams);
|
this.filterTypes = Object.keys(this.filterStreams);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.resetFilter$) {
|
if (this.resetFilter$) {
|
||||||
this.resetFilter$.subscribe(() => this.clearActiveFilter());
|
this.resetFilter$.subscribe(() => this.clearActiveFilter());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private getFilterStream<T>(extractor: (book: Book) => T[] | undefined, idKey: keyof T, nameKey: keyof T): Observable<Filter<T[keyof T]>[]> {
|
private getFilterStream<T>(
|
||||||
return combineLatest([this.bookService.bookState$, this.entity$ ?? of(null), this.entityType$ ?? of(EntityType.ALL_BOOKS)])
|
extractor: (book: Book) => T[] | undefined,
|
||||||
.pipe(
|
idKey: keyof T,
|
||||||
map(([state, entity, entityType]) => {
|
nameKey: keyof T
|
||||||
const filteredBooks = this.filterBooksByEntityType(state.books || [], entity, entityType);
|
): Observable<Filter<T[keyof T]>[]> {
|
||||||
const filterMap = new Map<any, Filter<any>>();
|
return combineLatest([this.bookService.bookState$, this.entity$ ?? of(null), this.entityType$ ?? of(EntityType.ALL_BOOKS)]).pipe(
|
||||||
filteredBooks.forEach((book) => {
|
map(([state, entity, entityType]) => {
|
||||||
(extractor(book) || []).forEach((item) => {
|
const filteredBooks = this.filterBooksByEntityType(state.books || [], entity, entityType);
|
||||||
const id = item[idKey];
|
const filterMap = new Map<any, Filter<any>>();
|
||||||
if (!filterMap.has(id)) {
|
filteredBooks.forEach((book) => {
|
||||||
filterMap.set(id, {value: item, bookCount: 0});
|
(extractor(book) || []).forEach((item) => {
|
||||||
}
|
const id = item[idKey];
|
||||||
filterMap.get(id)!.bookCount += 1;
|
if (!filterMap.has(id)) {
|
||||||
});
|
filterMap.set(id, {value: item, bookCount: 0});
|
||||||
|
}
|
||||||
|
filterMap.get(id)!.bookCount += 1;
|
||||||
});
|
});
|
||||||
return Array.from(filterMap.values()).sort((a, b) => b.bookCount - a.bookCount || a.value[nameKey].localeCompare(b.value[nameKey]));
|
});
|
||||||
})
|
return Array.from(filterMap.values()).sort(
|
||||||
);
|
(a, b) =>
|
||||||
|
b.bookCount - a.bookCount ||
|
||||||
|
a.value[nameKey].toString().localeCompare(b.value[nameKey].toString())
|
||||||
|
);
|
||||||
|
})
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private filterBooksByEntityType(books: Book[], entity: any, entityType: EntityType): Book[] {
|
private filterBooksByEntityType(books: Book[], entity: any, entityType: EntityType): Book[] {
|
||||||
@@ -104,7 +224,8 @@ export class BookFilterComponent implements OnInit {
|
|||||||
} else {
|
} else {
|
||||||
this.activeFilters[filterType].push(value);
|
this.activeFilters[filterType].push(value);
|
||||||
}
|
}
|
||||||
this.filterSelected.emit({ ...this.activeFilters });
|
|
||||||
|
this.filterSelected.emit({...this.activeFilters});
|
||||||
}
|
}
|
||||||
|
|
||||||
clearActiveFilter() {
|
clearActiveFilter() {
|
||||||
|
|||||||
Reference in New Issue
Block a user