Project

General

Profile

1
import {Injectable} from '@angular/core';
2
import {HttpClient, HttpHeaders} from '@angular/common/http';
3
import {BehaviorSubject, Observable} from 'rxjs';
4
import {Curator} from '../../utils/entities/CuratorInfo';
5
import {EnvProperties} from '../../utils/properties/env-properties';
6
import {COOKIE} from '../../login/utils/helper.class';
7

    
8
@Injectable()
9
export class CuratorService {
10

    
11
  private curatorsSubject: BehaviorSubject<Curator[]> = new BehaviorSubject([]);
12

    
13
  constructor(private http: HttpClient) {
14
  }
15

    
16
  public initCurators(properties: EnvProperties, url: string): void {
17
    this.http.get<Curator[]>((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url).
18
            subscribe((curators) => {
19
      this.curatorsSubject.next(curators);
20
    });
21
  }
22

    
23
  public get curators(): Observable<Curator[]> {
24
    return this.curatorsSubject.asObservable();
25
  }
26

    
27
  public updateCurators(curator: Curator) {
28
    const curators = this.curatorsSubject.value;
29
    if (curators && curator) {
30
      curator._id = null;
31
      curators.push(curator);
32
      this.curatorsSubject.next(curators);
33
    }
34
  }
35

    
36
  public updateCurator(url: string, curator: Curator) {
37
    return this.http.post<Curator>(url, curator, this.getAuthOptions());
38
  }
39

    
40
  public getCurator(properties: EnvProperties, url: string): Observable<Curator> {
41
    return this.http.get<Curator>((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url);
42
  }
43

    
44
  // TODO remove and use CustomOptions.getAuthOptions()
45
  private getAuthOptions(): any {
46
     const httpOptions = {
47
      headers: new HttpHeaders({
48
        'X-XSRF-TOKEN': COOKIE.getCookie(COOKIE.cookieName_id),
49
      }), withCredentials: true
50
    };
51
    return httpOptions;
52
  }
53
}
    (1-1/1)