Project

General

Profile

1
/**
2
 * Created by stefania on 7/13/17.
3
 */
4
import { Component, ViewChild, ElementRef } from '@angular/core';
5
import { ActivatedRoute, Router } from "@angular/router";
6
import { PageContentFormComponent } from "./page-help-content-form.component";
7
import { PageHelpContent } from "../../domain/page-help-content";
8
import { HelpContentService } from "../../services/help-content.service";
9
import { EnvProperties } from '../../openaireLibrary/utils/properties/env-properties';
10

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

    
17
@Component({
18
    selector: 'new-page-help-content',
19
    templateUrl: 'new-page-help-content.component.html',
20
})
21

    
22
export class NewPageHelpContentComponent {
23

    
24
    @ViewChild(PageContentFormComponent)
25
    public formComponent : PageContentFormComponent;
26

    
27
    //private errorMessage : string = null;
28

    
29
    public communityPid: string;
30

    
31
    public pageId: string;
32
    public page: Page;
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

    
47
    ngOnInit() {
48
      this.route.data
49
        .subscribe((data: { envSpecific: EnvProperties }) => {
50
           this.properties = data.envSpecific;
51
           this.route.queryParams.subscribe(params => {
52
             HelperFunctions.scroll();
53
             this.title.setTitle('Administration Dashboard | New Page Help Text');
54
             this.communityPid = params['communityId'];
55
             this.pageId = params['pageId'];
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.getPage(pageId,this.properties.adminToolsAPIURL).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(['/pageContents'], { queryParams: { "communityId": this.communityPid} });
74
        } else {
75
          this.page = page;
76
          this.formComponent.setPlacements(this.page);
77
          this.showLoading = false;
78
          console.info(this.page);
79
        }
80
      },
81
      error => this.handleError('System error retrieving page with id: '+pageId, error)
82
    );
83
  }
84

    
85
  public saveCustom() {
86
      if(!Session.isLoggedIn()){
87
        this.router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this.router.url} });
88
      } else {
89
        //this.errorMessage = null;
90

    
91
        if(this.formComponent.myForm.valid) {
92
            this.showLoading = true;
93
            this.updateErrorMessage = "";
94

    
95
            let pageHelpContent : PageHelpContent = this.formComponent.myForm.value;
96

    
97
            this._helpContentService.savePageHelpContent(pageHelpContent, this.properties.adminToolsAPIURL).subscribe(
98
                _ => {
99
                  if(this.pageId) {
100
                    this.router.navigate( ['/pageContents/'], { queryParams: { "communityId": this.communityPid, "pageId": this.pageId  } } );
101
                  } else {
102
                    this.router.navigate(['/pageContents'], { queryParams: { "communityId": this.communityPid} });
103
                  }
104
                  this.showLoading = false;
105
                },
106
                err => this.handleUpdateError('System error saving page content', err)
107
            );
108
        } else {
109
            this.errorMessage = "Please fill all required fields";
110
        }
111
      }
112
    }
113

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

    
122
  handleUpdateError(message: string, error) {
123
      this.updateErrorMessage = message;
124
      console.log('Server responded: ' + error);
125

    
126
      this.showLoading = false;
127
  }
128

    
129
  handleError(message: string, error) {
130
    this.errorMessage = message;
131
    console.log('Server responded: ' + error);
132
    this.showLoading = false;
133
  }
134
}
(7-7/15)