openaire-library/services/layout.service.ts

55 lines
1.8 KiB
TypeScript
Raw Normal View History

import {Injectable} from '@angular/core';
import {HttpClient, HttpErrorResponse} from "@angular/common/http";
import {throwError} from 'rxjs';
import {CustomOptions} from './servicesUtils/customOptions.class';
import {catchError} from "rxjs/operators";
@Injectable()
export class LayoutService {
constructor(private http: HttpClient) {
}
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]);
}
}
}
saveLayout(pid: string, url: string, layout: any) {
LayoutService.removeNulls(layout);
return this.http.post(url + 'community/' + pid + '/updateLayout', JSON.stringify(layout), CustomOptions.getAuthOptionsWithBody())
//.map(res => res.json())
.pipe(catchError(this.handleError));
}
getLayout(pid: string, url: string) {
return this.http.get(url + 'community/' + pid + '/layout')
//.map(res => res.json())
.pipe(catchError(this.handleError));
}
loadDefaultLayout(pid: string, url: string) {
return this.http.post(url + 'community/' + pid + '/resetLayout', null, CustomOptions.getAuthOptionsWithBody())
//.map(res => res.json())
.pipe(catchError(this.handleError));
}
mockLayout():any {
return this.http.get("./assets/customizationOptions.json") ;
}
private handleError(error: HttpErrorResponse) {
// 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);
return throwError(error.error || 'Server error');
}
}