More signals

This commit is contained in:
aditya.chandel
2024-12-18 16:08:22 -07:00
parent 71f5927364
commit ce515649f6
14 changed files with 160 additions and 129 deletions
@@ -6,24 +6,21 @@ import {BookUpdateEvent} from '../model/book.model';
providedIn: 'root',
})
export class BookProgressService {
private eventSource: EventSource | null = null;
connect(libraryId: number): Observable<BookUpdateEvent> {
const url = `http://localhost:8080/v1/library/${libraryId}/parse`;
return new Observable((observer) => {
this.eventSource = new EventSource(url);
this.eventSource.onmessage = (event) => {
const data: BookUpdateEvent = JSON.parse(event.data);
observer.next(data);
};
this.eventSource.onerror = (error) => {
console.error('SSE connection error:', error);
observer.error(error);
};
return () => {
this.closeConnection();
};
@@ -1,7 +1,9 @@
import {Observable} from 'rxjs';
import {Observable, of} from 'rxjs';
import {Book, BookMetadata, BookSetting, BookWithNeighborsDTO, PaginatedBooksResponse} from '../model/book.model';
import {Injectable} from '@angular/core';
import {computed, Injectable, signal} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {catchError, map} from 'rxjs/operators';
import {LibraryApiResponse} from '../model/library.model';
@Injectable({
providedIn: 'root',
@@ -11,6 +13,25 @@ export class BookService {
private readonly libraryUrl = 'http://localhost:8080/v1/library';
private readonly bookUrl = 'http://localhost:8080/v1/book';
#lastReadBooks = signal<Book[]>([]);
lastReadBooks = computed(this.#lastReadBooks);
#latestAddedBooks = signal<Book[]>([]);
latestAddedBooks = computed(this.#latestAddedBooks);
getBooksByLibraryId(libraryId: number, page: number) {
this.http.get<PaginatedBooksResponse>(`${this.libraryUrl}/${libraryId}/book?page=${page}&size=${this.pageSize}`)
.pipe(
map(response => {
catchError(error => {
console.error('Error loading libraries:', error);
return of([]);
})
})
).subscribe()
}
constructor(private http: HttpClient) {
}
@@ -28,15 +49,33 @@ export class BookService {
);
}
loadLatestBooks(page: number): Observable<PaginatedBooksResponse> {
return this.http.get<PaginatedBooksResponse>(
`${this.bookUrl}?page=${page}&size=10&sortBy=lastReadTime&sortDir=desc`
getLastReadBooks(page: number) {
this.http.get<PaginatedBooksResponse>(`${this.bookUrl}?page=${page}&size=25&sortBy=lastReadTime&sortDir=desc`).pipe(
map(response => response.content),
catchError(error => {
console.error('Error loading last read books:', error);
return of([]);
})
).subscribe(
(books) => {
this.#lastReadBooks.set([...this.#lastReadBooks(), ...books]);
console.log("Loaded last read books")
}
);
}
loadLatestAddedBooks(page: number): Observable<PaginatedBooksResponse> {
return this.http.get<PaginatedBooksResponse>(
`${this.bookUrl}?page=${page}&size=10&sortBy=addedOn&sortDir=desc`
getLatestAddedBooks(page: number) {
this.http.get<PaginatedBooksResponse>(`${this.bookUrl}?page=${page}&size=25&sortBy=addedOn&sortDir=desc`).pipe(
map(response => response.content),
catchError(error => {
console.error('Error loading latest added books:', error);
return of([]);
})
).subscribe(
(books) => {
this.#latestAddedBooks.set([...this.#latestAddedBooks(), ...books]);
console.log("Loaded latest added books")
}
);
}
@@ -43,24 +43,4 @@ export class LibraryService {
})
);
}
getLibraryById(libraryId: number): Library | undefined {
return this.#libraries().find(library => library.id === libraryId);
}
/*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;
})
);
}*/
}