Project

General

Profile

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

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

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

    
18
export class NewDivHelpContentComponent {
19

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

    
23
    //private errorMessage : string = null;
24

    
25
    public communityPid: string;
26

    
27
    public pageId: string;
28
    public page: Page;
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.route.queryParams.subscribe(params => {
47
             HelperFunctions.scroll();
48

    
49
             this.communityPid = params['communityId'];
50
             this.pageId = params['pageId'];
51

    
52

    
53
             if(this.pageId) {
54
               this.getPage(this.pageId);
55
             } else {
56
               this.showLoading = false;
57
             }
58
           });
59
        });
60
    }
61

    
62
    private getPage(pageId: string) {
63
      this._helpContentService.getPage(pageId,this.properties.adminToolsAPIURL).subscribe(
64
        page => {
65
          if( (this.communityPid == 'openaire' && !page.openaire)
66
            || (this.communityPid == 'connect' && !page.connect)
67
            || (this.communityPid != 'openaire' && this.communityPid != 'connect' && !page.communities)) {
68
            this.router.navigate(['/classContents'], { queryParams: { "communityId": this.communityPid} });
69
          } else {
70
            this.page = page;
71
            this.showLoading = false;
72
            console.info(this.page);
73
          }
74
        },
75
        error => this.handleError('System error retrieving page with id: '+pageId, error)
76
      );
77
    }
78

    
79
    public saveCustom() {
80
      if(!Session.isLoggedIn()){
81
        this.router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this.router.url} });
82
      } else {
83
        if(this.formComponent.myForm.valid) {
84
            this.showLoading = true;
85
            this.updateErrorMessage = "";
86

    
87
            let divHelpContent : DivHelpContent = this.formComponent.myForm.value;
88

    
89
            this._helpContentService.insertOrUpdateDivHelpContent(divHelpContent, this.properties.adminToolsAPIURL).subscribe(
90
                _ => {
91
                  if(this.pageId) {
92
                    this.router.navigate( ['/classContents/'], { queryParams: { "communityId": this.communityPid, "pageId": this.pageId  } } );
93
                  } else {
94
                    this.router.navigate(['/classContents'], { queryParams: { "communityId": this.communityPid } } );
95
                  }
96
                  this.showLoading = false;
97
                },
98
                err => this.handleUpdateError('System error saving page content', err)
99
            );
100
        } else {
101
            this.errorMessage = "Please fill all required fields";
102
        }
103
      }
104
    }
105

    
106
    public cancelCustom() {
107
      if(this.pageId) {
108
        this.router.navigate( ['/classContents/'], { queryParams: { "communityId": this.communityPid, "pageId": this.pageId  } } );
109
      } else {
110
        this.router.navigate(['/classContents'], { queryParams: { "communityId": this.communityPid } } );
111
      }
112
    }
113

    
114
    handleUpdateError(message: string, error) {
115
        this.errorMessage = message;
116
        console.log('Server responded: ' + error);
117
        this.showLoading = false;
118
    }
119

    
120
    handleError(message: string, error) {
121
      this.errorMessage = message;
122
      console.log('Server responded: ' + error);
123
      this.showLoading = false;
124
    }
125
}
(11-11/12)