2019-03-18 16:02:46 +01:00
|
|
|
import {Injectable} from '@angular/core';
|
2019-10-08 12:56:04 +02:00
|
|
|
import {HttpClient, HttpErrorResponse} from '@angular/common/http';
|
2019-10-08 15:37:11 +02:00
|
|
|
import {Observable, throwError} from 'rxjs';
|
2019-03-18 16:02:46 +01:00
|
|
|
import {CustomOptions} from './servicesUtils/customOptions.class';
|
2019-10-08 16:16:15 +02:00
|
|
|
import {CustomizationOptions} from '../connect/community/CustomizationOptions';
|
2019-03-18 16:02:46 +01:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class LayoutService {
|
2019-06-03 15:20:36 +02:00
|
|
|
constructor(private http: HttpClient) {
|
2019-03-18 16:02:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static removeNulls(obj) {
|
|
|
|
const isArray = obj instanceof Array;
|
|
|
|
for (let k in obj) {
|
|
|
|
if (obj[k] === null || obj[k] === '') {
|
|
|
|
isArray ? obj.splice(k, 1) : delete obj[k];
|
|
|
|
} else if (typeof obj[k] === 'object') {
|
|
|
|
LayoutService.removeNulls(obj[k]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-08 15:37:11 +02:00
|
|
|
saveLayout(communityId: string, url: string, layout: CustomizationOptions): Observable<CustomizationOptions> {
|
2019-03-18 16:02:46 +01:00
|
|
|
LayoutService.removeNulls(layout);
|
2019-10-08 15:37:11 +02:00
|
|
|
return this.http.post<CustomizationOptions>(url
|
|
|
|
+ communityId + '/layout', layout, CustomOptions.getAuthOptionsWithBody());
|
2019-03-18 16:02:46 +01:00
|
|
|
}
|
|
|
|
|
2019-10-08 15:37:11 +02:00
|
|
|
getLayout(communityId: string, url: string): Observable<CustomizationOptions> {
|
|
|
|
return this.http.get<CustomizationOptions>(url + communityId + '/layout');
|
2019-03-18 16:02:46 +01:00
|
|
|
}
|
2019-07-22 16:15:23 +02:00
|
|
|
|
2019-10-08 12:56:04 +02:00
|
|
|
mockLayout(): any {
|
|
|
|
return this.http.get('./assets/customizationOptions.json') ;
|
2019-07-22 16:15:23 +02:00
|
|
|
|
|
|
|
}
|
2019-03-18 16:02:46 +01:00
|
|
|
|
2019-06-03 15:20:36 +02:00
|
|
|
private handleError(error: HttpErrorResponse) {
|
2019-03-18 16:02:46 +01:00
|
|
|
// in a real world app, we may send the error to some remote logging infrastructure
|
|
|
|
// instead of just logging it to the console
|
|
|
|
console.error(error);
|
2019-06-03 15:20:36 +02:00
|
|
|
return throwError(error.error || 'Server error');
|
2019-03-18 16:02:46 +01:00
|
|
|
}
|
|
|
|
}
|