import { Component, ViewChild, ElementRef } from '@angular/core'; import { ActivatedRoute, Router } from "@angular/router"; import { DivContentFormComponent } from "./div-help-content-form.component"; import { DivHelpContent } from "../../domain/div-help-content"; import { HelpContentService } from "../../services/help-content.service"; import { EnvProperties } from '../../openaireLibrary/utils/properties/env-properties'; import {Session} from '../../openaireLibrary/login/utils/helper.class'; import {LoginErrorCodes} from '../../openaireLibrary/login/utils/guardHelper.class'; @Component({ selector: 'new-div-help-content', templateUrl: 'new-div-help-content.component.html', }) export class NewDivHelpContentComponent { @ViewChild(DivContentFormComponent) public formComponent : DivContentFormComponent; //private errorMessage : string = null; private communityPid: string; private pageId: string; public properties:EnvProperties = null; public showLoading: boolean = false; 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.route.queryParams.subscribe(params => { this.scroll(); this.communityPid = params['communityId']; this.pageId = params['pageId']; }); }); } 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 divHelpContent : DivHelpContent = this.formComponent.myForm.value; this._helpContentService.insertOrUpdateDivHelpContent(divHelpContent, this.properties.adminToolsAPIURL).subscribe( _ => { if(this.pageId) { this.router.navigate( ['/classContents/'], { queryParams: { "communityId": this.communityPid, "pageId": this.pageId } } ); } else { this.router.navigate(['/classContents'], { queryParams: { "communityId": this.communityPid } } ); } this.showLoading = false; }, err => this.handleUpdateError('System error saving page content', err) ); } else { this.errorMessage = "Please fill all required fields"; } } } private cancelCustom() { if(this.pageId) { this.router.navigate( ['/classContents/'], { queryParams: { "communityId": this.communityPid, "pageId": this.pageId } } ); } else { this.router.navigate(['/classContents'], { queryParams: { "communityId": this.communityPid } } ); } } handleUpdateError(message: string, error) { this.errorMessage = message; console.log('Server responded: ' + error); this.showLoading = false; } public scroll() { if (typeof document !== 'undefined') { this.element.nativeElement.scrollIntoView(); } } }