diff --git a/src-ui/messages.xlf b/src-ui/messages.xlf index 8a5a8db60..8fd347d6b 100644 --- a/src-ui/messages.xlf +++ b/src-ui/messages.xlf @@ -5565,7 +5565,7 @@ Other src/app/components/dashboard/widgets/statistics-widget/statistics-widget.component.ts - 66 + 67 diff --git a/src-ui/src/app/components/dashboard/widgets/statistics-widget/statistics-widget.component.spec.ts b/src-ui/src/app/components/dashboard/widgets/statistics-widget/statistics-widget.component.spec.ts index 93a8c1e0f..f4536d3f2 100644 --- a/src-ui/src/app/components/dashboard/widgets/statistics-widget/statistics-widget.component.spec.ts +++ b/src-ui/src/app/components/dashboard/widgets/statistics-widget/statistics-widget.component.spec.ts @@ -71,6 +71,13 @@ describe('StatisticsWidgetComponent', () => { expect(reloadSpy).toHaveBeenCalled() }) + it('should not call statistics endpoint on reload if already loading', () => { + httpTestingController.expectOne(`${environment.apiBaseUrl}statistics/`) + component.loading = true + component.reload() + httpTestingController.expectNone(`${environment.apiBaseUrl}statistics/`) + }) + it('should display inbox link with count', () => { const mockStats = { documents_total: 200, diff --git a/src-ui/src/app/components/dashboard/widgets/statistics-widget/statistics-widget.component.ts b/src-ui/src/app/components/dashboard/widgets/statistics-widget/statistics-widget.component.ts index 1e309aa1a..c2d5d7cb7 100644 --- a/src-ui/src/app/components/dashboard/widgets/statistics-widget/statistics-widget.component.ts +++ b/src-ui/src/app/components/dashboard/widgets/statistics-widget/statistics-widget.component.ts @@ -1,6 +1,6 @@ import { HttpClient } from '@angular/common/http' import { Component, OnDestroy, OnInit } from '@angular/core' -import { Observable, Subscription } from 'rxjs' +import { first, Observable, Subject, Subscription, takeUntil } from 'rxjs' import { FILTER_HAS_TAGS_ANY } from 'src/app/data/filter-rule-type' import { ConsumerStatusService } from 'src/app/services/consumer-status.service' import { DocumentListViewService } from 'src/app/services/document-list-view.service' @@ -35,7 +35,7 @@ export class StatisticsWidgetComponent extends ComponentWithPermissions implements OnInit, OnDestroy { - loading: boolean = true + loading: boolean = false constructor( private http: HttpClient, @@ -48,31 +48,32 @@ export class StatisticsWidgetComponent statistics: Statistics = {} subscription: Subscription - - private getStatistics(): Observable { - return this.http.get(`${environment.apiBaseUrl}statistics/`) - } + private unsubscribeNotifer: Subject = new Subject() reload() { + if (this.loading) return this.loading = true - this.getStatistics().subscribe((statistics) => { - this.loading = false - const fileTypeMax = 5 - if (statistics.document_file_type_counts?.length > fileTypeMax) { - const others = statistics.document_file_type_counts.slice(fileTypeMax) - statistics.document_file_type_counts = - statistics.document_file_type_counts.slice(0, fileTypeMax) - statistics.document_file_type_counts.push({ - mime_type: $localize`Other`, - mime_type_count: others.reduce( - (currentValue, documentFileType) => - documentFileType.mime_type_count + currentValue, - 0 - ), - }) - } - this.statistics = statistics - }) + this.http + .get(`${environment.apiBaseUrl}statistics/`) + .pipe(takeUntil(this.unsubscribeNotifer), first()) + .subscribe((statistics) => { + this.loading = false + const fileTypeMax = 5 + if (statistics.document_file_type_counts?.length > fileTypeMax) { + const others = statistics.document_file_type_counts.slice(fileTypeMax) + statistics.document_file_type_counts = + statistics.document_file_type_counts.slice(0, fileTypeMax) + statistics.document_file_type_counts.push({ + mime_type: $localize`Other`, + mime_type_count: others.reduce( + (currentValue, documentFileType) => + documentFileType.mime_type_count + currentValue, + 0 + ), + }) + } + this.statistics = statistics + }) } getFileTypeExtension(filetype: DocumentFileType): string { @@ -105,6 +106,8 @@ export class StatisticsWidgetComponent ngOnDestroy(): void { this.subscription.unsubscribe() + this.unsubscribeNotifer.next(true) + this.unsubscribeNotifer.complete() } goToInbox() { diff --git a/src-ui/src/app/services/tasks.service.spec.ts b/src-ui/src/app/services/tasks.service.spec.ts index 76a07c34e..41a374831 100644 --- a/src-ui/src/app/services/tasks.service.spec.ts +++ b/src-ui/src/app/services/tasks.service.spec.ts @@ -39,6 +39,12 @@ describe('TasksService', () => { expect(req.request.method).toEqual('GET') }) + it('does not call tasks api endpoint on reload if already loading', () => { + tasksService.loading = true + tasksService.reload() + httpTestingController.expectNone(`${environment.apiBaseUrl}tasks/`) + }) + it('calls acknowledge_tasks api endpoint on dismiss and reloads', () => { tasksService.dismissTasks(new Set([1, 2, 3])) const req = httpTestingController.expectOne( diff --git a/src-ui/src/app/services/tasks.service.ts b/src-ui/src/app/services/tasks.service.ts index 662dd1015..ed14c8071 100644 --- a/src-ui/src/app/services/tasks.service.ts +++ b/src-ui/src/app/services/tasks.service.ts @@ -50,6 +50,7 @@ export class TasksService { constructor(private http: HttpClient) {} public reload() { + if (this.loading) return this.loading = true this.http