Project

General

Profile

1
/**
2
 * Created by stefania on 7/17/17.
3
 */
4
import { Injectable } from '@angular/core';
5
import {Headers, Http, RequestOptions, 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
@Injectable()
11
export class HelpContentService {
12

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

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

    
19
  getActivePageContent(route: string) {
20
    const url = this._helpServiceUrl + "/page/route?q=" + route;
21
    console.log(`sending request at: ${url}`);
22

    
23
    return this.http.get(url)
24
        .map(res => <PageContent>res.json() )
25
        .catch(this.handleError);
26
  }
27

    
28
  private extractData(res: Response) {
29
    let body = res.json();
30
    return body.data || { };
31
  }
32

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