connect-admin/src/app/pages/divhelpcontent/div-help-content-form.compo...

166 lines
5.2 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 "../../domain/page";
import { DivId } from "../../domain/divId";
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: '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()){
console.info(this._router.url);
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()){
console.info(this._router.url);
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()){
console.info(this._router.url);
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()){
console.info(this._router.url);
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).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,
community: '',
_id : '',
});
}
public reset() {
this.myForm.patchValue({
divId: '',
content: '',
isActive: true,
community: '',
_id : ''
});
this.myForm.markAsPristine();
}
handleError(message: string, error) {
this.errorMessage = message;
console.log('Server responded: ' + error);
this.showLoading = false;
}
}