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 {ConnectHelper} from '../../connect/connectHelper';
14
import {DivHelpContent} from '../../utils/entities/adminTool/div-help-content';
15

    
16
declare var UIkit;
17

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

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

    
108
  }
109

    
110
  private updateForm(pageHelpContent: DivHelpContent) {
111
    this.pageHelpContent = pageHelpContent;
112
    this.myForm = this.form;
113
    if (this.pageHelpContent) {
114
      this.myForm.patchValue((pageHelpContent));
115
      this.myForm.get('divId').disable();
116
    }
117
    this.myForm.markAsPristine();
118
    
119
  }
120

    
121
  
122
  public setOptions(divIds) {
123
    this.classOptions = [];
124
    for(let divid of divIds){
125
      this.classOptions.push({label:divid.name, value:divid._id});
126
    }
127

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

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

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

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