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-12-13 12:50:07 +01:00
|
|
|
import {Composer} from "../../openaireLibrary/utils/email/composer";
|
2018-11-22 12:38:24 +01:00
|
|
|
|
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;
|
|
|
|
},
|
|
|
|
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;
|
|
|
|
if(res.status && res.status != 200) {
|
|
|
|
UIkit.notification({
|
2019-01-23 09:55:08 +01:00
|
|
|
message : '<strong>An error occured. Please try again!<strong>',
|
2018-10-01 14:46:21 +02:00
|
|
|
status : 'warning',
|
|
|
|
timeout : 3000,
|
|
|
|
pos : 'top-center'
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
if(!this.subscribed){
|
|
|
|
this.subscribed = true;
|
2019-02-14 10:56:11 +01:00
|
|
|
this._emailService.sendEmail(this.properties.adminToolsAPIURL + "/notifyForNewSubscribers/" + this.communityId, Composer.composeEmailToInformManagers(this.community.title, this.communityId, this.community.managers, email)).subscribe(
|
2018-11-22 12:38:24 +01:00
|
|
|
res => {
|
2019-02-14 12:11:12 +01:00
|
|
|
//console.log("The email has been sent successfully!")
|
2018-11-22 12:38:24 +01:00
|
|
|
},
|
2019-01-23 09:55:08 +01:00
|
|
|
error => {
|
|
|
|
|
|
|
|
console.log(error)
|
|
|
|
}
|
2018-11-22 12:38:24 +01:00
|
|
|
);
|
2018-10-01 14:46:21 +02:00
|
|
|
}
|
|
|
|
}
|
2019-01-23 09:55:08 +01:00
|
|
|
},
|
|
|
|
error => {
|
|
|
|
this.loading = false;
|
|
|
|
UIkit.notification({
|
|
|
|
message : '<strong>An error occured. Please try again!<strong>',
|
|
|
|
status : 'warning',
|
|
|
|
timeout : 3000,
|
|
|
|
pos : 'top-center'
|
|
|
|
});
|
|
|
|
console.log(error)
|
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({
|
2019-01-23 09:55:08 +01:00
|
|
|
message : '<strong>An error occured. Please try again!<strong>',
|
2018-10-01 14:46:21 +02:00
|
|
|
status : 'warning',
|
|
|
|
timeout : 3000,
|
|
|
|
pos : 'top-center'
|
|
|
|
});
|
|
|
|
} else {
|
2019-02-14 12:11:12 +01:00
|
|
|
//console.log(res);
|
2018-03-06 13:35:29 +01:00
|
|
|
if(this.subscribed){
|
|
|
|
this.subscribed = false;
|
|
|
|
}
|
2018-10-01 14:46:21 +02:00
|
|
|
}
|
2019-01-23 09:55:08 +01:00
|
|
|
},
|
|
|
|
error => {
|
|
|
|
this.loading = false;
|
|
|
|
UIkit.notification({
|
|
|
|
message : '<strong>An error occured. Please try again!<strong>',
|
|
|
|
status : 'warning',
|
|
|
|
timeout : 3000,
|
|
|
|
pos : 'top-center'
|
|
|
|
});
|
|
|
|
console.log(error)
|
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
|
|
|
}
|