Implement AND/OR filter mode toggle in book filters
This commit is contained in:
committed by
aditya.chandel
parent
253d1db35d
commit
743bb3b7e4
@@ -5,7 +5,7 @@ import {LibraryService} from '../../service/library.service';
|
||||
import {BookService} from '../../service/book.service';
|
||||
import {map, switchMap} from 'rxjs/operators';
|
||||
import {filter, take} from 'rxjs/operators';
|
||||
import {BehaviorSubject, Observable, of, Subject} from 'rxjs';
|
||||
import {BehaviorSubject, combineLatest, Observable, of, Subject} from 'rxjs';
|
||||
import {ShelfService} from '../../service/shelf.service';
|
||||
import {ShelfAssignerComponent} from '../shelf-assigner/shelf-assigner.component';
|
||||
import {DialogService, DynamicDialogRef} from 'primeng/dynamicdialog';
|
||||
@@ -100,6 +100,7 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
||||
@ViewChild(BookFilterComponent) bookFilterComponent!: BookFilterComponent;
|
||||
|
||||
selectedFilter = new BehaviorSubject<Record<string, any> | null>(null);
|
||||
selectedFilterMode = new BehaviorSubject<'and' | 'or'>('and');
|
||||
protected resetFilterSubject = new Subject<void>();
|
||||
|
||||
protected userService = inject(UserService);
|
||||
@@ -278,24 +279,31 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
||||
}
|
||||
|
||||
private sideBarFilter(bookState: BookState): Observable<BookState> {
|
||||
return this.selectedFilter.pipe(
|
||||
map((activeFilters: Record<string, any[]> | null) => {
|
||||
if (!activeFilters) {
|
||||
return bookState;
|
||||
}
|
||||
return combineLatest([this.selectedFilter, this.selectedFilterMode]).pipe(
|
||||
map(([activeFilters, mode]) => {
|
||||
if (!activeFilters) return bookState;
|
||||
const filteredBooks = (bookState.books || []).filter(book => {
|
||||
return Object.entries(activeFilters).every(([filterType, filterValues]) => {
|
||||
if (!Array.isArray(filterValues) || filterValues.length === 0) return true;
|
||||
|
||||
const matches = Object.entries(activeFilters).map(([filterType, filterValues]) => {
|
||||
if (!Array.isArray(filterValues) || filterValues.length === 0) {
|
||||
return mode === 'or';
|
||||
}
|
||||
switch (filterType) {
|
||||
case 'author':
|
||||
return filterValues.every(val => book.metadata?.authors?.includes(val));
|
||||
return mode === 'and'
|
||||
? filterValues.every(val => book.metadata?.authors?.includes(val))
|
||||
: filterValues.some(val => book.metadata?.authors?.includes(val));
|
||||
case 'category':
|
||||
return filterValues.every(val => book.metadata?.categories?.includes(val));
|
||||
return mode === 'and'
|
||||
? filterValues.every(val => book.metadata?.categories?.includes(val))
|
||||
: filterValues.some(val => book.metadata?.categories?.includes(val));
|
||||
case 'publisher':
|
||||
return filterValues.every(val => book.metadata?.publisher === val);
|
||||
return mode === 'and'
|
||||
? filterValues.every(val => book.metadata?.publisher === val)
|
||||
: filterValues.some(val => book.metadata?.publisher === val);
|
||||
case 'series':
|
||||
return filterValues.every(val => book.metadata?.seriesName === val);
|
||||
return mode === 'and'
|
||||
? filterValues.every(val => book.metadata?.seriesName === val)
|
||||
: filterValues.some(val => book.metadata?.seriesName === val);
|
||||
case 'amazonRating':
|
||||
return filterValues.some(range => isRatingInRange(book.metadata?.amazonRating, range));
|
||||
case 'goodreadsRating':
|
||||
@@ -314,9 +322,10 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
||||
case 'language':
|
||||
return filterValues.includes(book.metadata?.language);
|
||||
default:
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
});
|
||||
return mode === 'and' ? matches.every(m => m) : matches.some(m => m);
|
||||
});
|
||||
return { ...bookState, books: filteredBooks };
|
||||
})
|
||||
@@ -553,6 +562,10 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
||||
this.selectedFilter.next(filters);
|
||||
});
|
||||
|
||||
this.bookFilterComponent.filterModeChanged.subscribe((mode: 'and' | 'or') => {
|
||||
this.selectedFilterMode.next(mode);
|
||||
});
|
||||
|
||||
this.activatedRoute.queryParamMap.subscribe(paramMap => {
|
||||
const viewParam = paramMap.get(QUERY_PARAMS.VIEW);
|
||||
const sortParam = paramMap.get(QUERY_PARAMS.SORT);
|
||||
|
||||
+11
-1
@@ -1,5 +1,15 @@
|
||||
<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>
|
||||
<div class="flex items-center justify-between px-4 py-4">
|
||||
<span class="text font-semibold">Filter Mode</span>
|
||||
<p-selectButton
|
||||
[options]="filterModeOptions"
|
||||
[(ngModel)]="selectedFilterMode"
|
||||
optionLabel="label"
|
||||
optionValue="value"
|
||||
class="text-sm"
|
||||
allowEmpty="false"
|
||||
styleClass="h-8"></p-selectButton>
|
||||
</div>
|
||||
<p-accordion [value]="0">
|
||||
<p-accordion-panel *ngFor="let filterType of filterTypes; let i = index" [value]="i">
|
||||
<p-accordion-header>
|
||||
|
||||
+17
-2
@@ -10,7 +10,7 @@ import {Accordion, AccordionContent, AccordionHeader, AccordionPanel} from 'prim
|
||||
import {AsyncPipe, NgClass, NgForOf, NgIf, TitleCasePipe} from '@angular/common';
|
||||
import {Badge} from 'primeng/badge';
|
||||
import {FormsModule} from '@angular/forms';
|
||||
import {Divider} from 'primeng/divider';
|
||||
import {SelectButton} from 'primeng/selectbutton';
|
||||
|
||||
type Filter<T> = { value: T; bookCount: number };
|
||||
|
||||
@@ -117,11 +117,12 @@ function getPageCountRangeFilters(pageCount?: number) {
|
||||
AsyncPipe,
|
||||
TitleCasePipe,
|
||||
FormsModule,
|
||||
Divider
|
||||
SelectButton
|
||||
]
|
||||
})
|
||||
export class BookFilterComponent implements OnInit {
|
||||
@Output() filterSelected = new EventEmitter<Record<string, any> | null>();
|
||||
@Output() filterModeChanged = new EventEmitter<'and' | 'or'>();
|
||||
|
||||
@Input() showFilters: boolean = true;
|
||||
@Input() entity$!: Observable<Library | Shelf | null> | undefined;
|
||||
@@ -131,6 +132,11 @@ export class BookFilterComponent implements OnInit {
|
||||
activeFilters: Record<string, any> = {};
|
||||
filterStreams: Record<string, Observable<Filter<any>[]>> = {};
|
||||
filterTypes: string[] = [];
|
||||
filterModeOptions = [
|
||||
{ label: 'AND', value: 'and' },
|
||||
{ label: 'OR', value: 'or' }
|
||||
];
|
||||
private _selectedFilterMode: 'and' | 'or' = 'and';
|
||||
|
||||
readonly filterLabels: Record<string, string> = {
|
||||
author: 'Author',
|
||||
@@ -200,6 +206,15 @@ export class BookFilterComponent implements OnInit {
|
||||
);
|
||||
}
|
||||
|
||||
get selectedFilterMode(): 'and' | 'or' {
|
||||
return this._selectedFilterMode;
|
||||
}
|
||||
|
||||
set selectedFilterMode(mode: 'and' | 'or') {
|
||||
this._selectedFilterMode = mode;
|
||||
this.filterModeChanged.emit(mode);
|
||||
}
|
||||
|
||||
private filterBooksByEntityType(books: Book[], entity: any, entityType: EntityType): Book[] {
|
||||
if (entityType === EntityType.LIBRARY && entity && 'id' in entity) {
|
||||
return books.filter((book) => book.libraryId === entity.id);
|
||||
|
||||
Reference in New Issue
Block a user