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(['/classContents'], { 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.getPage(pageId,this.properties.adminToolsAPIURL).subscribe(
82
        page => {
83
          if( (this.communityPid == 'openaire' && !page.openaire)
84
            || (this.communityPid == 'connect' && !page.connect)
85
            || (this.communityPid != 'openaire' && this.communityPid != 'connect' && !page.communities)) {
86
            this.router.navigate(['/classContents'], { queryParams: { "communityId": this.communityPid} });
87
          } else {
88
            this.page = page;
89
            this.showLoading = false;
90
          }
91
        },
92
        error => this.handleError('System error retrieving page with id: '+pageId, error)
93
      );
94
    }
95

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

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

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

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

    
126
            if(!this.pageId) {
127
              this.formComponent.getDivs("");
128
            }
129

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

    
137
    private updateForm(divHelpContent : DivHelpContent) {
138
        this.divHelpContent = divHelpContent;
139

    
140
        this.getDivId(divHelpContent.divId as string);
141

    
142
        this.formComponent.myForm.patchValue((divHelpContent));
143
    }
144

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

    
153
            let divHelpContent : DivHelpContent = this.formComponent.myForm.value;
154

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

    
172
    public cancelCustom() {
173
      this.errorMessage = "";
174
      this.updateErrorMessage = "";
175

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