1
|
import {Component, OnInit, Input, ElementRef} 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 {SubscribeService} from "../../../openaireLibrary/utils/subscribe/subscribe.service";
|
9
|
import {EmailService} from "../../../openaireLibrary/utils/email/email.service";
|
10
|
|
11
|
import {EnvProperties} from '../../../openaireLibrary/utils/properties/env-properties';
|
12
|
|
13
|
import {CommunityInfo} from '../../../openaireLibrary/connect/community/communityInfo';
|
14
|
import {Email} from "../../../openaireLibrary/utils/email/email";
|
15
|
import {Composer} from "../../../openaireLibrary/utils/email/composer";
|
16
|
|
17
|
import {Session} from '../../../openaireLibrary/login/utils/helper.class';
|
18
|
import {LoginErrorCodes} from '../../../openaireLibrary/login/utils/guardHelper.class';
|
19
|
|
20
|
@Component({
|
21
|
selector: 'community-edit-form',
|
22
|
templateUrl: './community-edit-form.component.html',
|
23
|
})
|
24
|
|
25
|
export class CommunityEditFormComponent implements OnInit{
|
26
|
|
27
|
@Input('group')
|
28
|
myForm: FormGroup;
|
29
|
|
30
|
public showLoading: boolean = true;
|
31
|
public errorMessage: string = '';
|
32
|
public updateErrorMessage: string = '';
|
33
|
|
34
|
public successfulSaveMessage: string = '';
|
35
|
public successfulResetMessage: string = '';
|
36
|
|
37
|
public hasChanged: boolean = false;
|
38
|
|
39
|
public res=[];
|
40
|
|
41
|
params: any;
|
42
|
|
43
|
public communityId = null;
|
44
|
public community = null;
|
45
|
public firstVersionOfManagers: string[];
|
46
|
public newManagersToSubscribe: string[];
|
47
|
|
48
|
public email: Email;
|
49
|
|
50
|
public emailToInform: Email;
|
51
|
|
52
|
public note: string = '';
|
53
|
|
54
|
public properties:EnvProperties = null;
|
55
|
|
56
|
constructor (private element: ElementRef,
|
57
|
private route: ActivatedRoute,
|
58
|
private _router: Router,
|
59
|
public _fb: FormBuilder,
|
60
|
private _helpContentService: HelpContentService,
|
61
|
private _communityService: CommunityService,
|
62
|
private _subscribeService: SubscribeService,
|
63
|
private _emailService: EmailService){ }
|
64
|
|
65
|
|
66
|
ngOnInit() {
|
67
|
this.route.data.subscribe((data: { envSpecific: EnvProperties }) => {
|
68
|
this.properties = data.envSpecific;
|
69
|
this.route.queryParams.subscribe(
|
70
|
communityId => {
|
71
|
this.scroll();
|
72
|
|
73
|
this.communityId = communityId['communityId'];
|
74
|
|
75
|
this.email = {body: "", subject: "", recipients: []};
|
76
|
this.emailToInform = {body: "", subject: "", recipients: []};
|
77
|
|
78
|
if(!Session.isLoggedIn()){
|
79
|
console.info(this._router.url);
|
80
|
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
|
81
|
} else {
|
82
|
if (this.communityId != null && this.communityId != '') {
|
83
|
this.showLoading = true;
|
84
|
this.updateErrorMessage = "";
|
85
|
this.errorMessage = "";
|
86
|
|
87
|
this._communityService.getCommunity(this.properties, this.properties.communityAPI+this.communityId).subscribe (
|
88
|
community => {
|
89
|
this.community = community;
|
90
|
this.params = {community: encodeURIComponent('"'+community.queryId+'"')};
|
91
|
this.firstVersionOfManagers = community.managers.slice();
|
92
|
console.log("First version of managers " + this.firstVersionOfManagers);
|
93
|
console.log(community);
|
94
|
this.showLoading = false;
|
95
|
},
|
96
|
error => this.handleError('System error retrieving community profile', error)
|
97
|
);
|
98
|
}
|
99
|
}
|
100
|
});
|
101
|
});
|
102
|
}
|
103
|
|
104
|
public scroll() {
|
105
|
console.info("scroll into view");
|
106
|
if (typeof document !== 'undefined') {
|
107
|
this.element.nativeElement.scrollIntoView();
|
108
|
}
|
109
|
}
|
110
|
|
111
|
public addManager() {
|
112
|
this.community.managers.push("");
|
113
|
}
|
114
|
|
115
|
public removeManager(i : any) {
|
116
|
this.community.managers.splice(i,1);
|
117
|
}
|
118
|
|
119
|
// public addSubject() {
|
120
|
// this.community.subjects.push("");
|
121
|
// }
|
122
|
//
|
123
|
// public removeSubject(i : any) {
|
124
|
// this.community.subjects.splice(i,1);
|
125
|
// }
|
126
|
|
127
|
public resetForm(communityId: string) {
|
128
|
if(!Session.isLoggedIn()){
|
129
|
console.info(this._router.url);
|
130
|
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
|
131
|
} else {
|
132
|
if (communityId != null && communityId != '') {
|
133
|
this.showLoading = true;
|
134
|
this.updateErrorMessage = "";
|
135
|
this.errorMessage = "";
|
136
|
|
137
|
this._communityService.getCommunity(this.properties, this.properties.communityAPI+communityId).subscribe (
|
138
|
community => {
|
139
|
this.community = community;
|
140
|
this.params = {community: encodeURIComponent('"'+community.queryId+'"')};
|
141
|
this.showLoading = false;
|
142
|
this.handleSuccessfulReset('Form reseted!')
|
143
|
},
|
144
|
error => this.handleError('System error retrieving community profile', error)
|
145
|
);
|
146
|
}
|
147
|
this.resetChange();
|
148
|
}
|
149
|
}
|
150
|
|
151
|
public updateCommunity() {
|
152
|
if(!Session.isLoggedIn()){
|
153
|
console.info(this._router.url);
|
154
|
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
|
155
|
} else {
|
156
|
if (this.communityId != null && this.communityId != '') {
|
157
|
this.showLoading = true;
|
158
|
var community = this.parseUpdatedCommunity();
|
159
|
|
160
|
// TODO delete after testing
|
161
|
console.log(community);
|
162
|
|
163
|
if (!this.hasValidEmail(community) || !this.hasFilled(community["name"])) {
|
164
|
this._router.navigate(['/community-edit-form'], {queryParams: { "communityId": this.communityId}});
|
165
|
} else {
|
166
|
var newManagers = this.getNewManagers();
|
167
|
this._communityService.updateCommunity(this.properties.communityAPI+this.communityId, community).subscribe(
|
168
|
|
169
|
community => {
|
170
|
// TODO delete after testing
|
171
|
console.log("New managers in component: " + newManagers);
|
172
|
if (newManagers !== null) {
|
173
|
this.sendMailToNewManagers(newManagers);
|
174
|
console.log("Old managers in component: " + this.firstVersionOfManagers);
|
175
|
this.informOldManagersForTheNewOnes(this.firstVersionOfManagers);
|
176
|
for (let i = 0; i < newManagers.length; i++) {
|
177
|
console.log(newManagers[i]);
|
178
|
this._subscribeService.subscribeToCommunity(this.communityId, newManagers[i], this.properties.adminToolsAPIURL).subscribe(
|
179
|
res => {
|
180
|
console.log(res);
|
181
|
}
|
182
|
);
|
183
|
this._subscribeService.getCommunitySubscribers(this.communityId, this.properties.adminToolsAPIURL).subscribe(
|
184
|
res => {
|
185
|
console.log(res);
|
186
|
}
|
187
|
);
|
188
|
}
|
189
|
}
|
190
|
this.handleSuccessfulSave('Community saved!')
|
191
|
},
|
192
|
error => {
|
193
|
this.handleUpdateError('System error updating community profile', error)
|
194
|
},
|
195
|
() => { this.firstVersionOfManagers = this.community["managers"].slice(); }
|
196
|
);
|
197
|
this._router.navigate(['/community-edit-form'], {queryParams: { "communityId": this.communityId}});
|
198
|
}
|
199
|
}
|
200
|
this.resetChange();
|
201
|
}
|
202
|
}
|
203
|
|
204
|
|
205
|
private parseUpdatedCommunity() : {} {
|
206
|
var community = {};
|
207
|
|
208
|
community["name"] = this.community.title;
|
209
|
community["shortName"] = this.community.shortTitle;
|
210
|
community["status"] = this.community.status;
|
211
|
community["description"] = this.community.description;
|
212
|
community["logoUrl"] = this.community.logoUrl;
|
213
|
|
214
|
community['managers'] = new Array<string>();
|
215
|
this.community.managers = this.getNonEmptyItems(this.community.managers);
|
216
|
community['managers'] = this.community.managers;
|
217
|
|
218
|
return community;
|
219
|
}
|
220
|
|
221
|
private getNewManagers(): Array<string> {
|
222
|
|
223
|
let newManagers = null;
|
224
|
for (let i = 0; i < this.community['managers'].length; i++) {
|
225
|
if (!this.firstVersionOfManagers.includes(this.community['managers'][i])) {
|
226
|
if(newManagers === null){
|
227
|
newManagers = new Array<string>();
|
228
|
}
|
229
|
newManagers.push(this.community['managers'][i]);
|
230
|
}
|
231
|
}
|
232
|
console.log("New managers are: " + newManagers);
|
233
|
return newManagers;
|
234
|
}
|
235
|
|
236
|
private sendMailToNewManagers(managers: any) {
|
237
|
this._emailService.sendEmail(this.properties.adminToolsAPIURL + "/sendMail/",
|
238
|
Composer.composeEmailForNewManager(this.communityId, this.community.title, managers)).subscribe(
|
239
|
res => {
|
240
|
console.log("The email has been sent successfully!")
|
241
|
},
|
242
|
error => console.log(error)
|
243
|
);
|
244
|
}
|
245
|
|
246
|
private informOldManagersForTheNewOnes(managers: any) {
|
247
|
this._emailService.sendEmail(this.properties.adminToolsAPIURL + "/notifyForNewManagers/" + this.communityId,
|
248
|
Composer.composeEmailToInformOldManagersForTheNewOnes(this.community.title, this.communityId,
|
249
|
this.firstVersionOfManagers,
|
250
|
this.community.managers)).subscribe(
|
251
|
res => {
|
252
|
console.log("The email has been sent successfully!")
|
253
|
},
|
254
|
error => console.log(error)
|
255
|
);
|
256
|
}
|
257
|
|
258
|
private subscribeNewManagers(newManagers: string[]): boolean {
|
259
|
return true;
|
260
|
}
|
261
|
|
262
|
private getNonEmptyItems(data: string[]): string[] {
|
263
|
let length = data.length;
|
264
|
let arrayNonEmpty = new Array<string>();
|
265
|
|
266
|
let j = 0;
|
267
|
for (let i = 0; i < length; i++) {
|
268
|
if (this.isEmpty(data[i])) {
|
269
|
//console.log(data[i]);
|
270
|
} else if (this.isNonEmpty(data[i])) {
|
271
|
arrayNonEmpty[j] = data[i];
|
272
|
j++;
|
273
|
//console.log(data[i]);
|
274
|
}
|
275
|
}
|
276
|
return arrayNonEmpty;
|
277
|
}
|
278
|
|
279
|
private hasFilled(data: any): boolean {
|
280
|
if (this.isNonEmpty(data) && !this.isEmpty(data)) {
|
281
|
// TODO remove console message after final testing
|
282
|
console.log("NAME IS NOT EMPTY");
|
283
|
return true;
|
284
|
}
|
285
|
// TODO remove console message after final testing
|
286
|
console.log("NAME IS EMPTY");
|
287
|
return false;
|
288
|
}
|
289
|
|
290
|
private isEmpty(data: string): boolean {
|
291
|
if (data != undefined && !data.replace(/\s/g, '').length)
|
292
|
return true;
|
293
|
else
|
294
|
return false;
|
295
|
}
|
296
|
|
297
|
private isNonEmpty(data: string): boolean {
|
298
|
if (data != undefined && data != null)
|
299
|
return true;
|
300
|
else
|
301
|
return false;
|
302
|
}
|
303
|
|
304
|
private hasValidEmail(data: any): boolean {
|
305
|
let length = data['managers'].length;
|
306
|
|
307
|
for(let i = 0; i < length; i++) {
|
308
|
if (!this.emailValidator(data['managers'][i])){
|
309
|
// TODO remove console message after final testing
|
310
|
console.log("INVALID EMAIL");
|
311
|
return false;
|
312
|
}
|
313
|
}
|
314
|
// TODO remove console message after final testing
|
315
|
console.log("ALL EMAILS ARE VALID");
|
316
|
return true;
|
317
|
}
|
318
|
|
319
|
private emailValidator(email : any): boolean {
|
320
|
if (email.match("^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$"))
|
321
|
return true;
|
322
|
else
|
323
|
return false;
|
324
|
}
|
325
|
|
326
|
private change() {
|
327
|
this.hasChanged = true;
|
328
|
this.successfulSaveMessage = '';
|
329
|
this.successfulResetMessage = '';
|
330
|
// TODO remove after testing
|
331
|
console.log('I have changed: I AM TRUE');
|
332
|
}
|
333
|
|
334
|
private resetChange() {
|
335
|
this.hasChanged = false;
|
336
|
// TODO remove after testing
|
337
|
console.log('I have changed: I AM FALSE');
|
338
|
}
|
339
|
|
340
|
public get form() {
|
341
|
return this._fb.group({
|
342
|
_id : '',
|
343
|
name : ['', Validators.required]
|
344
|
});
|
345
|
}
|
346
|
|
347
|
public reset() {
|
348
|
this.myForm.patchValue({
|
349
|
name : '',
|
350
|
_id : ''
|
351
|
});
|
352
|
}
|
353
|
|
354
|
handleUpdateError(message: string, error) {
|
355
|
this.updateErrorMessage = message;
|
356
|
console.log('Server responded: ' +error);
|
357
|
|
358
|
this.showLoading = false;
|
359
|
}
|
360
|
|
361
|
handleError(message: string, error) {
|
362
|
this.errorMessage = message;
|
363
|
console.log('Server responded: ' + error);
|
364
|
|
365
|
this.showLoading = false;
|
366
|
}
|
367
|
|
368
|
handleSuccessfulSave(message) {
|
369
|
this.showLoading = false;
|
370
|
this.successfulSaveMessage = message;
|
371
|
}
|
372
|
|
373
|
handleSuccessfulReset(message) {
|
374
|
this.showLoading = false;
|
375
|
this.successfulResetMessage = message;
|
376
|
}
|
377
|
|
378
|
trackByFn(index: any, item: any) {
|
379
|
return index;
|
380
|
}
|
381
|
}
|