1
|
import {Component, EventEmitter, Input, Output, ViewChild} from '@angular/core';
|
2
|
import {ActivatedRoute, Router} from '@angular/router';
|
3
|
import {EnvProperties} from '../../openaireLibrary/utils/properties/env-properties';
|
4
|
import {AlertModal} from '../../openaireLibrary/utils/modal/alert';
|
5
|
|
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
|
import {Session, User} from '../../openaireLibrary/login/utils/helper.class';
|
10
|
|
11
|
import {Email} from "../../openaireLibrary/utils/email/email";
|
12
|
import {Composer} from "../../openaireLibrary/utils/email/composer";
|
13
|
import {LoginErrorCodes} from '../../openaireLibrary/login/utils/guardHelper.class';
|
14
|
import {UserManagementService} from "../../openaireLibrary/services/user-management.service";
|
15
|
|
16
|
declare var UIkit: any;
|
17
|
|
18
|
@Component({
|
19
|
selector: 'subscribe',
|
20
|
template: `
|
21
|
|
22
|
<span *ngIf="subscribed != null && !showNumbers && showTemplate">
|
23
|
<div *ngIf="!subscribed && showLoginAlert" class="uk-alert-warning uk-animation-slide-bottom" uk-alert="">
|
24
|
<a class="uk-alert-close" uk-close></a>
|
25
|
<p>Please login first to subscribe</p>
|
26
|
</div>
|
27
|
<button *ngIf="!subscribed" [class]="'uk-button portal-button uk-button-small uk-width-1-1 ' + (loading ? ' uk-disabled' : '')"
|
28
|
(click)="subscribe()">
|
29
|
<span class="uk-icon uk-flex uk-flex-middle">
|
30
|
<svg height="24" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
|
31
|
<path d="M0 0h24v24H0z" fill="none"></path>
|
32
|
<path d="M16 11c1.66 0 2.99-1.34 2.99-3S17.66 5 16 5c-1.66 0-3 1.34-3 3s1.34 3 3 3zm-8 0c1.66 0 2.99-1.34 2.99-3S9.66 5 8 5C6.34 5 5 6.34 5 8s1.34 3 3 3zm0 2c-2.33 0-7 1.17-7 3.5V19h14v-2.5c0-2.33-4.67-3.5-7-3.5zm8 0c-.29 0-.62.02-.97.05 1.16.84 1.97 1.97 1.97 3.45V19h6v-2.5c0-2.33-4.67-3.5-7-3.5z"></path>
|
33
|
</svg>
|
34
|
<span class="space">Subscribe</span>
|
35
|
</span>
|
36
|
</button>
|
37
|
<button *ngIf="subscribed" [class]="'subscribed-button uk-button uk-button-small uk-width-1-1 ' + (loading ? ' uk-disabled' : '')"
|
38
|
(click)="confirmOpen()">
|
39
|
<span class="uk-icon uk-flex uk-flex-middle">
|
40
|
<svg height="24" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
|
41
|
<path d="M0 0h24v24H0z" fill="none"></path>
|
42
|
<path d="M16 11c1.66 0 2.99-1.34 2.99-3S17.66 5 16 5c-1.66 0-3 1.34-3 3s1.34 3 3 3zm-8 0c1.66 0 2.99-1.34 2.99-3S9.66 5 8 5C6.34 5 5 6.34 5 8s1.34 3 3 3zm0 2c-2.33 0-7 1.17-7 3.5V19h14v-2.5c0-2.33-4.67-3.5-7-3.5zm8 0c-.29 0-.62.02-.97.05 1.16.84 1.97 1.97 1.97 3.45V19h6v-2.5c0-2.33-4.67-3.5-7-3.5z"></path>
|
43
|
</svg>
|
44
|
<span class="space">Subscribed</span>
|
45
|
</span>
|
46
|
</button>
|
47
|
</span>
|
48
|
|
49
|
<span *ngIf="showNumbers && subscribers !=null && subscribers > 0 && showTemplate" class="uk-display-inline-block">
|
50
|
<span class="lowOpacityColor uk-text-muted">Members: </span> {{subscribers}}
|
51
|
</span>
|
52
|
<modal-alert (alertOutput)="confirmClose($event)">
|
53
|
</modal-alert>
|
54
|
`
|
55
|
})
|
56
|
|
57
|
export class SubscribeComponent {
|
58
|
// @Input() showSubscribe:boolean = true;
|
59
|
@Input() showNumbers: boolean;
|
60
|
@Input() communityId: string;
|
61
|
@Input() showTemplate: boolean = true;
|
62
|
@Output() subscribeEvent = new EventEmitter();
|
63
|
|
64
|
public community = null;
|
65
|
public emailToInformManagers: Email;
|
66
|
|
67
|
loading: boolean = false;
|
68
|
subscribed: boolean = null;
|
69
|
@Input() properties: EnvProperties;
|
70
|
subscribers: number = null;
|
71
|
@Output() countSubscribersEvent = new EventEmitter();
|
72
|
showLoginAlert: Boolean = false;
|
73
|
@ViewChild(AlertModal) alert;
|
74
|
private user: User;
|
75
|
|
76
|
constructor(private route: ActivatedRoute,
|
77
|
private _subscribeService: SubscribeService,
|
78
|
private _emailService: EmailService,
|
79
|
private _communityService: CommunityService,
|
80
|
private router: Router,
|
81
|
private userManagementService: UserManagementService
|
82
|
) {}
|
83
|
|
84
|
public ngOnInit() {
|
85
|
this.route.data.subscribe((data: { envSpecific: EnvProperties }) => {
|
86
|
if(!this.properties) {
|
87
|
this.properties = data.envSpecific;
|
88
|
}
|
89
|
if(!this.showNumbers) {
|
90
|
this.userManagementService.getUserInfo().subscribe(
|
91
|
user => {
|
92
|
this.user = user;
|
93
|
this.init();
|
94
|
},
|
95
|
error => {},
|
96
|
() => {
|
97
|
this.init();
|
98
|
}
|
99
|
);
|
100
|
} else {
|
101
|
this.init();
|
102
|
}
|
103
|
});
|
104
|
//this.init();
|
105
|
}
|
106
|
|
107
|
private isSubscribed() {
|
108
|
this._subscribeService.isSubscribed.subscribe(
|
109
|
res => {
|
110
|
this.subscribed = res;
|
111
|
if (this.subscribed) {
|
112
|
this.subscribeEvent.emit({
|
113
|
value: "ok"
|
114
|
});
|
115
|
}
|
116
|
}
|
117
|
);
|
118
|
}
|
119
|
|
120
|
private init() {
|
121
|
if (!this.showNumbers) {
|
122
|
let email = (this.user) ? this.user.email : null;
|
123
|
if (email == null) {
|
124
|
this.subscribed = false;
|
125
|
} else {
|
126
|
if (this.communityId) {
|
127
|
// this._subscribeService.isSubscribedToCommunity(this.properties, this.communityId).subscribe(
|
128
|
// res => {
|
129
|
// this.subscribed = res;
|
130
|
// if (this.subscribed) {
|
131
|
// this.subscribeEvent.emit({
|
132
|
// value: "ok"
|
133
|
// });
|
134
|
// }
|
135
|
// },
|
136
|
// error => {
|
137
|
// this.handleError("Error getting response if email: " + email + " is subscribed to community with id: " + this.communityId, error);
|
138
|
// });
|
139
|
|
140
|
this._subscribeService.initIsSubscribedToCommunity(this.properties, this.communityId);
|
141
|
this.isSubscribed();
|
142
|
}
|
143
|
}
|
144
|
} else {
|
145
|
if (this.communityId) {
|
146
|
this._subscribeService.getNumberOfCommunitySubscribers(this.properties, this.communityId).subscribe(
|
147
|
res => {
|
148
|
this.subscribers = (res && res.value) ? res.value : 0;//(res && res.subscribers && res.subscribers.length) ? res.subscribers.length : 0;
|
149
|
this.countSubscribersEvent.emit({
|
150
|
value: this.subscribers
|
151
|
});
|
152
|
},
|
153
|
error => {
|
154
|
this.handleError("Error getting community subscribers for community with id: " + this.communityId, error);
|
155
|
});
|
156
|
}
|
157
|
}
|
158
|
if (this.communityId) {
|
159
|
this.emailToInformManagers = {body: "", subject: "", recipients: []};
|
160
|
|
161
|
this._communityService.getCommunity(this.properties, this.properties.communityAPI + this.communityId).subscribe(
|
162
|
community => {
|
163
|
this.community = community;
|
164
|
},
|
165
|
error => {
|
166
|
//console.log('System error retrieving community profile', error)
|
167
|
this.handleError("Error getting community with id: " + this.communityId, error);
|
168
|
}
|
169
|
);
|
170
|
}
|
171
|
}
|
172
|
|
173
|
subscribe() {
|
174
|
var email = (this.user) ? this.user.email : null;
|
175
|
if (email == null) {
|
176
|
this.subscribed = false;
|
177
|
// this.showLoginAlert = true;
|
178
|
this.router.navigate(['/user-info'], {
|
179
|
queryParams: {
|
180
|
"errorCode": LoginErrorCodes.ACTION_REQUIRES_LOGIN,
|
181
|
"redirectUrl": this.router.url
|
182
|
}
|
183
|
});
|
184
|
} else {
|
185
|
this.loading = true;
|
186
|
this.showLoginAlert = false;
|
187
|
this._subscribeService.subscribeToCommunity(this.properties, this.communityId).subscribe(
|
188
|
res => {
|
189
|
this.loading = false;
|
190
|
if (res.status && res.status != 200) {
|
191
|
this.subscribeEvent.emit({
|
192
|
value: "error"
|
193
|
});
|
194
|
UIkit.notification({
|
195
|
message: '<strong>An error occured. Please try again!<strong>',
|
196
|
status: 'warning',
|
197
|
timeout: 3000,
|
198
|
pos: 'top-center'
|
199
|
});
|
200
|
} else {
|
201
|
if (!this.subscribed) {
|
202
|
this.subscribed = true;
|
203
|
this.subscribeEvent.emit({
|
204
|
value: "ok"
|
205
|
});
|
206
|
this._emailService.notifyForNewManagers(this.properties, this.communityId, Composer.composeEmailToInformManagers(this.community.title, this.communityId, this.community.managers,
|
207
|
email, this.properties.adminPortalURL)).subscribe(
|
208
|
res => {
|
209
|
//console.log("The email has been sent successfully!")
|
210
|
},
|
211
|
error => {
|
212
|
//console.log(error)
|
213
|
this.handleError("Error notifying managers about new subscribers for community with id: " + this.communityId, error);
|
214
|
}
|
215
|
);
|
216
|
}
|
217
|
}
|
218
|
},
|
219
|
error => {
|
220
|
this.loading = false;
|
221
|
this.subscribeEvent.emit({
|
222
|
value: "error"
|
223
|
});
|
224
|
UIkit.notification({
|
225
|
message: '<strong>An error occured. Please try again!<strong>',
|
226
|
status: 'warning',
|
227
|
timeout: 3000,
|
228
|
pos: 'top-center'
|
229
|
});
|
230
|
//console.log(error)
|
231
|
this.handleError("Error subscribing email: " + email + " to community with id: " + this.communityId, error);
|
232
|
});
|
233
|
}
|
234
|
}
|
235
|
|
236
|
unsubscribe() {
|
237
|
var email = (this.user) ? this.user.email : null;
|
238
|
if (email == null) {
|
239
|
this.subscribed = false;
|
240
|
} else {
|
241
|
this.loading = true;
|
242
|
//this.properties.adminToolsAPIURL
|
243
|
this._subscribeService.unSubscribeToCommunity(this.properties, this.communityId).subscribe(
|
244
|
res => {
|
245
|
this.loading = false;
|
246
|
if (res.status && res.status != 200) {
|
247
|
UIkit.notification({
|
248
|
message: '<strong>An error occured. Please try again!<strong>',
|
249
|
status: 'warning',
|
250
|
timeout: 3000,
|
251
|
pos: 'top-center'
|
252
|
});
|
253
|
} else {
|
254
|
//console.log(res);
|
255
|
if (this.subscribed) {
|
256
|
this.subscribed = false;
|
257
|
}
|
258
|
}
|
259
|
},
|
260
|
error => {
|
261
|
this.loading = false;
|
262
|
UIkit.notification({
|
263
|
message: '<strong>An error occured. Please try again!<strong>',
|
264
|
status: 'warning',
|
265
|
timeout: 3000,
|
266
|
pos: 'top-center'
|
267
|
});
|
268
|
//console.log(error)
|
269
|
this.handleError("Error unsubscribing email: " + email + " from community with id: " + this.communityId, error);
|
270
|
});
|
271
|
}
|
272
|
}
|
273
|
|
274
|
confirmOpen() {
|
275
|
|
276
|
this.alert.cancelButton = true;
|
277
|
this.alert.okButton = true;
|
278
|
//this.alert.alertTitle = "Unsubscribe community ";
|
279
|
//this.alert.message = "Do you want to proceed? ";
|
280
|
this.alert.message = "You are subscribed to the Community Gateway. Do you want to unsubscribe?";
|
281
|
this.alert.okButtonText = "UNSUBSCRIBE";
|
282
|
this.alert.cancelButtonText = "CANCEL";
|
283
|
this.alert.open();
|
284
|
}
|
285
|
|
286
|
confirmClose(data) {
|
287
|
this.unsubscribe();
|
288
|
}
|
289
|
|
290
|
private handleError(message: string, error) {
|
291
|
console.error("Subscribe (component): " + message, error);
|
292
|
}
|
293
|
}
|