1
|
import {Component, OnInit, Input, ElementRef} from '@angular/core';
|
2
|
import {FormGroup, 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 {SubscribeService} from '../../../openaireLibrary/utils/subscribe/subscribe.service';
|
8
|
import {EmailService} from '../../../openaireLibrary/utils/email/email.service';
|
9
|
|
10
|
import {EnvProperties} from '../../../openaireLibrary/utils/properties/env-properties';
|
11
|
import {Email} from '../../../openaireLibrary/utils/email/email';
|
12
|
import {Composer} from '../../../openaireLibrary/utils/email/composer';
|
13
|
import {Validator} from '../../../openaireLibrary/utils/email/validator';
|
14
|
|
15
|
import {Session} from '../../../openaireLibrary/login/utils/helper.class';
|
16
|
import {LoginErrorCodes} from '../../../openaireLibrary/login/utils/guardHelper.class';
|
17
|
import {HelperFunctions} from "../../../openaireLibrary/utils/HelperFunctions.class";
|
18
|
import {Title} from '@angular/platform-browser';
|
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 = true;
|
31
|
public errorMessage = '';
|
32
|
public updateErrorMessage = '';
|
33
|
|
34
|
public successfulSaveMessage = '';
|
35
|
public successfulResetMessage = '';
|
36
|
|
37
|
public hasChanged = 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 = '';
|
53
|
|
54
|
public properties: EnvProperties = null;
|
55
|
|
56
|
constructor (private element: ElementRef,
|
57
|
private title: Title,
|
58
|
private route: ActivatedRoute,
|
59
|
private _router: Router,
|
60
|
public _fb: FormBuilder,
|
61
|
private _helpContentService: HelpContentService,
|
62
|
private _communityService: CommunityService,
|
63
|
private _subscribeService: SubscribeService,
|
64
|
private _emailService: EmailService) { }
|
65
|
|
66
|
|
67
|
ngOnInit() {
|
68
|
this.route.data.subscribe((data: { envSpecific: EnvProperties }) => {
|
69
|
this.properties = data.envSpecific;
|
70
|
this.route.queryParams.subscribe(
|
71
|
communityId => {
|
72
|
HelperFunctions.scroll();
|
73
|
this.title.setTitle('Administration Dashboard | Community Profile');
|
74
|
this.communityId = communityId['communityId'];
|
75
|
|
76
|
this.email = {body: '', subject: '', recipients: []};
|
77
|
this.emailToInform = {body: '', subject: '', recipients: []};
|
78
|
|
79
|
if (!Session.isLoggedIn()) {
|
80
|
this._router.navigate(['/user-info'], {
|
81
|
queryParams: { 'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url} });
|
82
|
} else {
|
83
|
if (this.communityId != null && this.communityId !== '') {
|
84
|
this.showLoading = true;
|
85
|
this.updateErrorMessage = '';
|
86
|
this.errorMessage = '';
|
87
|
|
88
|
this._communityService.getCommunity(this.properties,
|
89
|
this.properties.communityAPI + this.communityId).subscribe (
|
90
|
community => {
|
91
|
this.community = community;
|
92
|
this.params = {community: encodeURIComponent(
|
93
|
'"' + community.queryId + '"')};
|
94
|
this.firstVersionOfManagers = community.managers.slice();
|
95
|
this.showLoading = false;
|
96
|
},
|
97
|
error => this.handleError('System error retrieving community profile', error)
|
98
|
);
|
99
|
}
|
100
|
}
|
101
|
});
|
102
|
});
|
103
|
}
|
104
|
|
105
|
public addManager() {
|
106
|
this.community.managers.push('');
|
107
|
}
|
108
|
|
109
|
public removeManager(i: any) {
|
110
|
this.community.managers.splice(i, 1);
|
111
|
}
|
112
|
|
113
|
// public addSubject() {
|
114
|
// this.community.subjects.push("");
|
115
|
// }
|
116
|
//
|
117
|
// public removeSubject(i : any) {
|
118
|
// this.community.subjects.splice(i,1);
|
119
|
// }
|
120
|
|
121
|
public resetForm(communityId: string) {
|
122
|
if (!Session.isLoggedIn()) {
|
123
|
this._router.navigate(['/user-info'], { queryParams:
|
124
|
{ 'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url} });
|
125
|
} else {
|
126
|
if (communityId != null && communityId !== '') {
|
127
|
this.showLoading = true;
|
128
|
this.updateErrorMessage = '';
|
129
|
this.errorMessage = '';
|
130
|
|
131
|
this._communityService.getCommunity(this.properties,
|
132
|
this.properties.communityAPI + communityId).subscribe (
|
133
|
community => {
|
134
|
this.community = community;
|
135
|
this.params = {community: encodeURIComponent(
|
136
|
'"' + community.queryId + '"')};
|
137
|
this.showLoading = false;
|
138
|
this.handleSuccessfulReset('Form reset!');
|
139
|
},
|
140
|
error => this.handleError('System error retrieving community profile', error)
|
141
|
);
|
142
|
}
|
143
|
this.resetChange();
|
144
|
}
|
145
|
}
|
146
|
|
147
|
public updateCommunity() {
|
148
|
if (!Session.isLoggedIn()) {
|
149
|
this._router.navigate(['/user-info'], { queryParams:
|
150
|
{ 'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url} });
|
151
|
} else {
|
152
|
if (this.communityId != null && this.communityId !== '') {
|
153
|
this.showLoading = true;
|
154
|
const community = this.parseUpdatedCommunity();
|
155
|
|
156
|
if (!Validator.hasValidEmails(community['managers']) || !this.hasFilled(community['name'])) {
|
157
|
this._router.navigate(['/community-edit-form'], {
|
158
|
queryParams: { 'communityId': this.communityId}});
|
159
|
} else {
|
160
|
const newManagers = this.getNewManagers();
|
161
|
this._communityService.updateCommunity(
|
162
|
this.properties.communityAPI + this.communityId, community).subscribe(
|
163
|
|
164
|
community => {
|
165
|
if (newManagers !== null) {
|
166
|
this.sendMailToNewManagers(newManagers);
|
167
|
this.informOldManagersForTheNewOnes();
|
168
|
for (let i = 0; i < newManagers.length; i++) {
|
169
|
this._subscribeService.subscribeToCommunityByEmail(this.properties, this.communityId, newManagers[i]).subscribe(
|
170
|
res => {
|
171
|
// console.log(res);
|
172
|
}
|
173
|
);
|
174
|
this._subscribeService.getCommunitySubscribers
|
175
|
(this.properties, this.communityId).subscribe(
|
176
|
res => {
|
177
|
// console.log(res);
|
178
|
}
|
179
|
);
|
180
|
}
|
181
|
}
|
182
|
this.handleSuccessfulSave('Community saved!');
|
183
|
},
|
184
|
error => {
|
185
|
this.handleUpdateError('System error updating community profile', error);
|
186
|
},
|
187
|
() => { this.firstVersionOfManagers = this.community['managers'].slice(); }
|
188
|
);
|
189
|
this._router.navigate(['/community-edit-form'], {
|
190
|
queryParams: { 'communityId': this.communityId}});
|
191
|
}
|
192
|
}
|
193
|
this.resetChange();
|
194
|
}
|
195
|
}
|
196
|
|
197
|
|
198
|
private parseUpdatedCommunity(): {} {
|
199
|
const community = {};
|
200
|
|
201
|
community['name'] = this.community.title;
|
202
|
community['shortName'] = this.community.shortTitle;
|
203
|
community['status'] = this.community.status;
|
204
|
community['description'] = this.community.description;
|
205
|
community['logoUrl'] = this.community.logoUrl;
|
206
|
|
207
|
community['managers'] = new Array<string>();
|
208
|
this.community.managers = this.getNonEmptyItems(this.community.managers);
|
209
|
community['managers'] = this.community.managers;
|
210
|
|
211
|
return community;
|
212
|
}
|
213
|
|
214
|
private getNewManagers(): Array<string> {
|
215
|
|
216
|
let newManagers = null;
|
217
|
for (let i = 0; i < this.community['managers'].length; i++) {
|
218
|
if (!this.firstVersionOfManagers.includes(this.community['managers'][i])) {
|
219
|
if(newManagers === null) {
|
220
|
newManagers = new Array<string>();
|
221
|
}
|
222
|
newManagers.push(this.community['managers'][i]);
|
223
|
}
|
224
|
}
|
225
|
return newManagers;
|
226
|
}
|
227
|
|
228
|
private sendMailToNewManagers(managers: any) {
|
229
|
this._emailService.sendEmail(this.properties,
|
230
|
Composer.composeEmailForNewManager(this.communityId,
|
231
|
this.community.title, managers, this.properties.admins[0])).subscribe(
|
232
|
res => {
|
233
|
// console.log("The email has been sent successfully!")
|
234
|
},
|
235
|
error => console.log(error)
|
236
|
);
|
237
|
}
|
238
|
|
239
|
private informOldManagersForTheNewOnes() {
|
240
|
this._emailService.notifyForNewManagers(
|
241
|
this.properties, this.communityId,
|
242
|
Composer.composeEmailToInformOldManagersForTheNewOnes(this.community.title, this.communityId,
|
243
|
this.firstVersionOfManagers,
|
244
|
this.community.managers, this.properties.admins[0])).subscribe(
|
245
|
res => {
|
246
|
// console.log("The email has been sent successfully!")
|
247
|
},
|
248
|
error => console.log(error)
|
249
|
);
|
250
|
}
|
251
|
|
252
|
private subscribeNewManagers(newManagers: string[]): boolean {
|
253
|
return true;
|
254
|
}
|
255
|
|
256
|
private getNonEmptyItems(data: string[]): string[] {
|
257
|
const length = data.length;
|
258
|
const arrayNonEmpty = new Array<string>();
|
259
|
|
260
|
let j = 0;
|
261
|
for (let i = 0; i < length; i++) {
|
262
|
if (this.isEmpty(data[i])) {
|
263
|
// console.log(data[i]);
|
264
|
} else if (this.isNonEmpty(data[i])) {
|
265
|
arrayNonEmpty[j] = data[i];
|
266
|
j++;
|
267
|
// console.log(data[i]);
|
268
|
}
|
269
|
}
|
270
|
return arrayNonEmpty;
|
271
|
}
|
272
|
|
273
|
private hasFilled(data: any): boolean {
|
274
|
if (this.isNonEmpty(data) && !this.isEmpty(data)) {
|
275
|
return true;
|
276
|
}
|
277
|
return false;
|
278
|
}
|
279
|
|
280
|
private isEmpty(data: string): boolean {
|
281
|
if (data !== undefined && !data.replace(/\s/g, '').length) {
|
282
|
return true;
|
283
|
} else {
|
284
|
return false;
|
285
|
|
286
|
}
|
287
|
}
|
288
|
|
289
|
private isNonEmpty(data: string): boolean {
|
290
|
if (data !== undefined && data != null) {
|
291
|
return true;
|
292
|
} else {
|
293
|
return false;
|
294
|
|
295
|
}
|
296
|
}
|
297
|
|
298
|
private change() {
|
299
|
this.hasChanged = true;
|
300
|
this.successfulSaveMessage = '';
|
301
|
this.successfulResetMessage = '';
|
302
|
}
|
303
|
|
304
|
private resetChange() {
|
305
|
this.hasChanged = false;
|
306
|
}
|
307
|
|
308
|
public get form() {
|
309
|
return this._fb.group({
|
310
|
_id : '',
|
311
|
name : ['', Validators.required]
|
312
|
});
|
313
|
}
|
314
|
|
315
|
public reset() {
|
316
|
this.myForm.patchValue({
|
317
|
name : '',
|
318
|
_id : ''
|
319
|
});
|
320
|
}
|
321
|
|
322
|
handleUpdateError(message: string, error) {
|
323
|
this.updateErrorMessage = message;
|
324
|
console.log('Server responded: ' + error);
|
325
|
|
326
|
this.showLoading = false;
|
327
|
}
|
328
|
|
329
|
handleError(message: string, error) {
|
330
|
this.errorMessage = message;
|
331
|
console.log('Server responded: ' + error);
|
332
|
|
333
|
this.showLoading = false;
|
334
|
}
|
335
|
|
336
|
handleSuccessfulSave(message) {
|
337
|
this.showLoading = false;
|
338
|
this.successfulSaveMessage = message;
|
339
|
}
|
340
|
|
341
|
handleSuccessfulReset(message) {
|
342
|
this.showLoading = false;
|
343
|
this.successfulResetMessage = message;
|
344
|
}
|
345
|
|
346
|
trackByFn(index: any, item: any) {
|
347
|
return index;
|
348
|
}
|
349
|
}
|