Project

General

Profile

1 54074 konstantin
import { Component, ViewChild, OnInit, ElementRef } from '@angular/core';
2 53600 argiro.kok
import { FormGroup } from "@angular/forms";
3
import { ActivatedRoute, Router } from "@angular/router";
4
//import { Location } from "@angular/common";
5
import { HelpContentService } from "../../services/help-content.service";
6
import { HtmlPageContentService } from "./html-page-content.service";
7
import { HtmlPageContent, CheckHtmlPageContent } from "../../domain/html-page-content";
8
import { HtmlPageContentFormComponent } from "./html-page-content-form.component";
9
10
import { EnvProperties } from '../../openaireLibrary/utils/properties/env-properties';
11 55972 argiro.kok
import { Subscription } from "rxjs";
12 53600 argiro.kok
13 53735 konstantin
import {Session} from '../../openaireLibrary/login/utils/helper.class';
14
import {LoginErrorCodes} from '../../openaireLibrary/login/utils/guardHelper.class';
15 55416 konstantin
import {HelperFunctions} from "../../openaireLibrary/utils/HelperFunctions.class";
16 58371 k.triantaf
import {Title} from '@angular/platform-browser';
17 54674 konstantin
declare var UIkit: any;
18 53735 konstantin
19 53600 argiro.kok
@Component({
20
    selector: 'edit-htmlpage-content',
21
    templateUrl: './edit-htmlpage-content.component.html',
22
})
23
24
export class EditHtmlPageContentComponent implements OnInit {
25
    @ViewChild(HtmlPageContentFormComponent)
26
    public formComponent : HtmlPageContentFormComponent;
27
28 56471 argiro.kok
    communityPid: string;
29
    pageId: string;
30
    pageName: string = "";
31 54674 konstantin
    //public mode: string = "edit";
32 53600 argiro.kok
33
    private sub: Subscription;
34
35
    private htmlPageContent: HtmlPageContent;
36
37
    public properties:EnvProperties = null;
38
39
    public showLoading: boolean = true;
40
    public errorMessage: string = '';
41
    public updateErrorMessage: string = '';
42
43
    constructor(
44 54074 konstantin
        private element: ElementRef,
45 53600 argiro.kok
        private route: ActivatedRoute,
46
        private router: Router,
47 58371 k.triantaf
        private title: Title,
48 53600 argiro.kok
        private _helpContentService: HelpContentService,
49
        private _htmlContentService: HtmlPageContentService) {
50
          this.router.routeReuseStrategy.shouldReuseRoute = function(){
51
            return false;
52
          }
53
    }
54
55
    ngOnInit() {
56
      this.route.data
57
        .subscribe((data: { envSpecific: EnvProperties }) => {
58
           this.properties = data.envSpecific;
59
           this.sub = this.route.queryParams.subscribe(params => {
60 55416 konstantin
             HelperFunctions.scroll();
61 58371 k.triantaf
             this.title.setTitle('Administration Dashboard | Edit HTML page');
62 53600 argiro.kok
             this.communityPid = params['communityId'];
63
             this.pageId = params['pageId'];
64
65
             this.getPage(this.communityPid, this.pageId);
66
           });
67
      });
68
    }
69
    ngOnDestroy() {
70
        this.sub.unsubscribe();
71
    }
72
73
    handleError(message: string, error) {
74
        this.errorMessage = message;
75
        console.log('Server responded: ' + error);
76
        this.showLoading = false;
77
    }
78
79
    handleUpdateError(message: string, error) {
80
        this.updateErrorMessage = message;
81
        console.log('Server responded: ' + error);
82
        this.showLoading = false;
83
    }
84
85
    private getPage(communityId: string, pageId: string) {
86 53735 konstantin
      if(!Session.isLoggedIn()){
87
        this.router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this.router.url} });
88
      } else {
89
        this._helpContentService.getPage(pageId, this.properties.adminToolsAPIURL).subscribe(
90
          page => {
91
            this.pageName = page.name;
92
            this.getHtmlPageContent(communityId, page.route);
93
          },
94
          error => this.handleError('System error retrieving page', error));
95
      }
96 53600 argiro.kok
    }
97
98
    private getHtmlPageContent(communityId: string, pageRoute: string) {
99 53735 konstantin
      if(!Session.isLoggedIn()){
100
        this.router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this.router.url} });
101
      } else {
102
        this.showLoading = true;
103
        this.errorMessage = "";
104
        this.updateErrorMessage = "";
105 53600 argiro.kok
106 53735 konstantin
        this._htmlContentService.getHtmlPageContent(communityId, pageRoute, this.properties.adminToolsAPIURL).subscribe(
107
          htmlPageContent => {
108
            if(htmlPageContent.length > 0) {
109
              this.updateForm(htmlPageContent[0]);
110
            }
111
            this.showLoading = false;
112
          },
113
          error => this.handleError('System error retrieving page help content', error));
114
      }
115 53600 argiro.kok
    }
116
117
    private updateForm(htmlPageContent : HtmlPageContent) {
118
        this.htmlPageContent = htmlPageContent;
119
        this.formComponent.myForm.patchValue((htmlPageContent));
120
    }
121
122 56471 argiro.kok
    saveCustom() {
123 53735 konstantin
      if(!Session.isLoggedIn()){
124
        this.router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this.router.url} });
125
      } else {
126 54674 konstantin
        this.formComponent.myForm.value.content = this.formComponent.myeditor.instance.getData();
127 53600 argiro.kok
        if(this.formComponent.myForm.valid) {
128
            this.showLoading = true;
129
            this.updateErrorMessage = "";
130 54674 konstantin
            //this.mode = "";
131 53600 argiro.kok
132
            let htmlPageContent : HtmlPageContent = this.formComponent.myForm.value;
133
            this._htmlContentService.updateHtmlPageContent(htmlPageContent, this.properties.adminToolsAPIURL).subscribe(
134
                _ => {
135 54674 konstantin
                  //this.mode = "preview";
136
                  UIkit.tab('#html-tab').show(1);
137
                  UIkit.switcher('#html-tab-content').show(1);
138 53600 argiro.kok
                  this.showLoading = false;
139
                },
140
                err => {
141 54674 konstantin
                  //this.mode = "edit";
142
                  UIkit.tab('#html-tab').show(0);
143
                  UIkit.switcher('#html-tab-content').show(0);
144 53600 argiro.kok
                  this.handleUpdateError('System error updating page content', err);
145
                }
146
            );
147
        } else {
148 54674 konstantin
            //this.mode = "edit";
149
            UIkit.tab('#html-tab').show(0);
150
            UIkit.switcher('#html-tab-content').show(0);
151 53600 argiro.kok
            this.errorMessage = "Please fill all required fields";
152
        }
153 53735 konstantin
      }
154 53600 argiro.kok
    }
155
156 56471 argiro.kok
    cancelCustom() {
157 53600 argiro.kok
      this.errorMessage = "";
158
      this.updateErrorMessage = "";
159
      this.router.navigate(['/pages'], { queryParams: { "type": "html", "communityId": this.communityPid}});
160
      //this.location.back();
161
      //this.router.navigated = false;
162
      //this.router.navigate( ['/htmlPageContent/edit'], { queryParams: { "communityId": this.communityPid, "pageId": this.pageId } } );
163
    }
164
}