From c051fc5c02af5ef965dc0ca006c4a2e8d3e1e190 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20S=C3=A1ez=20Garc=C3=ADa?= Date: Thu, 23 Nov 2023 08:49:07 +0100 Subject: [PATCH] fix(Certificates): :bug: duplicated initial certificate group --- .../certificates/certificates.component.ts | 47 ++++++++++--------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/src/app/website/pages/certificates/certificates.component.ts b/src/app/website/pages/certificates/certificates.component.ts index 1256f26..bb5068c 100644 --- a/src/app/website/pages/certificates/certificates.component.ts +++ b/src/app/website/pages/certificates/certificates.component.ts @@ -1,6 +1,6 @@ import { ChangeDetectionStrategy, Component, OnInit, inject } from '@angular/core'; import { Store } from '@ngrx/store'; -import { Observable, combineLatest, pairwise, scan, startWith, take } from 'rxjs'; +import { BehaviorSubject, Observable, pairwise, startWith, take, zip } from 'rxjs'; import { CertificateGroup } from 'src/app/backoffice/tables/certificate-group/models/certificate-group.model'; import { certificateGroupActions } from 'src/app/backoffice/tables/certificate-group/state/certificate-group.actions'; import { certificateGroupReducer } from 'src/app/backoffice/tables/certificate-group/state/certificate-group.reducer'; @@ -34,6 +34,7 @@ export class CertificatesComponent implements OnInit { certificateGroups$: Observable = this.store.select(certificateGroupReducer.getAll); loadingMoreCertificateGroups$: Observable = this.store.select(certificateGroupReducer.getLoading); loadingCertificateGroups$: Observable = this.store.select(certificateGroupReducer.getLoading).pipe(take(2)); + certificateGroupCount$: BehaviorSubject = new BehaviorSubject(0); tabIndexes: { groupId: any; value: number }[] = []; @@ -63,28 +64,32 @@ export class CertificatesComponent implements OnInit { }; ngOnInit(): void { - combineLatest([ - this.certificateGroups$.pipe(startWith([]), pairwise()), - this.certificateGroups$.pipe(scan((count) => count + 1, 0)), - ]).subscribe(([[previousCertificateGroups, currentCertificateGroups], count]) => { - if (currentCertificateGroups.length === 0 || previousCertificateGroups.length < currentCertificateGroups.length) { - this.store.dispatch( - certificateGroupActions.loadMore({ - payload: { - first: count - 1, - rows: 1, - }, - }), - ); - } + zip([this.certificateGroups$.pipe(startWith([]), pairwise()), this.certificateGroupCount$]).subscribe( + ([[previousCertificateGroups, currentCertificateGroups], count]) => { + if ( + currentCertificateGroups.length === 0 || + previousCertificateGroups.length < currentCertificateGroups.length + ) { + this.store.dispatch( + certificateGroupActions.loadMore({ + payload: { + first: count, + rows: 1, + }, + }), + ); + } - currentCertificateGroups.forEach((certificateGroup) => { - this.tabIndexes.push({ - groupId: certificateGroup.id, - value: 0, + currentCertificateGroups.forEach((certificateGroup) => { + this.tabIndexes.push({ + groupId: certificateGroup.id, + value: 0, + }); }); - }); - }); + + this.certificateGroupCount$.next(count + 1); + }, + ); } orderByDate(certificates: Certificate[]): Certificate[] {