Add theme switching

More reactivity in Library Browser
This commit is contained in:
aditya.chandel
2024-12-18 11:34:24 -07:00
parent 61e9580409
commit 71f5927364
24 changed files with 239 additions and 132 deletions
@@ -1,10 +0,0 @@
package com.adityachandel.booklore.model.dto.response;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class ApiResponseMessage {
private String message;
}
+7 -3
View File
@@ -1,4 +1,4 @@
import {Component} from '@angular/core'; import {Component, OnInit} from '@angular/core';
import {LibraryService} from './book/service/library.service'; import {LibraryService} from './book/service/library.service';
@Component({ @Component({
@@ -7,7 +7,11 @@ import {LibraryService} from './book/service/library.service';
standalone: false, standalone: false,
styleUrl: './app.component.scss' styleUrl: './app.component.scss'
}) })
export class AppComponent { export class AppComponent implements OnInit {
constructor(private libraryService: LibraryService) {
constructor(private libraryService: LibraryService) {}
ngOnInit(): void {
this.libraryService.initializeLibraries();
} }
} }
@@ -2,7 +2,12 @@
<div class="book-metadata"> <div class="book-metadata">
<div class="book-header"> <div class="book-header">
<div class="cover-container"> <div class="cover-container">
<img [src]="coverImageSrc(book?.id)" class="book-cover placeholder" alt="Cover of {{ book?.metadata?.title }}" loading="lazy"/> <div class="cover-wrapper">
<img [src]="coverImageSrc(book?.id)" class="book-cover placeholder" alt="Cover of {{ book?.metadata?.title }}" loading="lazy"/>
<div class="overlay" (click)="readBook(book?.id)">
<p-button icon="pi pi-book" [rounded]="true"></p-button>
</div>
</div>
<div> <div>
<div class="book-navigation"> <div class="book-navigation">
<p-button <p-button
@@ -17,14 +17,36 @@
} }
.cover-container { .cover-container {
position: relative;
max-width: 200px; max-width: 200px;
flex-shrink: 0; flex-shrink: 0;
} }
.cover-wrapper {
position: relative;
}
.book-cover { .book-cover {
width: 100%; width: 100%;
border-radius: 8px; border-radius: 8px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2); box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
transition: filter 0.3s ease;
}
.cover-container:hover .book-cover {
filter: blur(2px);
}
.overlay {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
display: none;
}
.cover-container:hover .overlay {
display: block;
} }
.metadata { .metadata {
@@ -36,7 +36,7 @@ export class BookMetadataComponent implements OnInit, OnDestroy {
ngOnInit(): void { ngOnInit(): void {
this.routeSubscription = this.activatedRoute.paramMap.subscribe((paramMap) => { this.routeSubscription = this.activatedRoute.paramMap.subscribe((paramMap) => {
const bookId = +paramMap.get('bookId')!; const bookId = +paramMap.get('bookId')!;
const libraryId = +paramMap.get('libraryId')!; const libraryId = +paramMap.get('libraryId')!;
if (bookId && libraryId) { if (bookId && libraryId) {
this.loadBookWithNeighbors(bookId, libraryId); this.loadBookWithNeighbors(bookId, libraryId);
@@ -20,8 +20,8 @@
<div *ngFor="let metadata of bookMetadataList" class="book-metadata"> <div *ngFor="let metadata of bookMetadataList" class="book-metadata">
<div class="metadata-header"> <div class="metadata-header">
<div class="book-image-container"> <div class="book-image-container">
<img class="book-image" src="{{ metadata.thumbnail }}" alt="Book Cover"/> <img class="book-image" [src]="metadata.thumbnail || 'assets/book-cover-metadata.png'" alt="Book Cover"/>
<p-button class="select-button" (click)="selectMetadata(metadata)">Select</p-button> <p-button class="select-button" (click)="selectMetadata(metadata)">Select</p-button>
</div> </div>
<div> <div>
<h3>{{ metadata.title }}<span *ngIf="metadata.subtitle">: {{ metadata.subtitle }}</span></h3> <h3>{{ metadata.title }}<span *ngIf="metadata.subtitle">: {{ metadata.subtitle }}</span></h3>
@@ -32,10 +32,8 @@
<p><strong>Description:</strong> {{ truncateDescription(metadata.description) }}</p> <p><strong>Description:</strong> {{ truncateDescription(metadata.description) }}</p>
<p><strong>Page Count:</strong> {{ metadata.pageCount }}</p> <p><strong>Page Count:</strong> {{ metadata.pageCount }}</p>
<p *ngIf="metadata.googleBookId"> <p *ngIf="metadata.googleBookId">
<strong>Google Book Id:</strong> <strong>Google Book Id: </strong>
<a [href]="'https://books.google.com/books?id=' + metadata.googleBookId" target="_blank"> <a [href]="'https://books.google.com/books?id=' + metadata.googleBookId" target="_blank">{{ metadata.googleBookId }}</a>
{{ metadata.googleBookId }}
</a>
</p> </p>
</div> </div>
</div> </div>
@@ -2,7 +2,7 @@ import {Component, OnInit} from '@angular/core';
import {BookMetadata} from '../../model/book.model'; import {BookMetadata} from '../../model/book.model';
import {BookService} from '../../service/book.service'; import {BookService} from '../../service/book.service';
import {NgForOf, NgIf} from '@angular/common'; import {NgForOf, NgIf} from '@angular/common';
import {FormBuilder, FormsModule} from '@angular/forms'; import {FormsModule} from '@angular/forms';
import {DynamicDialogConfig, DynamicDialogRef} from 'primeng/dynamicdialog'; import {DynamicDialogConfig, DynamicDialogRef} from 'primeng/dynamicdialog';
import {Router} from '@angular/router'; import {Router} from '@angular/router';
import {Button} from 'primeng/button'; import {Button} from 'primeng/button';
@@ -1,4 +1,4 @@
import { Component, WritableSignal } from '@angular/core'; import {Component, Signal, WritableSignal} from '@angular/core';
import { LibraryService } from '../../service/library.service'; import { LibraryService } from '../../service/library.service';
import { Library } from '../../model/library.model'; import { Library } from '../../model/library.model';
import { Button } from 'primeng/button'; import { Button } from 'primeng/button';
@@ -19,7 +19,7 @@ import {DashboardScrollerComponent} from '../dashboard-scroller/dashboard-scroll
providers: [DialogService], providers: [DialogService],
}) })
export class DashboardComponent { export class DashboardComponent {
private libraries: WritableSignal<Library[]>; private libraries: Signal<Library[]>;
ref: DynamicDialogRef | undefined; ref: DynamicDialogRef | undefined;
constructor(private libraryService: LibraryService, public dialogService: DialogService) { constructor(private libraryService: LibraryService, public dialogService: DialogService) {
@@ -32,13 +32,10 @@ export class DashboardComponent {
createNewLibrary(event: MouseEvent) { createNewLibrary(event: MouseEvent) {
const buttonRect = (event.target as HTMLElement).getBoundingClientRect(); const buttonRect = (event.target as HTMLElement).getBoundingClientRect();
const dialogWidthPercentage = 50; const dialogWidthPercentage = 50;
const viewportWidth = window.innerWidth; const viewportWidth = window.innerWidth;
const dialogWidth = (dialogWidthPercentage / 100) * viewportWidth; const dialogWidth = (dialogWidthPercentage / 100) * viewportWidth;
const leftPosition = buttonRect.left + (buttonRect.width / 2) - (dialogWidth / 2); const leftPosition = buttonRect.left + (buttonRect.width / 2) - (dialogWidth / 2);
this.ref = this.dialogService.open(LibraryCreatorComponent, { this.ref = this.dialogService.open(LibraryCreatorComponent, {
modal: true, modal: true,
width: `${dialogWidthPercentage}%`, width: `${dialogWidthPercentage}%`,
@@ -2,7 +2,7 @@
<div class="flex"> <div class="flex">
<p-dropdown <p-dropdown
[options]="libraries" [options]="libraries()"
optionLabel="name" optionLabel="name"
placeholder="Select Library" placeholder="Select Library"
[(ngModel)]="selectedLibrary"> [(ngModel)]="selectedLibrary">
@@ -1,4 +1,4 @@
import {Component, OnInit} from '@angular/core'; import {Component, OnInit, Signal, WritableSignal} from '@angular/core';
import {Library} from '../../model/library.model'; import {Library} from '../../model/library.model';
import {LibraryService} from '../../service/library.service'; import {LibraryService} from '../../service/library.service';
import {FileUploadService} from '../../../file-upload.service'; import {FileUploadService} from '../../../file-upload.service';
@@ -20,8 +20,8 @@ import {DynamicDialogRef} from 'primeng/dynamicdialog';
FormsModule FormsModule
], ],
}) })
export class FileUploadComponent implements OnInit { export class FileUploadComponent {
libraries: Library[] = []; libraries: Signal<Library[]>;
selectedLibrary: Library | null = null; selectedLibrary: Library | null = null;
selectedPath: string = ''; selectedPath: string = '';
selectedFileName: string = ''; selectedFileName: string = '';
@@ -32,12 +32,8 @@ export class FileUploadComponent implements OnInit {
private libraryService: LibraryService, private libraryService: LibraryService,
private fileUploadService: FileUploadService, private fileUploadService: FileUploadService,
private toastService: ToastService, private toastService: ToastService,
private dynamicDialogRef: DynamicDialogRef private dynamicDialogRef: DynamicDialogRef) {
) { this.libraries = this.libraryService.libraries;
}
ngOnInit() {
this.libraries = this.libraryService.libraries();
} }
triggerFileInput(fileInput: HTMLInputElement): void { triggerFileInput(fileInput: HTMLInputElement): void {
@@ -1,4 +1,4 @@
import {Component, computed, OnInit} from '@angular/core'; import {Component, computed, OnInit, Signal, WritableSignal} from '@angular/core';
import {AppMenuitemComponent} from './app.menuitem.component'; import {AppMenuitemComponent} from './app.menuitem.component';
import {NgForOf, NgIf} from '@angular/common'; import {NgForOf, NgIf} from '@angular/common';
import {MenuModule} from 'primeng/menu'; import {MenuModule} from 'primeng/menu';
@@ -12,7 +12,7 @@ import {Library} from '../../model/library.model';
}) })
export class AppMenuComponent implements OnInit { export class AppMenuComponent implements OnInit {
home: any[] = []; home: any[] = [];
libraries: any; libraries: Signal<Library[]>;
libraryMenu = computed(() => [ libraryMenu = computed(() => [
{ {
@@ -1,4 +1,5 @@
<div class="layout-topbar"> <div class="layout-topbar">
<a class="layout-topbar-logo" routerLink=""> <a class="layout-topbar-logo" routerLink="">
<img src="assets/layout/images/{{layoutService.config().colorScheme === 'light' ? 'logo-dark' : 'logo-white'}}.svg" <img src="assets/layout/images/{{layoutService.config().colorScheme === 'light' ? 'logo-dark' : 'logo-white'}}.svg"
alt="logo"> alt="logo">
@@ -9,10 +10,11 @@
<i class="pi pi-bars"></i> <i class="pi pi-bars"></i>
</button> </button>
<app-search></app-search> <div>
<app-search></app-search>
</div>
<button #topbarmenubutton class="p-link layout-topbar-menu-button layout-topbar-button" <button #topbarmenubutton class="p-link layout-topbar-menu-button layout-topbar-button" (click)="layoutService.showProfileSidebar()">
(click)="layoutService.showProfileSidebar()">
<i class="pi pi-ellipsis-v"></i> <i class="pi pi-ellipsis-v"></i>
</button> </button>
@@ -4,7 +4,7 @@ import {LayoutService} from './service/app.layout.service';
import {RouterLink} from '@angular/router'; import {RouterLink} from '@angular/router';
import {DialogService as PrimeDialogService, DynamicDialogRef} from 'primeng/dynamicdialog'; import {DialogService as PrimeDialogService, DynamicDialogRef} from 'primeng/dynamicdialog';
import {LibraryCreatorComponent} from '../library-creator/library-creator.component'; import {LibraryCreatorComponent} from '../library-creator/library-creator.component';
import {ThemeSwitcherComponent} from '../../../theme-switcher/theme-switcher.component'; import {ThemeSwitcherComponent} from '../theme-switcher/theme-switcher.component';
import {TooltipModule} from 'primeng/tooltip'; import {TooltipModule} from 'primeng/tooltip';
import {FormsModule} from '@angular/forms'; import {FormsModule} from '@angular/forms';
import {InputTextModule} from 'primeng/inputtext'; import {InputTextModule} from 'primeng/inputtext';
@@ -1,15 +1,32 @@
<div class="book-list" <div>
infiniteScroll <div class="library-header">
[infiniteScrollDistance]="2" <h3 class="library-name">{{ libraryNameSignal() }}</h3>
[infiniteScrollThrottle]="50" <div class="library-header-right">
(scrolled)="loadMore()"> <p-dropdown
<div class="book-item" *ngFor="let book of books"> [options]="cities"
<img [src]="coverImageSrc(book.id)" class="book-cover placeholder" alt="Cover of {{ book.metadata.title }}" loading="lazy"/> [(ngModel)]="selectedCity"
<div class="book-info"> optionLabel="name"
<h4>{{ book.metadata.title }}</h4> placeholder="Select Sorting" />
<p>{{ getAuthorNames(book) }}</p> <div class="zoom-controls">
<p-button [rounded]="true" icon="pi pi-eye" class="view-btn" (click)="readBook(book.id)"></p-button> <p-button icon="pi pi-minus" [rounded]="true" [outlined]="true" (click)="decreaseSize()" [disabled]="isDecreaseDisabled()" severity="info"></p-button>
<p-button [rounded]="true" icon="pi pi-info" class="read-btn" (click)="openBookInfo(book.id, book.libraryId)"></p-button> <p-button icon="pi pi-plus" [rounded]="true" [outlined]="true" (click)="increaseSize()" [disabled]="isIncreaseDisabled()" severity="info"></p-button>
</div>
</div>
</div>
<div class="book-list"
infiniteScroll
[ngClass]="coverSizeClass"
[infiniteScrollDistance]="2"
[infiniteScrollThrottle]="50"
(scrolled)="loadMore()">
<div class="book-item" *ngFor="let book of books">
<img [src]="coverImageSrc(book.id)" class="book-cover placeholder" alt="Cover of {{ book.metadata.title }}" loading="lazy"/>
<div class="book-info">
<h4 class="book-title">{{ book.metadata.title }}</h4>
<p class="book-authors">{{ getAuthorNames(book) }}</p>
<p-button [rounded]="true" icon="pi pi-eye" class="view-btn" (click)="readBook(book.id)"></p-button>
<p-button [rounded]="true" icon="pi pi-info" class="read-btn" (click)="openBookInfo(book.id, book.libraryId)"></p-button>
</div>
</div> </div>
</div> </div>
</div> </div>
@@ -1,78 +1,94 @@
.library-header {
display: flex;
justify-content: space-between;
padding: 15px 15px 10px 17px;
background-color: var(--surface-card);
border-radius: 12px 12px 0 0;
margin-bottom: 15px;
}
.library-header-right {
display: flex;
gap: 2rem;
align-items: center;
}
.library-name {
margin: 0;
color: var(--text-color);
}
.zoom-controls {
display: flex;
gap: 1rem;
align-items: center;
}
.book-list { .book-list {
display: grid; display: grid;
grid-template-columns: repeat(auto-fill, minmax(145px, 1fr)); grid-template-columns: repeat(auto-fill, minmax(135px, 1fr));
gap: 20px; gap: 20px;
transition: all 0.3s ease-in-out;
}
.small {
grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
}
.medium {
grid-template-columns: repeat(auto-fill, minmax(135px, 1fr));
}
.large {
grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
}
.extra-large {
grid-template-columns: repeat(auto-fill, minmax(165px, 1fr));
} }
.book-item { .book-item {
color: #fff; background-color: var(--surface-card);
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3); box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
overflow: hidden;
transition: transform 0.3s ease-in-out;
display: flex; display: flex;
flex-direction: column; flex-direction: column;
justify-content: space-between; justify-content: space-between;
height: auto;
background-color: var(--surface-card);
border-radius: 8px;
position: relative; position: relative;
overflow: hidden;
} }
.book-cover { .book-cover {
width: 100%;
height: 225px;
object-fit: cover; object-fit: cover;
background-color: #444; transition: filter 0.3s ease-in-out;
transition: background-color 0.3s ease, filter 0.3s ease;
position: relative;
} }
.book-item:hover .book-cover { .book-item:hover .book-cover {
filter: blur(2px); filter: blur(2px);
} }
.book-item:hover .book-cover::after {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.5);
z-index: 0;
}
.book-info { .book-info {
padding: 10px; padding: 7px 7px 5px;
text-align: center; text-align: center;
display: flex;
flex-direction: column;
justify-content: flex-start;
flex-grow: 1;
z-index: 1;
} }
h4 { .book-title {
font-size: 1rem; font-size: 0.9rem;
color: var(--text-color); color: var(--text-color);
margin-bottom: 5px;
white-space: nowrap; white-space: nowrap;
overflow: hidden; overflow: hidden;
text-overflow: ellipsis; text-overflow: ellipsis;
max-width: 100%;
text-align: center; text-align: center;
margin-bottom: 5px;
} }
p { .book-authors {
color: var(--text-color-secondary); color: var(--text-color-secondary);
font-size: 0.8rem; font-size: 0.8rem;
line-height: 1.4; line-height: 1.4;
margin-top: 0; margin: 0 auto;
white-space: nowrap; white-space: nowrap;
overflow: hidden; overflow: hidden;
text-overflow: ellipsis; text-overflow: ellipsis;
max-width: 100%;
margin-bottom: auto;
} }
.authors { .authors {
@@ -88,18 +104,19 @@ p {
position: absolute; position: absolute;
top: 50%; top: 50%;
left: 50%; left: 50%;
transform: translate(-50%, -20%); transform: translate(-50%, -50%);
display: none; display: none;
margin: -60px 0; margin-top: -40px;
transition: opacity 0.2s ease-in-out;
} }
.book-item:hover .view-btn { .book-item:hover .view-btn,
.book-item:hover .read-btn {
display: block; display: block;
} }
.book-item:hover .read-btn { .book-item:hover .read-btn {
display: block; top: 70%;
top: 75%;
} }
.infinite-scroll { .infinite-scroll {
@@ -1,12 +1,15 @@
import { Component, OnInit, NgZone } from '@angular/core'; import {Component, computed, NgZone, OnChanges, OnInit, signal} from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router'; import {ActivatedRoute, Router} from '@angular/router';
import { BookProgressService } from '../../service/book-progress-service'; import {BookProgressService} from '../../service/book-progress-service';
import { BookService } from '../../service/book.service'; import {BookService} from '../../service/book.service';
import {Book, BookUpdateEvent} from '../../model/book.model'; import {Book, BookUpdateEvent} from '../../model/book.model';
import { combineLatest } from 'rxjs'; import {combineLatest} from 'rxjs';
import {InfiniteScrollDirective} from 'ngx-infinite-scroll'; import {InfiniteScrollDirective} from 'ngx-infinite-scroll';
import {Button} from 'primeng/button'; import {Button} from 'primeng/button';
import {NgForOf} from '@angular/common'; import {NgClass, NgForOf} from '@angular/common';
import {FormsModule} from '@angular/forms';
import {DropdownModule} from 'primeng/dropdown';
import {LibraryService} from '../../service/library.service';
@Component({ @Component({
selector: 'app-library-browser-v2', selector: 'app-library-browser-v2',
@@ -15,20 +18,35 @@ import {NgForOf} from '@angular/common';
imports: [ imports: [
InfiniteScrollDirective, InfiniteScrollDirective,
Button, Button,
NgForOf NgForOf,
FormsModule,
DropdownModule,
NgClass
] ]
}) })
export class LibraryBrowserComponent implements OnInit { export class LibraryBrowserComponent implements OnInit {
books: Book[] = []; books: Book[] = [];
private libraryId: number = 1; private libraryIdSignal = signal(1);
private currentPage: number = 0; private currentPage: number = 0;
cities: any[] | undefined;
selectedCity: any;
coverSizeClass = 'medium';
coverSizeClasses = ['small', 'medium', 'large', 'extra-large'];
libraryNameSignal = computed(() => {
const library = this.libraryService
.libraries()
.find((library) => library.id === this.libraryIdSignal());
return library ? library.name : 'Library not found';
});
constructor( constructor(
private bookService: BookService, private bookService: BookService,
private activatedRoute: ActivatedRoute, private activatedRoute: ActivatedRoute,
private router: Router, private router: Router,
private bookProgressService: BookProgressService, private bookProgressService: BookProgressService,
private ngZone: NgZone private ngZone: NgZone,
private libraryService: LibraryService
) {} ) {}
ngOnInit(): void { ngOnInit(): void {
@@ -36,7 +54,7 @@ export class LibraryBrowserComponent implements OnInit {
.subscribe(([params, queryParams]) => { .subscribe(([params, queryParams]) => {
const libraryId = params.get('libraryId'); const libraryId = params.get('libraryId');
if (libraryId) { if (libraryId) {
this.libraryId = +libraryId; this.libraryIdSignal.set(+libraryId);
this.resetState(); this.resetState();
this.loadBooks(); this.loadBooks();
} }
@@ -48,7 +66,7 @@ export class LibraryBrowserComponent implements OnInit {
} }
startListeningForProgress(): void { startListeningForProgress(): void {
this.bookProgressService.connect(this.libraryId).subscribe({ this.bookProgressService.connect(this.libraryIdSignal()).subscribe({
next: (event: BookUpdateEvent) => this.addBookToCollection(event), next: (event: BookUpdateEvent) => this.addBookToCollection(event),
error: (error) => console.error('Error receiving progress updates:', error), error: (error) => console.error('Error receiving progress updates:', error),
}); });
@@ -72,13 +90,12 @@ export class LibraryBrowserComponent implements OnInit {
} }
resetState(): void { resetState(): void {
console.log("resetState")
this.books = []; this.books = [];
this.currentPage = 0; this.currentPage = 0;
} }
loadBooks(): void { loadBooks(): void {
this.bookService.loadBooks(this.libraryId, this.currentPage).subscribe({ this.bookService.loadBooks(this.libraryIdSignal(), this.currentPage).subscribe({
next: (response) => { next: (response) => {
this.books = [...this.books, ...response.content]; this.books = [...this.books, ...response.content];
this.currentPage++; this.currentPage++;
@@ -107,4 +124,30 @@ export class LibraryBrowserComponent implements OnInit {
openBookInfo(bookId: number, libraryId: number) { openBookInfo(bookId: number, libraryId: number) {
this.router.navigate(['/library', libraryId, 'book', bookId, 'info']); this.router.navigate(['/library', libraryId, 'book', bookId, 'info']);
} }
getCurrentSizeIndex(): number {
return this.coverSizeClasses.indexOf(this.coverSizeClass);
}
increaseSize() {
const currentIndex = this.getCurrentSizeIndex();
if (currentIndex < this.coverSizeClasses.length - 1) {
this.coverSizeClass = this.coverSizeClasses[currentIndex + 1];
}
}
decreaseSize() {
const currentIndex = this.getCurrentSizeIndex();
if (currentIndex > 0) {
this.coverSizeClass = this.coverSizeClasses[currentIndex - 1];
}
}
isIncreaseDisabled(): boolean {
return this.coverSizeClass === this.coverSizeClasses[this.coverSizeClasses.length - 1];
}
isDecreaseDisabled(): boolean {
return this.coverSizeClass === this.coverSizeClasses[0];
}
} }
@@ -71,7 +71,7 @@ export class LibraryCreatorComponent {
this.dynamicDialogRef.close(); this.dynamicDialogRef.close();
} }
validateLibraryNameAndProceed(nextCallback: any) { /*validateLibraryNameAndProceed(nextCallback: any) {
if (this.value.trim()) { if (this.value.trim()) {
this.libraryServiceV2.checkLibraryNameExists(this.value).subscribe( this.libraryServiceV2.checkLibraryNameExists(this.value).subscribe(
(response) => { (response) => {
@@ -95,5 +95,5 @@ export class LibraryCreatorComponent {
} }
); );
} }
} }*/
} }
@@ -2,6 +2,7 @@
<span class="p-input-icon-left"> <span class="p-input-icon-left">
<i class="pi pi-search"></i> <i class="pi pi-search"></i>
<input <input
style="width: 400px;"
type="text" type="text"
pInputText pInputText
[(ngModel)]="searchQuery" [(ngModel)]="searchQuery"
@@ -7,6 +7,7 @@ import { FormsModule } from '@angular/forms';
import { InputTextModule } from 'primeng/inputtext'; import { InputTextModule } from 'primeng/inputtext';
import { NgForOf, NgIf } from '@angular/common'; import { NgForOf, NgIf } from '@angular/common';
import { ButtonDirective } from 'primeng/button'; import { ButtonDirective } from 'primeng/button';
import {Router} from '@angular/router';
@Component({ @Component({
selector: 'app-search', selector: 'app-search',
@@ -26,7 +27,7 @@ export class SearchComponent implements OnInit, OnDestroy {
#searchSubject = new Subject<string>(); #searchSubject = new Subject<string>();
#subscription!: Subscription; #subscription!: Subscription;
constructor(private bookService: BookService) {} constructor(private bookService: BookService, private router: Router) {}
ngOnInit(): void { ngOnInit(): void {
this.initializeSearch(); this.initializeSearch();
@@ -54,12 +55,13 @@ export class SearchComponent implements OnInit, OnDestroy {
onBookClick(book: Book): void { onBookClick(book: Book): void {
this.clearSearch(); this.clearSearch();
this.openBook(book.id); this.openBook(book.id, book.libraryId);
} }
openBook(bookId: number): void { openBook(bookId: number, libraryId: number): void {
const url = `/pdf-viewer/book/${bookId}`; /*const url = `/pdf-viewer/book/${bookId}`;
window.open(url, '_blank'); window.open(url, '_blank');*/
this.router.navigate(['/library', libraryId, 'book', bookId, 'info']);
} }
getBookCoverUrl(bookId: number): string { getBookCoverUrl(bookId: number): string {
@@ -7,7 +7,7 @@ import {HttpClient} from '@angular/common/http';
providedIn: 'root', providedIn: 'root',
}) })
export class BookService { export class BookService {
private readonly pageSize = 25; private readonly pageSize = 50;
private readonly libraryUrl = 'http://localhost:8080/v1/library'; private readonly libraryUrl = 'http://localhost:8080/v1/library';
private readonly bookUrl = 'http://localhost:8080/v1/book'; private readonly bookUrl = 'http://localhost:8080/v1/book';
@@ -1,4 +1,4 @@
import { Injectable, signal } from '@angular/core'; import {computed, Injectable, signal} from '@angular/core';
import { HttpClient } from '@angular/common/http'; import { HttpClient } from '@angular/common/http';
import { catchError, map, tap } from 'rxjs/operators'; import { catchError, map, tap } from 'rxjs/operators';
import { Observable, of } from 'rxjs'; import { Observable, of } from 'rxjs';
@@ -10,13 +10,12 @@ import { Library, LibraryApiResponse } from '../model/library.model';
export class LibraryService { export class LibraryService {
private libraryUrl: string = 'http://localhost:8080/v1/library'; private libraryUrl: string = 'http://localhost:8080/v1/library';
libraries = signal<Library[]>([]); #libraries = signal<Library[]>([]);
libraries = computed(this.#libraries);
constructor(private http: HttpClient) { constructor(private http: HttpClient) {}
this.getLibrariesFromAPI();
}
getLibrariesFromAPI(): void { initializeLibraries(): void {
this.http.get<LibraryApiResponse>(this.libraryUrl).pipe( this.http.get<LibraryApiResponse>(this.libraryUrl).pipe(
map(response => response.content), map(response => response.content),
catchError(error => { catchError(error => {
@@ -25,7 +24,8 @@ export class LibraryService {
}) })
).subscribe( ).subscribe(
(libraries) => { (libraries) => {
this.libraries.set(libraries); this.#libraries.set(libraries);
console.log("Library Initialized")
} }
); );
} }
@@ -33,7 +33,9 @@ export class LibraryService {
createLibrary(newLibrary: Library): Observable<Library> { createLibrary(newLibrary: Library): Observable<Library> {
return this.http.post<Library>(this.libraryUrl, newLibrary).pipe( return this.http.post<Library>(this.libraryUrl, newLibrary).pipe(
tap((createdLibrary) => { tap((createdLibrary) => {
this.libraries.set([...this.libraries(), createdLibrary]); const currentLibraries = this.#libraries();
currentLibraries.push(createdLibrary);
this.#libraries.set(currentLibraries);
}), }),
catchError(error => { catchError(error => {
console.error('Error creating library:', error); console.error('Error creating library:', error);
@@ -42,12 +44,23 @@ export class LibraryService {
); );
} }
checkLibraryNameExists(name: string): Observable<any> { getLibraryById(libraryId: number): Library | undefined {
return this.http.get<Library>(`${this.libraryUrl}/search?name=${name}`).pipe( return this.#libraries().find(library => library.id === libraryId);
catchError(error => { }
console.error('Error checking library name:', error);
return of(null); /*updateLibrary(id: number, updatedLibrary: Library): Observable<Library> {
const url = `${this.libraryUrl}/${id}`;
return this.http.put<Library>(url, updatedLibrary).pipe(
tap((updatedLibraryFromBackend) => {
const updatedLibraries = this._libraries().map((library) =>
library.id === id ? { ...library, ...updatedLibraryFromBackend } : library
);
this._libraries.set(updatedLibraries);
}),
catchError((error) => {
console.error('Error updating library:', error);
throw error;
}) })
); );
} }*/
} }