1
1
import { Component , OnInit } from '@angular/core' ;
2
2
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' ;
4
4
import { Resource , ServiceCard } from '../../types/dtos' ;
5
5
import { MarketplaceService } from '../../services/marketplace.service' ;
6
6
import { QueryService } from 'src/app/services/query.service' ;
@@ -38,6 +38,9 @@ export class MarketplaceComponent implements OnInit {
38
38
legalName : string | null = null ;
39
39
error : HttpErrorResponse | null = null ;
40
40
41
+ private legalNameSubject = new BehaviorSubject < string | null > ( null ) ;
42
+ private legalName$ = this . legalNameSubject . asObservable ( ) ;
43
+
41
44
public IndexType = IndexType ;
42
45
public serviceIndexes : Map < number , CarouselIndexes > = new Map < number , CarouselIndexes > ( ) ;
43
46
@@ -49,48 +52,51 @@ export class MarketplaceComponent implements OnInit {
49
52
) { }
50
53
51
54
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 ( ) ;
56
64
57
65
this . legalNames$ = this . marketplaceService . fetchLegalNames ( ) . pipe (
58
66
catchError ( ( error : HttpErrorResponse ) => {
59
67
this . error = error ;
60
68
return EMPTY ;
61
69
} ) ,
62
70
) ;
63
- }
64
71
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
+ ) ,
74
84
) ;
75
85
}
76
86
77
- navigateToQuery ( offerId : number ) : void {
78
- const query = this . queryService . buildOfferInfoQuery ( offerId ) ;
79
- this . router . navigate ( [ '/query' ] , {
80
- queryParams : { query } ,
81
- } ) ;
82
- }
83
-
84
87
onLegalNameChange ( event : MatSelectChange ) : void {
85
- this . legalName = event . value ;
86
-
87
88
this . router . navigate ( [ ] , {
88
89
relativeTo : this . route ,
89
- queryParams : { participant : this . legalName || null } ,
90
+ queryParams : { participant : event . value || null } ,
90
91
queryParamsHandling : 'merge' ,
91
92
} ) ;
93
+ }
92
94
93
- this . fetchData ( ) ;
95
+ navigateToQuery ( offerId : number ) : void {
96
+ const query = this . queryService . buildOfferInfoQuery ( offerId ) ;
97
+ this . router . navigate ( [ '/query' ] , {
98
+ queryParams : { query } ,
99
+ } ) ;
94
100
}
95
101
96
102
private getCarouselIndexes ( serviceId : number ) : CarouselIndexes {
0 commit comments