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 uk-button-primary' + (loading ? ' uk-disabled' : '')"
|
28
|
(click)="subscribe()"> Subscribe</button>
|
29
|
<button *ngIf="subscribed" [class]="'uk-button uk-button-primary' + (loading ? ' uk-disabled' : '')"
|
30
|
(click)="confirmOpen()"> Unsubscribe</button>
|
31
|
</span>
|
32
|
|
33
|
<span *ngIf="showNumbers && subscribers !=null && subscribers > 0 && showTemplate">
|
34
|
<span class="lowOpacityColor"> Members</span> {{subscribers}}
|
35
|
</span>
|
36
|
<modal-alert (alertOutput)="confirmClose($event)">
|
37
|
</modal-alert>
|
38
|
`
|
39
|
})
|
40
|
|
41
|
export class SubscribeComponent {
|
42
|
// @Input() showSubscribe:boolean = true;
|
43
|
@Input() showNumbers: boolean;
|
44
|
@Input() communityId: string;
|
45
|
@Input() showTemplate: boolean = true;
|
46
|
@Output() subscribeEvent = new EventEmitter();
|
47
|
|
48
|
public community = null;
|
49
|
public emailToInformManagers: Email;
|
50
|
|
51
|
loading: boolean = false;
|
52
|
subscribed: boolean = null;
|
53
|
properties: EnvProperties;
|
54
|
subscribers: number = null;
|
55
|
showLoginAlert: Boolean = false;
|
56
|
@ViewChild(AlertModal) alert;
|
57
|
private user: User;
|
58
|
|
59
|
constructor(private route: ActivatedRoute,
|
60
|
private _subscribeService: SubscribeService,
|
61
|
private _emailService: EmailService,
|
62
|
private _communityService: CommunityService,
|
63
|
private router: Router,
|
64
|
private userManagementService: UserManagementService
|
65
|
) {
|
66
|
}
|
67
|
|
68
|
public ngOnInit() {
|
69
|
this.route.data.subscribe((data: { envSpecific: EnvProperties }) => {
|
70
|
this.properties = data.envSpecific;
|
71
|
if(!this.showNumbers) {
|
72
|
this.userManagementService.getUserInfo().subscribe(user => {
|
73
|
this.user = user;
|
74
|
this.init();
|
75
|
});
|
76
|
} else {
|
77
|
this.init();
|
78
|
}
|
79
|
});
|
80
|
//this.init();
|
81
|
}
|
82
|
|
83
|
private init() {
|
84
|
if (!this.showNumbers) {
|
85
|
let email = (this.user) ? this.user.email : null;
|
86
|
if (email == null) {
|
87
|
this.subscribed = false;
|
88
|
} else {
|
89
|
if (this.communityId) {
|
90
|
this._subscribeService.isSubscribedToCommunity(this.properties, this.communityId).subscribe(
|
91
|
res => {
|
92
|
this.subscribed = res;
|
93
|
if (this.subscribed) {
|
94
|
this.subscribeEvent.emit({
|
95
|
value: "ok"
|
96
|
});
|
97
|
}
|
98
|
},
|
99
|
error => {
|
100
|
this.handleError("Error getting response if email: " + email + " is subscribed to community with id: " + this.communityId, error);
|
101
|
});
|
102
|
}
|
103
|
}
|
104
|
} else {
|
105
|
if (this.communityId) {
|
106
|
this._subscribeService.getNumberOfCommunitySubscribers(this.properties, this.communityId).subscribe(
|
107
|
res => {
|
108
|
this.subscribers = (res && res.value) ? res.value : 0;//(res && res.subscribers && res.subscribers.length) ? res.subscribers.length : 0;
|
109
|
},
|
110
|
error => {
|
111
|
this.handleError("Error getting community subscribers for community with id: " + this.communityId, error);
|
112
|
});
|
113
|
}
|
114
|
}
|
115
|
if (this.communityId) {
|
116
|
this.emailToInformManagers = {body: "", subject: "", recipients: []};
|
117
|
|
118
|
this._communityService.getCommunity(this.properties, this.properties.communityAPI + this.communityId).subscribe(
|
119
|
community => {
|
120
|
this.community = community;
|
121
|
},
|
122
|
error => {
|
123
|
//console.log('System error retrieving community profile', error)
|
124
|
this.handleError("Error getting community with id: " + this.communityId, error);
|
125
|
}
|
126
|
);
|
127
|
}
|
128
|
}
|
129
|
|
130
|
subscribe() {
|
131
|
var email = (this.user) ? this.user.email : null;
|
132
|
if (email == null) {
|
133
|
this.subscribed = false;
|
134
|
// this.showLoginAlert = true;
|
135
|
this.router.navigate(['/user-info'], {
|
136
|
queryParams: {
|
137
|
"errorCode": LoginErrorCodes.ACTION_REQUIRES_LOGIN,
|
138
|
"redirectUrl": this.router.url
|
139
|
}
|
140
|
});
|
141
|
} else {
|
142
|
this.loading = true;
|
143
|
this.showLoginAlert = false;
|
144
|
this._subscribeService.subscribeToCommunity(this.properties, this.communityId).subscribe(
|
145
|
res => {
|
146
|
this.loading = false;
|
147
|
if (res.status && res.status != 200) {
|
148
|
this.subscribeEvent.emit({
|
149
|
value: "error"
|
150
|
});
|
151
|
UIkit.notification({
|
152
|
message: '<strong>An error occured. Please try again!<strong>',
|
153
|
status: 'warning',
|
154
|
timeout: 3000,
|
155
|
pos: 'top-center'
|
156
|
});
|
157
|
} else {
|
158
|
if (!this.subscribed) {
|
159
|
this.subscribed = true;
|
160
|
this.subscribeEvent.emit({
|
161
|
value: "ok"
|
162
|
});
|
163
|
this._emailService.notifyForNewManagers(this.properties, this.communityId, Composer.composeEmailToInformManagers(this.community.title, this.communityId, this.community.managers, email)).subscribe(
|
164
|
res => {
|
165
|
//console.log("The email has been sent successfully!")
|
166
|
},
|
167
|
error => {
|
168
|
//console.log(error)
|
169
|
this.handleError("Error notifying managers about new subscribers for community with id: " + this.communityId, error);
|
170
|
}
|
171
|
);
|
172
|
}
|
173
|
}
|
174
|
},
|
175
|
error => {
|
176
|
this.loading = false;
|
177
|
this.subscribeEvent.emit({
|
178
|
value: "error"
|
179
|
});
|
180
|
UIkit.notification({
|
181
|
message: '<strong>An error occured. Please try again!<strong>',
|
182
|
status: 'warning',
|
183
|
timeout: 3000,
|
184
|
pos: 'top-center'
|
185
|
});
|
186
|
//console.log(error)
|
187
|
this.handleError("Error subscribing email: " + email + " to community with id: " + this.communityId, error);
|
188
|
});
|
189
|
}
|
190
|
}
|
191
|
|
192
|
unsubscribe() {
|
193
|
var email = (this.user) ? this.user.email : null;
|
194
|
if (email == null) {
|
195
|
this.subscribed = false;
|
196
|
} else {
|
197
|
this.loading = true;
|
198
|
//this.properties.adminToolsAPIURL
|
199
|
this._subscribeService.unSubscribeToCommunity(this.properties, this.communityId).subscribe(
|
200
|
res => {
|
201
|
this.loading = false;
|
202
|
if (res.status && res.status != 200) {
|
203
|
UIkit.notification({
|
204
|
message: '<strong>An error occured. Please try again!<strong>',
|
205
|
status: 'warning',
|
206
|
timeout: 3000,
|
207
|
pos: 'top-center'
|
208
|
});
|
209
|
} else {
|
210
|
//console.log(res);
|
211
|
if (this.subscribed) {
|
212
|
console.log('here')
|
213
|
this.subscribed = false;
|
214
|
}
|
215
|
}
|
216
|
},
|
217
|
error => {
|
218
|
this.loading = false;
|
219
|
UIkit.notification({
|
220
|
message: '<strong>An error occured. Please try again!<strong>',
|
221
|
status: 'warning',
|
222
|
timeout: 3000,
|
223
|
pos: 'top-center'
|
224
|
});
|
225
|
//console.log(error)
|
226
|
this.handleError("Error unsubscribing email: " + email + " from community with id: " + this.communityId, error);
|
227
|
});
|
228
|
}
|
229
|
}
|
230
|
|
231
|
confirmOpen() {
|
232
|
|
233
|
this.alert.cancelButton = true;
|
234
|
this.alert.okButton = true;
|
235
|
this.alert.alertTitle = "Unsubscribe community ";
|
236
|
this.alert.message = "Do you want to proceed? ";
|
237
|
this.alert.okButtonText = "Yes";
|
238
|
this.alert.cancelButtonText = "No";
|
239
|
this.alert.open();
|
240
|
}
|
241
|
|
242
|
confirmClose(data) {
|
243
|
this.unsubscribe();
|
244
|
}
|
245
|
|
246
|
private handleError(message: string, error) {
|
247
|
console.error("Subscribe (component): " + message, error);
|
248
|
}
|
249
|
}
|