From 69c1c443d928db0fdd3883fdfc1db4022bbfbd56 Mon Sep 17 00:00:00 2001 From: "aditya.chandel" <> Date: Tue, 21 Jan 2025 12:05:52 -0700 Subject: [PATCH] Make left menu work --- .../booklore/service/LibraryService.java | 69 ++++--- .../book-browser/book-browser.component.html | 8 + .../layout-menu/app.menu.component.ts | 15 +- .../layout-menu/app.menuitem.component.html | 37 ++-- .../layout-menu/app.menuitem.component.ts | 191 +++++++++--------- .../layout-topbar/app.topbar.component.html | 2 +- 6 files changed, 178 insertions(+), 144 deletions(-) diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/LibraryService.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/LibraryService.java index 27f36f17a..18faabccd 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/LibraryService.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/LibraryService.java @@ -50,43 +50,46 @@ public class LibraryService { Set deletedPaths = currentPaths.stream().filter(path -> !updatedPaths.contains(path)).collect(Collectors.toSet()); Set newPaths = updatedPaths.stream().filter(path -> !currentPaths.contains(path)).collect(Collectors.toSet()); - if (!deletedPaths.isEmpty()) { - Set pathsToRemove = library.getLibraryPaths().stream() - .filter(pathEntity -> deletedPaths.contains(pathEntity.getPath())) - .collect(Collectors.toSet()); - library.getLibraryPaths().removeAll(pathsToRemove); + if(newPaths.isEmpty() && deletedPaths.isEmpty()) { + return libraryMapper.toLibrary(libraryRepository.save(library)); + } else { + if (!deletedPaths.isEmpty()) { + Set pathsToRemove = library.getLibraryPaths().stream() + .filter(pathEntity -> deletedPaths.contains(pathEntity.getPath())) + .collect(Collectors.toSet()); + library.getLibraryPaths().removeAll(pathsToRemove); - List books = bookRepository.findAllBookIdsByLibraryPathIdIn(pathsToRemove.stream().map(LibraryPathEntity::getId).collect(Collectors.toSet())); - if (!books.isEmpty()) { - notificationService.sendMessage(Topic.BOOKS_REMOVE, books); + List books = bookRepository.findAllBookIdsByLibraryPathIdIn(pathsToRemove.stream().map(LibraryPathEntity::getId).collect(Collectors.toSet())); + if (!books.isEmpty()) { + notificationService.sendMessage(Topic.BOOKS_REMOVE, books); + } + + libraryPathRepository.deleteAll(pathsToRemove); + libraryPathRepository.saveAll(library.getLibraryPaths()); + libraryRepository.save(library); } - libraryPathRepository.deleteAll(pathsToRemove); - libraryPathRepository.saveAll(library.getLibraryPaths()); - libraryRepository.save(library); + if (!newPaths.isEmpty()) { + Set newPathEntities = newPaths.stream() + .map(path -> LibraryPathEntity.builder().path(path).library(library).build()) + .collect(Collectors.toSet()); + library.getLibraryPaths().addAll(newPathEntities); + libraryPathRepository.saveAll(library.getLibraryPaths()); + libraryRepository.save(library); + + Thread.startVirtualThread(() -> { + try { + libraryProcessingService.processLibrary(libraryId); + } catch (InvalidDataAccessApiUsageException e) { + log.warn("InvalidDataAccessApiUsageException - Library id: {}", libraryId); + } catch (IOException e) { + log.error("Error while parsing library books", e); + } + log.info("Parsing task completed!"); + }); + } + return libraryMapper.toLibrary(libraryRepository.save(library)); } - - if (!newPaths.isEmpty()) { - Set newPathEntities = newPaths.stream() - .map(path -> LibraryPathEntity.builder().path(path).library(library).build()) - .collect(Collectors.toSet()); - library.getLibraryPaths().addAll(newPathEntities); - libraryPathRepository.saveAll(library.getLibraryPaths()); - libraryRepository.save(library); - - Thread.startVirtualThread(() -> { - try { - libraryProcessingService.processLibrary(libraryId); - } catch (InvalidDataAccessApiUsageException e) { - log.warn("InvalidDataAccessApiUsageException - Library id: {}", libraryId); - } catch (IOException e) { - log.error("Error while parsing library books", e); - } - log.info("Parsing task completed!"); - }); - } - - return libraryMapper.toLibrary(library); } public Library createLibrary(CreateLibraryRequest request) { diff --git a/booklore-ui/src/app/book/components/book-browser/book-browser.component.html b/booklore-ui/src/app/book/components/book-browser/book-browser.component.html index a93f0e376..908b1a9ff 100644 --- a/booklore-ui/src/app/book/components/book-browser/book-browser.component.html +++ b/booklore-ui/src/app/book/components/book-browser/book-browser.component.html @@ -26,6 +26,14 @@ + + + + + | undefined; homeMenu$: Observable | undefined; - constructor(private libraryService: LibraryService, private shelfService: ShelfService, private bookService: BookService) { - } + private libraryService = inject(LibraryService); + private shelfService = inject(ShelfService); + private bookService = inject(BookService); + private libraryShelfMenuService = inject(LibraryShelfMenuService); + ngOnInit(): void { this.libraryMenu$ = this.libraryService.libraryState$.pipe( @@ -28,7 +32,9 @@ export class AppMenuComponent implements OnInit { label: 'Library', separator: false, items: state.libraries?.map((library) => ({ + menu: this.libraryShelfMenuService.initializeLibraryMenuItems(library), label: library.name, + type: 'Library', icon: 'pi pi-' + library.icon, routerLink: [`/library/${library.id}/books`], bookCount$: this.libraryService.getBookCount(library.id ?? 0), @@ -43,7 +49,9 @@ export class AppMenuComponent implements OnInit { label: 'Shelves', separator: false, items: state.shelves?.map((shelf) => ({ + menu: this.libraryShelfMenuService.initializeLibraryMenuItems(shelf), label: shelf.name, + type: 'Shelf', icon: 'pi pi-' + shelf.icon, routerLink: [`/shelf/${shelf.id}/books`], bookCount$: this.shelfService.getBookCount(shelf.id ?? 0), @@ -66,6 +74,7 @@ export class AppMenuComponent implements OnInit { }, { label: 'All Books', + type: 'All Books', icon: 'pi pi-fw pi-book', routerLink: ['/all-books'], bookCount$: of(bookState.books ? bookState.books.length : 0), diff --git a/booklore-ui/src/app/layout/component/layout-menu/app.menuitem.component.html b/booklore-ui/src/app/layout/component/layout-menu/app.menuitem.component.html index 5418e2bb1..55f9b52fb 100644 --- a/booklore-ui/src/app/layout/component/layout-menu/app.menuitem.component.html +++ b/booklore-ui/src/app/layout/component/layout-menu/app.menuitem.component.html @@ -28,19 +28,30 @@ - - - + + + + + + + + +

+ {{ (item.bookCount$ | async)?.toString() || '0' }} +

+
diff --git a/booklore-ui/src/app/layout/component/layout-menu/app.menuitem.component.ts b/booklore-ui/src/app/layout/component/layout-menu/app.menuitem.component.ts index 0ed02a91e..df6f41b10 100644 --- a/booklore-ui/src/app/layout/component/layout-menu/app.menuitem.component.ts +++ b/booklore-ui/src/app/layout/component/layout-menu/app.menuitem.component.ts @@ -7,119 +7,122 @@ import {MenuService} from './service/app.menu.service'; import {AsyncPipe, NgClass, NgForOf, NgIf} from '@angular/common'; import {Ripple} from 'primeng/ripple'; import {Button} from 'primeng/button'; +import {Menu} from 'primeng/menu'; +import {MenuItem} from 'primeng/api'; @Component({ - selector: '[app-menuitem]', - templateUrl: './app.menuitem.component.html', - styleUrl: './app.menuitem.component.scss', - imports: [ - RouterLink, - RouterLinkActive, - NgClass, - NgForOf, - Ripple, - NgIf, - AsyncPipe, - Button - ], - animations: [ - trigger('children', [ - state('collapsed', style({ - height: '0' - })), - state('expanded', style({ - height: '*' - })), - transition('collapsed <=> expanded', animate('400ms cubic-bezier(0.86, 0, 0.07, 1)')) - ]) - ] + selector: '[app-menuitem]', + templateUrl: './app.menuitem.component.html', + styleUrl: './app.menuitem.component.scss', + imports: [ + RouterLink, + RouterLinkActive, + NgClass, + NgForOf, + Ripple, + NgIf, + AsyncPipe, + Button, + Menu + ], + animations: [ + trigger('children', [ + state('collapsed', style({ + height: '0' + })), + state('expanded', style({ + height: '*' + })), + transition('collapsed <=> expanded', animate('400ms cubic-bezier(0.86, 0, 0.07, 1)')) + ]) + ] }) export class AppMenuitemComponent implements OnInit, OnDestroy { - @Input() item: any; - @Input() index!: number; - @Input() @HostBinding('class.layout-root-menuitem') root!: boolean; - @Input() parentKey!: string; + @Input() item: any; + @Input() index!: number; + @Input() @HostBinding('class.layout-root-menuitem') root!: boolean; + @Input() parentKey!: string; - hovered = false; + hovered = false; - active = false; - menuSourceSubscription: Subscription; - menuResetSubscription: Subscription; - key: string = ""; + active = false; + menuSourceSubscription: Subscription; + menuResetSubscription: Subscription; + key: string = ""; - constructor(public router: Router, private menuService: MenuService) { - this.menuSourceSubscription = this.menuService.menuSource$.subscribe(value => { - Promise.resolve(null).then(() => { - if (value.routeEvent) { - this.active = (value.key === this.key || value.key.startsWith(this.key + '-')) ? true : false; - } else { - if (value.key !== this.key && !value.key.startsWith(this.key + '-')) { - this.active = false; - } - } - }); - }); - - this.menuResetSubscription = this.menuService.resetSource$.subscribe(() => { + constructor(public router: Router, private menuService: MenuService) { + this.menuSourceSubscription = this.menuService.menuSource$.subscribe(value => { + Promise.resolve(null).then(() => { + if (value.routeEvent) { + this.active = (value.key === this.key || value.key.startsWith(this.key + '-')) ? true : false; + } else { + if (value.key !== this.key && !value.key.startsWith(this.key + '-')) { this.active = false; - }); + } + } + }); + }); - this.router.events.pipe(filter(event => event instanceof NavigationEnd)) - .subscribe(() => { - if (this.item.routerLink) { - this.updateActiveStateFromRoute(); - } - }); - } + this.menuResetSubscription = this.menuService.resetSource$.subscribe(() => { + this.active = false; + }); - ngOnInit() { - this.key = this.parentKey ? this.parentKey + '-' + this.index : String(this.index); + this.router.events.pipe(filter(event => event instanceof NavigationEnd)) + .subscribe(() => { if (this.item.routerLink) { - this.updateActiveStateFromRoute(); + this.updateActiveStateFromRoute(); } - } + }); + } - updateActiveStateFromRoute() { - const activeRoute = this.router.isActive(this.item.routerLink[0], { - paths: 'exact', - queryParams: 'ignored', - matrixParams: 'ignored', - fragment: 'ignored' - }); - if (activeRoute) { - this.menuService.onMenuStateChange({key: this.key, routeEvent: true}); - } + ngOnInit() { + this.key = this.parentKey ? this.parentKey + '-' + this.index : String(this.index); + if (this.item.routerLink) { + this.updateActiveStateFromRoute(); } + } - itemClick(event: Event) { - if (this.item.disabled) { - event.preventDefault(); - return; - } - if (this.item.command) { - this.item.command({originalEvent: event, item: this.item}); - } - if (this.item.items) { - this.active = !this.active; - } - this.menuService.onMenuStateChange({key: this.key}); + updateActiveStateFromRoute() { + const activeRoute = this.router.isActive(this.item.routerLink[0], { + paths: 'exact', + queryParams: 'ignored', + matrixParams: 'ignored', + fragment: 'ignored' + }); + if (activeRoute) { + this.menuService.onMenuStateChange({key: this.key, routeEvent: true}); } + } - get submenuAnimation() { - return this.root ? 'expanded' : (this.active ? 'expanded' : 'collapsed'); + itemClick(event: Event) { + if (this.item.disabled) { + event.preventDefault(); + return; } + if (this.item.command) { + this.item.command({originalEvent: event, item: this.item}); + } + if (this.item.items) { + this.active = !this.active; + } + this.menuService.onMenuStateChange({key: this.key}); + } - @HostBinding('class.active-menuitem') - get activeClass() { - return this.active && !this.root; - } + get submenuAnimation() { + return this.root ? 'expanded' : (this.active ? 'expanded' : 'collapsed'); + } - ngOnDestroy() { - if (this.menuSourceSubscription) { - this.menuSourceSubscription.unsubscribe(); - } - if (this.menuResetSubscription) { - this.menuResetSubscription.unsubscribe(); - } + @HostBinding('class.active-menuitem') + get activeClass() { + return this.active && !this.root; + } + + ngOnDestroy() { + if (this.menuSourceSubscription) { + this.menuSourceSubscription.unsubscribe(); } + if (this.menuResetSubscription) { + this.menuResetSubscription.unsubscribe(); + } + } } diff --git a/booklore-ui/src/app/layout/component/layout-topbar/app.topbar.component.html b/booklore-ui/src/app/layout/component/layout-topbar/app.topbar.component.html index 498eeb002..117661df7 100644 --- a/booklore-ui/src/app/layout/component/layout-topbar/app.topbar.component.html +++ b/booklore-ui/src/app/layout/component/layout-topbar/app.topbar.component.html @@ -19,7 +19,7 @@
-