2019-06-07 14:51:26 +02:00
|
|
|
import {Injectable} from '@angular/core';
|
2019-06-11 15:33:32 +02:00
|
|
|
import {HttpClient, HttpHeaders} from '@angular/common/http';
|
2020-11-12 16:41:03 +01:00
|
|
|
import {BehaviorSubject, Observable, Subscriber} from 'rxjs';
|
2019-06-07 14:51:26 +02:00
|
|
|
import {Affiliation} from '../../utils/entities/CuratorInfo';
|
2020-08-11 14:16:50 +02:00
|
|
|
import {properties} from "../../../../environments/environment";
|
2022-07-04 20:38:27 +02:00
|
|
|
import {AdvancedAsyncSubject} from "../../utils/AdvancedAsyncSubject";
|
2019-06-07 14:51:26 +02:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class AffiliationService {
|
2020-08-11 14:16:50 +02:00
|
|
|
|
2019-06-07 14:51:26 +02:00
|
|
|
private affiliationsSubject: BehaviorSubject<Affiliation[]> = new BehaviorSubject([]);
|
2020-11-12 16:41:03 +01:00
|
|
|
sub;
|
2022-07-04 20:38:27 +02:00
|
|
|
|
2019-06-07 14:51:26 +02:00
|
|
|
constructor(private http: HttpClient) {
|
|
|
|
}
|
2022-07-04 20:38:27 +02:00
|
|
|
|
2020-11-12 16:41:03 +01:00
|
|
|
ngOnDestroy() {
|
|
|
|
this.clearSubscriptions();
|
|
|
|
}
|
2022-07-04 20:38:27 +02:00
|
|
|
|
|
|
|
clearSubscriptions() {
|
2020-11-12 16:41:03 +01:00
|
|
|
if (this.sub instanceof Subscriber) {
|
|
|
|
this.sub.unsubscribe();
|
|
|
|
}
|
|
|
|
}
|
2022-07-04 20:38:27 +02:00
|
|
|
|
2020-08-11 14:16:50 +02:00
|
|
|
public initAffiliations(communityId: string): void {
|
2022-07-04 20:38:27 +02:00
|
|
|
this.sub = this.getAffiliations(communityId).subscribe((affiliations) => {
|
2020-08-11 14:16:50 +02:00
|
|
|
this.affiliationsSubject.next(affiliations);
|
|
|
|
},
|
|
|
|
error => {
|
|
|
|
this.affiliationsSubject.error(error);
|
|
|
|
});
|
2019-06-07 14:51:26 +02:00
|
|
|
}
|
2020-08-11 14:16:50 +02:00
|
|
|
|
2019-06-07 14:51:26 +02:00
|
|
|
public get affiliations(): Observable<Affiliation[]> {
|
|
|
|
return this.affiliationsSubject.asObservable();
|
|
|
|
}
|
2020-08-11 14:16:50 +02:00
|
|
|
|
2022-07-04 20:38:27 +02:00
|
|
|
public getAffiliations(communityId: string): Observable<Affiliation[]> {
|
|
|
|
let url = properties.communityAPI + communityId + "/organizations";
|
|
|
|
return this.http.get<Affiliation[]>((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url);
|
|
|
|
}
|
|
|
|
|
2019-06-11 15:33:32 +02:00
|
|
|
public updateAffiliation(url: string, affiliation: Affiliation) {
|
2020-08-11 14:16:50 +02:00
|
|
|
let headers = new HttpHeaders({'Content-Type': 'application/json', 'accept': 'application/json'});
|
2019-06-11 15:33:32 +02:00
|
|
|
return this.http.post<Affiliation>(url, JSON.stringify(affiliation), {headers: headers});
|
|
|
|
}
|
2020-08-11 14:16:50 +02:00
|
|
|
|
2019-06-11 15:33:32 +02:00
|
|
|
public deleteAffiliation(url: string, id: string) {
|
2020-08-11 14:16:50 +02:00
|
|
|
let headers = new HttpHeaders({'Content-Type': 'application/json', 'accept': 'application/json'});
|
|
|
|
return this.http.request('delete', url, {body: id, headers: headers})
|
2019-06-07 14:51:26 +02:00
|
|
|
}
|
2020-08-11 14:16:50 +02:00
|
|
|
|
2019-06-07 14:51:26 +02:00
|
|
|
}
|