Project

General

Profile

1
/**
2
 * Created by stefania on 7/14/17.
3
 */
4
import { Component, ViewChild, OnInit, OnDestroy } from '@angular/core';
5
import { PageContentFormComponent } from "./page-help-content-form.component";
6
import { Subscription } from "rxjs/Subscription";
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
@Component({
13
    selector: 'edit-page-help-content',
14
    templateUrl: 'edit-page-help-content.component.html',
15
})
16

    
17
export class EditPageHelpContentComponent implements OnInit, OnDestroy{
18

    
19
    @ViewChild(PageContentFormComponent)
20
    public formComponent : PageContentFormComponent;
21

    
22
    private communityPid: string;
23

    
24
    private pageId: string;
25

    
26
    private sub: Subscription;
27

    
28
    private pageHelpContent: PageHelpContent;
29

    
30
    //private errorMessage : string = null;
31
    public properties:EnvProperties = null;
32

    
33
    public showLoading: boolean = true;
34
    public errorMessage: string = '';
35
    public updateErrorMessage: string = '';
36

    
37
    constructor(
38
        private route: ActivatedRoute,
39
        private router: Router,
40
        private _helpContentService: HelpContentService) {}
41

    
42
    ngOnInit() {
43
      this.route.data
44
        .subscribe((data: { envSpecific: EnvProperties }) => {
45
           this.properties = data.envSpecific;
46
           this.sub = this.route.queryParams.subscribe(params => {
47
             let pageContentId = params['pageContentId'];
48
             this.communityPid = params['communityId'];
49
             this.pageId = params['pageId'];
50

    
51
             this.getPageHelpContent(pageContentId);
52
           });
53
      });
54
    }
55
    ngOnDestroy() {
56
        this.sub.unsubscribe();
57
    }
58

    
59
    handleError(message: string, error) {
60
        this.errorMessage = message;
61
        console.log('Server responded: ' + error);
62
        this.showLoading = false;
63
    }
64

    
65
    handleUpdateError(message: string, error) {
66
        this.updateErrorMessage = message;
67
        console.log('Server responded: ' + error);
68
        this.showLoading = false;
69
    }
70

    
71
    private getPageHelpContent(pageContentId: string) {
72
      this.showLoading = true;
73
      this.errorMessage = "";
74
      this.updateErrorMessage = "";
75

    
76
      this._helpContentService.getPageHelpContent(pageContentId as string, this.properties.adminToolsAPIURL).subscribe(
77
        pageHelpContent => {
78
          this.updateForm(pageHelpContent);
79
          this.showLoading = false;
80
        },
81
        error => this.handleError('System error retrieving page help content', error));
82
    }
83

    
84
    private updateForm(pageHelpContent : PageHelpContent) {
85
        this.pageHelpContent = pageHelpContent;
86
        console.info(pageHelpContent);
87
        this.formComponent.myForm.patchValue((pageHelpContent));
88
        // console.log("patching",pageHelpContent);
89
    }
90

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

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

    
114
    private cancelCustom() {
115
      this.errorMessage = "";
116
      this.updateErrorMessage = "";
117

    
118
      if(this.pageId) {
119
        this.router.navigate( ['/pageContents/'], { queryParams: { "communityId": this.communityPid, "pageId": this.pageId  } } );
120
      } else {
121
        this.router.navigate(['/pageContents'], { queryParams: { "communityId": this.communityPid} } );
122
      }
123
    }
124
}
(2-2/8)