diff --git a/booklore-ui/src/app/book/components/book-browser/book-browser.component.ts b/booklore-ui/src/app/book/components/book-browser/book-browser.component.ts index 5ec1a66e6..aa320419e 100644 --- a/booklore-ui/src/app/book/components/book-browser/book-browser.component.ts +++ b/booklore-ui/src/app/book/components/book-browser/book-browser.component.ts @@ -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 | null>(null); + selectedFilterMode = new BehaviorSubject<'and' | 'or'>('and'); protected resetFilterSubject = new Subject(); protected userService = inject(UserService); @@ -278,24 +279,31 @@ export class BookBrowserComponent implements OnInit, AfterViewInit { } private sideBarFilter(bookState: BookState): Observable { - return this.selectedFilter.pipe( - map((activeFilters: Record | 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); diff --git a/booklore-ui/src/app/book/components/book-browser/book-filter/book-filter.component.html b/booklore-ui/src/app/book/components/book-browser/book-filter/book-filter.component.html index daecd36bb..532ec54f3 100644 --- a/booklore-ui/src/app/book/components/book-browser/book-filter/book-filter.component.html +++ b/booklore-ui/src/app/book/components/book-browser/book-filter/book-filter.component.html @@ -1,5 +1,15 @@
-

Filters

+
+ Filter Mode + +
diff --git a/booklore-ui/src/app/book/components/book-browser/book-filter/book-filter.component.ts b/booklore-ui/src/app/book/components/book-browser/book-filter/book-filter.component.ts index d106e0980..31b8b4d50 100644 --- a/booklore-ui/src/app/book/components/book-browser/book-filter/book-filter.component.ts +++ b/booklore-ui/src/app/book/components/book-browser/book-filter/book-filter.component.ts @@ -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 = { 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 | null>(); + @Output() filterModeChanged = new EventEmitter<'and' | 'or'>(); @Input() showFilters: boolean = true; @Input() entity$!: Observable | undefined; @@ -131,6 +132,11 @@ export class BookFilterComponent implements OnInit { activeFilters: Record = {}; filterStreams: Record[]>> = {}; filterTypes: string[] = []; + filterModeOptions = [ + { label: 'AND', value: 'and' }, + { label: 'OR', value: 'or' } + ]; + private _selectedFilterMode: 'and' | 'or' = 'and'; readonly filterLabels: Record = { 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);