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 "../../domain/div-help-content";
5
import { HelpContentService } from "../../services/help-content.service";
6
import { EnvProperties } from '../../openaireLibrary/utils/properties/env-properties';
7

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

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

    
20
export class NewDivHelpContentComponent {
21

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

    
25
    //private errorMessage : string = null;
26

    
27
    public communityPid: string;
28

    
29
    public pageId: string;
30
    public page: Page;
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 title: Title,
43
        private _helpContentService: HelpContentService,
44
        private _clearCacheService: ClearCacheService) {}
45

    
46
    ngOnInit() {
47
      this.route.data
48
        .subscribe((data: { envSpecific: EnvProperties }) => {
49
           this.properties = data.envSpecific;
50
           this.route.queryParams.subscribe(params => {
51
             HelperFunctions.scroll();
52
             this.title.setTitle('Administration Dashboard | New Class Help Text');
53
             this.communityPid = params['communityId'];
54
             this.pageId = params['pageId'];
55

    
56

    
57
             if(this.pageId) {
58
               this.getPage(this.pageId);
59
             } else {
60
               this.showLoading = false;
61
             }
62
           });
63
        });
64
    }
65

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

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

    
92
            let divHelpContent : DivHelpContent = this.formComponent.myForm.value;
93

    
94
            this._helpContentService.saveDivHelpContent(divHelpContent, this.properties.adminToolsAPIURL, this.communityPid).subscribe(
95
                _ => {
96
                  if(this.pageId) {
97
                    this.router.navigate( ['/classContents/'], { queryParams: { "communityId": this.communityPid, "pageId": this.pageId  } } );
98
                  } else {
99
                    this.router.navigate(['/classContents'], { queryParams: { "communityId": this.communityPid } } );
100
                  }
101
                  this.showLoading = false;
102
                  this._clearCacheService.clearCache("class help content saved");
103
                },
104
                err => this.handleUpdateError('System error saving page content', err)
105
            );
106
        } else {
107
            this.errorMessage = "Please fill all required fields";
108
        }
109
      }
110
    }
111

    
112
    public cancelCustom() {
113
      if(this.pageId) {
114
        this.router.navigate( ['/classContents/'], { queryParams: { "communityId": this.communityPid, "pageId": this.pageId  } } );
115
      } else {
116
        this.router.navigate(['/classContents'], { queryParams: { "communityId": this.communityPid } } );
117
      }
118
    }
119

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

    
126
    handleError(message: string, error) {
127
      this.errorMessage = message;
128
      console.log('Server responded: ' + error);
129
      this.showLoading = false;
130
    }
131
}
(14-14/15)