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