Project

General

Profile

1 60507 argiro.kok
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 57928 argiro.kok
8
import {Session} from '../../login/utils/helper.class';
9
import {LoginErrorCodes} from '../../login/utils/guardHelper.class';
10 60507 argiro.kok
import {properties} from '../../../../environments/environment';
11 61148 k.triantaf
import {Subscriber, Subscription, zip} from 'rxjs';
12 60507 argiro.kok
import {HelperFunctions} from '../../utils/HelperFunctions.class';
13 60756 argiro.kok
import {ConnectHelper} from '../../connect/connectHelper';
14
import {DivHelpContent} from '../../utils/entities/adminTool/div-help-content';
15 61148 k.triantaf
16 60756 argiro.kok
declare var UIkit;
17 57928 argiro.kok
18
@Component({
19 60756 argiro.kok
  selector: 'class-content-form',
20
  templateUrl: './class-help-content-form.component.html',
21 57928 argiro.kok
})
22 60756 argiro.kok
export class ClassContentFormComponent implements OnInit {
23 60561 k.triantaf
24
  myForm: FormGroup;
25
  portal: string;
26
  pageId: string;
27
  pageContentId: string;
28
  page: Page;
29 60756 argiro.kok
  classOptions = [];
30
31 60561 k.triantaf
  public properties: EnvProperties = null;
32
33
  public showLoading: boolean = true;
34
  public errorMessage: string = '';
35
  @Input() updateErrorMessage: string = '';
36
  private subs: Subscription[] = [];
37 60756 argiro.kok
  public pageHelpContent: DivHelpContent;
38 60561 k.triantaf
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 61152 k.triantaf
      if(this.route.snapshot.data.portal) {
48
        properties.adminToolsPortalType = this.route.snapshot.data.portal;
49
      }
50 60561 k.triantaf
      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 60170 argiro.kok
            }
65 60561 k.triantaf
          });
66 60507 argiro.kok
        }
67 60561 k.triantaf
      }));
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 61148 k.triantaf
    let obs = zip(this._helpContentService.getPageByPortal(pageId, this.properties.adminToolsAPIURL, this.portal),
82 60756 argiro.kok
      this._helpContentService.getDivIdsFullByPortal(pageId, this.properties.adminToolsAPIURL, this.portal));
83 60561 k.triantaf
    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 60507 argiro.kok
        }
89 60756 argiro.kok
        this.setOptions(results[1]);
90 60561 k.triantaf
        if (!this.pageContentId) {
91 60756 argiro.kok
         this.updateForm(null);
92
          this.showLoading = false;
93 60561 k.triantaf
        } else {
94 60756 argiro.kok
          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 60507 argiro.kok
        }
106 60756 argiro.kok
      }));
107
108 60561 k.triantaf
  }
109 60507 argiro.kok
110 60756 argiro.kok
  private updateForm(pageHelpContent: DivHelpContent) {
111 60561 k.triantaf
    this.pageHelpContent = pageHelpContent;
112
    this.myForm = this.form;
113
    if (this.pageHelpContent) {
114
      this.myForm.patchValue((pageHelpContent));
115 60756 argiro.kok
      this.myForm.get('divId').disable();
116 60507 argiro.kok
    }
117 60561 k.triantaf
    this.myForm.markAsPristine();
118
119
  }
120 60756 argiro.kok
121 60561 k.triantaf
122 60756 argiro.kok
  public setOptions(divIds) {
123
    this.classOptions = [];
124
    for(let divid of divIds){
125
      this.classOptions.push({label:divid.name, value:divid._id});
126 57928 argiro.kok
    }
127 60756 argiro.kok
128 60561 k.triantaf
  }
129
130
  public get form() {
131
    return this._fb.group({
132 60756 argiro.kok
      divId: ['', Validators.required],
133 60561 k.triantaf
      content: ['', Validators.required],
134
      isActive: true,
135 60756 argiro.kok
      portal: this.portal,
136
      _id : '',
137 60561 k.triantaf
    });
138 60756 argiro.kok
139 60561 k.triantaf
  }
140
141
  public reset() {
142
    this.myForm.patchValue({
143 60756 argiro.kok
      divId: ['', Validators.required],
144
      content: ['', Validators.required],
145 60561 k.triantaf
      isActive: true,
146 60756 argiro.kok
      portal: '',
147
      _id : '',
148 60561 k.triantaf
    });
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 60756 argiro.kok
169 60561 k.triantaf
      if (this.myForm.valid) {
170 60507 argiro.kok
        this.showLoading = true;
171 60561 k.triantaf
        this.updateErrorMessage = "";
172 60756 argiro.kok
        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 60561 k.triantaf
          _ => {
176
            this._router.navigate(['../'], {queryParams: {"pageId": this.pageId}, relativeTo: this.route});
177 60756 argiro.kok
            UIkit.notification('Page content has been <b>successfully updated</b>', {
178
              status: 'success',
179
              timeout: 6000,
180
              pos: 'bottom-right'
181
            });
182 60561 k.triantaf
            this.showLoading = false;
183
          },
184
          err => this.handleUpdateError('System error saving page content', err)
185
        ));
186
      } else {
187 60507 argiro.kok
        this.showLoading = false;
188 60561 k.triantaf
        this.errorMessage = "Please fill all required fields";
189
      }
190 60507 argiro.kok
    }
191 60561 k.triantaf
  }
192 60756 argiro.kok
193 60561 k.triantaf
  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 60507 argiro.kok
    }
214 60561 k.triantaf
  }
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 60507 argiro.kok
    }
222 60561 k.triantaf
  }
223 57928 argiro.kok
}