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
|
import {HttpClient} from "@angular/common/http";
|
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, private httpClient: HttpClient) {
|
18
|
}
|
19
|
|
20
|
getActivePageContent(route: string) {
|
21
|
const url = this._helpServiceUrl + "/page/route?q=" + route;
|
22
|
console.log(`sending request at: ${url}`);
|
23
|
|
24
|
/*return this.http.get(url, {withCredentials: true})
|
25
|
.map(res => <PageContent>res.json() )
|
26
|
.catch(this.handleError);*/
|
27
|
return this.httpClient.get<PageContent>(url)
|
28
|
.catch(this.handleError);
|
29
|
}
|
30
|
|
31
|
private extractData(res: Response) {
|
32
|
let body = res.json();
|
33
|
return body.data || { };
|
34
|
}
|
35
|
|
36
|
private handleError (error: Response | any) {
|
37
|
// In a real world app, we might use a remote logging infrastructure
|
38
|
// We'd also dig deeper into the error to get a better message
|
39
|
let errMsg = "";
|
40
|
console.log(error);
|
41
|
if (error instanceof Response) {
|
42
|
const body = error.text() || '';
|
43
|
//const err = body.error || JSON.stringify(body);
|
44
|
errMsg = `${error.status} - ${error.statusText || ''} ${body}`;
|
45
|
} else {
|
46
|
errMsg = (error.message) ? error.message :
|
47
|
error.status ? `${error.status} - ${error.statusText}` : 'Server error';
|
48
|
console.error(errMsg); // log to console instead
|
49
|
}
|
50
|
return Observable.throw(errMsg);
|
51
|
}
|
52
|
}
|