|
1 |
| -import { HttpClient } from '@angular/common/http' |
| 1 | +import { HttpClient, HttpErrorResponse } from '@angular/common/http' |
2 | 2 | import { inject, Injectable } from '@angular/core'
|
3 | 3 | import type { Observable } from 'rxjs'
|
4 |
| -import { map, ReplaySubject, tap } from 'rxjs' |
| 4 | +import { catchError, map, of, ReplaySubject, Subject, takeUntil, tap } from 'rxjs' |
5 | 5 | import { naturalSort } from '../../utils'
|
6 |
| -import type { BriefOrganization, Organization, OrganizationsResponse } from './organization.types' |
| 6 | +import { UserService } from '../user' |
| 7 | +import type { BriefOrganization, Organization, OrganizationResponse, OrganizationsResponse } from './organization.types' |
| 8 | + |
7 | 9 |
|
8 | 10 | @Injectable({ providedIn: 'root' })
|
9 | 11 | export class OrganizationService {
|
10 | 12 | private _httpClient = inject(HttpClient)
|
| 13 | + private _userService = inject(UserService) |
11 | 14 | private _organizations = new ReplaySubject<BriefOrganization[]>(1)
|
| 15 | + private _currentOrganization = new ReplaySubject<Organization>(1) |
| 16 | + private readonly _unsubscribeAll$ = new Subject<void>() |
| 17 | + |
12 | 18 | organizations$ = this._organizations.asObservable()
|
| 19 | + currentOrganization$ = this._currentOrganization.asObservable() |
13 | 20 |
|
14 |
| - get(): Observable<Organization[]> { |
15 |
| - return this._get(false) as Observable<Organization[]> |
| 21 | + constructor() { |
| 22 | + // Fetch current org data whenever user org id changes |
| 23 | + this._userService.currentOrganizationId$.pipe(takeUntil(this._unsubscribeAll$)).subscribe((organizationId) => { |
| 24 | + this.getById(organizationId).subscribe() |
| 25 | + }) |
| 26 | + } |
| 27 | + |
| 28 | + get(org_id?: number): Observable<Organization[]> | Observable<Organization> { |
| 29 | + if (org_id) { |
| 30 | + return this.getById(org_id) |
| 31 | + } else { |
| 32 | + return this._get(false) as Observable<Organization[]> |
| 33 | + } |
16 | 34 | }
|
17 | 35 |
|
18 | 36 | getBrief(): Observable<BriefOrganization[]> {
|
19 | 37 | return this._get(true)
|
20 | 38 | }
|
21 | 39 |
|
| 40 | + getById(org_id: number): Observable<Organization> { |
| 41 | + const url = `/api/v3/organizations/${org_id}/` |
| 42 | + return this._httpClient.get<OrganizationResponse>(url).pipe( |
| 43 | + map((or) => { |
| 44 | + this._currentOrganization.next(or.organization) |
| 45 | + return or.organization |
| 46 | + }), |
| 47 | + catchError((error: HttpErrorResponse) => { |
| 48 | + // TODO need to figure out error handling |
| 49 | + console.error('Error occurred fetching organization: ', error.error) |
| 50 | + return of({} as Organization) |
| 51 | + }), |
| 52 | + ) |
| 53 | + } |
| 54 | + |
22 | 55 | private _get(brief = false): Observable<(BriefOrganization | Organization)[]> {
|
23 | 56 | const url = brief ? '/api/v3/organizations/?brief=true' : '/api/v3/organizations/'
|
24 | 57 | return this._httpClient.get<OrganizationsResponse>(url).pipe(
|
|
0 commit comments