Skip to content

Commit 867a79f

Browse files
committed
refactor: make legalName reactive
1 parent 15bc620 commit 867a79f

File tree

1 file changed

+32
-26
lines changed

1 file changed

+32
-26
lines changed

src/app/components/marketplace/marketplace.component.ts

+32-26
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Component, OnInit } from '@angular/core';
22
import { ActivatedRoute, Router } from '@angular/router';
3-
import { Observable, EMPTY, catchError, tap } from 'rxjs';
3+
import { BehaviorSubject, EMPTY, Observable, catchError, map, switchMap, tap } from 'rxjs';
44
import { Resource, ServiceCard } from '../../types/dtos';
55
import { MarketplaceService } from '../../services/marketplace.service';
66
import { QueryService } from 'src/app/services/query.service';
@@ -38,6 +38,9 @@ export class MarketplaceComponent implements OnInit {
3838
legalName: string | null = null;
3939
error: HttpErrorResponse | null = null;
4040

41+
private legalNameSubject = new BehaviorSubject<string | null>(null);
42+
private legalName$ = this.legalNameSubject.asObservable();
43+
4144
public IndexType = IndexType;
4245
public serviceIndexes: Map<number, CarouselIndexes> = new Map<number, CarouselIndexes>();
4346

@@ -49,48 +52,51 @@ export class MarketplaceComponent implements OnInit {
4952
) {}
5053

5154
ngOnInit(): void {
52-
this.route.queryParams.subscribe((params) => {
53-
this.legalName = params['participant'] || null;
54-
this.fetchData();
55-
});
55+
this.route.queryParams
56+
.pipe(
57+
map((params) => params['participant'] || null),
58+
tap((participant) => {
59+
this.legalName = participant;
60+
this.legalNameSubject.next(participant);
61+
}),
62+
)
63+
.subscribe();
5664

5765
this.legalNames$ = this.marketplaceService.fetchLegalNames().pipe(
5866
catchError((error: HttpErrorResponse) => {
5967
this.error = error;
6068
return EMPTY;
6169
}),
6270
);
63-
}
6471

65-
fetchData(): void {
66-
this.services$ = this.marketplaceService.fetchServiceData(this.legalName).pipe(
67-
tap((services: ServiceCard[]) => {
68-
services.forEach((service) => this.getCarouselIndexes(service.serviceOffering.id));
69-
}),
70-
catchError((error: HttpErrorResponse) => {
71-
this.error = error;
72-
return EMPTY;
73-
}),
72+
this.services$ = this.legalName$.pipe(
73+
switchMap((name) =>
74+
this.marketplaceService.fetchServiceData(name).pipe(
75+
tap((services: ServiceCard[]) =>
76+
services.forEach((s) => this.getCarouselIndexes(s.serviceOffering.id)),
77+
),
78+
catchError((error: HttpErrorResponse) => {
79+
this.error = error;
80+
return EMPTY;
81+
}),
82+
),
83+
),
7484
);
7585
}
7686

77-
navigateToQuery(offerId: number): void {
78-
const query = this.queryService.buildOfferInfoQuery(offerId);
79-
this.router.navigate(['/query'], {
80-
queryParams: { query },
81-
});
82-
}
83-
8487
onLegalNameChange(event: MatSelectChange): void {
85-
this.legalName = event.value;
86-
8788
this.router.navigate([], {
8889
relativeTo: this.route,
89-
queryParams: { participant: this.legalName || null },
90+
queryParams: { participant: event.value || null },
9091
queryParamsHandling: 'merge',
9192
});
93+
}
9294

93-
this.fetchData();
95+
navigateToQuery(offerId: number): void {
96+
const query = this.queryService.buildOfferInfoQuery(offerId);
97+
this.router.navigate(['/query'], {
98+
queryParams: { query },
99+
});
94100
}
95101

96102
private getCarouselIndexes(serviceId: number): CarouselIndexes {

0 commit comments

Comments
 (0)