Project

General

Profile

1
/**
2
 * Created by stefania on 7/17/17.
3
 */
4
import { Injectable } from '@angular/core';
5
import { Http, Response } from '@angular/http';
6
import { Observable } from 'rxjs/Rx';
7
import { Content, PageContent } from "../domain/page-content";
8
import {helpServiceUrl} from "../domain/tempAPI";
9

    
10

    
11
@Injectable()
12
export class HelpContentService {
13

    
14
  private _helpServiceUrl = process.env.FAQ_ENDPOINT;
15
  /*private _helpServiceUrl = helpServiceUrl;*/
16

    
17
  constructor (private http: Http) {
18
  }
19

    
20
  cache : any = {};
21

    
22
  getActivePageContent(route: string) : Observable<PageContent> {
23
    if (!this.cache[route]) {
24
      this.cache[route] = this.http.get(this._helpServiceUrl + "/page/route?q=" + route)
25
        .map(res => <PageContent> res.json())
26
        .catch(this.handleError)
27
        .share();
28
    }
29
    return this.cache[route];
30
  }
31

    
32
  private extractData(res: Response) {
33
    let body = res.json();
34
    return body.data || { };
35
  }
36

    
37
  private handleError (error: Response | any) {
38
    // In a real world app, we might use a remote logging infrastructure
39
    // We'd also dig deeper into the error to get a better message
40
    let errMsg = "";
41
    console.log(error);
42
    if (error instanceof Response) {
43
      const body = error.text() || '';
44
      //const err = body.error || JSON.stringify(body);
45
      errMsg = `${error.status} - ${error.statusText || ''} ${body}`;
46
    } else {
47
      errMsg = (error.message) ? error.message :
48
        error.status ? `${error.status} - ${error.statusText}` : 'Server error';
49
      console.error(errMsg); // log to console instead
50
    }
51
    return Observable.throw(errMsg);
52
  }
53
}
(4-4/8)