Project

General

Profile

1
import { Component, ViewChild, ElementRef } from '@angular/core';
2
import { ActivatedRoute, Router } from "@angular/router";
3
import { DivContentFormComponent } from "./div-help-content-form.component";
4
import { DivHelpContent } from "../../utils/entities/adminTool/div-help-content";
5
import { HelpContentService } from "../../services/help-content.service";
6
import { EnvProperties } from '../../utils/properties/env-properties';
7

    
8
import {Session} from '../../login/utils/helper.class';
9
import {LoginErrorCodes} from '../../login/utils/guardHelper.class';
10
import {HelperFunctions} from "../../utils/HelperFunctions.class";
11
import {Page} from "../../utils/entities/adminTool/page";
12

    
13
@Component({
14
    selector: 'new-div-help-content',
15
    templateUrl: 'new-div-help-content.component.html',
16
})
17

    
18
export class NewDivHelpContentComponent {
19

    
20
    @ViewChild(DivContentFormComponent)
21
    public formComponent : DivContentFormComponent;
22

    
23
    //private errorMessage : string = null;
24

    
25
    public communityPid: string;
26

    
27
    public pageId: string;
28
    public page: Page;
29

    
30
    public properties:EnvProperties = null;
31

    
32
    public showLoading: boolean = true;
33
    public errorMessage: string = '';
34
    public updateErrorMessage: string = '';
35

    
36
    constructor(
37
        private element: ElementRef,
38
        private route: ActivatedRoute,
39
        private router: Router,
40
        private _helpContentService: HelpContentService) {}
41

    
42
    ngOnInit() {
43
      this.route.data
44
        .subscribe((data: { envSpecific: EnvProperties }) => {
45
           this.properties = data.envSpecific;
46
           this.route.queryParams.subscribe(params => {
47
             HelperFunctions.scroll();
48

    
49
             this.communityPid = params['communityId'];
50
             this.pageId = params['pageId'];
51

    
52

    
53
             if(this.pageId) {
54
               this.getPage(this.pageId);
55
             } else {
56
               this.showLoading = false;
57
             }
58
           });
59
        });
60
    }
61

    
62
    private getPage(pageId: string) {
63
      this._helpContentService.getPageByPortal(pageId,this.properties.adminToolsAPIURL, this.communityPid).subscribe(
64
        page => {
65
          if(this.properties.adminToolsPortalType != page.portalType) {
66
            this.router.navigate(['../../'], { queryParams: { "communityId": this.communityPid}, relativeTo: this.route });
67
          } else {
68
            this.page = page;
69
            this.showLoading = false;
70
            console.info(this.page);
71
          }
72
        },
73
        error => this.handleError('System error retrieving page with id: '+pageId, error)
74
      );
75
    }
76

    
77
    public saveCustom() {
78
      if(!Session.isLoggedIn()){
79
        this.router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this.router.url} });
80
      } else {
81
        if(this.formComponent.myForm.valid) {
82
            this.showLoading = true;
83
            this.updateErrorMessage = "";
84

    
85
            let divHelpContent : DivHelpContent = this.formComponent.myForm.value;
86

    
87
            this._helpContentService.insertOrUpdateDivHelpContent(divHelpContent, this.properties.adminToolsAPIURL, this.communityPid).subscribe(
88
                _ => {
89
                  if(this.pageId) {
90
                    this.router.navigate( ['../../'], { queryParams: { "communityId": this.communityPid, "pageId": this.pageId  } , relativeTo: this.route} );
91
                  } else {
92
                    this.router.navigate(['../../'], { queryParams: { "communityId": this.communityPid } , relativeTo: this.route} );
93
                  }
94
                  this.showLoading = false;
95
                },
96
                err => this.handleUpdateError('System error saving page content', err)
97
            );
98
        } else {
99
            this.errorMessage = "Please fill all required fields";
100
        }
101
      }
102
    }
103

    
104
    public cancelCustom() {
105
      if(this.pageId) {
106
        this.router.navigate( ['../../'], { queryParams: { "communityId": this.communityPid, "pageId": this.pageId  }, relativeTo: this.route } );
107
      } else {
108
        this.router.navigate(['../../'], { queryParams: { "communityId": this.communityPid }, relativeTo: this.route } );
109
      }
110
    }
111

    
112
    handleUpdateError(message: string, error) {
113
        this.errorMessage = message;
114
        console.log('Server responded: ' + error);
115
        this.showLoading = false;
116
    }
117

    
118
    handleError(message: string, error) {
119
      this.errorMessage = message;
120
      console.log('Server responded: ' + error);
121
      this.showLoading = false;
122
    }
123
}
(14-14/15)