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
|
public properties: EnvProperties = properties;
|
30
|
public showLoading: boolean = true;
|
31
|
public errorMessage: string = '';
|
32
|
@Input() updateErrorMessage: string = '';
|
33
|
private subs: Subscription[] = [];
|
34
|
public pageHelpContent: DivHelpContent;
|
35
|
|
36
|
constructor(private route: ActivatedRoute, private _router: Router, private _fb: FormBuilder, private _helpContentService: HelpContentService) {
|
37
|
}
|
38
|
|
39
|
ngOnInit() {
|
40
|
this.subs.push(this.route.params.subscribe(params => {
|
41
|
this.portal = (this.route.snapshot.data.portal) ? this.route.snapshot.data.portal : this.route.snapshot.params[this.route.snapshot.data.param];
|
42
|
this.subs.push(this.route.queryParams.subscribe(params => {
|
43
|
this.pageId = params['pageId'];
|
44
|
this.myForm = this.form;
|
45
|
this.pageContentId = params['pageContentId'];
|
46
|
if (!this.pageId) {
|
47
|
this._router.navigate(['../'], {relativeTo: this.route});
|
48
|
}
|
49
|
this.getInfo(this.pageId);
|
50
|
}));
|
51
|
}));
|
52
|
}
|
53
|
|
54
|
ngOnDestroy() {
|
55
|
this.subs.forEach(value => {
|
56
|
if (value instanceof Subscriber) {
|
57
|
value.unsubscribe();
|
58
|
}
|
59
|
});
|
60
|
}
|
61
|
|
62
|
getInfo(pageId: string) {
|
63
|
this.showLoading = true;
|
64
|
let obs = zip(this._helpContentService.getPageByPortal(pageId, this.properties.adminToolsAPIURL, this.portal),
|
65
|
this._helpContentService.getDivIdsFullByPortal(pageId, this.properties.adminToolsAPIURL, this.portal));
|
66
|
this.subs.push(obs.subscribe(
|
67
|
results => {
|
68
|
this.page = results[0];
|
69
|
if (this.properties.adminToolsPortalType != this.page.portalType) {
|
70
|
this._router.navigate(['../'], {relativeTo: this.route});
|
71
|
}
|
72
|
this.setOptions(results[1]);
|
73
|
if (!this.pageContentId) {
|
74
|
this.updateForm(null);
|
75
|
this.showLoading = false;
|
76
|
} else {
|
77
|
this.subs.push(this._helpContentService.getDivHelpContent( this.pageContentId, this.properties.adminToolsAPIURL, this.portal).subscribe(pageHelpContent=>{
|
78
|
this.pageHelpContent = pageHelpContent;
|
79
|
if (this.properties.adminToolsPortalType != this.page.portalType) {
|
80
|
this._router.navigate(['../'], {relativeTo: this.route});
|
81
|
}
|
82
|
this.updateForm(this.pageHelpContent);
|
83
|
this.showLoading = false;
|
84
|
},
|
85
|
error => {
|
86
|
this.handleError('System error retrieving content by id '+ this.pageContentId, error)
|
87
|
}));
|
88
|
}
|
89
|
}));
|
90
|
|
91
|
}
|
92
|
|
93
|
private updateForm(pageHelpContent: DivHelpContent) {
|
94
|
this.pageHelpContent = pageHelpContent;
|
95
|
this.myForm = this.form;
|
96
|
if (this.pageHelpContent) {
|
97
|
this.myForm.patchValue((pageHelpContent));
|
98
|
this.myForm.get('divId').disable();
|
99
|
}
|
100
|
this.myForm.markAsPristine();
|
101
|
|
102
|
}
|
103
|
|
104
|
|
105
|
public setOptions(divIds) {
|
106
|
this.classOptions = [];
|
107
|
for(let divid of divIds){
|
108
|
this.classOptions.push({label:divid.name, value:divid._id});
|
109
|
}
|
110
|
|
111
|
}
|
112
|
|
113
|
public get form() {
|
114
|
return this._fb.group({
|
115
|
divId: ['', Validators.required],
|
116
|
content: ['', Validators.required],
|
117
|
isActive: true,
|
118
|
portal: this.portal,
|
119
|
_id : '',
|
120
|
});
|
121
|
|
122
|
}
|
123
|
|
124
|
public reset() {
|
125
|
this.myForm.patchValue({
|
126
|
divId: ['', Validators.required],
|
127
|
content: ['', Validators.required],
|
128
|
isActive: true,
|
129
|
portal: '',
|
130
|
_id : '',
|
131
|
});
|
132
|
this.myForm.markAsPristine();
|
133
|
}
|
134
|
|
135
|
handleError(message: string, error) {
|
136
|
this.errorMessage = message;
|
137
|
console.error('Server responded: ' + error);
|
138
|
|
139
|
this.showLoading = false;
|
140
|
}
|
141
|
|
142
|
public saveCustom() {
|
143
|
if (!Session.isLoggedIn()) {
|
144
|
this._router.navigate(['/user-info'], {
|
145
|
queryParams: {
|
146
|
"errorCode": LoginErrorCodes.NOT_VALID,
|
147
|
"redirectUrl": this._router.url
|
148
|
}
|
149
|
});
|
150
|
} else {
|
151
|
|
152
|
if (this.myForm.valid) {
|
153
|
this.showLoading = true;
|
154
|
this.updateErrorMessage = "";
|
155
|
this.myForm.get('divId').enable();
|
156
|
let pageHelpContent: DivHelpContent = this.myForm.value;
|
157
|
this.subs.push(this._helpContentService.insertOrUpdateDivHelpContent(pageHelpContent, this.properties.adminToolsAPIURL, this.portal).subscribe(
|
158
|
_ => {
|
159
|
this._router.navigate(['../'], {queryParams: {"pageId": this.pageId}, relativeTo: this.route});
|
160
|
UIkit.notification('Page content has been <b>successfully updated</b>', {
|
161
|
status: 'success',
|
162
|
timeout: 6000,
|
163
|
pos: 'bottom-right'
|
164
|
});
|
165
|
this.showLoading = false;
|
166
|
},
|
167
|
err => this.handleUpdateError('System error saving page content', err)
|
168
|
));
|
169
|
} else {
|
170
|
this.showLoading = false;
|
171
|
this.errorMessage = "Please fill all required fields";
|
172
|
}
|
173
|
}
|
174
|
}
|
175
|
|
176
|
public resetCustom() {
|
177
|
this.showLoading = true;
|
178
|
this.updateForm(this.pageHelpContent);
|
179
|
this.showLoading = false;
|
180
|
}
|
181
|
|
182
|
handleUpdateError(message: string, error) {
|
183
|
|
184
|
this.updateErrorMessage = message;
|
185
|
console.error('Server responded: ' + error);
|
186
|
|
187
|
this.showLoading = false;
|
188
|
}
|
189
|
|
190
|
changeStatus() {
|
191
|
this.myForm.get('isActive').setValue(!this.myForm.get('isActive').value);
|
192
|
if (this.pageHelpContent && this.myForm.get('isActive').value != this.pageHelpContent.isActive || !this.pageHelpContent && !this.myForm.get('isActive').value) {
|
193
|
this.myForm.get('isActive').markAsDirty();
|
194
|
} else {
|
195
|
this.myForm.get('isActive').markAsPristine()
|
196
|
}
|
197
|
}
|
198
|
|
199
|
contentChanged() {
|
200
|
if (this.pageHelpContent && this.myForm.get('content').value != this.pageHelpContent.content || !this.pageHelpContent && this.myForm.get('content').value != '') {
|
201
|
this.myForm.get('content').markAsDirty();
|
202
|
} else {
|
203
|
this.myForm.get('content').markAsPristine()
|
204
|
}
|
205
|
}
|
206
|
}
|