138 lines
4.9 KiB
TypeScript
138 lines
4.9 KiB
TypeScript
/**
|
|
* Created by stefania on 7/14/17.
|
|
*/
|
|
import { Component, ViewChild, OnInit, OnDestroy, ElementRef } from '@angular/core';
|
|
import { PageContentFormComponent } from "./page-help-content-form.component";
|
|
import { Subscription } from "rxjs";
|
|
import { HelpContentService } from "../../services/help-content.service";
|
|
import { PageHelpContent } from "../../domain/page-help-content";
|
|
import { ActivatedRoute, Router } from "@angular/router";
|
|
import { EnvProperties } from '../../openaireLibrary/utils/properties/env-properties';
|
|
|
|
import {Session} from '../../openaireLibrary/login/utils/helper.class';
|
|
import {LoginErrorCodes} from '../../openaireLibrary/login/utils/guardHelper.class';
|
|
import {HelperFunctions} from "../../openaireLibrary/utils/HelperFunctions.class";
|
|
|
|
@Component({
|
|
selector: 'edit-page-help-content',
|
|
templateUrl: 'edit-page-help-content.component.html',
|
|
})
|
|
|
|
export class EditPageHelpContentComponent implements OnInit, OnDestroy{
|
|
|
|
@ViewChild(PageContentFormComponent)
|
|
public formComponent : PageContentFormComponent;
|
|
|
|
private communityPid: string;
|
|
|
|
private pageId: string;
|
|
|
|
private sub: Subscription;
|
|
|
|
private pageHelpContent: PageHelpContent;
|
|
|
|
//private errorMessage : string = null;
|
|
public properties:EnvProperties = null;
|
|
|
|
public showLoading: boolean = true;
|
|
public errorMessage: string = '';
|
|
public updateErrorMessage: string = '';
|
|
|
|
constructor(
|
|
private element: ElementRef,
|
|
private route: ActivatedRoute,
|
|
private router: Router,
|
|
private _helpContentService: HelpContentService) {}
|
|
|
|
ngOnInit() {
|
|
this.route.data
|
|
.subscribe((data: { envSpecific: EnvProperties }) => {
|
|
this.properties = data.envSpecific;
|
|
this.sub = this.route.queryParams.subscribe(params => {
|
|
HelperFunctions.scroll();
|
|
|
|
let pageContentId = params['pageContentId'];
|
|
this.communityPid = params['communityId'];
|
|
this.pageId = params['pageId'];
|
|
|
|
this.getPageHelpContent(pageContentId);
|
|
});
|
|
});
|
|
}
|
|
ngOnDestroy() {
|
|
this.sub.unsubscribe();
|
|
}
|
|
|
|
handleError(message: string, error) {
|
|
this.errorMessage = message;
|
|
console.log('Server responded: ' + error);
|
|
this.showLoading = false;
|
|
}
|
|
|
|
handleUpdateError(message: string, error) {
|
|
this.updateErrorMessage = message;
|
|
console.log('Server responded: ' + error);
|
|
this.showLoading = false;
|
|
}
|
|
|
|
private getPageHelpContent(pageContentId: string) {
|
|
if(!Session.isLoggedIn()){
|
|
this.router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this.router.url} });
|
|
} else {
|
|
this.showLoading = true;
|
|
this.errorMessage = "";
|
|
this.updateErrorMessage = "";
|
|
|
|
this._helpContentService.getPageHelpContent(pageContentId as string, this.properties.adminToolsAPIURL).subscribe(
|
|
pageHelpContent => {
|
|
this.updateForm(pageHelpContent);
|
|
this.showLoading = false;
|
|
},
|
|
error => this.handleError('System error retrieving page help content', error));
|
|
}
|
|
}
|
|
|
|
private updateForm(pageHelpContent : PageHelpContent) {
|
|
this.pageHelpContent = pageHelpContent;
|
|
this.formComponent.myForm.patchValue((pageHelpContent));
|
|
// console.log("patching",pageHelpContent);
|
|
}
|
|
|
|
private saveCustom() {
|
|
if(!Session.isLoggedIn()){
|
|
this.router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this.router.url} });
|
|
} else {
|
|
if(this.formComponent.myForm.valid) {
|
|
this.showLoading = true;
|
|
this.updateErrorMessage = "";
|
|
|
|
let pageHelpContent : PageHelpContent = this.formComponent.myForm.value;
|
|
this._helpContentService.updatePageHelpContent(pageHelpContent, this.properties.adminToolsAPIURL).subscribe(
|
|
_ => {
|
|
if(this.pageId) {
|
|
this.router.navigate( ['/pageContents/'], { queryParams: { "communityId": this.communityPid, "pageId": this.pageId } } );
|
|
} else {
|
|
this.router.navigate(['/pageContents'], { queryParams: { "communityId": this.communityPid} } );
|
|
}
|
|
this.showLoading = false;
|
|
},
|
|
err => this.handleUpdateError('System error updating page content', err)
|
|
);
|
|
} else {
|
|
this.errorMessage = "Please fill all required fields";
|
|
}
|
|
}
|
|
}
|
|
|
|
private cancelCustom() {
|
|
this.errorMessage = "";
|
|
this.updateErrorMessage = "";
|
|
|
|
if(this.pageId) {
|
|
this.router.navigate( ['/pageContents/'], { queryParams: { "communityId": this.communityPid, "pageId": this.pageId } } );
|
|
} else {
|
|
this.router.navigate(['/pageContents'], { queryParams: { "communityId": this.communityPid} } );
|
|
}
|
|
}
|
|
}
|