Project

General

Profile

1 55008 k.triantaf
import {Injectable} from '@angular/core';
2 57307 k.triantaf
import {HttpClient, HttpErrorResponse} from '@angular/common/http';
3 57311 k.triantaf
import {Observable, throwError} from 'rxjs';
4 55008 k.triantaf
import {CustomOptions} from './servicesUtils/customOptions.class';
5 57317 k.triantaf
import {CustomizationOptions} from '../connect/community/CustomizationOptions';
6 55008 k.triantaf
7
@Injectable()
8
export class LayoutService {
9 55964 argiro.kok
    constructor(private http: HttpClient) {
10 55008 k.triantaf
    }
11
12
    static removeNulls(obj) {
13
        const isArray = obj instanceof Array;
14
        for (let k in obj) {
15
            if (obj[k] === null || obj[k] === '') {
16
                isArray ? obj.splice(k, 1) : delete obj[k];
17
            } else if (typeof obj[k] === 'object') {
18
                LayoutService.removeNulls(obj[k]);
19
            }
20
        }
21
    }
22
23 57311 k.triantaf
    saveLayout(communityId: string, url: string, layout: CustomizationOptions): Observable<CustomizationOptions> {
24 55008 k.triantaf
        LayoutService.removeNulls(layout);
25 57311 k.triantaf
        return this.http.post<CustomizationOptions>(url
26
          + communityId + '/layout', layout, CustomOptions.getAuthOptionsWithBody());
27 55008 k.triantaf
    }
28
29 57311 k.triantaf
    getLayout(communityId: string, url: string): Observable<CustomizationOptions> {
30
        return this.http.get<CustomizationOptions>(url + communityId + '/layout');
31 55008 k.triantaf
    }
32
33 57307 k.triantaf
    mockLayout(): any {
34
        return this.http.get('./assets/customizationOptions.json') ;
35 56677 argiro.kok
36
    }
37
38 55964 argiro.kok
    private handleError(error: HttpErrorResponse) {
39 55008 k.triantaf
        // in a real world app, we may send the error to some remote logging infrastructure
40
        // instead of just logging it to the console
41
        console.error(error);
42 55964 argiro.kok
        return throwError(error.error || 'Server error');
43 55008 k.triantaf
    }
44
}