2018-03-13 17:06:04 +01:00
|
|
|
import { Component, Input, ViewChild } from '@angular/core';
|
2018-03-06 13:35:29 +01:00
|
|
|
import { Location } from '@angular/common';
|
|
|
|
import {ActivatedRoute} from '@angular/router';
|
|
|
|
import { EnvProperties} from '../../openaireLibrary/utils/properties/env-properties';
|
2018-03-13 17:06:04 +01:00
|
|
|
import {AlertModal} from '../../openaireLibrary/utils/modal/alert';
|
2018-03-06 13:35:29 +01:00
|
|
|
|
2018-11-22 12:38:24 +01:00
|
|
|
import {CommunityService} from '../../openaireLibrary/connect/community/community.service';
|
2018-07-06 12:30:20 +02:00
|
|
|
import {SubscribeService} from '../../openaireLibrary/utils/subscribe/subscribe.service';
|
2018-11-22 12:38:24 +01:00
|
|
|
import {EmailService} from "../../openaireLibrary/utils/email/email.service";
|
2018-03-06 13:35:29 +01:00
|
|
|
import {Session} from '../../openaireLibrary/login/utils/helper.class';
|
2018-10-01 14:46:21 +02:00
|
|
|
|
2018-11-22 12:38:24 +01:00
|
|
|
import {Email} from "../../openaireLibrary/utils/email/email";
|
|
|
|
|
2018-10-01 14:46:21 +02:00
|
|
|
declare var UIkit: any;
|
|
|
|
|
2018-03-06 13:35:29 +01:00
|
|
|
@Component({
|
|
|
|
selector: 'subscribe',
|
|
|
|
template: `
|
|
|
|
|
2018-04-03 14:38:28 +02:00
|
|
|
<span *ngIf="subscribed != null && !showNumbers">
|
2018-03-13 17:06:04 +01:00
|
|
|
<div *ngIf="!subscribed && showLoginAlert" class="uk-alert-warning uk-animation-slide-bottom" uk-alert="" >
|
|
|
|
<a class="uk-alert-close" uk-close></a>
|
|
|
|
<p>Please login first to subscribe</p>
|
|
|
|
</div>
|
2018-10-05 01:10:13 +02:00
|
|
|
<a *ngIf="!subscribed" [class]="'uk-button portal-button' + (loading ? ' uk-disabled' : '')" (click)="subscribe()"> Subscribe</a>
|
2018-10-01 14:46:21 +02:00
|
|
|
<a *ngIf="subscribed" [class]="'uk-button uk-button-danger' + (loading ? ' uk-disabled' : '')" (click)="confirmOpen()"> Unsubscribe</a>
|
2018-04-03 14:38:28 +02:00
|
|
|
</span>
|
2018-03-13 17:06:04 +01:00
|
|
|
|
|
|
|
<span *ngIf="showNumbers && subscribers !=null && subscribers > 0" >
|
|
|
|
Members: {{subscribers}}
|
|
|
|
</span>
|
|
|
|
<modal-alert (alertOutput)="confirmClose($event)">
|
|
|
|
</modal-alert>
|
2018-03-06 13:35:29 +01:00
|
|
|
`
|
|
|
|
})
|
|
|
|
|
|
|
|
export class SubscribeComponent {
|
|
|
|
// @Input() showSubscribe:boolean = true;
|
|
|
|
@Input() showNumbers:boolean;
|
|
|
|
@Input() communityId:string;
|
|
|
|
|
2018-11-22 12:38:24 +01:00
|
|
|
public community = null;
|
|
|
|
public emailToInformManagers: Email;
|
|
|
|
|
2018-10-01 14:46:21 +02:00
|
|
|
loading: boolean = false;
|
2018-03-06 13:35:29 +01:00
|
|
|
subscribed:boolean = null;
|
|
|
|
properties:EnvProperties;
|
2018-03-13 17:06:04 +01:00
|
|
|
subscribers:number= null;
|
|
|
|
showLoginAlert:Boolean = false;
|
|
|
|
@ViewChild(AlertModal) alert;
|
2018-03-06 13:35:29 +01:00
|
|
|
constructor (private route: ActivatedRoute,
|
2018-11-22 12:38:24 +01:00
|
|
|
private _subscribeService: SubscribeService,
|
|
|
|
private _emailService: EmailService,
|
|
|
|
private _communityService: CommunityService
|
2018-03-06 13:35:29 +01:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
public ngOnInit() {
|
|
|
|
this.route.data
|
|
|
|
.subscribe((data: { envSpecific: EnvProperties }) => {
|
|
|
|
this.properties = data.envSpecific;
|
2018-03-13 17:06:04 +01:00
|
|
|
if(!this.showNumbers){
|
|
|
|
var email = Session.getUserEmail();
|
|
|
|
if(email == null){
|
|
|
|
this.subscribed = false;
|
|
|
|
}else{
|
|
|
|
this._subscribeService.isSubscribedToCommunity(this.communityId, email,this.properties.adminToolsAPIURL).subscribe (
|
|
|
|
res => {
|
|
|
|
this.subscribed = res;
|
|
|
|
});
|
|
|
|
}
|
2018-03-06 13:35:29 +01:00
|
|
|
}else{
|
2018-03-13 17:06:04 +01:00
|
|
|
this._subscribeService.getCommunitySubscribers(this.communityId, this.properties.adminToolsAPIURL).subscribe (
|
2018-03-06 13:35:29 +01:00
|
|
|
res => {
|
2018-03-13 17:06:04 +01:00
|
|
|
|
|
|
|
this.subscribers = (res && res.subscribers && res.subscribers.length )?res.subscribers.length:0;
|
2018-03-06 13:35:29 +01:00
|
|
|
});
|
|
|
|
}
|
2018-11-22 12:38:24 +01:00
|
|
|
|
|
|
|
this.emailToInformManagers = {body: "", subject: "", recipients: []};
|
|
|
|
|
|
|
|
this._communityService.getCommunity(this.properties, this.properties.communityAPI+this.communityId).subscribe (
|
|
|
|
community => {
|
|
|
|
this.community = community;
|
|
|
|
console.log(this.community);
|
|
|
|
},
|
|
|
|
error => console.log('System error retrieving community profile', error)
|
|
|
|
);
|
2018-03-06 13:35:29 +01:00
|
|
|
});
|
|
|
|
|
2018-03-13 17:06:04 +01:00
|
|
|
|
2018-03-06 13:35:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
subscribe(){
|
|
|
|
var email = Session.getUserEmail();
|
|
|
|
if(email == null){
|
|
|
|
this.subscribed = false;
|
2018-03-13 17:06:04 +01:00
|
|
|
this.showLoginAlert = true;
|
2018-03-06 13:35:29 +01:00
|
|
|
}else{
|
2018-10-01 14:46:21 +02:00
|
|
|
this.loading = true;
|
2018-03-13 17:06:04 +01:00
|
|
|
this.showLoginAlert = false;
|
2018-03-06 13:35:29 +01:00
|
|
|
this._subscribeService.subscribeToCommunity(this.communityId, email, this.properties.adminToolsAPIURL).subscribe (
|
|
|
|
res => {
|
2018-10-01 14:46:21 +02:00
|
|
|
this.loading = false;
|
2018-03-06 13:35:29 +01:00
|
|
|
console.log(res);
|
2018-10-01 14:46:21 +02:00
|
|
|
if(res.status && res.status != 200) {
|
|
|
|
UIkit.notification({
|
|
|
|
message : '<strong>There was an error in your subscription. Please try again!<strong>',
|
|
|
|
status : 'warning',
|
|
|
|
timeout : 3000,
|
|
|
|
pos : 'top-center'
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
if(!this.subscribed){
|
|
|
|
this.subscribed = true;
|
2018-11-26 17:05:33 +01:00
|
|
|
this._emailService.sendEmail(this.properties.adminToolsAPIURL + "/notifyForNewSubscribers/" + this.communityId, this.composeEmailToInformManagers(this.community.title, this.communityId, this.community.managers)).subscribe(
|
2018-11-22 12:38:24 +01:00
|
|
|
res => {
|
|
|
|
console.log("The email has been sent successfully!")
|
|
|
|
},
|
|
|
|
error => console.log(error)
|
|
|
|
);
|
2018-10-01 14:46:21 +02:00
|
|
|
}
|
|
|
|
}
|
2018-03-06 13:35:29 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-22 12:38:24 +01:00
|
|
|
// TODO find the right place to write it
|
|
|
|
composeEmailToInformManagers(communityName: string, communityId: string, managers: any): Email {
|
2018-11-23 22:30:47 +01:00
|
|
|
this.emailToInformManagers.subject = "[OpenAIRE-Connect] " + communityName + ": New subscriber notification";
|
2018-11-25 15:36:13 +01:00
|
|
|
this.emailToInformManagers.body = "<div style='font-size:14px;'>"
|
|
|
|
+"<p>There is a new subscriber for \"" + communityName + "\" community. Click "
|
2018-11-23 22:30:47 +01:00
|
|
|
+ "<a href='https://beta.admin.connect.openaire.eu/manage-subscribers?communityId="
|
|
|
|
+ communityId + "'>here</a> to manage the subscibers list. </p>"
|
2018-11-25 15:36:13 +01:00
|
|
|
+ "<p>OpenAIRE team<br> <a href='https://www.openaire.eu/'>www.openaire.eu</a></p><br>"
|
2018-11-23 22:30:47 +01:00
|
|
|
+ "<p style='font-size:11px;'>You are receiving this e-mail as manager of the community "
|
|
|
|
+ "<a href='https://beta." + communityId + ".openaire.eu/'>" + communityName + "</a>. "
|
|
|
|
+ "If you are not responsible for this community, please "
|
|
|
|
+ "<a href='mailto:openaire.test@gmail.com'>contact us</a>."
|
|
|
|
+ "<br>"
|
|
|
|
+ "Click <a href='https://beta.admin.connect.openaire.eu/manage-user-notifications?communityId=" + communityId
|
2018-11-25 15:36:13 +01:00
|
|
|
+ "'>here</a> to manage your notification settings. </p>"
|
|
|
|
+ "</div>";
|
2018-11-22 12:38:24 +01:00
|
|
|
|
|
|
|
this.emailToInformManagers.recipients = managers;
|
|
|
|
console.log(this.emailToInformManagers);
|
|
|
|
|
|
|
|
return this.emailToInformManagers;
|
|
|
|
}
|
|
|
|
|
2018-03-06 13:35:29 +01:00
|
|
|
unsubscribe(){
|
|
|
|
var email = Session.getUserEmail();
|
|
|
|
if(email == null){
|
|
|
|
this.subscribed = false;
|
|
|
|
}else{
|
2018-10-01 14:46:21 +02:00
|
|
|
this.loading = true;
|
2018-03-06 13:35:29 +01:00
|
|
|
//this.properties.adminToolsAPIURL
|
2018-03-13 17:06:04 +01:00
|
|
|
this._subscribeService.unSubscribeToCommunity(this.communityId, email,this.properties.adminToolsAPIURL).subscribe (
|
2018-03-06 13:35:29 +01:00
|
|
|
res => {
|
2018-10-01 14:46:21 +02:00
|
|
|
this.loading = false;
|
|
|
|
if(res.status && res.status != 200) {
|
|
|
|
UIkit.notification({
|
|
|
|
message : '<strong>There was an error in your unsubscription. Please try again!<strong>',
|
|
|
|
status : 'warning',
|
|
|
|
timeout : 3000,
|
|
|
|
pos : 'top-center'
|
|
|
|
});
|
|
|
|
} else {
|
2018-03-06 13:35:29 +01:00
|
|
|
console.log(res);
|
|
|
|
if(this.subscribed){
|
|
|
|
this.subscribed = false;
|
|
|
|
}
|
2018-10-01 14:46:21 +02:00
|
|
|
}
|
2018-03-06 13:35:29 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2018-03-13 17:06:04 +01:00
|
|
|
|
|
|
|
confirmOpen(){
|
|
|
|
|
|
|
|
this.alert.cancelButton = true;
|
|
|
|
this.alert.okButton = true;
|
|
|
|
this.alert.alertTitle = "Unsubscribe community ";
|
|
|
|
this.alert.message = "Do you want to proceed? ";
|
|
|
|
this.alert.okButtonText = "Yes";
|
|
|
|
this.alert.cancelButtonText = "No";
|
|
|
|
this.alert.open();
|
|
|
|
}
|
|
|
|
confirmClose(data){
|
|
|
|
this.unsubscribe();
|
|
|
|
}
|
2018-03-06 13:35:29 +01:00
|
|
|
}
|