Fix dashboard scroller not reactively using last read time
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
+10
-3
@@ -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)
|
||||
}))
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user