Project

General

Profile

1
import {Injectable} from '@angular/core';
2
import {HttpClient, HttpErrorResponse} from '@angular/common/http';
3
import {Observable, throwError} from 'rxjs';
4
import {CustomOptions} from './servicesUtils/customOptions.class';
5
import {CustomizationOptions} from '../connect/community/CustomizationOptions';
6

    
7
@Injectable()
8
export class LayoutService {
9
    constructor(private http: HttpClient) {
10
    }
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
    saveLayout(communityId: string, url: string, layout: CustomizationOptions): Observable<CustomizationOptions> {
24
        LayoutService.removeNulls(layout);
25
        return this.http.post<CustomizationOptions>(url
26
          + communityId + '/layout', layout, CustomOptions.getAuthOptionsWithBody());
27
    }
28

    
29
    getLayout(communityId: string, url: string): Observable<CustomizationOptions> {
30
        return this.http.get<CustomizationOptions>(url + communityId + '/layout');
31
    }
32

    
33
    mockLayout(): any {
34
        return this.http.get('./assets/customizationOptions.json') ;
35

    
36
    }
37

    
38
    private handleError(error: HttpErrorResponse) {
39
        // 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
        return throwError(error.error || 'Server error');
43
    }
44
}
(4-4/21)