|
1 | 1 | import { HttpClient, HttpHeaders } from '@angular/common/http' |
2 | 2 | import { Injectable } from '@angular/core' |
3 | | -import { BehaviorSubject, Observable, ReplaySubject } from 'rxjs' |
| 3 | +import { BehaviorSubject, EMPTY, Observable, ReplaySubject } from 'rxjs' |
4 | 4 | import { catchError, map, switchMap, tap, retry } from 'rxjs/operators' |
5 | 5 | import { AMOUNT_OF_RETRIEVE_NOTIFICATIONS_PER_CALL } from 'src/app/constants' |
6 | 6 | import { ERROR_REPORT } from 'src/app/errors' |
@@ -222,19 +222,24 @@ export class InboxService { |
222 | 222 | ) |
223 | 223 | } |
224 | 224 |
|
225 | | - private _fetchAndUpdateUnreadCount(): Observable<number> { |
| 225 | + private _fetchAndUpdateUnreadCount(): Observable<number | null> { |
226 | 226 | return this._http |
227 | | - .get<number>(runtimeEnvironment.BASE_URL + 'inbox/unreadCount.json', { |
228 | | - headers: this.headers, |
229 | | - }) |
| 227 | + .get<number>( |
| 228 | + `${runtimeEnvironment.BASE_URL}inbox/unreadCount.json`, |
| 229 | + { headers: this.headers, observe: 'response' } // full response |
| 230 | + ) |
230 | 231 | .pipe( |
231 | | - retry(3), |
232 | | - tap((count: number) => { |
233 | | - this._unreadCountSubject.next(count) |
| 232 | + map((resp) => { |
| 233 | + // If the server bounced us to /login, treat as “not logged in” |
| 234 | + return resp.url?.includes('/login') ? null : resp.body ?? null |
234 | 235 | }), |
235 | | - catchError((error) => |
236 | | - this._errorHandler.handleError(error, ERROR_REPORT.STANDARD_VERBOSE) |
237 | | - ) |
| 236 | + tap((count) => { |
| 237 | + if (count !== null) { |
| 238 | + // If we got a valid count, update the BehaviorSubject |
| 239 | + this._unreadCountSubject.next(count) |
| 240 | + } |
| 241 | + }), |
| 242 | + catchError(() => EMPTY) // If the request fails, we just return an empty observable |
238 | 243 | ) |
239 | 244 | } |
240 | 245 |
|
|
0 commit comments