Add all books menu item

This commit is contained in:
aditya.chandel
2024-12-30 11:17:46 -07:00
parent f931d026fa
commit 0e3cdbe41d
7 changed files with 129 additions and 75 deletions
@@ -13,6 +13,9 @@ const routes: Routes = [
{
path: '', component: DashboardComponent,
},
{
path: 'all-books', component: BookBrowserComponent,
},
{
path: 'library/:libraryId/books', component: BookBrowserComponent,
},
@@ -1,9 +1,26 @@
<div>
<div class="book-browser-header">
<div class="left-items">
<p class="entity-name text-2xl">{{ (entityType$ | async) }}: {{ (entity$ | async)?.name }}</p>
<p-button icon="pi pi-ellipsis-v" [rounded]="true" [text]="true" severity="info" (click)="entitymenu.toggle($event)"/>
<p-menu #entitymenu [model]="entityOptions" [popup]="true" appendTo="body"></p-menu>
<ng-container *ngIf="entityType$ | async as entityType">
<p *ngIf="entityType !== 'all-books'" class="entity-name text-2xl">
{{ entityType }}: {{ (entity$ | async)?.name }}
</p>
<p *ngIf="entityType === 'all-books'" class="entity-name text-2xl">
All Books
</p>
<div *ngIf="entityType !== 'all-books'">
<p-button
icon="pi pi-ellipsis-v"
[rounded]="true"
[text]="true"
severity="info"
(click)="entitymenu.toggle($event)" />
<p-menu #entitymenu [model]="entityOptions" [popup]="true" appendTo="body"></p-menu>
</div>
</ng-container>
</div>
<div class="header-right-items">
@@ -53,12 +70,12 @@
</div>
<div class="book-browser-footer" *ngIf="selectedBooks.size > 0">
<div class="footer-items-container">
<!--<p-button *ngIf="(entityType$ | async) === 'Library'" icon="pi pi-ellipsis-v" label="Options" severity="info" (click)="menu.toggle($event)"></p-button>-->
<p-button *ngIf="(entityType$ | async) === 'Library'" icon="pi pi-book" label="Assign Shelf" severity="info" (onClick)="openShelfAssigner()"></p-button>
<p-button *ngIf="(entityType$ | async) === 'Shelf'" icon="pi pi-minus-circle" label="Unshelf" severity="info" (click)="unshelfBooks()"></p-button>
<p-button icon="pi pi-times" label="Deselect" severity="warn" (click)="deselectAllBooks()"></p-button>
<ng-container *ngIf="entityType$ | async as entityType">
<p-button *ngIf="entityType === 'Library' || entityType ==='all-books'" icon="pi pi-book" label="Assign Shelf" severity="info" (onClick)="openShelfAssigner()"></p-button>
<p-button *ngIf="entityType === 'Shelf'" icon="pi pi-minus-circle" label="Unshelf" severity="info" (click)="unshelfBooks()"></p-button>
<p-button icon="pi pi-times" label="Deselect" severity="warn" (click)="deselectAllBooks()"></p-button>
</ng-container>
</div>
<!--<p-menu #menu [model]="multiSelectItems" [popup]="true" appendTo="body"></p-menu>-->
</div>
</div>
@@ -32,7 +32,6 @@ export class BookBrowserComponent implements OnInit {
entityType: string = '';
bookTitle: string = '';
entityOptions: MenuItem[] | undefined;
multiSelectItems: MenuItem[] | undefined;
selectedBooks: Set<number> = new Set();
selectedSort: SortOption | null = null;
sortOptions: SortOption[] = [];
@@ -54,31 +53,43 @@ export class BookBrowserComponent implements OnInit {
ngOnInit(): void {
this.sortOptions = SortOptionsHelper.generateSortOptions();
const routeParam$ = this.getRouteParams();
const isAllBooksRoute = this.activatedRoute.snapshot.routeConfig?.path === 'all-books';
this.bookState$ = routeParam$.pipe(
switchMap(({libraryId, shelfId}) => this.fetchBooksByEntity(libraryId, shelfId)),
);
if (isAllBooksRoute) {
this.entityType$ = this.activatedRoute.url.pipe(
map(segments => {
const fullPath = segments.map(segment => segment.path).join('/');
return fullPath === 'all-books' ? 'all-books' : null;
})
);
this.bookState$ = this.fetchAllBooks();
} else {
this.entity$ = routeParam$.pipe(
switchMap(({libraryId, shelfId}) => this.fetchEntity(libraryId, shelfId))
);
const routeParam$ = this.getRouteParams();
this.entityType$ = routeParam$.pipe(
map(({libraryId, shelfId}) => libraryId ? 'Library' : shelfId ? 'Shelf' : null)
);
this.bookState$ = routeParam$.pipe(
switchMap(({libraryId, shelfId}) => this.fetchBooksByEntity(libraryId, shelfId)),
);
this.entity$.subscribe(entity => {
this.entity = entity;
this.setSelectedSortFromEntity(entity);
});
this.entity$ = routeParam$.pipe(
switchMap(({libraryId, shelfId}) => this.fetchEntity(libraryId, shelfId))
);
this.entityType$ = routeParam$.pipe(
map(({libraryId, shelfId}) => libraryId ? 'Library' : shelfId ? 'Shelf' : null)
);
this.entity$.subscribe(entity => {
this.entity = entity;
this.setSelectedSortFromEntity(entity);
});
}
this.activatedRoute.paramMap.subscribe(() => {
this.bookTitle$.next('');
this.bookTitle = '';
});
//this.setupMenu();
}
private getRouteParams(): Observable<{ libraryId: number; shelfId: number }> {
@@ -127,15 +138,12 @@ export class BookBrowserComponent implements OnInit {
});
}
private fetchBooks(bookFilter: (book: Book) => boolean): Observable<BookState> {
private fetchAllBooks(): Observable<BookState> {
return this.bookService.bookState$.pipe(
map(bookState => {
if (bookState.loaded && !bookState.error) {
const filteredBooks = bookState.books!.filter(book => {
return bookFilter(book);
});
const sortedBooks = this.sortService.applySort(filteredBooks, this.selectedSort);
return { ...bookState, books: sortedBooks };
const sortedBooks = this.sortService.applySort(bookState.books || [], this.selectedSort);
return {...bookState, books: sortedBooks};
} else {
return bookState;
}
@@ -145,7 +153,34 @@ export class BookBrowserComponent implements OnInit {
map(title => {
if (title && title.trim() !== '') {
const filteredBooks = bookState.books?.filter(book => book.metadata?.title?.toLowerCase().includes(title.toLowerCase())) || null;
return { ...bookState, books: filteredBooks };
return {...bookState, books: filteredBooks};
}
return bookState;
})
)
)
);
}
private fetchBooks(bookFilter: (book: Book) => boolean): Observable<BookState> {
return this.bookService.bookState$.pipe(
map(bookState => {
if (bookState.loaded && !bookState.error) {
const filteredBooks = bookState.books!.filter(book => {
return bookFilter(book);
});
const sortedBooks = this.sortService.applySort(filteredBooks, this.selectedSort);
return {...bookState, books: sortedBooks};
} else {
return bookState;
}
}),
switchMap(bookState =>
this.bookTitle$.pipe(
map(title => {
if (title && title.trim() !== '') {
const filteredBooks = bookState.books?.filter(book => book.metadata?.title?.toLowerCase().includes(title.toLowerCase())) || null;
return {...bookState, books: filteredBooks};
}
return bookState;
})
@@ -253,23 +288,6 @@ export class BookBrowserComponent implements OnInit {
});
}
/*setupMenu(): void {
this.multiSelectItems = [
{
label: 'Options',
items: [
{
label: 'Edit shelf',
icon: 'pi pi-folder',
command: () => {
}
}
],
},
];
}*/
private initializeLibraryMenuItems(): void {
this.entityOptions = [
{
@@ -1,7 +1,9 @@
<ul class="layout-menu">
<ng-container *ngFor="let item of home; let i = index;">
<li app-menuitem *ngIf="!item.separator" [item]="item" [index]="i" [root]="true"></li>
<li *ngIf="item.separator" class="menu-separator"></li>
<ng-container *ngIf="homeMenu$ | async as homeMenu">
<ng-container *ngFor="let item of homeMenu; let i = index;">
<li app-menuitem *ngIf="!item.separator" [item]="item" [index]="i" [root]="true"></li>
<li *ngIf="item.separator" class="menu-separator"></li>
</ng-container>
</ng-container>
</ul>
@@ -1,11 +1,12 @@
import { Component, OnInit } from '@angular/core';
import { AppMenuitemComponent } from './app.menuitem.component';
import { AsyncPipe, NgForOf, NgIf } from '@angular/common';
import { MenuModule } from 'primeng/menu';
import { LibraryService } from '../../service/library.service';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { ShelfService } from '../../service/shelf.service';
import {Component, OnInit} from '@angular/core';
import {AppMenuitemComponent} from './app.menuitem.component';
import {AsyncPipe, NgForOf, NgIf} from '@angular/common';
import {MenuModule} from 'primeng/menu';
import {LibraryService} from '../../service/library.service';
import {Observable, of} from 'rxjs';
import {map} from 'rxjs/operators';
import {ShelfService} from '../../service/shelf.service';
import {BookService} from '../../service/book.service';
@Component({
selector: 'app-menu',
@@ -13,11 +14,12 @@ import { ShelfService } from '../../service/shelf.service';
templateUrl: './app.menu.component.html',
})
export class AppMenuComponent implements OnInit {
home: any[] = [];
libraryMenu$: Observable<any> | undefined;
shelfMenu$: Observable<any> | undefined;
homeMenu$: Observable<any> | undefined;
constructor(private libraryService: LibraryService, private shelfService: ShelfService) {}
constructor(private libraryService: LibraryService, private shelfService: ShelfService, private bookService: BookService) {
}
ngOnInit(): void {
this.libraryMenu$ = this.libraryService.libraryState$.pipe(
@@ -50,17 +52,29 @@ export class AppMenuComponent implements OnInit {
])
);
this.populateHome();
this.homeMenu$ = this.bookService.bookState$.pipe(
map((bookState) => {
return [
{
label: 'Home',
separator: false,
items: [
{
label: 'Dashboard',
icon: 'pi pi-fw pi-home',
routerLink: ['/'],
},
{
label: 'All Books',
icon: 'pi pi-fw pi-book',
routerLink: ['/all-books'],
bookCount$: of(bookState.books ? bookState.books.length : 0),
},
],
},
];
})
);
}
populateHome(): void {
this.home = [
{
label: 'Home',
items: [
{ label: 'Dashboard', icon: 'pi pi-fw pi-home', routerLink: ['/'] },
],
},
];
}
}
@@ -1,9 +1,9 @@
import { RxStompService } from './rx-stomp.service';
import { myRxStompConfig } from './my-rx-stomp.config';
import { rxStompConfig } from './rx-stomp.config';
export function rxStompServiceFactory() {
const rxStomp = new RxStompService();
rxStomp.configure(myRxStompConfig);
rxStomp.configure(rxStompConfig);
rxStomp.activate();
return rxStomp;
}
@@ -1,6 +1,6 @@
import { RxStompConfig } from '@stomp/rx-stomp';
export const myRxStompConfig: RxStompConfig = {
export const rxStompConfig: RxStompConfig = {
// Which server?
brokerURL: 'ws://localhost:8080/ws',