Project

General

Profile

1
import { Component, ViewChild, OnInit, ElementRef } from '@angular/core';
2
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
import { Subscription } from "rxjs";
12

    
13
import {Session} from '../../openaireLibrary/login/utils/helper.class';
14
import {LoginErrorCodes} from '../../openaireLibrary/login/utils/guardHelper.class';
15
import {HelperFunctions} from "../../openaireLibrary/utils/HelperFunctions.class";
16
import {Title} from '@angular/platform-browser';
17
declare var UIkit: any;
18

    
19
@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
    communityPid: string;
29
    pageId: string;
30
    pageName: string = "";
31
    //public mode: string = "edit";
32

    
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
        private element: ElementRef,
45
        private route: ActivatedRoute,
46
        private router: Router,
47
        private title: Title,
48
        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
             HelperFunctions.scroll();
61
             this.title.setTitle('Administration Dashboard | Edit HTML page');
62
             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
      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
    }
97

    
98
    private getHtmlPageContent(communityId: string, pageRoute: string) {
99
      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

    
106
        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
    }
116

    
117
    private updateForm(htmlPageContent : HtmlPageContent) {
118
        this.htmlPageContent = htmlPageContent;
119
        this.formComponent.myForm.patchValue((htmlPageContent));
120
    }
121

    
122
    saveCustom() {
123
      if(!Session.isLoggedIn()){
124
        this.router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this.router.url} });
125
      } else {
126
        this.formComponent.myForm.value.content = this.formComponent.myeditor.instance.getData();
127
        if(this.formComponent.myForm.valid) {
128
            this.showLoading = true;
129
            this.updateErrorMessage = "";
130
            //this.mode = "";
131

    
132
            let htmlPageContent : HtmlPageContent = this.formComponent.myForm.value;
133
            this._htmlContentService.updateHtmlPageContent(htmlPageContent, this.properties.adminToolsAPIURL).subscribe(
134
                _ => {
135
                  //this.mode = "preview";
136
                  UIkit.tab('#html-tab').show(1);
137
                  UIkit.switcher('#html-tab-content').show(1);
138
                  this.showLoading = false;
139
                },
140
                err => {
141
                  //this.mode = "edit";
142
                  UIkit.tab('#html-tab').show(0);
143
                  UIkit.switcher('#html-tab-content').show(0);
144
                  this.handleUpdateError('System error updating page content', err);
145
                }
146
            );
147
        } else {
148
            //this.mode = "edit";
149
            UIkit.tab('#html-tab').show(0);
150
            UIkit.switcher('#html-tab-content').show(0);
151
            this.errorMessage = "Please fill all required fields";
152
        }
153
      }
154
    }
155

    
156
    cancelCustom() {
157
      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
}
(4-4/7)