Fix dashboard scroller not reactively using last read time

This commit is contained in:
aditya.chandel
2025-01-19 16:13:50 -07:00
parent 8f54b93bb8
commit d87b3f2ba0
2 changed files with 18 additions and 18 deletions
@@ -98,17 +98,13 @@ export class BookService {
return this.http.put<void>(`${this.url}/${bookId}/book-viewer-setting`, bookSetting);
}
updateLastReadTime(bookId: number): Observable<void> {
const timestamp = Math.floor(Date.now() / 1000).toString();
return this.http.put<void>(`${this.url}/${bookId}/update-last-read/${timestamp}`, {}).pipe(
tap(() => {
const currentState = this.bookStateSubject.value;
const updatedBooks = (currentState.books || []).map(book =>
book.id === bookId ? {...book, lastReadTime: timestamp} : book
);
this.bookStateSubject.next({...currentState, books: updatedBooks});
})
updateLastReadTime(bookId: number) {
const timestamp = new Date().toISOString();
const currentState = this.bookStateSubject.value;
const updatedBooks = (currentState.books || []).map(book =>
book.id === bookId ? {...book, lastReadTime: timestamp} : book
);
this.bookStateSubject.next({...currentState, books: updatedBooks});
}
readBook(bookId: number): void {
@@ -118,14 +114,11 @@ export class BookService {
if (book.bookType === "PDF") {
const url = `/pdf-viewer/book/${book.id}`;
window.open(url, '_blank');
this.updateLastReadTime(book.id).subscribe({
next: () => {
},
error: err => console.error('Failed to update last read time', err),
});
this.updateLastReadTime(book.id);
} else if (book.bookType === "EPUB") {
const url = `/epub-viewer/book/${book.id}`;
window.open(url, '_blank');
this.updateLastReadTime(book.id);
} else {
console.error('Unsupported book type:', book.bookType);
}
@@ -38,19 +38,26 @@ export class DashboardScrollerComponent implements OnInit {
...state,
books: (state.books || [])
.filter(book => book.lastReadTime)
.sort((a, b) => new Date(b.lastReadTime!).getTime() - new Date(a.lastReadTime!).getTime())
.sort((a, b) => {
const aTime = new Date(a.lastReadTime!).getTime();
const bTime = new Date(b.lastReadTime!).getTime();
return bTime - aTime;
})
.slice(0, 25)
}))
);
}
if (this.bookListType === 'latestAdded') {
this.bookState$ = this.bookService.bookState$.pipe(
map((state: BookState) => ({
...state,
books: (state.books || [])
.filter(book => book.addedOn)
.sort((a, b) => new Date(b.addedOn!).getTime() - new Date(a.addedOn!).getTime())
.sort((a, b) => {
const aTime = new Date(a.addedOn!).getTime();
const bTime = new Date(b.addedOn!).getTime();
return bTime - aTime;
})
.slice(0, 25)
}))
);