|
1 | 1 | import { Component, OnInit } from '@angular/core'; |
2 | 2 | import { ActivatedRoute } from '@angular/router'; |
3 | 3 | import { HttpClient } from '@angular/common/http'; |
| 4 | +import { combineLatest } from 'rxjs'; |
4 | 5 | import { REVIEW_ID_ROUTE_PARAM } from 'src/app/_helpers/router-helpers'; |
5 | 6 | import { NotificationsFilter, SiteNotification } from 'src/app/_models/notificationsModel'; |
6 | 7 | import { UserProfile } from 'src/app/_models/userProfile'; |
@@ -38,17 +39,18 @@ export class NavBarComponent implements OnInit { |
38 | 39 |
|
39 | 40 | ngOnInit(): void { |
40 | 41 | this.reviewId = this.route.snapshot.paramMap.get(REVIEW_ID_ROUTE_PARAM); |
41 | | - this.authService.isLoggedIn().subscribe(isLoggedIn => { |
42 | | - this.isLoggedIn = isLoggedIn; |
43 | | - }); |
44 | 42 |
|
45 | | - this.userProfileService.getUserProfile().subscribe( |
46 | | - (userProfile : any) => { |
47 | | - this.userProfile = userProfile; |
48 | | - // Check if user is an approver |
| 43 | + // Use combineLatest to wait for both isLoggedIn and userProfile before checking approver status |
| 44 | + combineLatest([ |
| 45 | + this.authService.isLoggedIn(), |
| 46 | + this.userProfileService.getUserProfile() |
| 47 | + ]).subscribe(([isLoggedIn, userProfile]) => { |
| 48 | + this.isLoggedIn = isLoggedIn; |
| 49 | + this.userProfile = userProfile; |
| 50 | + if (isLoggedIn && userProfile) { |
49 | 51 | this.checkApproverStatus(); |
50 | 52 | } |
51 | | - ); |
| 53 | + }); |
52 | 54 |
|
53 | 55 | this.notificationsService.notifications$.subscribe(notifications => { |
54 | 56 | this.notifications = notifications; |
@@ -78,22 +80,24 @@ export class NavBarComponent implements OnInit { |
78 | 80 | } |
79 | 81 |
|
80 | 82 | private checkApproverStatus() { |
81 | | - if (this.userProfile?.userName) { |
82 | | - // Call the API to get allowed approvers |
83 | | - this.http.get<string>(`${this.configService.apiUrl}/Reviews/allowedApprovers`).subscribe({ |
84 | | - next: (allowedApprovers) => { |
85 | | - if (allowedApprovers) { |
86 | | - // Split comma-separated string and check if current user is in the list |
87 | | - const approversList = allowedApprovers.split(',').map(username => username.trim()); |
88 | | - this.isApprover = approversList.includes(this.userProfile?.userName || ''); |
89 | | - } |
90 | | - }, |
91 | | - error: (error) => { |
92 | | - console.error('Failed to fetch allowed approvers:', error); |
| 83 | + if (!this.userProfile?.userName || !this.isLoggedIn) { |
| 84 | + this.isApprover = false; |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + this.http.get<string>(`${this.configService.apiUrl}/Reviews/allowedApprovers`, { withCredentials: true }).subscribe({ |
| 89 | + next: (allowedApprovers) => { |
| 90 | + if (allowedApprovers) { |
| 91 | + const approversList = allowedApprovers.split(',').map(username => username.trim()); |
| 92 | + this.isApprover = approversList.includes(this.userProfile?.userName || ''); |
| 93 | + } else { |
93 | 94 | this.isApprover = false; |
94 | | - // Optionally, notify the user here if desired |
95 | 95 | } |
96 | | - }); |
97 | | - } |
| 96 | + }, |
| 97 | + error: (error) => { |
| 98 | + console.error('Failed to fetch allowed approvers:', error); |
| 99 | + this.isApprover = false; |
| 100 | + } |
| 101 | + }); |
98 | 102 | } |
99 | 103 | } |
0 commit comments