Implement AND/OR filter mode toggle in book filters
This commit is contained in:
committed by
Aditya Chandel
parent
006cd8981b
commit
bbf922b525
@@ -5,7 +5,7 @@ import {LibraryService} from '../../service/library.service';
|
|||||||
import {BookService} from '../../service/book.service';
|
import {BookService} from '../../service/book.service';
|
||||||
import {map, switchMap} from 'rxjs/operators';
|
import {map, switchMap} from 'rxjs/operators';
|
||||||
import {filter, take} 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 {ShelfService} from '../../service/shelf.service';
|
||||||
import {ShelfAssignerComponent} from '../shelf-assigner/shelf-assigner.component';
|
import {ShelfAssignerComponent} from '../shelf-assigner/shelf-assigner.component';
|
||||||
import {DialogService, DynamicDialogRef} from 'primeng/dynamicdialog';
|
import {DialogService, DynamicDialogRef} from 'primeng/dynamicdialog';
|
||||||
@@ -100,6 +100,7 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
|||||||
@ViewChild(BookFilterComponent) bookFilterComponent!: BookFilterComponent;
|
@ViewChild(BookFilterComponent) bookFilterComponent!: BookFilterComponent;
|
||||||
|
|
||||||
selectedFilter = new BehaviorSubject<Record<string, any> | null>(null);
|
selectedFilter = new BehaviorSubject<Record<string, any> | null>(null);
|
||||||
|
selectedFilterMode = new BehaviorSubject<'and' | 'or'>('and');
|
||||||
protected resetFilterSubject = new Subject<void>();
|
protected resetFilterSubject = new Subject<void>();
|
||||||
|
|
||||||
protected userService = inject(UserService);
|
protected userService = inject(UserService);
|
||||||
@@ -278,24 +279,31 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private sideBarFilter(bookState: BookState): Observable<BookState> {
|
private sideBarFilter(bookState: BookState): Observable<BookState> {
|
||||||
return this.selectedFilter.pipe(
|
return combineLatest([this.selectedFilter, this.selectedFilterMode]).pipe(
|
||||||
map((activeFilters: Record<string, any[]> | null) => {
|
map(([activeFilters, mode]) => {
|
||||||
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]) => {
|
const matches = Object.entries(activeFilters).map(([filterType, filterValues]) => {
|
||||||
if (!Array.isArray(filterValues) || filterValues.length === 0) return true;
|
if (!Array.isArray(filterValues) || filterValues.length === 0) {
|
||||||
|
return mode === 'or';
|
||||||
|
}
|
||||||
switch (filterType) {
|
switch (filterType) {
|
||||||
case 'author':
|
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':
|
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':
|
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':
|
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':
|
case 'amazonRating':
|
||||||
return filterValues.some(range => isRatingInRange(book.metadata?.amazonRating, range));
|
return filterValues.some(range => isRatingInRange(book.metadata?.amazonRating, range));
|
||||||
case 'goodreadsRating':
|
case 'goodreadsRating':
|
||||||
@@ -314,9 +322,10 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
|||||||
case 'language':
|
case 'language':
|
||||||
return filterValues.includes(book.metadata?.language);
|
return filterValues.includes(book.metadata?.language);
|
||||||
default:
|
default:
|
||||||
return true;
|
return false;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
return mode === 'and' ? matches.every(m => m) : matches.some(m => m);
|
||||||
});
|
});
|
||||||
return { ...bookState, books: filteredBooks };
|
return { ...bookState, books: filteredBooks };
|
||||||
})
|
})
|
||||||
@@ -553,6 +562,10 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
|||||||
this.selectedFilter.next(filters);
|
this.selectedFilter.next(filters);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.bookFilterComponent.filterModeChanged.subscribe((mode: 'and' | 'or') => {
|
||||||
|
this.selectedFilterMode.next(mode);
|
||||||
|
});
|
||||||
|
|
||||||
this.activatedRoute.queryParamMap.subscribe(paramMap => {
|
this.activatedRoute.queryParamMap.subscribe(paramMap => {
|
||||||
const viewParam = paramMap.get(QUERY_PARAMS.VIEW);
|
const viewParam = paramMap.get(QUERY_PARAMS.VIEW);
|
||||||
const sortParam = paramMap.get(QUERY_PARAMS.SORT);
|
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">
|
<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 [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>
|
||||||
|
|||||||
+17
-2
@@ -10,7 +10,7 @@ import {Accordion, AccordionContent, AccordionHeader, AccordionPanel} from 'prim
|
|||||||
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 {FormsModule} from '@angular/forms';
|
||||||
import {Divider} from 'primeng/divider';
|
import {SelectButton} from 'primeng/selectbutton';
|
||||||
|
|
||||||
type Filter<T> = { value: T; bookCount: number };
|
type Filter<T> = { value: T; bookCount: number };
|
||||||
|
|
||||||
@@ -117,11 +117,12 @@ function getPageCountRangeFilters(pageCount?: number) {
|
|||||||
AsyncPipe,
|
AsyncPipe,
|
||||||
TitleCasePipe,
|
TitleCasePipe,
|
||||||
FormsModule,
|
FormsModule,
|
||||||
Divider
|
SelectButton
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
export class BookFilterComponent implements OnInit {
|
export class BookFilterComponent implements OnInit {
|
||||||
@Output() filterSelected = new EventEmitter<Record<string, any> | null>();
|
@Output() filterSelected = new EventEmitter<Record<string, any> | null>();
|
||||||
|
@Output() filterModeChanged = new EventEmitter<'and' | 'or'>();
|
||||||
|
|
||||||
@Input() showFilters: boolean = true;
|
@Input() showFilters: boolean = true;
|
||||||
@Input() entity$!: Observable<Library | Shelf | null> | undefined;
|
@Input() entity$!: Observable<Library | Shelf | null> | undefined;
|
||||||
@@ -131,6 +132,11 @@ export class BookFilterComponent implements OnInit {
|
|||||||
activeFilters: Record<string, any> = {};
|
activeFilters: Record<string, any> = {};
|
||||||
filterStreams: Record<string, Observable<Filter<any>[]>> = {};
|
filterStreams: Record<string, Observable<Filter<any>[]>> = {};
|
||||||
filterTypes: string[] = [];
|
filterTypes: string[] = [];
|
||||||
|
filterModeOptions = [
|
||||||
|
{ label: 'AND', value: 'and' },
|
||||||
|
{ label: 'OR', value: 'or' }
|
||||||
|
];
|
||||||
|
private _selectedFilterMode: 'and' | 'or' = 'and';
|
||||||
|
|
||||||
readonly filterLabels: Record<string, string> = {
|
readonly filterLabels: Record<string, string> = {
|
||||||
author: 'Author',
|
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[] {
|
private filterBooksByEntityType(books: Book[], entity: any, entityType: EntityType): Book[] {
|
||||||
if (entityType === EntityType.LIBRARY && entity && 'id' in entity) {
|
if (entityType === EntityType.LIBRARY && entity && 'id' in entity) {
|
||||||
return books.filter((book) => book.libraryId === entity.id);
|
return books.filter((book) => book.libraryId === entity.id);
|
||||||
|
|||||||
Reference in New Issue
Block a user