Project

General

Profile

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

    
8
import {Session} from '../../login/utils/helper.class';
9
import {LoginErrorCodes} from '../../login/utils/guardHelper.class';
10
import {properties} from '../../../../environments/environment';
11
import {Subscriber, Subscription, zip} from 'rxjs';
12
import {HelperFunctions} from '../../utils/HelperFunctions.class';
13
import {DivHelpContent} from '../../utils/entities/adminTool/div-help-content';
14

    
15
declare var UIkit;
16

    
17
@Component({
18
  selector: 'class-content-form',
19
  templateUrl: './class-help-content-form.component.html',
20
})
21
export class ClassContentFormComponent implements OnInit {
22
  
23
  myForm: FormGroup;
24
  portal: string;
25
  pageId: string;
26
  pageContentId: string;
27
  page: Page;
28
  classOptions = [];
29

    
30
  public properties: EnvProperties = null;
31
  
32
  public showLoading: boolean = true;
33
  public errorMessage: string = '';
34
  @Input() updateErrorMessage: string = '';
35
  private subs: Subscription[] = [];
36
  public pageHelpContent: DivHelpContent;
37
  
38
  constructor(private route: ActivatedRoute, private _router: Router, private _fb: FormBuilder, private _helpContentService: HelpContentService) {
39
  }
40
  
41
  ngOnInit() {
42
    
43
    this.properties = properties;
44
    this.subs.push(this.route.params.subscribe(params => {
45
      this.portal = (this.route.snapshot.data.portal) ? this.route.snapshot.data.portal : this.route.snapshot.params[this.route.snapshot.data.param];
46
      this.subs.push(this.route.queryParams.subscribe(params => {
47
        HelperFunctions.scroll();
48
        this.pageId = params['pageId'];
49
        this.myForm = this.form;
50
        this.pageContentId = params['pageContentId'];
51
        if (!this.pageId) {
52
          this._router.navigate(['../'], {relativeTo: this.route});
53
        }
54
        this.getInfo(this.pageId);
55
        if (!Session.isLoggedIn()) {
56
          this._router.navigate(['/user-info'], {
57
            queryParams: {
58
              "errorCode": LoginErrorCodes.NOT_VALID,
59
              "redirectUrl": this._router.url
60
            }
61
          });
62
        }
63
      }));
64
    }));
65
  }
66
  
67
  ngOnDestroy() {
68
    this.subs.forEach(value => {
69
      if (value instanceof Subscriber) {
70
        value.unsubscribe();
71
      }
72
    });
73
  }
74
  
75
  getInfo(pageId: string) {
76
    this.showLoading = true;
77
    let obs = zip(this._helpContentService.getPageByPortal(pageId, this.properties.adminToolsAPIURL, this.portal),
78
      this._helpContentService.getDivIdsFullByPortal(pageId, this.properties.adminToolsAPIURL, this.portal));
79
    this.subs.push(obs.subscribe(
80
      results => {
81
        this.page = results[0];
82
        if (this.properties.adminToolsPortalType != this.page.portalType) {
83
          this._router.navigate(['../'], {relativeTo: this.route});
84
        }
85
        this.setOptions(results[1]);
86
        if (!this.pageContentId) {
87
         this.updateForm(null);
88
          this.showLoading = false;
89
        } else {
90
          this.subs.push(this._helpContentService.getDivHelpContent( this.pageContentId, this.properties.adminToolsAPIURL, this.portal).subscribe(pageHelpContent=>{
91
            this.pageHelpContent = pageHelpContent;
92
            if (this.properties.adminToolsPortalType != this.page.portalType) {
93
              this._router.navigate(['../'], {relativeTo: this.route});
94
            }
95
            this.updateForm(this.pageHelpContent);
96
            this.showLoading = false;
97
          },
98
            error => {
99
            this.handleError('System error retrieving content by id '+ this.pageContentId, error)
100
          }));
101
        }
102
      }));
103

    
104
  }
105

    
106
  private updateForm(pageHelpContent: DivHelpContent) {
107
    this.pageHelpContent = pageHelpContent;
108
    this.myForm = this.form;
109
    if (this.pageHelpContent) {
110
      this.myForm.patchValue((pageHelpContent));
111
      this.myForm.get('divId').disable();
112
    }
113
    this.myForm.markAsPristine();
114
    
115
  }
116

    
117
  
118
  public setOptions(divIds) {
119
    this.classOptions = [];
120
    for(let divid of divIds){
121
      this.classOptions.push({label:divid.name, value:divid._id});
122
    }
123

    
124
  }
125
  
126
  public get form() {
127
    return this._fb.group({
128
      divId: ['', Validators.required],
129
      content: ['', Validators.required],
130
      isActive: true,
131
      portal: this.portal,
132
      _id : '',
133
    });
134

    
135
  }
136
  
137
  public reset() {
138
    this.myForm.patchValue({
139
      divId: ['', Validators.required],
140
      content: ['', Validators.required],
141
      isActive: true,
142
      portal: '',
143
      _id : '',
144
    });
145
    this.myForm.markAsPristine();
146
  }
147
  
148
  handleError(message: string, error) {
149
    this.errorMessage = message;
150
    console.error('Server responded: ' + error);
151
    
152
    this.showLoading = false;
153
  }
154
  
155
  public saveCustom() {
156
    if (!Session.isLoggedIn()) {
157
      this._router.navigate(['/user-info'], {
158
        queryParams: {
159
          "errorCode": LoginErrorCodes.NOT_VALID,
160
          "redirectUrl": this._router.url
161
        }
162
      });
163
    } else {
164

    
165
      if (this.myForm.valid) {
166
        this.showLoading = true;
167
        this.updateErrorMessage = "";
168
        this.myForm.get('divId').enable();
169
        let pageHelpContent: DivHelpContent = this.myForm.value;
170
        this.subs.push(this._helpContentService.insertOrUpdateDivHelpContent(pageHelpContent, this.properties.adminToolsAPIURL, this.portal).subscribe(
171
          _ => {
172
            this._router.navigate(['../'], {queryParams: {"pageId": this.pageId}, relativeTo: this.route});
173
            UIkit.notification('Page content has been <b>successfully updated</b>', {
174
              status: 'success',
175
              timeout: 6000,
176
              pos: 'bottom-right'
177
            });
178
            this.showLoading = false;
179
          },
180
          err => this.handleUpdateError('System error saving page content', err)
181
        ));
182
      } else {
183
        this.showLoading = false;
184
        this.errorMessage = "Please fill all required fields";
185
      }
186
    }
187
  }
188

    
189
  public resetCustom() {
190
    this.showLoading = true;
191
    this.updateForm(this.pageHelpContent);
192
    this.showLoading = false;
193
  }
194
  
195
  handleUpdateError(message: string, error) {
196
    
197
    this.updateErrorMessage = message;
198
    console.error('Server responded: ' + error);
199
    
200
    this.showLoading = false;
201
  }
202
  
203
  changeStatus() {
204
    this.myForm.get('isActive').setValue(!this.myForm.get('isActive').value);
205
    if (this.pageHelpContent && this.myForm.get('isActive').value != this.pageHelpContent.isActive || !this.pageHelpContent && !this.myForm.get('isActive').value) {
206
      this.myForm.get('isActive').markAsDirty();
207
    } else {
208
      this.myForm.get('isActive').markAsPristine()
209
    }
210
  }
211
  
212
  contentChanged() {
213
    if (this.pageHelpContent && this.myForm.get('content').value != this.pageHelpContent.content || !this.pageHelpContent && this.myForm.get('content').value != '') {
214
      this.myForm.get('content').markAsDirty();
215
    } else {
216
      this.myForm.get('content').markAsPristine()
217
    }
218
  }
219
}
(3-3/8)