From 0e3cdbe41d973adb24711c5f9ff57d01fb55386b Mon Sep 17 00:00:00 2001
From: "aditya.chandel" <>
Date: Mon, 30 Dec 2024 11:17:46 -0700
Subject: [PATCH] Add all books menu item
---
booklore-ui/src/app/app-routing.module.ts | 3 +
.../book-browser/book-browser.component.html | 33 +++++--
.../book-browser/book-browser.component.ts | 98 +++++++++++--------
.../component/layout/app.menu.component.html | 8 +-
.../component/layout/app.menu.component.ts | 56 +++++++----
.../book/service/rx-stomp-service-factory.ts | 4 +-
...-rx-stomp.config.ts => rx-stomp.config.ts} | 2 +-
7 files changed, 129 insertions(+), 75 deletions(-)
rename booklore-ui/src/app/book/service/{my-rx-stomp.config.ts => rx-stomp.config.ts} (94%)
diff --git a/booklore-ui/src/app/app-routing.module.ts b/booklore-ui/src/app/app-routing.module.ts
index a40af3580..8749c5cdc 100644
--- a/booklore-ui/src/app/app-routing.module.ts
+++ b/booklore-ui/src/app/app-routing.module.ts
@@ -13,6 +13,9 @@ const routes: Routes = [
{
path: '', component: DashboardComponent,
},
+ {
+ path: 'all-books', component: BookBrowserComponent,
+ },
{
path: 'library/:libraryId/books', component: BookBrowserComponent,
},
diff --git a/booklore-ui/src/app/book/component/book-browser/book-browser.component.html b/booklore-ui/src/app/book/component/book-browser/book-browser.component.html
index de1bd2c57..0ff1fe16c 100644
--- a/booklore-ui/src/app/book/component/book-browser/book-browser.component.html
+++ b/booklore-ui/src/app/book/component/book-browser/book-browser.component.html
@@ -1,9 +1,26 @@
diff --git a/booklore-ui/src/app/book/component/book-browser/book-browser.component.ts b/booklore-ui/src/app/book/component/book-browser/book-browser.component.ts
index 8ceb8e9eb..ec1de9782 100644
--- a/booklore-ui/src/app/book/component/book-browser/book-browser.component.ts
+++ b/booklore-ui/src/app/book/component/book-browser/book-browser.component.ts
@@ -32,7 +32,6 @@ export class BookBrowserComponent implements OnInit {
entityType: string = '';
bookTitle: string = '';
entityOptions: MenuItem[] | undefined;
- multiSelectItems: MenuItem[] | undefined;
selectedBooks: Set
= 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 {
+ private fetchAllBooks(): Observable {
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 {
+ 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 = [
{
diff --git a/booklore-ui/src/app/book/component/layout/app.menu.component.html b/booklore-ui/src/app/book/component/layout/app.menu.component.html
index a176e29c7..e8011363b 100644
--- a/booklore-ui/src/app/book/component/layout/app.menu.component.html
+++ b/booklore-ui/src/app/book/component/layout/app.menu.component.html
@@ -1,7 +1,9 @@
diff --git a/booklore-ui/src/app/book/component/layout/app.menu.component.ts b/booklore-ui/src/app/book/component/layout/app.menu.component.ts
index 1bf4e1a34..b321b53cf 100644
--- a/booklore-ui/src/app/book/component/layout/app.menu.component.ts
+++ b/booklore-ui/src/app/book/component/layout/app.menu.component.ts
@@ -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 | undefined;
shelfMenu$: Observable | undefined;
+ homeMenu$: Observable | 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: ['/'] },
- ],
- },
- ];
- }
}
diff --git a/booklore-ui/src/app/book/service/rx-stomp-service-factory.ts b/booklore-ui/src/app/book/service/rx-stomp-service-factory.ts
index 01176a810..1906e2acc 100644
--- a/booklore-ui/src/app/book/service/rx-stomp-service-factory.ts
+++ b/booklore-ui/src/app/book/service/rx-stomp-service-factory.ts
@@ -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;
}
diff --git a/booklore-ui/src/app/book/service/my-rx-stomp.config.ts b/booklore-ui/src/app/book/service/rx-stomp.config.ts
similarity index 94%
rename from booklore-ui/src/app/book/service/my-rx-stomp.config.ts
rename to booklore-ui/src/app/book/service/rx-stomp.config.ts
index 08e4a565a..91176ac28 100644
--- a/booklore-ui/src/app/book/service/my-rx-stomp.config.ts
+++ b/booklore-ui/src/app/book/service/rx-stomp.config.ts
@@ -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',