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
      ConnectHelper.setPortalTypeFromPid(this.portal);
48
      this.subs.push(this.route.queryParams.subscribe(params => {
49
        HelperFunctions.scroll();
50
        this.pageId = params['pageId'];
51
        this.myForm = this.form;
52
        this.pageContentId = params['pageContentId'];
53
        if (!this.pageId) {
54
          this._router.navigate(['../'], {relativeTo: this.route});
55
        }
56
        this.getInfo(this.pageId);
57
        if (!Session.isLoggedIn()) {
58
          this._router.navigate(['/user-info'], {
59
            queryParams: {
60
              "errorCode": LoginErrorCodes.NOT_VALID,
61
              "redirectUrl": this._router.url
62
            }
63
          });
64
        }
65
      }));
66
    }));
67
  }
68
  
69
  ngOnDestroy() {
70
    this.subs.forEach(value => {
71
      if (value instanceof Subscriber) {
72
        value.unsubscribe();
73
      }
74
    });
75
  }
76
  
77
  getInfo(pageId: string) {
78
    this.showLoading = true;
79
    let obs = zip(this._helpContentService.getPageByPortal(pageId, this.properties.adminToolsAPIURL, this.portal),
80
      this._helpContentService.getDivIdsFullByPortal(pageId, this.properties.adminToolsAPIURL, this.portal));
81
    this.subs.push(obs.subscribe(
82
      results => {
83
        this.page = results[0];
84
        if (this.properties.adminToolsPortalType != this.page.portalType) {
85
          this._router.navigate(['../'], {relativeTo: this.route});
86
        }
87
        this.setOptions(results[1]);
88
        if (!this.pageContentId) {
89
         this.updateForm(null);
90
          this.showLoading = false;
91
        } else {
92
          this.subs.push(this._helpContentService.getDivHelpContent( this.pageContentId, this.properties.adminToolsAPIURL, this.portal).subscribe(pageHelpContent=>{
93
            this.pageHelpContent = pageHelpContent;
94
            if (this.properties.adminToolsPortalType != this.page.portalType) {
95
              this._router.navigate(['../'], {relativeTo: this.route});
96
            }
97
            this.updateForm(this.pageHelpContent);
98
            this.showLoading = false;
99
          },
100
            error => {
101
            this.handleError('System error retrieving content by id '+ this.pageContentId, error)
102
          }));
103
        }
104
      }));
105

    
106
  }
107

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

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

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

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

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

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