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

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

    
18
export class EditDivHelpContentComponent implements OnInit, OnDestroy{
19

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

    
23
    public communityPid: string;
24
    public pageId: string;
25

    
26
    private sub: Subscription;
27

    
28
    private divHelpContent: DivHelpContent;
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.sub = this.route.queryParams.subscribe(params => {
47
             HelperFunctions.scroll();
48

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

    
54
            this.getDivHelpContent(divContentId);
55
          });
56
        });
57
    }
58
    ngOnDestroy() {
59
        this.sub.unsubscribe();
60
    }
61

    
62
    handleError(message: string, error) {
63
        this.errorMessage = message;
64
        console.log('Server responded: ' + error);
65
        this.showLoading = false;
66
    }
67

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

    
74
    private getDivHelpContent(divContentId: string) {
75
      if(!Session.isLoggedIn()){
76
        this.router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this.router.url} });
77
      } else {
78
        this.showLoading = true;
79
        this.errorMessage = "";
80
        this.updateErrorMessage = "";
81

    
82
        this._helpContentService.getDivHelpContent(divContentId, this.properties.adminToolsAPIURL).subscribe(
83
            divHelpContent => {
84
              this.updateForm(divHelpContent);
85
              this.showLoading = false;
86
            },
87
            error => this.handleError('System error retrieving class help content', error));
88
      }
89
    }
90

    
91
    getDivId(divId: string) {
92
      if(!Session.isLoggedIn()){
93
        this.router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this.router.url} });
94
      } else {
95
        this.showLoading = true;
96

    
97
        this._helpContentService.getDivIdFull(divId, this.properties.adminToolsAPIURL).subscribe(
98
          div => {
99
            this.formComponent.selectedDiv = div;
100

    
101
            if(!this.pageId) {
102
              this.formComponent.getDivs("");
103
            }
104

    
105
            this.showLoading = false;
106
          },
107
          error => this.handleError('System error retrieving class', error)
108
       );
109
      }
110
    }
111

    
112
    private updateForm(divHelpContent : DivHelpContent) {
113
        this.divHelpContent = divHelpContent;
114

    
115
        this.getDivId(divHelpContent.divId as string);
116

    
117
        this.formComponent.myForm.patchValue((divHelpContent));
118
    }
119

    
120
    public saveCustom() {
121
      if(!Session.isLoggedIn()){
122
        this.router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this.router.url} });
123
      } else {
124
        if(this.formComponent.myForm.valid) {
125
            this.showLoading = true;
126
            this.updateErrorMessage = "";
127

    
128
            let divHelpContent : DivHelpContent = this.formComponent.myForm.value;
129

    
130
            this._helpContentService.insertOrUpdateDivHelpContent(divHelpContent, this.properties.adminToolsAPIURL).subscribe(
131
                _ => {
132
                  if(this.pageId) {
133
                    this.router.navigate( ['/classContents/'], { queryParams: { "communityId": this.communityPid, "pageId": this.pageId  } } );
134
                  } else {
135
                    this.router.navigate(['/classContents'], { queryParams: { "communityId": this.communityPid } } );
136
                  }
137
                  this.showLoading = false;
138
                },
139
                err => this.handleUpdateError('System error updating class content', err)
140
            );
141
        } else {
142
            this.errorMessage = "Please fill all required fields";
143
        }
144
      }
145
    }
146

    
147
    public cancelCustom() {
148
      this.errorMessage = "";
149
      this.updateErrorMessage = "";
150

    
151
      if(this.pageId) {
152
        this.router.navigate( ['/classContents/'], { queryParams: { "communityId": this.communityPid, "pageId": this.pageId  } } );
153
      } else {
154
        this.router.navigate(['/classContents'], { queryParams: { "communityId": this.communityPid } } );
155
      }
156
    }
157
}
(10-10/15)