Project

General

Profile

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

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

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

    
19
export class EditDivHelpContentComponent implements OnInit, OnDestroy{
20

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

    
24
    public communityPid: string;
25
    public pageId: string;
26
    public page: Page;
27

    
28
    private sub: Subscription;
29

    
30
    private divHelpContent: DivHelpContent;
31

    
32
    public properties:EnvProperties = null;
33

    
34
    public showLoading: boolean = true;
35
    public errorMessage: string = '';
36
    public updateErrorMessage: string = '';
37

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

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

    
51
            //let id = params['id'];
52
            let divContentId = params['classContentId'];
53
            this.communityPid = params['communityId'];
54
            this.pageId = params['pageId'];
55

    
56
             if(!divContentId) {
57
               this.router.navigate(['../../'], { queryParams: { "communityId": this.communityPid} });
58
             }
59

    
60
            this.getDivHelpContent(divContentId);
61
          });
62
        });
63
    }
64
    ngOnDestroy() {
65
        this.sub.unsubscribe();
66
    }
67

    
68
    handleError(message: string, error) {
69
        this.errorMessage = message;
70
        console.log('Server responded: ' + error);
71
        this.showLoading = false;
72
    }
73

    
74
    handleUpdateError(message: string, error) {
75
        this.updateErrorMessage = message;
76
        console.log('Server responded: ' + error);
77
        this.showLoading = false;
78
    }
79

    
80
    private getPage(pageId: string) {
81
      this._helpContentService.getPageByPortal(pageId,this.properties.adminToolsAPIURL, this.communityPid).subscribe(
82
        page => {
83
          if(this.properties.adminToolsPortalType != page.portalType) {
84
            this.router.navigate(['../../'], { queryParams: { "communityId": this.communityPid} });
85
          } else {
86
            this.page = page;
87
            this.showLoading = false;
88
          }
89
        },
90
        error => this.handleError('System error retrieving page with id: '+pageId, error)
91
      );
92
    }
93

    
94
    private getDivHelpContent(divContentId: string) {
95
      if(!Session.isLoggedIn()){
96
        this.router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this.router.url} });
97
      } else {
98
        this.showLoading = true;
99
        this.errorMessage = "";
100
        this.updateErrorMessage = "";
101

    
102
        this._helpContentService.getDivHelpContent(divContentId, this.properties.adminToolsAPIURL, this.communityPid).subscribe(
103
            divHelpContent => {
104
              if(this.pageId) {
105
                this.getPage(this.pageId);
106
              }
107
              this.updateForm(divHelpContent);
108
              this.showLoading = false;
109
            },
110
            error => this.handleError('System error retrieving class help content', error));
111
      }
112
    }
113

    
114
    getDivId(divId: string) {
115
      if(!Session.isLoggedIn()){
116
        this.router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this.router.url} });
117
      } else {
118
        this.showLoading = true;
119

    
120
        this._helpContentService.getDivIdFull(divId, this.properties.adminToolsAPIURL, this.communityPid).subscribe(
121
          div => {
122
            this.formComponent.selectedDiv = div;
123

    
124
            if(!this.pageId) {
125
              this.formComponent.getDivs("");
126
            }
127

    
128
            this.showLoading = false;
129
          },
130
          error => this.handleError('System error retrieving class', error)
131
       );
132
      }
133
    }
134

    
135
    private updateForm(divHelpContent : DivHelpContent) {
136
        this.divHelpContent = divHelpContent;
137

    
138
        this.getDivId(divHelpContent.divId as string);
139

    
140
        this.formComponent.myForm.patchValue((divHelpContent));
141
    }
142

    
143
    public saveCustom() {
144
      if(!Session.isLoggedIn()){
145
        this.router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this.router.url} });
146
      } else {
147
        if(this.formComponent.myForm.valid) {
148
            this.showLoading = true;
149
            this.updateErrorMessage = "";
150

    
151
            let divHelpContent : DivHelpContent = this.formComponent.myForm.value;
152

    
153
            this._helpContentService.insertOrUpdateDivHelpContent(divHelpContent, this.properties.adminToolsAPIURL, this.communityPid).subscribe(
154
                _ => {
155
                  if(this.pageId) {
156
                    this.router.navigate( ['../../'], { queryParams: { "communityId": this.communityPid, "pageId": this.pageId  } } );
157
                  } else {
158
                    this.router.navigate(['../../'], { queryParams: { "communityId": this.communityPid } } );
159
                  }
160
                  this.showLoading = false;
161
                },
162
                err => this.handleUpdateError('System error updating class content', err)
163
            );
164
        } else {
165
            this.errorMessage = "Please fill all required fields";
166
        }
167
      }
168
    }
169

    
170
    public cancelCustom() {
171
      this.errorMessage = "";
172
      this.updateErrorMessage = "";
173

    
174
      if(this.pageId) {
175
        this.router.navigate( ['../../'], { queryParams: { "communityId": this.communityPid, "pageId": this.pageId  } } );
176
      } else {
177
        this.router.navigate(['../../'], { queryParams: { "communityId": this.communityPid } } );
178
      }
179
    }
180
}
(10-10/15)