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 "../../domain/div-help-content";
6
import { ActivatedRoute, Router } from "@angular/router";
7
import { EnvProperties } from '../../openaireLibrary/utils/properties/env-properties';
8

    
9
import {Session} from '../../openaireLibrary/login/utils/helper.class';
10
import {LoginErrorCodes} from '../../openaireLibrary/login/utils/guardHelper.class';
11
import {HelperFunctions} from "../../openaireLibrary/utils/HelperFunctions.class";
12
import {Page} from "../../domain/page";
13
import {Title} from '@angular/platform-browser';
14
import {ClearCacheService} from "../../openaireLibrary/services/clear-cache.service";
15

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

    
21
export class EditDivHelpContentComponent implements OnInit, OnDestroy{
22

    
23
    @ViewChild(DivContentFormComponent)
24
    public formComponent : DivContentFormComponent;
25

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

    
30
    private sub: Subscription;
31

    
32
    private divHelpContent: DivHelpContent;
33

    
34
    public properties:EnvProperties = null;
35

    
36
    public showLoading: boolean = true;
37
    public errorMessage: string = '';
38
    public updateErrorMessage: string = '';
39

    
40
    constructor(
41
        private element: ElementRef,
42
        private route: ActivatedRoute,
43
        private router: Router,
44
        private title: Title,
45
        private _helpContentService: HelpContentService,
46
        private _clearCacheService: ClearCacheService) {}
47

    
48
    ngOnInit() {
49
      this.route.data
50
        .subscribe((data: { envSpecific: EnvProperties }) => {
51
           this.properties = data.envSpecific;
52
           this.sub = this.route.queryParams.subscribe(params => {
53
             HelperFunctions.scroll();
54

    
55
            //let id = params['id'];
56
            let divContentId = params['classContentId'];
57
            this.communityPid = params['communityId'];
58
            this.pageId = params['pageId'];
59
            this.title.setTitle('Administration Dashboard | Edit Class Help Text');
60
             if(!divContentId) {
61
               this.router.navigate(['/classContents'], { queryParams: { "communityId": this.communityPid} });
62
             }
63

    
64
            this.getDivHelpContent(divContentId);
65
          });
66
        });
67
    }
68
    ngOnDestroy() {
69
        this.sub.unsubscribe();
70
    }
71

    
72
    handleError(message: string, error) {
73
        this.errorMessage = message;
74
        console.log('Server responded: ' + error);
75
        this.showLoading = false;
76
    }
77

    
78
    handleUpdateError(message: string, error) {
79
        this.updateErrorMessage = message;
80
        console.log('Server responded: ' + error);
81
        this.showLoading = false;
82
    }
83

    
84
    private getPage(pageId: string) {
85
      this._helpContentService.getPageByPortal(pageId,this.properties.adminToolsAPIURL, this.communityPid).subscribe(
86
        page => {
87
          // if( (this.communityPid == 'openaire' && !page.openaire)
88
          //   || (this.communityPid == 'connect' && !page.connect)
89
          //   || (this.communityPid != 'openaire' && this.communityPid != 'connect' && !page.communities)) {
90
          if(this.properties.adminToolsPortalType != page.portalType) {
91
            this.router.navigate(['/classContents'], { queryParams: { "communityId": this.communityPid} });
92
          } else {
93
            this.page = page;
94
            this.showLoading = false;
95
          }
96
        },
97
        error => this.handleError('System error retrieving page with id: '+pageId, error)
98
      );
99
    }
100

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

    
109
        this._helpContentService.getDivHelpContent(divContentId, this.properties.adminToolsAPIURL, this.communityPid).subscribe(
110
            divHelpContent => {
111
              if(this.pageId) {
112
                this.getPage(this.pageId);
113
              }
114
              this.updateForm(divHelpContent);
115
              this.showLoading = false;
116
            },
117
            error => this.handleError('System error retrieving class help content', error));
118
      }
119
    }
120

    
121
    getDivId(divId: string) {
122
      if(!Session.isLoggedIn()){
123
        this.router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this.router.url} });
124
      } else {
125
        this.showLoading = true;
126

    
127
        this._helpContentService.getDivIdFull(divId, this.properties.adminToolsAPIURL, this.communityPid).subscribe(
128
          div => {
129
            this.formComponent.selectedDiv = div;
130

    
131
            if(!this.pageId) {
132
              this.formComponent.getDivs("");
133
            }
134

    
135
            this.showLoading = false;
136
          },
137
          error => this.handleError('System error retrieving class', error)
138
       );
139
      }
140
    }
141

    
142
    private updateForm(divHelpContent : DivHelpContent) {
143
        this.divHelpContent = divHelpContent;
144

    
145
        this.getDivId(divHelpContent.divId as string);
146

    
147
        this.formComponent.myForm.patchValue((divHelpContent));
148
    }
149

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

    
158
            let divHelpContent : DivHelpContent = this.formComponent.myForm.value;
159

    
160
            this._helpContentService.updateDivHelpContent(divHelpContent, this.properties.adminToolsAPIURL, this.communityPid).subscribe(
161
                _ => {
162
                  if(this.pageId) {
163
                    this.router.navigate( ['/classContents/'], { queryParams: { "communityId": this.communityPid, "pageId": this.pageId  } } );
164
                  } else {
165
                    this.router.navigate(['/classContents'], { queryParams: { "communityId": this.communityPid } } );
166
                  }
167
                  this.showLoading = false;
168
                  this._clearCacheService.clearCache("class help content updated");
169
                },
170
                err => this.handleUpdateError('System error updating class content', err)
171
            );
172
        } else {
173
            this.errorMessage = "Please fill all required fields";
174
        }
175
      }
176
    }
177

    
178
    public cancelCustom() {
179
      this.errorMessage = "";
180
      this.updateErrorMessage = "";
181

    
182
      if(this.pageId) {
183
        this.router.navigate( ['/classContents/'], { queryParams: { "communityId": this.communityPid, "pageId": this.pageId  } } );
184
      } else {
185
        this.router.navigate(['/classContents'], { queryParams: { "communityId": this.communityPid } } );
186
      }
187
    }
188
}
(10-10/15)