Project

General

Profile

1
import { Component, OnInit, Input } from '@angular/core';
2
import { ActivatedRoute, Router } from "@angular/router";
3
import { FormGroup, FormBuilder, Validators } from "@angular/forms";
4
import { Page } from "../../utils/entities/adminTool/page";
5
import { DivId } from "../../utils/entities/adminTool/divId";
6
import { HelpContentService } from "../../services/help-content.service";
7
import { EnvProperties } from '../../utils/properties/env-properties';
8

    
9
import {Session} from '../../login/utils/helper.class';
10
import {LoginErrorCodes} from '../../login/utils/guardHelper.class';
11

    
12
@Component({
13
    selector: 'div-content-form',
14
    templateUrl: './div-help-content-form.component.html',
15
})
16

    
17
export class DivContentFormComponent implements OnInit{
18

    
19
    @Input('group')
20
    myForm: FormGroup;
21
    @Input('communityPid')
22
    communityPid: string;
23
    @Input('pageId')
24
    pageId: string;
25
    @Input('editMode')
26
    editMode: boolean = false;
27

    
28
    //public divIdName: string = '';
29

    
30
    private communityId: string = '';
31

    
32
    showPageSelect: boolean = true;
33
    selectedDiv: DivId;
34

    
35
    private availablePages : Page[] = [];
36
    private availableDivs : DivId[] = [];
37

    
38
    private ckeditorContent : string;
39

    
40
    public properties:EnvProperties = null;
41

    
42
    public showLoading: boolean = true;
43
    public errorMessage: string = '';
44
    @Input() updateErrorMessage: string = '';
45

    
46
    constructor(private route: ActivatedRoute, private _router: Router, private _fb: FormBuilder, private _helpContentService: HelpContentService){}
47

    
48
    ngOnInit() {
49
      this.myForm = this.form;
50
      this.route.data
51
        .subscribe((data: { envSpecific: EnvProperties }) => {
52
           this.properties = data.envSpecific;
53
           this.route.queryParams.subscribe(params => {
54

    
55
             if(!Session.isLoggedIn()){
56
               this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this._router.url} });
57
             } else {
58
               if(this.pageId) {
59
                 this.showPageSelect = false;
60
                 this.getDivs(this.pageId);
61
               } else {
62
                 if(!this.editMode) {
63
                   this._helpContentService.getCommunityPagesWithDivId(this.communityPid, this.properties.adminToolsAPIURL).subscribe(
64
                     pages => {
65
                       this.availablePages = pages;
66
                       this.showLoading = false;
67
                     },
68
                     error => this.handleError('System error retrieving pages', error));
69
                   }
70
              }
71

    
72
              this.getCommunity(this.communityPid);
73
            }
74
           });
75
        });
76

    
77
    }
78

    
79
    public pageSelected(event) {
80
      if(!Session.isLoggedIn()){
81
        this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this._router.url} });
82
      } else {
83
        this.getDivs(event.target.value);
84
      }
85
    }
86

    
87
    public divIdSelected(div: DivId) {
88
      this.selectedDiv = div;
89
    }
90

    
91
    public getCommunity(communityPid: string) {
92
      if(!Session.isLoggedIn()){
93
        this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this._router.url} });
94
      } else {
95
        this.showLoading = true;
96
        this.errorMessage = '';
97

    
98
        this._helpContentService.getCommunity(this.communityPid, this.properties.adminToolsAPIURL).subscribe(
99
          community => {
100
            this.communityId = community._id;
101

    
102
            this.myForm.patchValue({
103
              community: this.communityId
104
            });
105
            this.showLoading = false;
106
          },
107
          error => this.handleError('System error retrieving community', error)
108
        );
109
      }
110
    }
111

    
112
    public getDivs(pageId: string) {
113
      if(!Session.isLoggedIn()){
114
        this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this._router.url} });
115
      } else {
116
        //this.showLoading = true;
117
        this.errorMessage = '';
118

    
119
        this._helpContentService.getDivIdsFullByPortal(pageId, this.properties.adminToolsAPIURL, this.communityPid).subscribe(
120
            divs => {
121
              this.availableDivs = divs;
122
              this.pageId = pageId;
123

    
124
              this.showLoading = false;
125
            },
126
            error => this.handleError('System error retrieving pages', error));
127
      }
128
    }
129

    
130
    // public selectedDiv(event) {
131
    //   console.info(event.target.value);
132
    // }
133

    
134
    public get form() {
135
        return this._fb.group({
136
            divId: ['', Validators.required],
137
            content: ['', Validators.required],
138
            isActive: true,
139
            portal: '',
140
            _id : '',
141
        });
142
    }
143

    
144
    public reset() {
145
        this.myForm.patchValue({
146
          divId: '',
147
          content: '',
148
          isActive: true,
149
          portal: '',
150
          _id : ''
151
        });
152
        this.myForm.markAsPristine();
153
    }
154

    
155
    handleError(message: string, error) {
156
        this.errorMessage = message;
157
        console.log('Server responded: ' + error);
158

    
159
        this.showLoading = false;
160
    }
161
}
(2-2/15)