openaire-library/dashboard/divhelpcontent/div-help-content-form.compo...

162 lines
5.0 KiB
TypeScript

import { Component, OnInit, Input } from '@angular/core';
import { ActivatedRoute, Router } from "@angular/router";
import { FormGroup, FormBuilder, Validators } from "@angular/forms";
import { Page } from "../../utils/entities/adminTool/page";
import { DivId } from "../../utils/entities/adminTool/divId";
import { HelpContentService } from "../../services/help-content.service";
import { EnvProperties } from '../../utils/properties/env-properties';
import {Session} from '../../login/utils/helper.class';
import {LoginErrorCodes} from '../../login/utils/guardHelper.class';
@Component({
selector: 'div-content-form',
templateUrl: './div-help-content-form.component.html',
})
export class DivContentFormComponent implements OnInit{
@Input('group')
myForm: FormGroup;
@Input('communityPid')
communityPid: string;
@Input('pageId')
pageId: string;
@Input('editMode')
editMode: boolean = false;
//public divIdName: string = '';
private communityId: string = '';
showPageSelect: boolean = true;
selectedDiv: DivId;
private availablePages : Page[] = [];
private availableDivs : DivId[] = [];
private ckeditorContent : string;
public properties:EnvProperties = null;
public showLoading: boolean = true;
public errorMessage: string = '';
@Input() updateErrorMessage: string = '';
constructor(private route: ActivatedRoute, private _router: Router, private _fb: FormBuilder, private _helpContentService: HelpContentService){}
ngOnInit() {
this.myForm = this.form;
this.route.data
.subscribe((data: { envSpecific: EnvProperties }) => {
this.properties = data.envSpecific;
this.route.queryParams.subscribe(params => {
if(!Session.isLoggedIn()){
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
} else {
if(this.pageId) {
this.showPageSelect = false;
this.getDivs(this.pageId);
} else {
if(!this.editMode) {
this._helpContentService.getCommunityPagesWithDivId(this.communityPid, this.properties.adminToolsAPIURL).subscribe(
pages => {
this.availablePages = pages;
this.showLoading = false;
},
error => this.handleError('System error retrieving pages', error));
}
}
this.getCommunity(this.communityPid);
}
});
});
}
public pageSelected(event) {
if(!Session.isLoggedIn()){
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
} else {
this.getDivs(event.target.value);
}
}
public divIdSelected(div: DivId) {
this.selectedDiv = div;
}
public getCommunity(communityPid: 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._helpContentService.getCommunity(this.communityPid, this.properties.adminToolsAPIURL).subscribe(
community => {
this.communityId = community._id;
this.myForm.patchValue({
community: this.communityId
});
this.showLoading = false;
},
error => this.handleError('System error retrieving community', error)
);
}
}
public getDivs(pageId: 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._helpContentService.getDivIdsFull(pageId, this.properties.adminToolsAPIURL, this.communityPid).subscribe(
divs => {
this.availableDivs = divs;
this.pageId = pageId;
this.showLoading = false;
},
error => this.handleError('System error retrieving pages', error));
}
}
// public selectedDiv(event) {
// console.info(event.target.value);
// }
public get form() {
return this._fb.group({
divId: ['', Validators.required],
content: ['', Validators.required],
isActive: true,
portal: '',
_id : '',
});
}
public reset() {
this.myForm.patchValue({
divId: '',
content: '',
isActive: true,
portal: '',
_id : ''
});
this.myForm.markAsPristine();
}
handleError(message: string, error) {
this.errorMessage = message;
console.log('Server responded: ' + error);
this.showLoading = false;
}
}