fix(loading): update loading checks to use signal() for reactive state management (#727)
This commit is contained in:
+4
-4
@@ -83,8 +83,8 @@
|
||||
[text]="true"
|
||||
severity="info"
|
||||
(onClick)="fetchMetadataByIsbn()"
|
||||
[disabled]="!isbn.trim() || isFetchingMetadata"
|
||||
[loading]="isFetchingMetadata"
|
||||
[disabled]="!isbn.trim() || isFetchingMetadata()"
|
||||
[loading]="isFetchingMetadata()"
|
||||
[pTooltip]="t('fetchMetadataTooltip')"
|
||||
tooltipPosition="top"/>
|
||||
</div>
|
||||
@@ -250,8 +250,8 @@
|
||||
icon="pi pi-plus"
|
||||
severity="success"
|
||||
(onClick)="createBook()"
|
||||
[disabled]="!canCreate() || isLoading"
|
||||
[loading]="isLoading"/>
|
||||
[disabled]="!canCreate() || isLoading()"
|
||||
[loading]="isLoading()"/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
+9
-9
@@ -181,7 +181,7 @@ describe('AddPhysicalBookDialogComponent', () => {
|
||||
component.fetchMetadataByIsbn();
|
||||
|
||||
expect(lookupByIsbn).toHaveBeenCalledWith('9780441172719');
|
||||
expect(component.isFetchingMetadata).toBe(true);
|
||||
expect(component.isFetchingMetadata()).toBe(true);
|
||||
|
||||
lookupResult$.next(createMetadata({
|
||||
title: 'Dune',
|
||||
@@ -205,7 +205,7 @@ describe('AddPhysicalBookDialogComponent', () => {
|
||||
expect(component.pageCount).toBe(412);
|
||||
expect(component.categories).toEqual(['Science Fiction']);
|
||||
expect(component.coverUrl).toBe('https://covers.example/dune.jpg');
|
||||
expect(component.isFetchingMetadata).toBe(false);
|
||||
expect(component.isFetchingMetadata()).toBe(false);
|
||||
});
|
||||
|
||||
it('does not start isbn lookup for blank or in-flight requests and clears the flag on error', () => {
|
||||
@@ -216,17 +216,17 @@ describe('AddPhysicalBookDialogComponent', () => {
|
||||
expect(lookupByIsbn).not.toHaveBeenCalled();
|
||||
|
||||
component.isbn = '9780441172719';
|
||||
component.isFetchingMetadata = true;
|
||||
component.isFetchingMetadata.set(true);
|
||||
component.fetchMetadataByIsbn();
|
||||
expect(lookupByIsbn).not.toHaveBeenCalled();
|
||||
|
||||
component.isFetchingMetadata = false;
|
||||
component.isFetchingMetadata.set(false);
|
||||
lookupByIsbn.mockReturnValueOnce(throwError(() => new Error('lookup failed')));
|
||||
|
||||
component.fetchMetadataByIsbn();
|
||||
|
||||
expect(lookupByIsbn).toHaveBeenCalledWith('9780441172719');
|
||||
expect(component.isFetchingMetadata).toBe(false);
|
||||
expect(component.isFetchingMetadata()).toBe(false);
|
||||
});
|
||||
|
||||
it('gates creation on required fields and does not submit when already loading', () => {
|
||||
@@ -240,7 +240,7 @@ describe('AddPhysicalBookDialogComponent', () => {
|
||||
component.title = 'Physical Copy';
|
||||
expect(component.canCreate()).toBe(true);
|
||||
|
||||
component.isLoading = true;
|
||||
component.isLoading.set(true);
|
||||
component.createBook();
|
||||
|
||||
expect(createPhysicalBook).not.toHaveBeenCalled();
|
||||
@@ -270,7 +270,7 @@ describe('AddPhysicalBookDialogComponent', () => {
|
||||
|
||||
component.createBook();
|
||||
|
||||
expect(component.isLoading).toBe(true);
|
||||
expect(component.isLoading()).toBe(true);
|
||||
expect(createPhysicalBook).toHaveBeenCalledWith({
|
||||
libraryId: 2,
|
||||
title: 'The Left Hand of Darkness',
|
||||
@@ -288,7 +288,7 @@ describe('AddPhysicalBookDialogComponent', () => {
|
||||
createResult$.next(createdBook);
|
||||
createResult$.complete();
|
||||
|
||||
expect(component.isLoading).toBe(false);
|
||||
expect(component.isLoading()).toBe(false);
|
||||
expect(dialogRef.close).toHaveBeenCalledWith(createdBook);
|
||||
});
|
||||
|
||||
@@ -314,7 +314,7 @@ describe('AddPhysicalBookDialogComponent', () => {
|
||||
categories: undefined,
|
||||
thumbnailUrl: undefined,
|
||||
});
|
||||
expect(component.isLoading).toBe(false);
|
||||
expect(component.isLoading()).toBe(false);
|
||||
expect(dialogRef.close).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
+11
-11
@@ -1,4 +1,4 @@
|
||||
import {Component, computed, effect, inject} from '@angular/core';
|
||||
import {Component, computed, effect, inject, signal} from '@angular/core';
|
||||
import {DynamicDialogConfig, DynamicDialogRef} from 'primeng/dynamicdialog';
|
||||
import {FormsModule} from '@angular/forms';
|
||||
import {Button} from 'primeng/button';
|
||||
@@ -57,8 +57,8 @@ export class AddPhysicalBookDialogComponent {
|
||||
filteredCategories: string[] = [];
|
||||
|
||||
coverUrl: string | null = null;
|
||||
isLoading: boolean = false;
|
||||
isFetchingMetadata: boolean = false;
|
||||
isLoading = signal(false);
|
||||
isFetchingMetadata = signal(false);
|
||||
private readonly initializeSelectedLibraryEffect = effect(() => {
|
||||
const libraries = this.libraries;
|
||||
if (libraries.length === 0) {
|
||||
@@ -117,9 +117,9 @@ export class AddPhysicalBookDialogComponent {
|
||||
|
||||
fetchMetadataByIsbn(): void {
|
||||
const isbnValue = this.isbn.trim();
|
||||
if (!isbnValue || this.isFetchingMetadata) return;
|
||||
if (!isbnValue || this.isFetchingMetadata()) return;
|
||||
|
||||
this.isFetchingMetadata = true;
|
||||
this.isFetchingMetadata.set(true);
|
||||
this.bookMetadataService.lookupByIsbn(isbnValue).subscribe({
|
||||
next: (metadata) => {
|
||||
if (metadata.title) this.title = metadata.title;
|
||||
@@ -131,10 +131,10 @@ export class AddPhysicalBookDialogComponent {
|
||||
if (metadata.pageCount) this.pageCount = metadata.pageCount;
|
||||
if (metadata.categories?.length) this.categories = [...metadata.categories];
|
||||
this.coverUrl = metadata.thumbnailUrl || null;
|
||||
this.isFetchingMetadata = false;
|
||||
this.isFetchingMetadata.set(false);
|
||||
},
|
||||
error: () => {
|
||||
this.isFetchingMetadata = false;
|
||||
this.isFetchingMetadata.set(false);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -148,9 +148,9 @@ export class AddPhysicalBookDialogComponent {
|
||||
}
|
||||
|
||||
createBook(): void {
|
||||
if (!this.canCreate() || this.isLoading) return;
|
||||
if (!this.canCreate() || this.isLoading()) return;
|
||||
|
||||
this.isLoading = true;
|
||||
this.isLoading.set(true);
|
||||
|
||||
const request: CreatePhysicalBookRequest = {
|
||||
libraryId: this.selectedLibraryId!,
|
||||
@@ -168,11 +168,11 @@ export class AddPhysicalBookDialogComponent {
|
||||
|
||||
this.bookService.createPhysicalBook(request).subscribe({
|
||||
next: (book) => {
|
||||
this.isLoading = false;
|
||||
this.isLoading.set(false);
|
||||
this.dynamicDialogRef.close(book);
|
||||
},
|
||||
error: () => {
|
||||
this.isLoading = false;
|
||||
this.isLoading.set(false);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<div class="book-notes-container">
|
||||
<div class="notes-content">
|
||||
@if (loading) {
|
||||
@if (loading()) {
|
||||
<div class="loading-state">
|
||||
<p-progressSpinner/>
|
||||
<span class="loading-text">Loading notes...</span>
|
||||
|
||||
@@ -85,7 +85,7 @@ describe('BookNotesComponent', () => {
|
||||
component.loadNotes();
|
||||
|
||||
expect(bookNoteService.getNotesForBook).not.toHaveBeenCalled();
|
||||
expect(component.loading).toBe(false);
|
||||
expect(component.loading()).toBe(false);
|
||||
});
|
||||
|
||||
it('loads notes and orders them by most recent update first', () => {
|
||||
@@ -98,7 +98,7 @@ describe('BookNotesComponent', () => {
|
||||
|
||||
expect(bookNoteService.getNotesForBook).toHaveBeenCalledWith(42);
|
||||
expect(component.notes.map(note => note.id)).toEqual([2, 3, 1]);
|
||||
expect(component.loading).toBe(false);
|
||||
expect(component.loading()).toBe(false);
|
||||
});
|
||||
|
||||
it('shows an error toast when loading notes fails', () => {
|
||||
@@ -106,7 +106,7 @@ describe('BookNotesComponent', () => {
|
||||
|
||||
component.loadNotes();
|
||||
|
||||
expect(component.loading).toBe(false);
|
||||
expect(component.loading()).toBe(false);
|
||||
expect(messageService.add).toHaveBeenCalledWith({
|
||||
severity: 'error',
|
||||
summary: 'common.error',
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import {Component, DestroyRef, inject, Input, OnChanges, OnInit, SimpleChanges} from '@angular/core';
|
||||
import {Component, DestroyRef, inject, Input, OnChanges, OnInit, signal, SimpleChanges} from '@angular/core';
|
||||
import {FormsModule} from '@angular/forms';
|
||||
import {takeUntilDestroyed} from '@angular/core/rxjs-interop';
|
||||
import {Button} from 'primeng/button';
|
||||
@@ -38,7 +38,7 @@ export class BookNotesComponent implements OnInit, OnChanges {
|
||||
private readonly t = inject(TranslocoService);
|
||||
|
||||
notes: BookNote[] = [];
|
||||
loading = false;
|
||||
loading = signal(false);
|
||||
showCreateDialog = false;
|
||||
showEditDialog = false;
|
||||
selectedNote: BookNote | null = null;
|
||||
@@ -70,7 +70,7 @@ export class BookNotesComponent implements OnInit, OnChanges {
|
||||
loadNotes(): void {
|
||||
if (!this.bookId) return;
|
||||
|
||||
this.loading = true;
|
||||
this.loading.set(true);
|
||||
this.bookNoteService.getNotesForBook(this.bookId)
|
||||
.pipe(takeUntilDestroyed(this.destroyRef))
|
||||
.subscribe({
|
||||
@@ -78,11 +78,11 @@ export class BookNotesComponent implements OnInit, OnChanges {
|
||||
this.notes = notes.sort((a, b) =>
|
||||
new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime()
|
||||
);
|
||||
this.loading = false;
|
||||
this.loading.set(false);
|
||||
},
|
||||
error: (error) => {
|
||||
console.error('Failed to load notes:', error);
|
||||
this.loading = false;
|
||||
this.loading.set(false);
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: this.t.translate('common.error'),
|
||||
|
||||
+12
-12
@@ -1,7 +1,7 @@
|
||||
<ng-container *transloco="let t; prefix: 'book.reviews'">
|
||||
<div class="book-reviews-container">
|
||||
<div class="reviews-content">
|
||||
@if (loading) {
|
||||
@if (loading()) {
|
||||
<div class="loading-state">
|
||||
<p-progressSpinner/>
|
||||
<span class="loading-text">{{ t('labels.loadingReviews') }}</span>
|
||||
@@ -156,9 +156,9 @@
|
||||
[icon]="reviewsLocked ? 'pi pi-lock' : 'pi pi-lock-open'"
|
||||
size="small"
|
||||
[severity]="reviewsLocked ? 'danger' : 'success'"
|
||||
[disabled]="!reviewDownloadEnabled || loading"
|
||||
[disabled]="!reviewDownloadEnabled || loading()"
|
||||
(click)="toggleReviewsLock()"
|
||||
[pTooltip]="loading ? t('tooltip.pleaseWait') : (!reviewDownloadEnabled ? t('tooltip.enableDownloads') : (reviewsLocked ? t('tooltip.unlockReviews') : t('tooltip.lockReviews')))"
|
||||
[pTooltip]="loading() ? t('tooltip.pleaseWait') : (!reviewDownloadEnabled ? t('tooltip.enableDownloads') : (reviewsLocked ? t('tooltip.unlockReviews') : t('tooltip.lockReviews')))"
|
||||
tooltipPosition="left"
|
||||
class="action-btn"/>
|
||||
|
||||
@@ -167,10 +167,10 @@
|
||||
icon="pi pi-refresh"
|
||||
size="small"
|
||||
severity="primary"
|
||||
[loading]="loading"
|
||||
[disabled]="reviewsLocked || !reviewDownloadEnabled || loading"
|
||||
[loading]="loading()"
|
||||
[disabled]="reviewsLocked || !reviewDownloadEnabled || loading()"
|
||||
(click)="fetchNewReviews()"
|
||||
[pTooltip]="loading ? t('tooltip.pleaseWait') : (!reviewDownloadEnabled ? t('tooltip.enableDownloads') : (reviewsLocked ? t('tooltip.reviewsLocked') : t('tooltip.fetchNewReviews')))"
|
||||
[pTooltip]="loading() ? t('tooltip.pleaseWait') : (!reviewDownloadEnabled ? t('tooltip.enableDownloads') : (reviewsLocked ? t('tooltip.reviewsLocked') : t('tooltip.fetchNewReviews')))"
|
||||
tooltipPosition="left"
|
||||
class="action-btn"/>
|
||||
}
|
||||
@@ -181,9 +181,9 @@
|
||||
icon="pi pi-trash"
|
||||
size="small"
|
||||
severity="danger"
|
||||
[disabled]="reviewsLocked || loading"
|
||||
[disabled]="reviewsLocked || loading()"
|
||||
(click)="deleteAllReviews()"
|
||||
[pTooltip]="loading ? t('tooltip.pleaseWait') : (reviewsLocked ? t('tooltip.reviewsLocked') : t('tooltip.deleteAllReviews'))"
|
||||
[pTooltip]="loading() ? t('tooltip.pleaseWait') : (reviewsLocked ? t('tooltip.reviewsLocked') : t('tooltip.deleteAllReviews'))"
|
||||
tooltipPosition="left"
|
||||
class="action-btn"/>
|
||||
}
|
||||
@@ -194,9 +194,9 @@
|
||||
[icon]="allSpoilersRevealed ? 'pi pi-eye-slash' : 'pi pi-eye'"
|
||||
size="small"
|
||||
[severity]="allSpoilersRevealed ? 'warn' : 'info'"
|
||||
[disabled]="loading"
|
||||
[disabled]="loading()"
|
||||
(click)="toggleSpoilerVisibility()"
|
||||
[pTooltip]="loading ? t('tooltip.pleaseWait') : (allSpoilersRevealed ? t('tooltip.hideAllSpoilers') : t('tooltip.revealAllSpoilers'))"
|
||||
[pTooltip]="loading() ? t('tooltip.pleaseWait') : (allSpoilersRevealed ? t('tooltip.hideAllSpoilers') : t('tooltip.revealAllSpoilers'))"
|
||||
tooltipPosition="left"
|
||||
class="action-btn"/>
|
||||
|
||||
@@ -205,9 +205,9 @@
|
||||
icon="pi pi-sort-amount-down"
|
||||
size="small"
|
||||
severity="contrast"
|
||||
[disabled]="loading"
|
||||
[disabled]="loading()"
|
||||
(click)="toggleSortOrder()"
|
||||
[pTooltip]="loading ? t('tooltip.pleaseWait') : (sortAscending ? t('tooltip.sortNewest') : t('tooltip.sortOldest'))"
|
||||
[pTooltip]="loading() ? t('tooltip.pleaseWait') : (sortAscending ? t('tooltip.sortNewest') : t('tooltip.sortOldest'))"
|
||||
tooltipPosition="left"
|
||||
class="action-btn"/>
|
||||
}
|
||||
|
||||
+95
-4
@@ -1,6 +1,7 @@
|
||||
import {SimpleChange} from '@angular/core';
|
||||
import {TestBed} from '@angular/core/testing';
|
||||
import {of, throwError} from 'rxjs';
|
||||
import {of, throwError, delay} from 'rxjs';
|
||||
|
||||
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
|
||||
import {ConfirmationService, MessageService} from 'primeng/api';
|
||||
import {TranslocoService} from '@jsverse/transloco';
|
||||
@@ -151,7 +152,7 @@ describe('BookReviewsComponent', () => {
|
||||
expect(reviewService.getByBookId).toHaveBeenCalledWith(42);
|
||||
expect(component.reviews?.map(review => review.id)).toEqual([3, 2, 1]);
|
||||
expect(component.reviewsLocked).toBe(true);
|
||||
expect(component.loading).toBe(false);
|
||||
expect(component.loading()).toBe(false);
|
||||
});
|
||||
|
||||
it('refreshes reviews, clears revealed spoilers, and shows a success toast', () => {
|
||||
@@ -167,7 +168,7 @@ describe('BookReviewsComponent', () => {
|
||||
expect(component.reviews?.map(review => review.id)).toEqual([5, 4]);
|
||||
expect(component.revealedSpoilers.size).toBe(0);
|
||||
expect(component.allSpoilersRevealed).toBe(false);
|
||||
expect(component.loading).toBe(false);
|
||||
expect(component.loading()).toBe(false);
|
||||
expect(messageService.add).toHaveBeenCalledWith({
|
||||
severity: 'success',
|
||||
summary: 'book.reviews.toast.reviewsUpdatedSummary',
|
||||
@@ -184,7 +185,7 @@ describe('BookReviewsComponent', () => {
|
||||
component.fetchNewReviews();
|
||||
|
||||
expect(errorSpy).toHaveBeenCalledWith('Failed to fetch new reviews:', error);
|
||||
expect(component.loading).toBe(false);
|
||||
expect(component.loading()).toBe(false);
|
||||
expect(messageService.add).toHaveBeenCalledWith({
|
||||
severity: 'error',
|
||||
summary: 'book.reviews.toast.fetchFailedSummary',
|
||||
@@ -339,4 +340,94 @@ describe('BookReviewsComponent', () => {
|
||||
expect(component.getProviderSeverity('goodreads')).toBe('success');
|
||||
expect(component.formatDate('2026-03-28T12:00:00.000Z')).toBe('March 28, 2026');
|
||||
});
|
||||
|
||||
it('ignores stale responses if the bookId changes while a request is in-flight', async () => {
|
||||
const book1Reviews = [createReview(1)];
|
||||
const book2Reviews = [createReview(2)];
|
||||
|
||||
// First request is slow
|
||||
reviewService.getByBookId.mockReturnValueOnce(of(book1Reviews).pipe(delay(50)));
|
||||
// Second request is fast
|
||||
reviewService.getByBookId.mockReturnValueOnce(of(book2Reviews));
|
||||
|
||||
// Select book 1
|
||||
component.bookId = 1;
|
||||
component.ngOnChanges({
|
||||
bookId: new SimpleChange(undefined, 1, true),
|
||||
});
|
||||
|
||||
// Immediately select book 2 while book 1 is still loading
|
||||
component.bookId = 2;
|
||||
component.ngOnChanges({
|
||||
bookId: new SimpleChange(1, 2, false),
|
||||
});
|
||||
|
||||
// Wait for both to complete
|
||||
await new Promise(resolve => setTimeout(resolve, 100));
|
||||
|
||||
// Should only have book 2's reviews
|
||||
expect(component.reviews?.map(r => r.id)).toEqual([2]);
|
||||
expect(component.loading()).toBe(false);
|
||||
});
|
||||
|
||||
it('does not prematurely clear loading if a refresh finishes while a new book load is in-flight', async () => {
|
||||
// 1. fetchNewReviews(42) starts
|
||||
reviewService.refreshReviews.mockReturnValueOnce(of([]).pipe(delay(50)));
|
||||
component.fetchNewReviews();
|
||||
expect(component.loading()).toBe(true);
|
||||
|
||||
// 2. Switch to book 43 -> loadReviews(43) starts
|
||||
// Use a longer delay for the new book load
|
||||
reviewService.getByBookId.mockReturnValueOnce(of([]).pipe(delay(100)));
|
||||
component.bookId = 43;
|
||||
component.ngOnChanges({
|
||||
bookId: new SimpleChange(42, 43, false),
|
||||
});
|
||||
|
||||
// 3. Wait for fetchNewReviews(42) to finish (after 50ms)
|
||||
await new Promise(resolve => setTimeout(resolve, 75));
|
||||
|
||||
// 4. loading should still be true because loadReviews(43) is in flight
|
||||
expect(component.loading()).toBe(true);
|
||||
|
||||
// 5. Wait for loadReviews(43) to finish (after 100ms total)
|
||||
await new Promise(resolve => setTimeout(resolve, 50));
|
||||
expect(component.loading()).toBe(false);
|
||||
|
||||
});
|
||||
|
||||
it('ignores stale responses in a 1 -> 2 -> 1 navigation sequence (ABA problem)', async () => {
|
||||
const book1InitialReviews = [createReview(1, {body: 'First Request'})];
|
||||
const book1LatestReviews = [createReview(1, {body: 'Second Request'})];
|
||||
const book2Reviews = [createReview(2)];
|
||||
|
||||
// 1. First request for Book 1 is very slow
|
||||
reviewService.getByBookId.mockReturnValueOnce(of(book1InitialReviews).pipe(delay(100)));
|
||||
// 2. Request for Book 2 is fast
|
||||
reviewService.getByBookId.mockReturnValueOnce(of(book2Reviews));
|
||||
// 3. Second request for Book 1 is slow
|
||||
reviewService.getByBookId.mockReturnValueOnce(of(book1LatestReviews).pipe(delay(50)));
|
||||
|
||||
// Navigate: Book 1
|
||||
component.bookId = 1;
|
||||
component.ngOnChanges({bookId: new SimpleChange(undefined, 1, true)});
|
||||
|
||||
// Navigate: Book 2
|
||||
component.bookId = 2;
|
||||
component.ngOnChanges({bookId: new SimpleChange(1, 2, false)});
|
||||
|
||||
// Navigate: Book 1 again
|
||||
component.bookId = 1;
|
||||
component.ngOnChanges({bookId: new SimpleChange(2, 1, false)});
|
||||
|
||||
// Wait for all requests to finish
|
||||
await new Promise(resolve => setTimeout(resolve, 150));
|
||||
|
||||
// Should have the reviews from the SECOND request for Book 1, not the first
|
||||
expect(component.reviews?.[0].body).toBe('Second Request');
|
||||
expect(component.loading()).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import {Component, DestroyRef, effect, inject, Input, OnChanges, OnInit, signal, SimpleChanges} from '@angular/core';
|
||||
|
||||
import {takeUntilDestroyed} from '@angular/core/rxjs-interop';
|
||||
import {finalize} from 'rxjs';
|
||||
|
||||
import {BookReview, BookReviewService} from './book-review-service';
|
||||
import {ProgressSpinner} from 'primeng/progressspinner';
|
||||
import {Rating} from 'primeng/rating';
|
||||
@@ -37,8 +39,13 @@ export class BookReviewsComponent implements OnInit, OnChanges {
|
||||
private destroyRef = inject(DestroyRef);
|
||||
private readonly t = inject(TranslocoService);
|
||||
private bookIdState = signal<number | null>(null);
|
||||
private loadingBookId: number | null = null;
|
||||
private loadingRequestSeq = 0;
|
||||
private activeLoadingRequestSeq: number | null = null;
|
||||
|
||||
loading = false;
|
||||
|
||||
|
||||
loading = signal(false);
|
||||
hasPermission = false;
|
||||
revealedSpoilers = new Set<number>();
|
||||
sortAscending = false;
|
||||
@@ -72,22 +79,36 @@ export class BookReviewsComponent implements OnInit, OnChanges {
|
||||
}
|
||||
|
||||
private loadReviews(): void {
|
||||
if (this.loading || !this.bookId) {
|
||||
const requestedBookId = this.bookId;
|
||||
if (!requestedBookId || this.loadingBookId === requestedBookId) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.loading = true;
|
||||
this.reviewService.getByBookId(this.bookId)
|
||||
.pipe(takeUntilDestroyed(this.destroyRef))
|
||||
const requestSeq = ++this.loadingRequestSeq;
|
||||
this.loadingBookId = requestedBookId;
|
||||
this.activeLoadingRequestSeq = requestSeq;
|
||||
this.loading.set(true);
|
||||
this.reviewService.getByBookId(requestedBookId)
|
||||
.pipe(
|
||||
takeUntilDestroyed(this.destroyRef),
|
||||
finalize(() => {
|
||||
if (this.activeLoadingRequestSeq === requestSeq) {
|
||||
this.loadingBookId = null;
|
||||
this.activeLoadingRequestSeq = null;
|
||||
this.loading.set(false);
|
||||
}
|
||||
})
|
||||
)
|
||||
.subscribe({
|
||||
next: (reviews) => {
|
||||
if (this.bookId !== requestedBookId || this.activeLoadingRequestSeq !== requestSeq) return;
|
||||
this.reviews = this.sortReviewsByDate(reviews || []);
|
||||
this.loading = false;
|
||||
},
|
||||
error: (error) => {
|
||||
console.error('Failed to load reviews for bookId', this.bookId, ':', error);
|
||||
if (this.bookId !== requestedBookId || this.activeLoadingRequestSeq !== requestSeq) return;
|
||||
console.error('Failed to load reviews for bookId', requestedBookId, ':', error);
|
||||
this.reviews = [];
|
||||
this.loading = false;
|
||||
|
||||
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
@@ -99,18 +120,33 @@ export class BookReviewsComponent implements OnInit, OnChanges {
|
||||
});
|
||||
}
|
||||
|
||||
fetchNewReviews(): void {
|
||||
if (!this.bookId || this.loading || this.reviewsLocked) return;
|
||||
|
||||
this.loading = true;
|
||||
fetchNewReviews(): void {
|
||||
const requestedBookId = this.bookId;
|
||||
if (!requestedBookId || this.loadingBookId === requestedBookId || this.loading() || this.reviewsLocked) return;
|
||||
|
||||
const requestSeq = ++this.loadingRequestSeq;
|
||||
this.loadingBookId = requestedBookId;
|
||||
this.activeLoadingRequestSeq = requestSeq;
|
||||
this.loading.set(true);
|
||||
this.revealedSpoilers.clear();
|
||||
|
||||
this.reviewService.refreshReviews(this.bookId)
|
||||
.pipe(takeUntilDestroyed(this.destroyRef))
|
||||
this.reviewService.refreshReviews(requestedBookId)
|
||||
.pipe(
|
||||
takeUntilDestroyed(this.destroyRef),
|
||||
finalize(() => {
|
||||
if (this.activeLoadingRequestSeq === requestSeq) {
|
||||
this.loadingBookId = null;
|
||||
this.activeLoadingRequestSeq = null;
|
||||
this.loading.set(false);
|
||||
}
|
||||
})
|
||||
)
|
||||
.subscribe({
|
||||
next: (reviews) => {
|
||||
if (this.bookId !== requestedBookId || this.activeLoadingRequestSeq !== requestSeq) return;
|
||||
this.reviews = this.sortReviewsByDate(reviews || []);
|
||||
this.loading = false;
|
||||
|
||||
this.updateSpoilerState();
|
||||
this.messageService.add({
|
||||
severity: 'success',
|
||||
@@ -120,8 +156,9 @@ export class BookReviewsComponent implements OnInit, OnChanges {
|
||||
});
|
||||
},
|
||||
error: (error) => {
|
||||
if (this.bookId !== requestedBookId || this.activeLoadingRequestSeq !== requestSeq) return;
|
||||
console.error('Failed to fetch new reviews:', error);
|
||||
this.loading = false;
|
||||
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: this.t.translate('book.reviews.toast.fetchFailedSummary'),
|
||||
@@ -132,6 +169,8 @@ export class BookReviewsComponent implements OnInit, OnChanges {
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
deleteAllReviews(): void {
|
||||
if (!this.reviews || this.reviews.length === 0 || this.reviewsLocked) return;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user