Skip to content

Commit 64f6faa

Browse files
committed
organization backend now active
1 parent dbffdeb commit 64f6faa

File tree

4 files changed

+66
-14
lines changed

4 files changed

+66
-14
lines changed

src/@seed/api/organization/organization.service.ts

+38-5
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,57 @@
1-
import { HttpClient } from '@angular/common/http'
1+
import { HttpClient, HttpErrorResponse } from '@angular/common/http'
22
import { inject, Injectable } from '@angular/core'
33
import type { Observable } from 'rxjs'
4-
import { map, ReplaySubject, tap } from 'rxjs'
4+
import { catchError, map, of, ReplaySubject, Subject, takeUntil, tap } from 'rxjs'
55
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+
79

810
@Injectable({ providedIn: 'root' })
911
export class OrganizationService {
1012
private _httpClient = inject(HttpClient)
13+
private _userService = inject(UserService)
1114
private _organizations = new ReplaySubject<BriefOrganization[]>(1)
15+
private _currentOrganization = new ReplaySubject<Organization>(1)
16+
private readonly _unsubscribeAll$ = new Subject<void>()
17+
1218
organizations$ = this._organizations.asObservable()
19+
currentOrganization$ = this._currentOrganization.asObservable()
1320

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+
}
1634
}
1735

1836
getBrief(): Observable<BriefOrganization[]> {
1937
return this._get(true)
2038
}
2139

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+
2255
private _get(brief = false): Observable<(BriefOrganization | Organization)[]> {
2356
const url = brief ? '/api/v3/organizations/?brief=true' : '/api/v3/organizations/'
2457
return this._httpClient.get<OrganizationsResponse>(url).pipe(

src/@seed/api/organization/organization.types.ts

+4
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ export type Organization = BriefOrganization & {
7373
default_reports_y_axis_options: Column[];
7474
require_2fa: boolean;
7575
}
76+
export type OrganizationResponse = {
77+
status: string;
78+
organization: Organization;
79+
}
7680
export type OrganizationsResponse = {
7781
organizations: (BriefOrganization | Organization)[];
7882
}
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
<div class="prose">Settings Content</div>
1+
2+
<div class="prose">
3+
@if (organization) {
4+
Organization: {{organization.name}}
5+
}
6+
</div>
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
1-
import type { OnInit } from '@angular/core'
2-
import { Component, ViewEncapsulation } from '@angular/core'
1+
import { Component, inject, type OnDestroy, type OnInit } from '@angular/core'
32
import { MatIconModule } from '@angular/material/icon'
3+
import { Subject, takeUntil } from 'rxjs'
4+
import { type Organization, OrganizationService } from '@seed/api/organization'
5+
import { SharedImports } from '@seed/directives'
46

57
@Component({
68
selector: 'seed-organizations-settings',
79
templateUrl: './organizations-settings.component.html',
8-
encapsulation: ViewEncapsulation.None,
9-
imports: [
10-
MatIconModule,
11-
],
10+
imports: [SharedImports, MatIconModule],
1211
})
13-
export class OrganizationsSettingsComponent implements OnInit {
12+
export class OrganizationsSettingsComponent implements OnInit, OnDestroy {
13+
private _organizationService = inject(OrganizationService)
14+
organization: Organization
15+
private readonly _unsubscribeAll$ = new Subject<void>()
16+
1417
ngOnInit(): void {
15-
console.log('organizations settings')
18+
this._organizationService.currentOrganization$.pipe(takeUntil(this._unsubscribeAll$)).subscribe((o) => {
19+
this.organization = o
20+
})
21+
}
22+
23+
ngOnDestroy(): void {
24+
this._unsubscribeAll$.next()
25+
this._unsubscribeAll$.complete()
1626
}
1727
}

0 commit comments

Comments
 (0)