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
import {ClearCacheService} from "../../openaireLibrary/services/clear-cache.service";
18

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

    
24
export class EditPageHelpContentComponent implements OnInit, OnDestroy{
25

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

    
29
    public communityPid: string;
30

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

    
34
    private sub: Subscription;
35

    
36
    private pageHelpContent: PageHelpContent;
37

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
163
  public cancelCustom() {
164
      this.errorMessage = "";
165
      this.updateErrorMessage = "";
166

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