Project

General

Profile

1
import {Injectable} from '@angular/core';
2
import {HttpClient, HttpHeaders} from '@angular/common/http';
3
import {BehaviorSubject, Observable, Subscriber} from 'rxjs';
4
import {Affiliation} from '../../utils/entities/CuratorInfo';
5
import {properties} from "../../../../environments/environment";
6

    
7
@Injectable()
8
export class AffiliationService {
9
  
10
  private affiliationsSubject: BehaviorSubject<Affiliation[]> = new BehaviorSubject([]);
11
  sub;
12
  constructor(private http: HttpClient) {
13
  }
14
  ngOnDestroy() {
15
    this.clearSubscriptions();
16
  }
17
  clearSubscriptions(){
18
    if (this.sub instanceof Subscriber) {
19
      this.sub.unsubscribe();
20
    }
21
  }
22
  public initAffiliations(communityId: string): void {
23
    let url = properties.communityAPI + communityId + "/organizations";
24
    this.sub = this.http.get<Affiliation[]>((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url).subscribe((affiliations) => {
25
        this.affiliationsSubject.next(affiliations);
26
      },
27
      error => {
28
        this.affiliationsSubject.error(error);
29
      });
30
  }
31
  
32
  public get affiliations(): Observable<Affiliation[]> {
33
    return this.affiliationsSubject.asObservable();
34
  }
35
  
36
  public updateAffiliation(url: string, affiliation: Affiliation) {
37
    let headers = new HttpHeaders({'Content-Type': 'application/json', 'accept': 'application/json'});
38
    return this.http.post<Affiliation>(url, JSON.stringify(affiliation), {headers: headers});
39
  }
40
  
41
  public deleteAffiliation(url: string, id: string) {
42
    let headers = new HttpHeaders({'Content-Type': 'application/json', 'accept': 'application/json'});
43
    return this.http.request('delete', url, {body: id, headers: headers})
44
  }
45
  
46
}
    (1-1/1)