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

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

    
20
export class EditDivHelpContentComponent implements OnInit, OnDestroy{
21

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

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

    
29
    private sub: Subscription;
30

    
31
    private divHelpContent: DivHelpContent;
32

    
33
    public properties:EnvProperties = null;
34

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

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

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

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

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

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

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

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

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

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

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

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

    
129
            if(!this.pageId) {
130
              this.formComponent.getDivs("");
131
            }
132

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

    
140
    private updateForm(divHelpContent : DivHelpContent) {
141
        this.divHelpContent = divHelpContent;
142

    
143
        this.getDivId(divHelpContent.divId as string);
144

    
145
        this.formComponent.myForm.patchValue((divHelpContent));
146
    }
147

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

    
156
            let divHelpContent : DivHelpContent = this.formComponent.myForm.value;
157

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

    
175
    public cancelCustom() {
176
      this.errorMessage = "";
177
      this.updateErrorMessage = "";
178

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