Project

General

Profile

1
import {Injectable} from '@angular/core';
2
import {Http, Response} from '@angular/http';
3
import {HttpClient, HttpErrorResponse} from "@angular/common/http";
4
import {Observable, throwError} from 'rxjs';
5
import {CustomOptions} from './servicesUtils/customOptions.class';
6
import {catchError} from "rxjs/operators";
7

    
8
@Injectable()
9
export class LayoutService {
10
    constructor(private http: HttpClient) {
11
    }
12

    
13
    static removeNulls(obj) {
14
        const isArray = obj instanceof Array;
15
        for (let k in obj) {
16
            if (obj[k] === null || obj[k] === '') {
17
                isArray ? obj.splice(k, 1) : delete obj[k];
18
            } else if (typeof obj[k] === 'object') {
19
                LayoutService.removeNulls(obj[k]);
20
            }
21
        }
22
    }
23

    
24
    saveLayout(pid: string, url: string, layout: any) {
25
        LayoutService.removeNulls(layout);
26
        return this.http.post(url + 'community/' + pid + '/updateLayout', JSON.stringify(layout), CustomOptions.getAuthOptionsWithBody())
27
            //.map(res => res.json())
28
            .pipe(catchError(this.handleError));
29
    }
30

    
31
    getLayout(pid: string, url: string) {
32
        return this.http.get(url + 'community/' + pid + '/layout')
33
            //.map(res => res.json())
34
            .pipe(catchError(this.handleError));
35
    }
36

    
37
    loadDefaultLayout(pid: string, url: string) {
38
        return this.http.post(url + 'community/' + pid + '/resetLayout', null, CustomOptions.getAuthOptionsWithBody())
39
            //.map(res => res.json())
40
            .pipe(catchError(this.handleError));
41
    }
42

    
43
    private handleError(error: HttpErrorResponse) {
44
        // in a real world app, we may send the error to some remote logging infrastructure
45
        // instead of just logging it to the console
46
        console.error(error);
47
        return throwError(error.error || 'Server error');
48
    }
49
}
(4-4/23)