Project

General

Profile

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

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

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

    
23
export class EditPageHelpContentComponent implements OnInit, OnDestroy{
24

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

    
28
    public communityPid: string;
29

    
30
    public pageId: string;
31
    public page: Page;
32

    
33
    private sub: Subscription;
34

    
35
    private pageHelpContent: PageHelpContent;
36

    
37
    //private errorMessage : string = null;
38
    public properties:EnvProperties = null;
39

    
40
    public showLoading: boolean = true;
41
    public errorMessage: string = '';
42
    public updateErrorMessage: string = '';
43

    
44
    constructor(
45
        private element: ElementRef,
46
        private route: ActivatedRoute,
47
        private router: Router,
48
        private title: Title,
49
        private _helpContentService: HelpContentService) {}
50

    
51
    ngOnInit() {
52
      this.route.data
53
        .subscribe((data: { envSpecific: EnvProperties }) => {
54
           this.properties = data.envSpecific;
55
           this.sub = this.route.queryParams.subscribe(params => {
56
             HelperFunctions.scroll();
57

    
58
             let pageContentId = params['pageContentId'];
59
             this.communityPid = params['communityId'];
60
             this.pageId = params['pageId'];
61
             this.title.setTitle('Administration Dashboard | Edit Page Help Text');
62
             if(!pageContentId) {
63
               this.router.navigate(['/pageContents'], { queryParams: { "communityId": this.communityPid} });
64
             }
65

    
66
             this.getPageHelpContent(pageContentId);
67
           });
68
      });
69
    }
70
    ngOnDestroy() {
71
        this.sub.unsubscribe();
72
    }
73

    
74
    handleError(message: string, error) {
75
        this.errorMessage = message;
76
        console.log('Server responded: ' + error);
77
        this.showLoading = false;
78
    }
79

    
80
    handleUpdateError(message: string, error) {
81
        this.updateErrorMessage = message;
82
        console.log('Server responded: ' + error);
83
        this.showLoading = false;
84
    }
85

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

    
104
    private getPageHelpContent(pageContentId: string) {
105
      if(!Session.isLoggedIn()){
106
        this.router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this.router.url} });
107
      } else {
108
        this.showLoading = true;
109
        this.errorMessage = "";
110
        this.updateErrorMessage = "";
111

    
112
        this._helpContentService.getPageHelpContent(pageContentId as string, this.properties.adminToolsAPIURL).subscribe(
113
          pageHelpContent => {
114
            if(this.pageId && this.pageId != pageHelpContent.page) {
115
              this.router.navigate(['/pageContents'], { queryParams: { "communityId": this.communityPid} });
116
            } else if(this.pageId) {
117
              this.getPage(this.pageId);
118
            } else {
119
              this.getPage(<string>pageHelpContent.page);
120
            }
121
            this.updateForm(pageHelpContent);
122
            //this.showLoading = false;
123
          },
124
          error => this.handleError('System error retrieving page help content', error));
125
      }
126
    }
127

    
128
    private updateForm(pageHelpContent : PageHelpContent) {
129
        this.pageHelpContent = pageHelpContent;
130
        this.formComponent.myForm.patchValue((pageHelpContent));
131
        // console.log("patching",pageHelpContent);
132
    }
133

    
134
  public saveCustom() {
135
      if(!Session.isLoggedIn()){
136
        this.router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this.router.url} });
137
      } else {
138
        if(this.formComponent.myForm.valid) {
139
            this.showLoading = true;
140
            this.updateErrorMessage = "";
141

    
142
            let pageHelpContent : PageHelpContent = this.formComponent.myForm.value;
143
            this._helpContentService.updatePageHelpContent(pageHelpContent, this.properties.adminToolsAPIURL).subscribe(
144
                _ => {
145
                  if(this.pageId) {
146
                    this.router.navigate( ['/pageContents/'], { queryParams: { "communityId": this.communityPid, "pageId": this.pageId  } } );
147
                  } else {
148
                    this.router.navigate(['/pageContents'], { queryParams: { "communityId": this.communityPid} } );
149
                  }
150
                  this.showLoading = false;
151
                },
152
                err => this.handleUpdateError('System error updating page content', err)
153
            );
154
        } else {
155
            this.errorMessage = "Please fill all required fields";
156
        }
157
      }
158
    }
159

    
160
  public cancelCustom() {
161
      this.errorMessage = "";
162
      this.updateErrorMessage = "";
163

    
164
      if(this.pageId) {
165
        this.router.navigate( ['/pageContents/'], { queryParams: { "communityId": this.communityPid, "pageId": this.pageId  } } );
166
      } else {
167
        this.router.navigate(['/pageContents'], { queryParams: { "communityId": this.communityPid} } );
168
      }
169
    }
170
}
(3-3/15)