1
|
import {Component, OnInit, Input} from '@angular/core';
|
2
|
import {SimpleChanges, OnChanges} from '@angular/core';
|
3
|
import {FormGroup, FormArray, FormBuilder, Validators} from "@angular/forms";
|
4
|
import {ActivatedRoute, Router} from '@angular/router';
|
5
|
|
6
|
import {HelpContentService} from "../../../services/help-content.service";
|
7
|
import {CommunityService} from "../../../openaireLibrary/connect/community/community.service";
|
8
|
import {SubjectsService} from "../subjects.service"
|
9
|
import {EnvProperties} from '../../../openaireLibrary/utils/properties/env-properties';
|
10
|
|
11
|
import {CommunityInfo} from '../../../openaireLibrary/connect/community/communityInfo';
|
12
|
|
13
|
import {Session} from '../../../openaireLibrary/login/utils/helper.class';
|
14
|
import {LoginErrorCodes} from '../../../openaireLibrary/login/utils/guardHelper.class';
|
15
|
|
16
|
import { concat } from 'rxjs/observable/concat';
|
17
|
@Component({
|
18
|
selector: 'subjects-edit-form',
|
19
|
templateUrl: './subjects-edit-form.component.html',
|
20
|
})
|
21
|
|
22
|
export class SubjectsEditFormComponent implements OnInit{
|
23
|
|
24
|
@Input('group')
|
25
|
myForm: FormGroup;
|
26
|
|
27
|
public showLoading: boolean = true;
|
28
|
public errorMessage: string = '';
|
29
|
public updateErrorMessage: string = '';
|
30
|
|
31
|
public successfulSaveMessage: string = '';
|
32
|
public successfulResetMessage: string = '';
|
33
|
|
34
|
public hasChanged: boolean = false;
|
35
|
|
36
|
public res=[];
|
37
|
|
38
|
params: any;
|
39
|
|
40
|
public communityId = null;
|
41
|
public community = null;
|
42
|
public properties: EnvProperties = null;
|
43
|
public newsubject ="";
|
44
|
public edit = null;
|
45
|
public editSubjectOriginalValue = null;
|
46
|
public originalSubjects = [];
|
47
|
|
48
|
constructor (private route: ActivatedRoute,
|
49
|
private _router: Router,
|
50
|
public _fb: FormBuilder,
|
51
|
private _helpContentService: HelpContentService,
|
52
|
private _communityService: CommunityService,
|
53
|
private _subjectsService: SubjectsService){ }
|
54
|
|
55
|
|
56
|
ngOnInit() {
|
57
|
this.route.data.subscribe((data: { envSpecific: EnvProperties }) => {
|
58
|
this.properties = data.envSpecific;
|
59
|
this.route.queryParams.subscribe(
|
60
|
communityId => {
|
61
|
this.communityId = communityId['communityId'];
|
62
|
|
63
|
if(!Session.isLoggedIn()){
|
64
|
console.info(this._router.url);
|
65
|
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
|
66
|
} else {
|
67
|
if (this.communityId != null && this.communityId != '') {
|
68
|
this.showLoading = true;
|
69
|
this.updateErrorMessage = "";
|
70
|
this.errorMessage = "";
|
71
|
|
72
|
this._communityService.getCommunity(this.properties, this.properties.communityAPI+this.communityId).subscribe (
|
73
|
community => {
|
74
|
this.community = community;
|
75
|
this.params = {community: encodeURIComponent('"' + community.queryId + '"')};
|
76
|
this.originalSubjects = [];
|
77
|
for (var i = 0; i < this.community.subjects.length; i++) {
|
78
|
this.originalSubjects.push(this.community.subjects[i]);
|
79
|
}
|
80
|
if (this.community.subjects.length == 0) {
|
81
|
this.community.subjects.push("");
|
82
|
}
|
83
|
this.showLoading = false;
|
84
|
},
|
85
|
error => this.handleError('System error retrieving community profile', error)
|
86
|
);
|
87
|
}
|
88
|
}
|
89
|
});
|
90
|
});
|
91
|
}
|
92
|
|
93
|
|
94
|
|
95
|
public addSubject() {
|
96
|
if(!Session.isLoggedIn()){
|
97
|
console.info(this._router.url);
|
98
|
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
|
99
|
} else {
|
100
|
this.community.subjects.push("");
|
101
|
}
|
102
|
}
|
103
|
|
104
|
public removeSubject(i: any) {
|
105
|
if(!Session.isLoggedIn()){
|
106
|
console.info(this._router.url);
|
107
|
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
|
108
|
} else {
|
109
|
this.community.subjects.splice(i, 1);
|
110
|
}
|
111
|
}
|
112
|
|
113
|
public resetForm(communityId: string) {
|
114
|
if(!Session.isLoggedIn()){
|
115
|
console.info(this._router.url);
|
116
|
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
|
117
|
} else {
|
118
|
if (communityId != null && communityId != '') {
|
119
|
this.showLoading = true;
|
120
|
this.updateErrorMessage = "";
|
121
|
this.errorMessage = "";
|
122
|
|
123
|
this._communityService.getCommunity(this.properties, this.properties.communityAPI + communityId).subscribe (
|
124
|
community => {
|
125
|
this.community = community;
|
126
|
this.params = {community: encodeURIComponent('"' + community.queryId + '"')};
|
127
|
this.showLoading = false;
|
128
|
this.handleSuccessfulReset('Form reseted!');
|
129
|
},
|
130
|
error => this.handleError('System error retrieving community profile', error)
|
131
|
);
|
132
|
}
|
133
|
this.resetChange();
|
134
|
}
|
135
|
}
|
136
|
|
137
|
// save(newValue){
|
138
|
// this.edit=null;
|
139
|
// console.log(this.editSubjectOriginalValue + "-->"+newValue)
|
140
|
// if (this.communityId != null && this.communityId != '' && this.editSubjectOriginalValue != newValue) {
|
141
|
// this.showLoading = true;
|
142
|
// var subjectsToDelete = this.parseUpdatedSubjects([this.editSubjectOriginalValue]);
|
143
|
// var subjectsToAdd = this.parseUpdatedSubjects([newValue]);
|
144
|
// let obs = concat(this._subjectsService.addSubjects(this.properties.communityAPI+this.communityId+"/subjects", subjectsToAdd),
|
145
|
// this._subjectsService.removeSubjects(this.properties.communityAPI+this.communityId+"/subjects", subjectsToDelete));
|
146
|
// obs.subscribe(res => {
|
147
|
// console.log(res);
|
148
|
// this.community.subjects = res[1]["subjects"];
|
149
|
// this.originalSubjects = this.community.subjects;
|
150
|
// this.handleSuccessfulSave('Subjects updated!')
|
151
|
// this.showLoading = false;
|
152
|
// });
|
153
|
// }
|
154
|
// this.editSubjectOriginalValue = null;
|
155
|
// }
|
156
|
public getSubjectsExistOnlyInFirst(firstArray: string[], secondArray: string[]): string[] {
|
157
|
|
158
|
var difference = [];
|
159
|
for (var i = 0; i < firstArray.length; i++) {
|
160
|
if (secondArray.indexOf(firstArray[i]) == -1) {
|
161
|
difference.push(firstArray[i]);
|
162
|
}
|
163
|
|
164
|
}
|
165
|
return difference;
|
166
|
|
167
|
}
|
168
|
public updateSubjects() {
|
169
|
if(!Session.isLoggedIn()){
|
170
|
console.info(this._router.url);
|
171
|
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
|
172
|
} else {
|
173
|
if (this.communityId != null && this.communityId != '') {
|
174
|
this.showLoading = true;
|
175
|
var subjectsToDeleteAr = this.getSubjectsExistOnlyInFirst(this.originalSubjects, this.community.subjects);
|
176
|
var subjectsToAddAr = this.getSubjectsExistOnlyInFirst(this.community.subjects, this.originalSubjects);
|
177
|
|
178
|
var subjectsToDelete = this.parseUpdatedSubjects(subjectsToDeleteAr);
|
179
|
var subjectsToAdd = this.parseUpdatedSubjects(subjectsToAddAr);
|
180
|
if(subjectsToAddAr.length > 0 && subjectsToDeleteAr.length > 0){
|
181
|
let obs = concat(this._subjectsService.addSubjects(this.properties.communityAPI + this.communityId + "/subjects", subjectsToAdd),
|
182
|
this._subjectsService.removeSubjects(this.properties.communityAPI + this.communityId + "/subjects", subjectsToDelete));
|
183
|
obs.subscribe(res => {
|
184
|
console.log(res);
|
185
|
if (res["method"] == "delete") {
|
186
|
this.afterUpdateActions(res);
|
187
|
}
|
188
|
},
|
189
|
error => this.handleUpdateError('System error updating subjects', error)
|
190
|
);
|
191
|
}else if (subjectsToAddAr.length > 0){
|
192
|
this._subjectsService.addSubjects(this.properties.communityAPI + this.communityId +"/subjects", subjectsToAdd).subscribe(res => {
|
193
|
console.log(res);
|
194
|
this.afterUpdateActions(res);
|
195
|
|
196
|
},
|
197
|
error => this.handleUpdateError('System error updating subjects', error)
|
198
|
);
|
199
|
}else if (subjectsToDeleteAr.length > 0) {
|
200
|
this._subjectsService.removeSubjects(this.properties.communityAPI + this.communityId + "/subjects", subjectsToDelete).subscribe(res => {
|
201
|
console.log(res);
|
202
|
this.afterUpdateActions(res);
|
203
|
|
204
|
},
|
205
|
error => this.handleUpdateError('System error updating subjects', error)
|
206
|
);
|
207
|
}
|
208
|
// this._router.navigate(['/manage-subjects'], {queryParams: { "communityId": this.communityId}});
|
209
|
|
210
|
}
|
211
|
this.resetChange();
|
212
|
}
|
213
|
}
|
214
|
afterUpdateActions(res){
|
215
|
this.community.subjects = res["subjects"];
|
216
|
this.originalSubjects = [];
|
217
|
for (var i = 0; i < this.community.subjects.length; i++) {
|
218
|
this.originalSubjects.push(this.community.subjects[i]);
|
219
|
}
|
220
|
if (this.community.subjects.length == 0) {
|
221
|
this.community.subjects.push("");
|
222
|
}
|
223
|
this.handleSuccessfulSave('Subjects updated!')
|
224
|
this.showLoading = false;
|
225
|
}
|
226
|
private parseUpdatedSubjects(subjects): {} {
|
227
|
var parsedSubjects = this.getNonEmptyItems(subjects);
|
228
|
return parsedSubjects;
|
229
|
}
|
230
|
|
231
|
|
232
|
|
233
|
private getNonEmptyItems(data: string[]): string[] {
|
234
|
let length = data.length;
|
235
|
let arrayNonEmpty = new Array<string>();
|
236
|
|
237
|
let j = 0;
|
238
|
for (let i = 0; i < length; i++) {
|
239
|
if (this.isEmpty(data[i])) {
|
240
|
// console.log(data[i]);
|
241
|
} else if (this.isNonEmpty(data[i])) {
|
242
|
arrayNonEmpty[j] = data[i];
|
243
|
j++;
|
244
|
// console.log(data[i]);
|
245
|
}
|
246
|
}
|
247
|
return arrayNonEmpty;
|
248
|
}
|
249
|
|
250
|
private hasFilled(data: any): boolean {
|
251
|
if (this.isNonEmpty(data) && !this.isEmpty(data)) {
|
252
|
// TODO remove console message after final testing
|
253
|
console.log("NAME IS NOT EMPTY");
|
254
|
return true;
|
255
|
}
|
256
|
// TODO remove console message after final testing
|
257
|
console.log("NAME IS EMPTY");
|
258
|
return false;
|
259
|
}
|
260
|
|
261
|
private isEmpty(data: string): boolean {
|
262
|
if (data != undefined && !data.replace(/\s/g, '').length)
|
263
|
return true;
|
264
|
else
|
265
|
return false;
|
266
|
}
|
267
|
|
268
|
private isNonEmpty(data: string): boolean {
|
269
|
if (data != undefined && data != null)
|
270
|
return true;
|
271
|
else
|
272
|
return false;
|
273
|
}
|
274
|
|
275
|
private hasValidEmail(data: any): boolean {
|
276
|
let length = data['managers'].length;
|
277
|
|
278
|
for (let i = 0; i < length; i++) {
|
279
|
if (!this.emailValidator(data['managers'][i])){
|
280
|
// TODO remove console message after final testing
|
281
|
console.log("INVALID EMAIL");
|
282
|
return false;
|
283
|
}
|
284
|
}
|
285
|
// TODO remove console message after final testing
|
286
|
console.log("ALL EMAILS ARE VALID");
|
287
|
return true;
|
288
|
}
|
289
|
|
290
|
private emailValidator(email: any): boolean {
|
291
|
if (email.match("^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$"))
|
292
|
return true;
|
293
|
else
|
294
|
return false;
|
295
|
}
|
296
|
|
297
|
private change() {
|
298
|
this.hasChanged = true;
|
299
|
this.successfulSaveMessage = '';
|
300
|
this.successfulResetMessage = '';
|
301
|
// TODO remove after testing
|
302
|
console.log('I have changed: I AM TRUE');
|
303
|
}
|
304
|
|
305
|
private resetChange() {
|
306
|
this.hasChanged = false;
|
307
|
// TODO remove after testing
|
308
|
console.log('I have changed: I AM FALSE');
|
309
|
}
|
310
|
|
311
|
public get form() {
|
312
|
return this._fb.group({
|
313
|
_id : '',
|
314
|
name : ['', Validators.required]
|
315
|
});
|
316
|
}
|
317
|
|
318
|
public reset() {
|
319
|
this.myForm.patchValue({
|
320
|
name : '',
|
321
|
_id : ''
|
322
|
});
|
323
|
}
|
324
|
|
325
|
handleUpdateError(message: string, error) {
|
326
|
this.updateErrorMessage = message;
|
327
|
console.log('Server responded: ' +error);
|
328
|
|
329
|
this.showLoading = false;
|
330
|
}
|
331
|
|
332
|
handleError(message: string, error) {
|
333
|
this.errorMessage = message;
|
334
|
console.log('Server responded: ' + error);
|
335
|
|
336
|
this.showLoading = false;
|
337
|
}
|
338
|
|
339
|
handleSuccessfulSave(message) {
|
340
|
this.showLoading = false;
|
341
|
this.successfulSaveMessage = message;
|
342
|
}
|
343
|
|
344
|
handleSuccessfulReset(message) {
|
345
|
this.showLoading = false;
|
346
|
this.successfulResetMessage = message;
|
347
|
}
|
348
|
|
349
|
trackByFn(index: any, item: any) {
|
350
|
return index;
|
351
|
}
|
352
|
|
353
|
}
|