Implement magic shelves with dynamic rule-based filters
This commit is contained in:
committed by
Aditya Chandel
parent
0777d473dc
commit
63518b7eb8
@@ -3,7 +3,7 @@ import {ActivatedRoute, Router} from '@angular/router';
|
||||
import {ConfirmationService, MenuItem, MessageService, PrimeTemplate} from 'primeng/api';
|
||||
import {LibraryService} from '../../service/library.service';
|
||||
import {BookService} from '../../service/book.service';
|
||||
import {debounceTime, filter, map, switchMap, take} from 'rxjs/operators';
|
||||
import {catchError, debounceTime, filter, map, switchMap, take} from 'rxjs/operators';
|
||||
import {BehaviorSubject, combineLatest, Observable, of, Subject} from 'rxjs';
|
||||
import {ShelfService} from '../../service/shelf.service';
|
||||
import {DynamicDialogRef} from 'primeng/dynamicdialog';
|
||||
@@ -45,10 +45,14 @@ import {MultiSelect} from 'primeng/multiselect';
|
||||
import {TableColumnPreferenceService} from './table-column-preference-service';
|
||||
import {TieredMenu} from 'primeng/tieredmenu';
|
||||
import {BookMenuService} from '../../service/book-menu.service';
|
||||
import {MagicShelf, MagicShelfService} from '../../../magic-shelf-service';
|
||||
import {BookRuleEvaluatorService} from '../../../book-rule-evaluator.service';
|
||||
import {GroupRule} from '../../../magic-shelf-component/magic-shelf-component';
|
||||
|
||||
export enum EntityType {
|
||||
LIBRARY = 'Library',
|
||||
SHELF = 'Shelf',
|
||||
MAGIC_SHELF = 'Magic Shelf',
|
||||
ALL_BOOKS = 'All Books',
|
||||
UNSHELVED = 'Unshelved Books',
|
||||
}
|
||||
@@ -98,11 +102,11 @@ const SORT_DIRECTION = {
|
||||
})
|
||||
export class BookBrowserComponent implements OnInit, AfterViewInit {
|
||||
bookState$: Observable<BookState> | undefined;
|
||||
entity$: Observable<Library | Shelf | null> | undefined;
|
||||
entity$: Observable<Library | Shelf | MagicShelf | null> | undefined;
|
||||
entityType$: Observable<EntityType> | undefined;
|
||||
searchTerm$ = new BehaviorSubject<string>('');
|
||||
|
||||
entity: Library | Shelf | null = null;
|
||||
entity: Library | Shelf | MagicShelf | null = null;
|
||||
entityType: EntityType | undefined;
|
||||
bookTitle: string = '';
|
||||
entityOptions: MenuItem[] | undefined;
|
||||
@@ -143,6 +147,8 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
||||
private libraryShelfMenuService = inject(LibraryShelfMenuService);
|
||||
protected seriesCollapseFilter = inject(SeriesCollapseFilter);
|
||||
protected confirmationService = inject(ConfirmationService);
|
||||
protected magicShelfService = inject(MagicShelfService);
|
||||
protected bookRuleEvaluatorService = inject(BookRuleEvaluatorService);
|
||||
|
||||
private sideBarFilter = new SideBarFilter(this.selectedFilter, this.selectedFilterMode);
|
||||
private headerFilter = new HeaderFilter(this.searchTerm$);
|
||||
@@ -203,7 +209,9 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
||||
this.entityOptions = entity
|
||||
? this.isLibrary(entity)
|
||||
? this.libraryShelfMenuService.initializeLibraryMenuItems(entity)
|
||||
: this.libraryShelfMenuService.initializeShelfMenuItems(entity)
|
||||
: this.isMagicShelf(entity)
|
||||
? this.libraryShelfMenuService.initializeMagicShelfMenuItems(entity)
|
||||
: this.libraryShelfMenuService.initializeShelfMenuItems(entity)
|
||||
: [];
|
||||
});
|
||||
}
|
||||
@@ -238,6 +246,7 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
||||
const sortParam = paramMap.get(QUERY_PARAMS.SORT);
|
||||
const directionParam = paramMap.get(QUERY_PARAMS.DIRECTION);
|
||||
const filterParams = paramMap.get(QUERY_PARAMS.FILTER);
|
||||
const sidebarParam = paramMap.get(QUERY_PARAMS.SIDEBAR);
|
||||
|
||||
const parsedFilters: Record<string, string[]> = {};
|
||||
|
||||
@@ -320,7 +329,7 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
||||
: VIEW_MODES.GRID)
|
||||
: (effectivePrefs.view?.toLowerCase() ?? VIEW_MODES.GRID);
|
||||
|
||||
//this.showFilter = sidebarParam === 'true' || (sidebarParam === null && this.filterVisibility);
|
||||
//this.showFilter = sidebarParam === 'true' || (sidebarParam === null && filterParams !== filterParams);
|
||||
|
||||
this.bookSorter.updateSortOptions();
|
||||
|
||||
@@ -369,10 +378,14 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
||||
});
|
||||
}
|
||||
|
||||
private isLibrary(entity: Library | Shelf): entity is Library {
|
||||
private isLibrary(entity: Library | Shelf | MagicShelf): entity is Library {
|
||||
return (entity as Library).paths !== undefined;
|
||||
}
|
||||
|
||||
private isMagicShelf(entity: any): entity is MagicShelf {
|
||||
return entity && 'filterJson' in entity;
|
||||
}
|
||||
|
||||
get viewIcon(): string {
|
||||
return this.currentViewMode === VIEW_MODES.GRID ? 'pi pi-objects-column' : 'pi pi-table';
|
||||
}
|
||||
@@ -382,6 +395,7 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
||||
map(params => {
|
||||
const libraryId = Number(params.get('libraryId') || NaN);
|
||||
const shelfId = Number(params.get('shelfId') || NaN);
|
||||
const magicShelfId = Number(params.get('magicShelfId') || NaN);
|
||||
|
||||
if (!isNaN(libraryId)) {
|
||||
this.entityType = EntityType.LIBRARY;
|
||||
@@ -389,6 +403,9 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
||||
} else if (!isNaN(shelfId)) {
|
||||
this.entityType = EntityType.SHELF;
|
||||
return {entityId: shelfId, entityType: EntityType.SHELF};
|
||||
} else if (!isNaN(magicShelfId)) {
|
||||
this.entityType = EntityType.MAGIC_SHELF;
|
||||
return {entityId: magicShelfId, entityType: EntityType.MAGIC_SHELF};
|
||||
} else {
|
||||
this.entityType = EntityType.ALL_BOOKS;
|
||||
return {entityId: NaN, entityType: EntityType.ALL_BOOKS};
|
||||
@@ -397,12 +414,14 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
||||
);
|
||||
}
|
||||
|
||||
private fetchEntity(entityId: number, entityType: EntityType): Observable<Library | Shelf | null> {
|
||||
private fetchEntity(entityId: number, entityType: EntityType): Observable<Library | Shelf | MagicShelf | null> {
|
||||
switch (entityType) {
|
||||
case EntityType.LIBRARY:
|
||||
return this.fetchLibrary(entityId);
|
||||
case EntityType.SHELF:
|
||||
return this.fetchShelf(entityId);
|
||||
case EntityType.MAGIC_SHELF:
|
||||
return this.fetchMagicShelf(entityId);
|
||||
default:
|
||||
return of(null);
|
||||
}
|
||||
@@ -416,6 +435,8 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
||||
return this.fetchBooks(book =>
|
||||
book.shelves?.some(shelf => shelf.id === entityId) ?? false
|
||||
);
|
||||
case EntityType.MAGIC_SHELF:
|
||||
return this.fetchBooksMagicShelfBooks(entityId);
|
||||
case EntityType.ALL_BOOKS:
|
||||
default:
|
||||
return this.fetchAllBooks();
|
||||
@@ -440,6 +461,25 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
||||
);
|
||||
}
|
||||
|
||||
private fetchBooksMagicShelfBooks(magicShelfId: number): Observable<BookState> {
|
||||
return combineLatest([
|
||||
this.bookService.bookState$,
|
||||
this.magicShelfService.getShelf(magicShelfId)
|
||||
]).pipe(
|
||||
map(([bookState, magicShelf]) => {
|
||||
if (!bookState.loaded || bookState.error || !magicShelf?.filterJson) {
|
||||
return bookState;
|
||||
}
|
||||
const filteredBooks: Book[] | undefined = bookState.books?.filter(book =>
|
||||
this.bookRuleEvaluatorService.evaluateGroup(book, JSON.parse(magicShelf.filterJson!) as GroupRule)
|
||||
);
|
||||
const sortedBooks = this.sortService.applySort(filteredBooks ?? [], this.bookSorter.selectedSort!);
|
||||
return {...bookState, books: sortedBooks};
|
||||
}),
|
||||
switchMap(bookState => this.applyBookFilters(bookState))
|
||||
);
|
||||
}
|
||||
|
||||
private fetchBooks(bookFilter: (book: Book) => boolean): Observable<BookState> {
|
||||
return this.bookService.bookState$.pipe(
|
||||
map(bookState => {
|
||||
@@ -491,6 +531,22 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
||||
);
|
||||
}
|
||||
|
||||
private fetchMagicShelf(magicShelfId: number): Observable<MagicShelf | null> {
|
||||
return this.magicShelfService.shelvesState$.pipe(
|
||||
take(1),
|
||||
switchMap((state) => {
|
||||
const cached = state.shelves?.find(s => s.id === magicShelfId) ?? null;
|
||||
if (cached) {
|
||||
return of(cached);
|
||||
}
|
||||
return this.magicShelfService.getShelf(magicShelfId).pipe(
|
||||
map(shelf => shelf ?? null),
|
||||
catchError(() => of(null))
|
||||
);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
onCheckboxClicked(event: { index: number; bookId: number; selected: boolean; shiftKey: boolean }) {
|
||||
const {index, bookId, selected, shiftKey} = event;
|
||||
if (!shiftKey || this.lastSelectedIndex === null) {
|
||||
|
||||
+1
-4
@@ -12,7 +12,6 @@
|
||||
styleClass="h-8"></p-selectButton>
|
||||
</div>
|
||||
|
||||
<!-- Outer scroll -->
|
||||
<div class="flex-1 overflow-y-auto pb-4 pr-1" style="overscroll-behavior: contain;">
|
||||
<p-accordion [value]="expandedPanels">
|
||||
@for (filterType of filterTypes; track filterType; let i = $index) {
|
||||
@@ -30,7 +29,6 @@
|
||||
|
||||
<p-accordion-content>
|
||||
@if (filterStreams[filterType] | async; as filters) {
|
||||
<!-- Inner scrollable content with nested scroll behavior -->
|
||||
<div
|
||||
class="max-h-[27.5rem] overflow-y-auto overscroll-contain pr-1"
|
||||
style="overscroll-behavior: contain;">
|
||||
@@ -38,8 +36,7 @@
|
||||
<div
|
||||
class="cursor-pointer transition-colors duration-200 ease-in-out pb-1 flex items-center gap-2"
|
||||
[ngClass]="{
|
||||
'text-[var(--primary-color)]':
|
||||
activeFilters[filterType]?.includes(filter.value?.id || filter.value)
|
||||
'text-[var(--primary-color)]': activeFilters[filterType]?.includes(filter.value?.id || filter.value)
|
||||
}"
|
||||
(click)="handleFilterClick(filterType, filter.value?.id || filter.value)">
|
||||
<p-badge [value]="filter.bookCount"></p-badge>
|
||||
|
||||
+17
-1
@@ -13,6 +13,9 @@ import {FormsModule} from '@angular/forms';
|
||||
import {SelectButton} from 'primeng/selectbutton';
|
||||
import {UserService} from '../../../../settings/user-management/user.service';
|
||||
import {FilterSortPreferenceService} from '../filters/filter-sorting-preferences.service';
|
||||
import {MagicShelf} from '../../../../magic-shelf-service';
|
||||
import {GroupRule} from '../../../../magic-shelf-component/magic-shelf-component';
|
||||
import {BookRuleEvaluatorService} from '../../../../book-rule-evaluator.service';
|
||||
|
||||
type Filter<T> = { value: T; bookCount: number };
|
||||
|
||||
@@ -148,7 +151,7 @@ export class BookFilterComponent implements OnInit, OnDestroy {
|
||||
@Output() filterSelected = new EventEmitter<Record<string, any> | null>();
|
||||
@Output() filterModeChanged = new EventEmitter<'and' | 'or'>();
|
||||
|
||||
@Input() entity$!: Observable<Library | Shelf | null> | undefined;
|
||||
@Input() entity$!: Observable<Library | Shelf | MagicShelf | null> | undefined;
|
||||
@Input() entityType$!: Observable<EntityType> | undefined;
|
||||
@Input() resetFilter$!: Subject<void>;
|
||||
@Input() showFilter: boolean = false;
|
||||
@@ -185,6 +188,7 @@ export class BookFilterComponent implements OnInit, OnDestroy {
|
||||
bookService = inject(BookService);
|
||||
userService = inject(UserService);
|
||||
filterSortPreferenceService = inject(FilterSortPreferenceService);
|
||||
bookRuleEvaluatorService = inject(BookRuleEvaluatorService);
|
||||
|
||||
ngOnInit(): void {
|
||||
combineLatest([
|
||||
@@ -283,9 +287,21 @@ export class BookFilterComponent implements OnInit, OnDestroy {
|
||||
if (entityType === EntityType.LIBRARY && entity && 'id' in entity) {
|
||||
return books.filter((book) => book.libraryId === entity.id);
|
||||
}
|
||||
|
||||
if (entityType === EntityType.SHELF && entity && 'id' in entity) {
|
||||
return books.filter((book) => book.shelves?.some((shelf) => shelf.id === entity.id));
|
||||
}
|
||||
|
||||
if (entityType === EntityType.MAGIC_SHELF && entity && 'filterJson' in entity) {
|
||||
try {
|
||||
const groupRule = JSON.parse(entity.filterJson) as GroupRule;
|
||||
return books.filter((book) => this.bookRuleEvaluatorService.evaluateGroup(book, groupRule));
|
||||
} catch (e) {
|
||||
console.warn('Invalid filterJson for MagicShelf:', e);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
return books;
|
||||
}
|
||||
|
||||
|
||||
@@ -63,7 +63,7 @@ export class BookService {
|
||||
return currentState.books.filter(book => idSet.has(+book.id));
|
||||
}
|
||||
|
||||
updateBookShelves(bookIds: Set<number | undefined>, shelvesToAssign: Set<number | undefined>, shelvesToUnassign: Set<number | undefined>): Observable<Book[]> {
|
||||
updateBookShelves(bookIds: Set<number | undefined>, shelvesToAssign: Set<number | null | undefined>, shelvesToUnassign: Set<number | null | undefined>): Observable<Book[]> {
|
||||
const requestPayload = {
|
||||
bookIds: Array.from(bookIds),
|
||||
shelvesToAssign: Array.from(shelvesToAssign),
|
||||
@@ -422,6 +422,10 @@ export class BookService {
|
||||
);
|
||||
}
|
||||
|
||||
getMagicShelfBookCount(number: number) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*------------------ All the websocket handlers go below ------------------*/
|
||||
|
||||
|
||||
@@ -10,6 +10,8 @@ import {MetadataFetchOptionsComponent} from '../../metadata/metadata-options-dia
|
||||
import {MetadataRefreshType} from '../../metadata/model/request/metadata-refresh-type.enum';
|
||||
import {LibraryCreatorComponent} from '../components/library-creator/library-creator.component';
|
||||
import {ShelfEditDialogComponent} from '../components/shelf-edit-dialog/shelf-edit-dialog.component';
|
||||
import {MagicShelf, MagicShelfService} from '../../magic-shelf-service';
|
||||
import {MagicShelfComponent} from '../../magic-shelf-component/magic-shelf-component';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
@@ -22,8 +24,9 @@ export class LibraryShelfMenuService {
|
||||
private shelfService = inject(ShelfService);
|
||||
private router = inject(Router);
|
||||
private dialogService = inject(DialogService);
|
||||
private magicShelfService = inject(MagicShelfService);
|
||||
|
||||
initializeLibraryMenuItems(entity: Library | Shelf | null): MenuItem[] {
|
||||
initializeLibraryMenuItems(entity: Library | Shelf | MagicShelf | null): MenuItem[] {
|
||||
return [
|
||||
{
|
||||
label: 'Options',
|
||||
@@ -184,4 +187,53 @@ export class LibraryShelfMenuService {
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
initializeMagicShelfMenuItems(entity: any): MenuItem[] {
|
||||
return [
|
||||
{
|
||||
label: 'Options',
|
||||
items: [
|
||||
{
|
||||
label: 'Edit Magic Shelf',
|
||||
icon: 'pi pi-pen-to-square',
|
||||
command: () => {
|
||||
this.dialogService.open(MagicShelfComponent, {
|
||||
header: 'Edit Magic Shelf',
|
||||
modal: true,
|
||||
closable: true,
|
||||
data: {
|
||||
id: entity?.id
|
||||
}
|
||||
})
|
||||
}
|
||||
},
|
||||
{
|
||||
label: 'Delete Magic Shelf',
|
||||
icon: 'pi pi-trash',
|
||||
command: () => {
|
||||
this.confirmationService.confirm({
|
||||
message: `Are you sure you want to delete magic shelf: ${entity?.name}?`,
|
||||
header: 'Confirmation',
|
||||
accept: () => {
|
||||
this.magicShelfService.deleteShelf(entity?.id!).subscribe({
|
||||
complete: () => {
|
||||
this.router.navigate(['/']);
|
||||
this.messageService.add({severity: 'info', summary: 'Success', detail: 'Magic shelf was deleted'});
|
||||
},
|
||||
error: () => {
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: 'Failed',
|
||||
detail: 'Failed to delete shelf',
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user