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); return this.http.put<void>(`${this.url}/${bookId}/book-viewer-setting`, bookSetting);
} }
updateLastReadTime(bookId: number): Observable<void> { updateLastReadTime(bookId: number) {
const timestamp = Math.floor(Date.now() / 1000).toString(); const timestamp = new Date().toISOString();
return this.http.put<void>(`${this.url}/${bookId}/update-last-read/${timestamp}`, {}).pipe(
tap(() => {
const currentState = this.bookStateSubject.value; const currentState = this.bookStateSubject.value;
const updatedBooks = (currentState.books || []).map(book => const updatedBooks = (currentState.books || []).map(book =>
book.id === bookId ? {...book, lastReadTime: timestamp} : book book.id === bookId ? {...book, lastReadTime: timestamp} : book
); );
this.bookStateSubject.next({...currentState, books: updatedBooks}); this.bookStateSubject.next({...currentState, books: updatedBooks});
})
);
} }
readBook(bookId: number): void { readBook(bookId: number): void {
@@ -118,14 +114,11 @@ export class BookService {
if (book.bookType === "PDF") { if (book.bookType === "PDF") {
const url = `/pdf-viewer/book/${book.id}`; const url = `/pdf-viewer/book/${book.id}`;
window.open(url, '_blank'); window.open(url, '_blank');
this.updateLastReadTime(book.id).subscribe({ this.updateLastReadTime(book.id);
next: () => {
},
error: err => console.error('Failed to update last read time', err),
});
} else if (book.bookType === "EPUB") { } else if (book.bookType === "EPUB") {
const url = `/epub-viewer/book/${book.id}`; const url = `/epub-viewer/book/${book.id}`;
window.open(url, '_blank'); window.open(url, '_blank');
this.updateLastReadTime(book.id);
} else { } else {
console.error('Unsupported book type:', book.bookType); console.error('Unsupported book type:', book.bookType);
} }
@@ -38,19 +38,26 @@ export class DashboardScrollerComponent implements OnInit {
...state, ...state,
books: (state.books || []) books: (state.books || [])
.filter(book => book.lastReadTime) .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) .slice(0, 25)
})) }))
); );
} }
if (this.bookListType === 'latestAdded') { if (this.bookListType === 'latestAdded') {
this.bookState$ = this.bookService.bookState$.pipe( this.bookState$ = this.bookService.bookState$.pipe(
map((state: BookState) => ({ map((state: BookState) => ({
...state, ...state,
books: (state.books || []) books: (state.books || [])
.filter(book => book.addedOn) .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) .slice(0, 25)
})) }))
); );