1
|
import {Component, OnInit, Input} from '@angular/core';
|
2
|
import {FormGroup, FormArray, FormBuilder, Validators} from "@angular/forms";
|
3
|
import {ActivatedRoute, Router} from '@angular/router';
|
4
|
|
5
|
import {HelpContentService} from "../../../services/help-content.service";
|
6
|
import {CommunityService} from "../../../openaireLibrary/connect/community/community.service";
|
7
|
import {EnvProperties} from '../../../openaireLibrary/utils/properties/env-properties';
|
8
|
|
9
|
import {CommunityInfo} from '../../../openaireLibrary/connect/community/communityInfo';
|
10
|
|
11
|
@Component({
|
12
|
selector: 'community-edit-form',
|
13
|
templateUrl: './community-edit-form.component.html',
|
14
|
})
|
15
|
|
16
|
export class CommunityEditFormComponent implements OnInit{
|
17
|
|
18
|
@Input('group')
|
19
|
myForm: FormGroup;
|
20
|
|
21
|
public showLoading: boolean = true;
|
22
|
public errorMessage: string = '';
|
23
|
public updateErrorMessage: string = '';
|
24
|
|
25
|
public res=[];
|
26
|
|
27
|
params: any;
|
28
|
|
29
|
public communityId = null;
|
30
|
public community = null;
|
31
|
public properties:EnvProperties = null;
|
32
|
|
33
|
constructor (private route: ActivatedRoute,
|
34
|
private _router: Router,
|
35
|
public _fb: FormBuilder,
|
36
|
private _helpContentService: HelpContentService,
|
37
|
private _communityService: CommunityService){ }
|
38
|
|
39
|
|
40
|
ngOnInit() {
|
41
|
this.route.data.subscribe((data: { envSpecific: EnvProperties }) => {
|
42
|
this.properties = data.envSpecific;
|
43
|
this.route.queryParams.subscribe(
|
44
|
communityId => {
|
45
|
this.communityId = communityId['communityId'];
|
46
|
|
47
|
if (this.communityId != null && this.communityId != '') {
|
48
|
this.showLoading = true;
|
49
|
this.updateErrorMessage = "";
|
50
|
this.errorMessage = "";
|
51
|
|
52
|
this._communityService.getCommunity(this.properties, this.properties.communityAPI+this.communityId).subscribe (
|
53
|
community => {
|
54
|
this.community = community;
|
55
|
this.params = {community: encodeURIComponent('"'+community.queryId+'"')};
|
56
|
// TODO remove after testing!!!!!!!
|
57
|
console.log(community);
|
58
|
this.showLoading = false;
|
59
|
},
|
60
|
error => this.handleError('System error retrieving community profile', error)
|
61
|
);
|
62
|
}
|
63
|
});
|
64
|
});
|
65
|
}
|
66
|
|
67
|
public addManager() {
|
68
|
this.community.managers.push("");
|
69
|
}
|
70
|
|
71
|
public removeManager(i : any) {
|
72
|
this.community.managers.splice(i,1);
|
73
|
}
|
74
|
|
75
|
public addSubject() {
|
76
|
this.community.subjects.push("");
|
77
|
}
|
78
|
|
79
|
public removeSubject(i : any) {
|
80
|
this.community.subjects.splice(i,1);
|
81
|
}
|
82
|
|
83
|
public resetForm(communityId: string) {
|
84
|
if (communityId != null && communityId != '') {
|
85
|
this.showLoading = true;
|
86
|
this.updateErrorMessage = "";
|
87
|
this.errorMessage = "";
|
88
|
|
89
|
this._communityService.getCommunity(this.properties, this.properties.communityAPI+communityId).subscribe (
|
90
|
community => {
|
91
|
this.community = community;
|
92
|
this.params = {community: encodeURIComponent('"'+community.queryId+'"')};
|
93
|
this.showLoading = false;
|
94
|
},
|
95
|
error => this.handleError('System error retrieving community profile', error)
|
96
|
);
|
97
|
}
|
98
|
}
|
99
|
|
100
|
public updateCommunity() {
|
101
|
|
102
|
if (this.communityId != null && this.communityId != '') {
|
103
|
|
104
|
var community = this.parseUpdatedCommunity();
|
105
|
|
106
|
// TODO delete after testing
|
107
|
console.log(community);
|
108
|
|
109
|
if (!this.hasValidEmail(community) || !this.hasFilled(community["name"])) {
|
110
|
//console.info("NAVIGAAAATE");
|
111
|
this._router.navigate(['/community-edit-form'], {queryParams: { "communityId": this.communityId}});
|
112
|
} else {
|
113
|
this._communityService.updateCommunity(this.properties.communityAPI+this.communityId, community).subscribe(
|
114
|
community => {
|
115
|
|
116
|
},
|
117
|
error => this.handleUpdateError('System error updating community profile', error)
|
118
|
);
|
119
|
this._router.navigate(['/community-edit-form'], {queryParams: { "communityId": this.communityId}});
|
120
|
}
|
121
|
}
|
122
|
}
|
123
|
|
124
|
private parseUpdatedCommunity() : {} {
|
125
|
var community = {};
|
126
|
|
127
|
community["name"] = this.community.title;
|
128
|
community["shortName"] = this.community.shortTitle;
|
129
|
community["status"] = this.community.status;
|
130
|
community["description"] = this.community.description;
|
131
|
community["logoUrl"] = this.community.logoUrl;
|
132
|
|
133
|
// TODO delete after testing
|
134
|
//console.log(this.community.managers);
|
135
|
|
136
|
community['managers'] = new Array<string>();
|
137
|
this.community.managers = this.getNonEmptyItems(this.community.managers);
|
138
|
community['managers'] = this.community.managers;
|
139
|
|
140
|
// TODO delete after testing
|
141
|
//console.log(this.community.subjects);
|
142
|
|
143
|
community['subjects'] = new Array<string>();
|
144
|
this.community.subjects = this.getNonEmptyItems(this.community.subjects);
|
145
|
community['subjects'] = this.community.subjects;
|
146
|
|
147
|
// TODO delete after testing
|
148
|
//console.log(community);
|
149
|
|
150
|
return community;
|
151
|
}
|
152
|
|
153
|
private getNonEmptyItems(data: string[]): string[] {
|
154
|
let length = data.length;
|
155
|
let arrayNonEmpty = new Array<string>();
|
156
|
|
157
|
let j = 0;
|
158
|
for (let i = 0; i < length; i++) {
|
159
|
if (this.isEmpty(data[i])) {
|
160
|
//console.log(data[i]);
|
161
|
} else if (this.isNonEmpty(data[i])) {
|
162
|
arrayNonEmpty[j] = data[i];
|
163
|
j++;
|
164
|
//console.log(data[i]);
|
165
|
}
|
166
|
}
|
167
|
return arrayNonEmpty;
|
168
|
}
|
169
|
|
170
|
private hasFilled(data: any): boolean {
|
171
|
if (this.isNonEmpty(data) && !this.isEmpty(data)) {
|
172
|
// TODO remove console message after final testing
|
173
|
console.log("NAME IS NOT EMPTY");
|
174
|
return true;
|
175
|
}
|
176
|
// TODO remove console message after final testing
|
177
|
console.log("NAME IS EMPTY");
|
178
|
return false;
|
179
|
}
|
180
|
|
181
|
private isEmpty(data: string): boolean {
|
182
|
if (data != undefined && !data.replace(/\s/g, '').length)
|
183
|
return true;
|
184
|
else
|
185
|
return false;
|
186
|
}
|
187
|
|
188
|
private isNonEmpty(data: string): boolean {
|
189
|
if (data != undefined && data != null)
|
190
|
return true;
|
191
|
else
|
192
|
return false;
|
193
|
}
|
194
|
|
195
|
private hasValidEmail(data: any): boolean {
|
196
|
let length = data['managers'].length;
|
197
|
|
198
|
for(let i = 0; i < length; i++) {
|
199
|
if (!this.emailValidator(data['managers'][i])){
|
200
|
// TODO remove console message after final testing
|
201
|
console.log("INVALID EMAIL");
|
202
|
return false;
|
203
|
}
|
204
|
}
|
205
|
// TODO remove console message after final testing
|
206
|
console.log("ALL EMAILS ARE VALID");
|
207
|
return true;
|
208
|
}
|
209
|
|
210
|
private emailValidator(email : any): boolean {
|
211
|
if (email.match("^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$"))
|
212
|
return true;
|
213
|
else
|
214
|
return false;
|
215
|
}
|
216
|
|
217
|
public get form() {
|
218
|
return this._fb.group({
|
219
|
_id : '',
|
220
|
name : ['', Validators.required]
|
221
|
});
|
222
|
}
|
223
|
|
224
|
public reset() {
|
225
|
this.myForm.patchValue({
|
226
|
name : '',
|
227
|
_id : ''
|
228
|
});
|
229
|
}
|
230
|
|
231
|
handleUpdateError(message: string, error) {
|
232
|
this.updateErrorMessage = message;
|
233
|
console.log('Server responded: ' +error);
|
234
|
|
235
|
this.showLoading = false;
|
236
|
}
|
237
|
|
238
|
handleError(message: string, error) {
|
239
|
this.errorMessage = message;
|
240
|
console.log('Server responded: ' + error);
|
241
|
|
242
|
this.showLoading = false;
|
243
|
}
|
244
|
|
245
|
trackByFn(index: any, item: any) {
|
246
|
return index;
|
247
|
}
|
248
|
}
|