2021-02-25 16:55:56 +01:00
|
|
|
import {Component, Input, OnInit} from '@angular/core';
|
|
|
|
import {ActivatedRoute, Router} from '@angular/router';
|
|
|
|
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
|
|
|
|
import {Page} from '../../utils/entities/adminTool/page';
|
|
|
|
import {HelpContentService} from '../../services/help-content.service';
|
|
|
|
import {EnvProperties} from '../../utils/properties/env-properties';
|
2019-12-20 12:38:13 +01:00
|
|
|
|
|
|
|
import {Session} from '../../login/utils/helper.class';
|
|
|
|
import {LoginErrorCodes} from '../../login/utils/guardHelper.class';
|
2021-02-25 16:55:56 +01:00
|
|
|
import {properties} from '../../../../environments/environment';
|
2021-02-26 11:36:17 +01:00
|
|
|
import {from, Observable, Subscriber, Subscription} from 'rxjs';
|
2021-02-25 16:55:56 +01:00
|
|
|
import {HelperFunctions} from '../../utils/HelperFunctions.class';
|
|
|
|
import {PageHelpContent} from '../../utils/entities/adminTool/page-help-content';
|
2019-12-20 12:38:13 +01:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'page-content-form',
|
|
|
|
templateUrl: './page-help-content-form.component.html',
|
|
|
|
})
|
|
|
|
|
|
|
|
export class PageContentFormComponent implements OnInit{
|
|
|
|
|
|
|
|
myForm: FormGroup;
|
|
|
|
communityPid: string;
|
|
|
|
pageId: string;
|
2021-02-25 16:55:56 +01:00
|
|
|
pageContentId: string;
|
2019-12-20 12:38:13 +01:00
|
|
|
page: Page;
|
2021-02-25 16:55:56 +01:00
|
|
|
placementsOptions = [];
|
|
|
|
orderOptions = [];
|
2019-12-20 12:38:13 +01:00
|
|
|
|
|
|
|
private availablePages : Page[] = [];
|
|
|
|
//private errorMessage: string;
|
|
|
|
|
|
|
|
private ckeditorContent : string;
|
|
|
|
public properties:EnvProperties = null;
|
|
|
|
|
|
|
|
public showLoading: boolean = true;
|
|
|
|
public errorMessage: string = '';
|
|
|
|
@Input() updateErrorMessage: string = '';
|
2020-12-18 16:09:38 +01:00
|
|
|
private subs: Subscription[] = [];
|
2021-02-25 16:55:56 +01:00
|
|
|
private pageHelpContent: PageHelpContent;
|
2019-12-20 12:38:13 +01:00
|
|
|
constructor(private route: ActivatedRoute, private _router: Router, private _fb: FormBuilder, private _helpContentService: HelpContentService){}
|
|
|
|
|
|
|
|
ngOnInit() {
|
2020-12-18 16:09:38 +01:00
|
|
|
|
2021-02-25 16:55:56 +01:00
|
|
|
this.properties = properties;
|
|
|
|
this.subs.push(this.route.params.subscribe(params => {
|
|
|
|
this.communityPid = params['community'];
|
|
|
|
this.subs.push(this.route.queryParams.subscribe(params => {
|
|
|
|
HelperFunctions.scroll();
|
|
|
|
this.pageId = params['pageId'];
|
|
|
|
this.myForm = this.form;
|
|
|
|
this.pageContentId = params['pageContentId'];
|
|
|
|
if (!this.pageId) {
|
|
|
|
this._router.navigate(['../'], {relativeTo: this.route});
|
|
|
|
}
|
2021-02-26 11:36:17 +01:00
|
|
|
/* this.getPage(this.pageId);
|
2021-02-25 16:55:56 +01:00
|
|
|
if (this.pageContentId) {
|
|
|
|
this.getPageHelpContent(this.pageContentId);
|
|
|
|
}else{
|
|
|
|
this.myForm = this.form;
|
|
|
|
|
2021-02-26 11:36:17 +01:00
|
|
|
}*/
|
|
|
|
this.getInfo(this.pageId);
|
2021-02-25 16:55:56 +01:00
|
|
|
|
|
|
|
if (!Session.isLoggedIn()) {
|
|
|
|
this._router.navigate(['/user-info'], {
|
|
|
|
queryParams: {
|
|
|
|
"errorCode": LoginErrorCodes.NOT_VALID,
|
|
|
|
"redirectUrl": this._router.url
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}));
|
|
|
|
}));
|
2020-12-18 16:09:38 +01:00
|
|
|
}
|
|
|
|
ngOnDestroy() {
|
|
|
|
this.subs.forEach(value => {
|
|
|
|
if (value instanceof Subscriber) {
|
|
|
|
value.unsubscribe();
|
|
|
|
}
|
2019-12-20 12:38:13 +01:00
|
|
|
});
|
|
|
|
}
|
2021-02-26 11:36:17 +01:00
|
|
|
getInfo(pageId: string){
|
|
|
|
this.showLoading = true;
|
|
|
|
let obs = Observable.zip(this._helpContentService.getPageByPortal(pageId,this.properties.adminToolsAPIURL, this.communityPid), this._helpContentService.getCommunityPageHelpContents(this.communityPid, this.properties.adminToolsAPIURL, pageId));
|
|
|
|
this.subs.push(obs.subscribe(
|
|
|
|
results => {
|
|
|
|
this.page = results[0];
|
|
|
|
if(this.properties.adminToolsPortalType != this.page.portalType) {
|
|
|
|
this._router.navigate(['../'], {relativeTo: this.route});
|
|
|
|
}
|
|
|
|
let countPageContents = results[1]?results[1].length:0;
|
|
|
|
console.log(results[1]);
|
|
|
|
for (let content of (results[1] as Array<PageHelpContent>)) {
|
|
|
|
// if(content.page['_id'] == pageId){
|
|
|
|
// countPageContents++;
|
|
|
|
if(this.pageContentId && this.pageContentId == content._id) {
|
|
|
|
this.pageHelpContent = content;
|
|
|
|
// this.pageHelpContent.page = pageId;
|
|
|
|
// this.pageHelpContent.portal = this.communityPid;
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
this.setOptions(this.page, countPageContents + (this.pageHelpContent?0:1));
|
|
|
|
if(!this.pageContentId) {
|
|
|
|
this.myForm = this.form;
|
|
|
|
this.initFormWithSelectOptions();
|
|
|
|
}else{
|
|
|
|
this.updateForm(this.pageHelpContent);
|
|
|
|
}
|
|
|
|
this.showLoading = false;
|
|
|
|
},
|
|
|
|
error => this.handleError('System error retrieving page with id: '+pageId, error)
|
|
|
|
));
|
|
|
|
|
|
|
|
}
|
|
|
|
/*private getPage(pageId: string) {
|
2021-02-25 16:55:56 +01:00
|
|
|
this.subs.push(this._helpContentService.getPageByPortal(pageId,this.properties.adminToolsAPIURL, this.communityPid).subscribe(
|
|
|
|
page => {
|
|
|
|
if(this.properties.adminToolsPortalType != page.portalType) {
|
|
|
|
this._router.navigate(['../'], {relativeTo: this.route});
|
|
|
|
} else {
|
|
|
|
this.page = page;
|
|
|
|
this.getPageContents(pageId);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
error => this.handleError('System error retrieving page with id: '+pageId, error)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
private getPageContents(pageId: string) {
|
|
|
|
this.subs.push(this._helpContentService.getCommunityPageHelpContents(this.communityPid, this.properties.adminToolsAPIURL).subscribe(
|
|
|
|
pageHelpContents => {
|
|
|
|
let countPageContents = 1;
|
|
|
|
for (let content of (pageHelpContents as Array<PageHelpContent>)) {
|
|
|
|
if(content.page['_id'] == pageId){
|
|
|
|
countPageContents++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.setOptions(this.page, countPageContents);
|
|
|
|
if(!this.pageContentId) {
|
|
|
|
this.showLoading = false;
|
|
|
|
this.initFormWithSelectOptions();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
error => this.handleError('System error retrieving page contents with id: ', error)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
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.subs.push(this._helpContentService.getPageHelpContent(pageContentId as string, this.properties.adminToolsAPIURL, this.communityPid).subscribe(
|
|
|
|
pageHelpContent => {
|
|
|
|
this.updateForm(pageHelpContent);
|
|
|
|
this.showLoading = false;
|
|
|
|
},
|
|
|
|
error => this.handleError('System error retrieving page help content', error)));
|
|
|
|
}
|
2021-02-26 11:36:17 +01:00
|
|
|
}*/
|
2021-02-25 16:55:56 +01:00
|
|
|
private updateForm(pageHelpContent : PageHelpContent) {
|
|
|
|
this.pageHelpContent = pageHelpContent;
|
|
|
|
this.myForm = this.form;
|
|
|
|
if(this.pageHelpContent) {
|
|
|
|
this.myForm.patchValue((pageHelpContent));
|
|
|
|
}else{
|
|
|
|
this.initFormWithSelectOptions();
|
|
|
|
}
|
|
|
|
if(this.orderOptions.length ==1){
|
|
|
|
this.myForm.get('order').disable()
|
|
|
|
}
|
|
|
|
if(this.placementsOptions.length ==1){
|
|
|
|
this.myForm.get('placement').disable()
|
|
|
|
}
|
|
|
|
this.myForm.markAsPristine();
|
|
|
|
|
|
|
|
}
|
|
|
|
initFormWithSelectOptions(){
|
|
|
|
if (this.placementsOptions.length == 1) {
|
|
|
|
this.myForm.get("placement").setValue(this.placementsOptions[0].value);
|
|
|
|
}
|
|
|
|
this.myForm.get("order").setValue(this.orderOptions[this.orderOptions.length - 1].value);
|
|
|
|
if(this.orderOptions.length ==1){
|
|
|
|
this.myForm.get('order').disable()
|
|
|
|
}
|
|
|
|
if(this.placementsOptions.length ==1){
|
|
|
|
this.myForm.get('placement').disable()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public setOptions(page: Page, countContents:number) {
|
|
|
|
this.placementsOptions = [];
|
|
|
|
if(page.top){
|
|
|
|
this.placementsOptions.push({label: "top", value: "top"});
|
|
|
|
}
|
|
|
|
if(page.bottom){
|
|
|
|
this.placementsOptions.push({label: "bottom", value: "bottom"});
|
|
|
|
}
|
|
|
|
if(page.left){
|
|
|
|
this.placementsOptions.push({label: "left", value: "left"});
|
|
|
|
}
|
|
|
|
this.orderOptions = [];
|
|
|
|
for(let i=1; i<countContents+1; i++){
|
|
|
|
this.orderOptions.push({label: ""+i, value: i});
|
|
|
|
}
|
2019-12-20 12:38:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public get form() {
|
|
|
|
return this._fb.group({
|
|
|
|
page : [this.pageId, Validators.required],
|
2020-09-24 13:18:24 +02:00
|
|
|
portal : this.communityPid,
|
2019-12-20 12:38:13 +01:00
|
|
|
placement : ['', Validators.required],
|
|
|
|
content : ['', Validators.required],
|
|
|
|
order : [1, Validators.required],
|
|
|
|
isActive : true,
|
|
|
|
//isPriorTo : false,
|
|
|
|
_id : '',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public reset() {
|
|
|
|
this.myForm.patchValue({
|
|
|
|
page : '',
|
2020-09-24 13:18:24 +02:00
|
|
|
portal : this.communityPid,
|
2019-12-20 12:38:13 +01:00
|
|
|
placement : '',
|
|
|
|
content : [''],
|
|
|
|
order : 1,
|
|
|
|
isActive : true,
|
|
|
|
//isPriorTo : false,
|
|
|
|
_id : ''
|
|
|
|
});
|
|
|
|
this.myForm.markAsPristine();
|
|
|
|
}
|
|
|
|
|
|
|
|
handleError(message: string, error) {
|
|
|
|
this.errorMessage = message;
|
2021-02-25 16:55:56 +01:00
|
|
|
console.error('Server responded: ' + error);
|
2019-12-20 12:38:13 +01:00
|
|
|
|
|
|
|
this.showLoading = false;
|
|
|
|
}
|
2021-02-25 16:55:56 +01:00
|
|
|
|
|
|
|
public saveCustom() {
|
|
|
|
if(!Session.isLoggedIn()){
|
|
|
|
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
|
|
|
|
} else {
|
|
|
|
//this.errorMessage = null;
|
|
|
|
|
|
|
|
if(this.myForm.valid) {
|
|
|
|
this.showLoading = true;
|
|
|
|
this.updateErrorMessage = "";
|
|
|
|
this.myForm.get('order').enable();
|
|
|
|
this.myForm.get('placement').enable();
|
|
|
|
let pageHelpContent : PageHelpContent = this.myForm.value;
|
|
|
|
this.subs.push(this._helpContentService.savePageHelpContent(pageHelpContent, this.properties.adminToolsAPIURL, this.communityPid).subscribe(
|
|
|
|
_ => {
|
|
|
|
this._router.navigate( ['../'], { queryParams: { "pageId": this.pageId }, relativeTo: this.route } );
|
|
|
|
this.showLoading = false;
|
|
|
|
},
|
|
|
|
err => this.handleUpdateError('System error saving page content', err)
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
this.showLoading = false;
|
|
|
|
this.errorMessage = "Please fill all required fields";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public cancelCustom() {
|
|
|
|
this._router.navigate( ['../'], { queryParams: { "pageId": this.pageId }, relativeTo: this.route } );
|
|
|
|
}
|
|
|
|
|
|
|
|
public resetCustom() {
|
|
|
|
this.showLoading = true;
|
|
|
|
this.updateForm(this.pageHelpContent);
|
|
|
|
this.showLoading = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
handleUpdateError(message: string, error) {
|
|
|
|
|
|
|
|
this.updateErrorMessage = message;
|
|
|
|
console.error('Server responded: ' + error);
|
|
|
|
|
|
|
|
this.showLoading = false;
|
|
|
|
}
|
|
|
|
changeStatus(){
|
|
|
|
this.myForm.get('isActive').setValue(!this.myForm.get('isActive').value);
|
|
|
|
if(this.pageHelpContent && this.myForm.get('isActive').value != this.pageHelpContent.isActive || !this.pageHelpContent && !this.myForm.get('isActive').value ){
|
|
|
|
this.myForm.get('isActive').markAsDirty();
|
|
|
|
}else{
|
|
|
|
this.myForm.get('isActive').markAsPristine()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
contentChanged(){
|
|
|
|
if(this.pageHelpContent && this.myForm.get('content').value != this.pageHelpContent.content || !this.pageHelpContent && this.myForm.get('content').value != '' ){
|
|
|
|
this.myForm.get('content').markAsDirty();
|
|
|
|
}else{
|
|
|
|
this.myForm.get('content').markAsPristine()
|
|
|
|
}
|
|
|
|
}
|
2019-12-20 12:38:13 +01:00
|
|
|
}
|