connect/src/app/utils/subscribe/subscribe.component.ts

192 lines
7.8 KiB
TypeScript
Raw Normal View History

import { Component, Input, ViewChild } from '@angular/core';
import { Location } from '@angular/common';
import {ActivatedRoute} from '@angular/router';
import { EnvProperties} from '../../openaireLibrary/utils/properties/env-properties';
import {AlertModal} from '../../openaireLibrary/utils/modal/alert';
import {CommunityService} from '../../openaireLibrary/connect/community/community.service';
import {SubscribeService} from '../../openaireLibrary/utils/subscribe/subscribe.service';
import {EmailService} from "../../openaireLibrary/utils/email/email.service";
import {Session} from '../../openaireLibrary/login/utils/helper.class';
import {Email} from "../../openaireLibrary/utils/email/email";
declare var UIkit: any;
@Component({
selector: 'subscribe',
template: `
<span *ngIf="subscribed != null && !showNumbers">
<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>
<a *ngIf="!subscribed" [class]="'uk-button portal-button' + (loading ? ' uk-disabled' : '')" (click)="subscribe()"> Subscribe</a>
<a *ngIf="subscribed" [class]="'uk-button uk-button-danger' + (loading ? ' uk-disabled' : '')" (click)="confirmOpen()"> Unsubscribe</a>
</span>
<span *ngIf="showNumbers && subscribers !=null && subscribers > 0" >
Members: {{subscribers}}
</span>
<modal-alert (alertOutput)="confirmClose($event)">
</modal-alert>
`
})
export class SubscribeComponent {
// @Input() showSubscribe:boolean = true;
@Input() showNumbers:boolean;
@Input() communityId:string;
public community = null;
public emailToInformManagers: Email;
loading: boolean = false;
subscribed:boolean = null;
properties:EnvProperties;
subscribers:number= null;
showLoginAlert:Boolean = false;
@ViewChild(AlertModal) alert;
constructor (private route: ActivatedRoute,
private _subscribeService: SubscribeService,
private _emailService: EmailService,
private _communityService: CommunityService
) {
}
public ngOnInit() {
this.route.data
.subscribe((data: { envSpecific: EnvProperties }) => {
this.properties = data.envSpecific;
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;
});
}
}else{
this._subscribeService.getCommunitySubscribers(this.communityId, this.properties.adminToolsAPIURL).subscribe (
res => {
this.subscribers = (res && res.subscribers && res.subscribers.length )?res.subscribers.length:0;
});
}
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)
);
});
}
subscribe(){
var email = Session.getUserEmail();
if(email == null){
this.subscribed = false;
this.showLoginAlert = true;
}else{
this.loading = true;
this.showLoginAlert = false;
this._subscribeService.subscribeToCommunity(this.communityId, email, this.properties.adminToolsAPIURL).subscribe (
res => {
this.loading = false;
console.log(res);
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;
this._emailService.sendEmail(this.properties.adminToolsAPIURL + "/notifyForNewSubscribers/" + this.communityId, this.composeEmailToInformManagers(this.community.title, this.communityId, this.community.managers)).subscribe(
res => {
console.log("The email has been sent successfully!")
},
error => console.log(error)
);
}
}
});
}
}
// TODO find the right place to write it
composeEmailToInformManagers(communityName: string, communityId: string, managers: any): Email {
this.emailToInformManagers.subject = "[OpenAIRE-Connect] " + communityName + ": New subscriber notification";
this.emailToInformManagers.body = "<div style='font-size:14px;'>"
+"<p>There is a new subscriber for \"" + communityName + "\" community. Click "
+ "<a href='https://beta.admin.connect.openaire.eu/manage-subscribers?communityId="
+ communityId + "'>here</a> to manage the subscibers list. </p>"
+ "<p>OpenAIRE team<br> <a href='https://www.openaire.eu/'>www.openaire.eu</a></p><br>"
+ "<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
+ "'>here</a> to manage your notification settings. </p>"
+ "</div>";
this.emailToInformManagers.recipients = managers;
console.log(this.emailToInformManagers);
return this.emailToInformManagers;
}
unsubscribe(){
var email = Session.getUserEmail();
if(email == null){
this.subscribed = false;
}else{
this.loading = true;
//this.properties.adminToolsAPIURL
this._subscribeService.unSubscribeToCommunity(this.communityId, email,this.properties.adminToolsAPIURL).subscribe (
res => {
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 {
console.log(res);
if(this.subscribed){
this.subscribed = false;
}
}
});
}
}
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();
}
}