Project

General

Profile

1 53600 argiro.kok
/**
2
 * Created by stefania on 7/14/17.
3
 */
4 54074 konstantin
import { Component, ViewChild, OnInit, OnDestroy, ElementRef } from '@angular/core';
5 53600 argiro.kok
import { PageContentFormComponent } from "./page-help-content-form.component";
6 55972 argiro.kok
import { Subscription } from "rxjs";
7 53600 argiro.kok
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 53735 konstantin
import {Session} from '../../openaireLibrary/login/utils/helper.class';
13
import {LoginErrorCodes} from '../../openaireLibrary/login/utils/guardHelper.class';
14 55416 konstantin
import {HelperFunctions} from "../../openaireLibrary/utils/HelperFunctions.class";
15 56740 konstantin
import {Page} from "../../domain/page";
16 58371 k.triantaf
import {Title} from '@angular/platform-browser';
17 53735 konstantin
18 53600 argiro.kok
@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 56740 konstantin
    public communityPid: string;
29 53600 argiro.kok
30 56740 konstantin
    public pageId: string;
31
    public page: Page;
32 53600 argiro.kok
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 54074 konstantin
        private element: ElementRef,
46 53600 argiro.kok
        private route: ActivatedRoute,
47
        private router: Router,
48 58371 k.triantaf
        private title: Title,
49 53600 argiro.kok
        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 55416 konstantin
             HelperFunctions.scroll();
57 54074 konstantin
58 53600 argiro.kok
             let pageContentId = params['pageContentId'];
59
             this.communityPid = params['communityId'];
60
             this.pageId = params['pageId'];
61 58371 k.triantaf
             this.title.setTitle('Administration Dashboard | Edit Page Help Text');
62 56740 konstantin
             if(!pageContentId) {
63
               this.router.navigate(['/pageContents'], { queryParams: { "communityId": this.communityPid} });
64
             }
65
66 53600 argiro.kok
             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 56740 konstantin
    private getPage(pageId: string) {
87
      this._helpContentService.getPage(pageId,this.properties.adminToolsAPIURL).subscribe(
88
        page => {
89 59297 konstantin
          // 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 56740 konstantin
            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 53600 argiro.kok
    private getPageHelpContent(pageContentId: string) {
105 53735 konstantin
      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 53600 argiro.kok
112 53735 konstantin
        this._helpContentService.getPageHelpContent(pageContentId as string, this.properties.adminToolsAPIURL).subscribe(
113
          pageHelpContent => {
114 56740 konstantin
            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 53735 konstantin
            this.updateForm(pageHelpContent);
122 56740 konstantin
            //this.showLoading = false;
123 53735 konstantin
          },
124
          error => this.handleError('System error retrieving page help content', error));
125
      }
126 53600 argiro.kok
    }
127
128
    private updateForm(pageHelpContent : PageHelpContent) {
129
        this.pageHelpContent = pageHelpContent;
130
        this.formComponent.myForm.patchValue((pageHelpContent));
131
        // console.log("patching",pageHelpContent);
132
    }
133
134 56471 argiro.kok
  public saveCustom() {
135 53735 konstantin
      if(!Session.isLoggedIn()){
136
        this.router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this.router.url} });
137
      } else {
138 53600 argiro.kok
        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 53735 konstantin
      }
158 53600 argiro.kok
    }
159
160 56471 argiro.kok
  public cancelCustom() {
161 53600 argiro.kok
      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
}