2021-03-19 17:51:02 +01:00
|
|
|
import {Component, OnInit} from '@angular/core';
|
2021-02-25 16:55:56 +01:00
|
|
|
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';
|
|
|
|
import {properties} from '../../../../environments/environment';
|
2021-07-14 13:19:57 +02:00
|
|
|
import {Subscriber, Subscription, zip} 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
|
|
|
|
2021-03-18 15:11:41 +01:00
|
|
|
declare var UIkit;
|
|
|
|
|
2019-12-20 12:38:13 +01:00
|
|
|
@Component({
|
2021-03-02 16:52:09 +01:00
|
|
|
selector: 'page-content-form',
|
|
|
|
templateUrl: './page-help-content-form.component.html',
|
2019-12-20 12:38:13 +01:00
|
|
|
})
|
2021-03-02 16:52:09 +01:00
|
|
|
export class PageContentFormComponent implements OnInit {
|
|
|
|
|
|
|
|
myForm: FormGroup;
|
|
|
|
portal: string;
|
|
|
|
pageId: string;
|
|
|
|
pageContentId: string;
|
|
|
|
page: Page;
|
|
|
|
placementsOptions = [];
|
|
|
|
orderOptions = [];
|
2021-03-19 17:51:02 +01:00
|
|
|
public properties: EnvProperties = properties;
|
2021-03-02 16:52:09 +01:00
|
|
|
public showLoading: boolean = true;
|
|
|
|
private subs: Subscription[] = [];
|
2021-03-04 12:32:57 +01:00
|
|
|
public pageHelpContent: PageHelpContent;
|
2021-03-02 16:52:09 +01:00
|
|
|
|
|
|
|
constructor(private route: ActivatedRoute, private _router: Router, private _fb: FormBuilder, private _helpContentService: HelpContentService) {
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
this.subs.push(this.route.params.subscribe(params => {
|
|
|
|
this.portal = (this.route.snapshot.data.portal) ? this.route.snapshot.data.portal : this.route.snapshot.params[this.route.snapshot.data.param];
|
|
|
|
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});
|
|
|
|
}
|
|
|
|
this.getInfo(this.pageId);
|
|
|
|
}));
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnDestroy() {
|
|
|
|
this.subs.forEach(value => {
|
|
|
|
if (value instanceof Subscriber) {
|
|
|
|
value.unsubscribe();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getInfo(pageId: string) {
|
|
|
|
this.showLoading = true;
|
2021-07-14 13:19:57 +02:00
|
|
|
let obs = zip(this._helpContentService.getPageByPortal(pageId, this.properties.adminToolsAPIURL, this.portal), this._helpContentService.getCommunityPageHelpContents(this.portal, this.properties.adminToolsAPIURL, pageId));
|
2021-03-02 16:52:09 +01:00
|
|
|
this.subs.push(obs.subscribe(
|
|
|
|
results => {
|
|
|
|
this.page = results[0];
|
|
|
|
if (this.properties.adminToolsPortalType != this.page.portalType) {
|
|
|
|
this._router.navigate(['../'], {relativeTo: this.route});
|
2021-02-25 16:55:56 +01:00
|
|
|
}
|
2021-03-02 16:52:09 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
// }
|
2021-02-25 16:55:56 +01:00
|
|
|
}
|
2021-03-02 16:52:09 +01:00
|
|
|
this.setOptions(this.page, countPageContents + (this.pageHelpContent ? 0 : 1));
|
|
|
|
if (!this.pageContentId) {
|
|
|
|
this.myForm = this.form;
|
|
|
|
this.initFormWithSelectOptions();
|
|
|
|
} else {
|
|
|
|
this.updateForm(this.pageHelpContent);
|
2021-02-25 16:55:56 +01:00
|
|
|
}
|
2021-03-02 16:52:09 +01:00
|
|
|
this.showLoading = false;
|
|
|
|
},
|
|
|
|
error => this.handleError('System error retrieving page with id: ' + pageId, error)
|
|
|
|
));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/*private getPage(pageId: string) {
|
|
|
|
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) {
|
2021-03-19 17:51:02 +01:00
|
|
|
this.showLoading = true;
|
2021-02-25 16:55:56 +01:00
|
|
|
|
2021-03-19 17:51:02 +01:00
|
|
|
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-03-02 16:52:09 +01:00
|
|
|
}*/
|
|
|
|
private updateForm(pageHelpContent: PageHelpContent) {
|
|
|
|
this.pageHelpContent = pageHelpContent;
|
|
|
|
this.myForm = this.form;
|
|
|
|
if (this.pageHelpContent) {
|
|
|
|
this.myForm.patchValue((pageHelpContent));
|
|
|
|
} else {
|
|
|
|
this.initFormWithSelectOptions();
|
2021-02-25 16:55:56 +01:00
|
|
|
}
|
2021-03-02 16:52:09 +01:00
|
|
|
if (this.orderOptions.length == 1) {
|
|
|
|
this.myForm.get('order').disable()
|
2021-02-25 16:55:56 +01:00
|
|
|
}
|
2021-03-02 16:52:09 +01:00
|
|
|
if (this.placementsOptions.length == 1) {
|
|
|
|
this.myForm.get('placement').disable()
|
2019-12-20 12:38:13 +01:00
|
|
|
}
|
2021-03-02 16:52:09 +01:00
|
|
|
this.myForm.markAsPristine();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
initFormWithSelectOptions() {
|
|
|
|
if (this.placementsOptions.length == 1) {
|
|
|
|
this.myForm.get("placement").setValue(this.placementsOptions[0].value);
|
2019-12-20 12:38:13 +01:00
|
|
|
}
|
2021-03-02 16:52:09 +01:00
|
|
|
this.myForm.get("order").setValue(this.orderOptions[this.orderOptions.length - 1].value);
|
|
|
|
if (this.orderOptions.length == 1) {
|
|
|
|
this.myForm.get('order').disable()
|
2019-12-20 12:38:13 +01:00
|
|
|
}
|
2021-03-02 16:52:09 +01:00
|
|
|
if (this.placementsOptions.length == 1) {
|
|
|
|
this.myForm.get('placement').disable()
|
2019-12-20 12:38:13 +01:00
|
|
|
}
|
2021-03-02 16:52:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public setOptions(page: Page, countContents: number) {
|
|
|
|
this.placementsOptions = [];
|
|
|
|
if (page.top) {
|
|
|
|
this.placementsOptions.push({label: "top", value: "top"});
|
2021-02-25 16:55:56 +01:00
|
|
|
}
|
2021-03-02 16:52:09 +01:00
|
|
|
if (page.bottom) {
|
|
|
|
this.placementsOptions.push({label: "bottom", value: "bottom"});
|
2021-02-25 16:55:56 +01:00
|
|
|
}
|
2021-03-02 16:52:09 +01:00
|
|
|
if (page.left) {
|
|
|
|
this.placementsOptions.push({label: "left", value: "left"});
|
2021-02-25 16:55:56 +01:00
|
|
|
}
|
2021-03-02 16:52:09 +01:00
|
|
|
this.orderOptions = [];
|
|
|
|
for (let i = 1; i < countContents + 1; i++) {
|
|
|
|
this.orderOptions.push({label: "" + i, value: i});
|
2021-02-25 16:55:56 +01:00
|
|
|
}
|
2021-03-02 16:52:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public get form() {
|
|
|
|
return this._fb.group({
|
|
|
|
page: [this.pageId, Validators.required],
|
|
|
|
portal: this.portal,
|
|
|
|
placement: ['', Validators.required],
|
|
|
|
content: ['', Validators.required],
|
|
|
|
order: [1, Validators.required],
|
|
|
|
isActive: true,
|
|
|
|
//isPriorTo : false,
|
|
|
|
_id: '',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public reset() {
|
|
|
|
this.myForm.patchValue({
|
|
|
|
page: '',
|
|
|
|
portal: this.portal,
|
|
|
|
placement: '',
|
|
|
|
content: [''],
|
|
|
|
order: 1,
|
|
|
|
isActive: true,
|
|
|
|
//isPriorTo : false,
|
|
|
|
_id: ''
|
|
|
|
});
|
|
|
|
this.myForm.markAsPristine();
|
|
|
|
}
|
|
|
|
|
|
|
|
handleError(message: string, error) {
|
2021-03-18 15:11:41 +01:00
|
|
|
UIkit.notification(message, {
|
|
|
|
status: 'danger',
|
|
|
|
timeout: 6000,
|
|
|
|
pos: 'bottom-right'
|
|
|
|
});
|
2021-03-02 16:52:09 +01:00
|
|
|
console.error('Server responded: ' + error);
|
|
|
|
|
|
|
|
this.showLoading = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public saveCustom() {
|
2021-03-19 17:51:02 +01:00
|
|
|
if (this.myForm.valid) {
|
|
|
|
this.showLoading = true;
|
|
|
|
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.portal).subscribe(
|
|
|
|
_ => {
|
|
|
|
UIkit.notification('Page content has been <b>successfully ' + (this.pageContentId ? 'updated' : 'created') + '</b>', {
|
|
|
|
status: 'success',
|
|
|
|
timeout: 6000,
|
|
|
|
pos: 'bottom-right'
|
|
|
|
});
|
|
|
|
this._router.navigate(['../'], {queryParams: {"pageId": this.pageId}, relativeTo: this.route});
|
|
|
|
this.showLoading = false;
|
|
|
|
},
|
|
|
|
err => this.handleUpdateError('System error saving page content', err)
|
|
|
|
));
|
2021-03-02 16:52:09 +01:00
|
|
|
} else {
|
2021-03-19 17:51:02 +01:00
|
|
|
this.showLoading = false;
|
2021-02-25 16:55:56 +01:00
|
|
|
}
|
2021-03-02 16:52:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2021-03-18 15:11:41 +01:00
|
|
|
UIkit.notification(message, {
|
|
|
|
status: 'danger',
|
|
|
|
timeout: 6000,
|
|
|
|
pos: 'bottom-right'
|
|
|
|
});
|
2021-03-02 16:52:09 +01:00
|
|
|
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()
|
2021-02-25 16:55:56 +01:00
|
|
|
}
|
2021-03-02 16:52:09 +01:00
|
|
|
}
|
2019-12-20 12:38:13 +01:00
|
|
|
}
|