2019-06-05 15:39:02 +02:00
|
|
|
import {Component, EventEmitter, Input, Output, ViewChild} from '@angular/core';
|
2019-04-15 16:08:37 +02:00
|
|
|
import {ActivatedRoute, Router} from '@angular/router';
|
2019-06-05 15:39:02 +02:00
|
|
|
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";
|
2019-09-13 09:47:29 +02:00
|
|
|
import {Session, User} 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";
|
2019-04-15 16:08:37 +02:00
|
|
|
import {LoginErrorCodes} from '../../openaireLibrary/login/utils/guardHelper.class';
|
2019-09-13 09:47:29 +02:00
|
|
|
import {UserManagementService} from "../../openaireLibrary/services/user-management.service";
|
2020-07-17 01:25:23 +02:00
|
|
|
import {Subscription} from "rxjs";
|
|
|
|
import {properties} from "../../../environments/environment";
|
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({
|
2019-09-13 09:47:29 +02:00
|
|
|
selector: 'subscribe',
|
|
|
|
template: `
|
2018-03-06 13:35:29 +01:00
|
|
|
|
2019-04-15 16:08:37 +02:00
|
|
|
<span *ngIf="subscribed != null && !showNumbers && showTemplate">
|
2019-09-13 09:47:29 +02:00
|
|
|
<div *ngIf="!subscribed && showLoginAlert" class="uk-alert-warning uk-animation-slide-bottom" uk-alert="">
|
2018-03-13 17:06:04 +01:00
|
|
|
<a class="uk-alert-close" uk-close></a>
|
|
|
|
<p>Please login first to subscribe</p>
|
|
|
|
</div>
|
2020-04-24 18:38:35 +02:00
|
|
|
<button *ngIf="!subscribed" [class]="'uk-button portal-button uk-button-small uk-width-1-1 ' + (loading ? ' uk-disabled' : '')"
|
2020-04-28 13:07:36 +02:00
|
|
|
(click)="subscribe()">
|
|
|
|
<span class="uk-icon uk-flex uk-flex-middle">
|
|
|
|
<svg height="24" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
|
|
|
|
<path d="M0 0h24v24H0z" fill="none"></path>
|
|
|
|
<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>
|
|
|
|
</svg>
|
|
|
|
<span class="space">Subscribe</span>
|
|
|
|
</span>
|
|
|
|
</button>
|
2020-04-24 18:38:35 +02:00
|
|
|
<button *ngIf="subscribed" [class]="'subscribed-button uk-button uk-button-small uk-width-1-1 ' + (loading ? ' uk-disabled' : '')"
|
2020-04-28 13:07:36 +02:00
|
|
|
(click)="confirmOpen()">
|
|
|
|
<span class="uk-icon uk-flex uk-flex-middle">
|
|
|
|
<svg height="24" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
|
|
|
|
<path d="M0 0h24v24H0z" fill="none"></path>
|
|
|
|
<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>
|
|
|
|
</svg>
|
|
|
|
<span class="space">Subscribed</span>
|
|
|
|
</span>
|
|
|
|
</button>
|
2018-04-03 14:38:28 +02:00
|
|
|
</span>
|
2018-03-13 17:06:04 +01:00
|
|
|
|
2020-04-28 13:07:36 +02:00
|
|
|
<span *ngIf="showNumbers && subscribers !=null && subscribers > 0 && showTemplate" class="uk-display-inline-block">
|
2020-04-24 18:38:35 +02:00
|
|
|
<span class="lowOpacityColor uk-text-muted">Members: </span> {{subscribers}}
|
2018-03-13 17:06:04 +01:00
|
|
|
</span>
|
|
|
|
<modal-alert (alertOutput)="confirmClose($event)">
|
|
|
|
</modal-alert>
|
2019-09-13 09:47:29 +02:00
|
|
|
`
|
2018-03-06 13:35:29 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
export class SubscribeComponent {
|
|
|
|
// @Input() showSubscribe:boolean = true;
|
2019-09-13 09:47:29 +02:00
|
|
|
@Input() showNumbers: boolean;
|
|
|
|
@Input() communityId: string;
|
|
|
|
@Input() showTemplate: boolean = true;
|
2019-04-15 16:08:37 +02:00
|
|
|
@Output() subscribeEvent = new EventEmitter();
|
2018-03-06 13:35:29 +01:00
|
|
|
|
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;
|
2019-09-13 09:47:29 +02:00
|
|
|
subscribed: boolean = null;
|
2020-04-24 18:38:35 +02:00
|
|
|
@Input() properties: EnvProperties;
|
2019-09-13 09:47:29 +02:00
|
|
|
subscribers: number = null;
|
2020-05-06 18:08:49 +02:00
|
|
|
@Output() countSubscribersEvent = new EventEmitter();
|
2019-09-13 09:47:29 +02:00
|
|
|
showLoginAlert: Boolean = false;
|
2018-03-13 17:06:04 +01:00
|
|
|
@ViewChild(AlertModal) alert;
|
2019-09-13 09:47:29 +02:00
|
|
|
private user: User;
|
|
|
|
|
2020-07-17 01:25:23 +02:00
|
|
|
subs: Subscription[] = [];
|
|
|
|
|
2019-09-13 09:47:29 +02:00
|
|
|
constructor(private route: ActivatedRoute,
|
|
|
|
private _subscribeService: SubscribeService,
|
|
|
|
private _emailService: EmailService,
|
|
|
|
private _communityService: CommunityService,
|
|
|
|
private router: Router,
|
|
|
|
private userManagementService: UserManagementService
|
2020-05-05 10:52:11 +02:00
|
|
|
) {}
|
2019-09-13 09:47:29 +02:00
|
|
|
|
|
|
|
public ngOnInit() {
|
2020-07-17 01:25:23 +02:00
|
|
|
if(!this.properties) {
|
|
|
|
this.properties = properties;
|
|
|
|
}
|
|
|
|
if(!this.showNumbers) {
|
|
|
|
this.subs.push(this.userManagementService.getUserInfo().subscribe(
|
|
|
|
user => {
|
|
|
|
this.user = user;
|
|
|
|
this.init();
|
|
|
|
},
|
|
|
|
error => {},
|
|
|
|
() => {
|
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
this.init();
|
|
|
|
}
|
2020-04-10 07:24:24 +02:00
|
|
|
//this.init();
|
2019-11-08 12:04:10 +01:00
|
|
|
}
|
2019-09-13 09:47:29 +02:00
|
|
|
|
2020-07-17 01:25:23 +02:00
|
|
|
public ngOnDestroy() {
|
|
|
|
for (let sub of this.subs) {
|
|
|
|
sub.unsubscribe();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-28 13:07:36 +02:00
|
|
|
private isSubscribed() {
|
2020-07-17 01:25:23 +02:00
|
|
|
this.subs.push(this._subscribeService.isSubscribed.subscribe(
|
2020-04-28 13:07:36 +02:00
|
|
|
res => {
|
|
|
|
this.subscribed = res;
|
|
|
|
if (this.subscribed) {
|
|
|
|
this.subscribeEvent.emit({
|
|
|
|
value: "ok"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2020-07-17 01:25:23 +02:00
|
|
|
));
|
2020-04-28 13:07:36 +02:00
|
|
|
}
|
|
|
|
|
2019-11-08 12:04:10 +01:00
|
|
|
private init() {
|
|
|
|
if (!this.showNumbers) {
|
2019-11-08 13:08:15 +01:00
|
|
|
let email = (this.user) ? this.user.email : null;
|
2019-11-08 12:04:10 +01:00
|
|
|
if (email == null) {
|
|
|
|
this.subscribed = false;
|
|
|
|
} else {
|
|
|
|
if (this.communityId) {
|
2020-04-28 13:07:36 +02:00
|
|
|
// this._subscribeService.isSubscribedToCommunity(this.properties, this.communityId).subscribe(
|
|
|
|
// res => {
|
|
|
|
// this.subscribed = res;
|
|
|
|
// if (this.subscribed) {
|
|
|
|
// this.subscribeEvent.emit({
|
|
|
|
// value: "ok"
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
// },
|
|
|
|
// error => {
|
|
|
|
// this.handleError("Error getting response if email: " + email + " is subscribed to community with id: " + this.communityId, error);
|
|
|
|
// });
|
|
|
|
|
|
|
|
this._subscribeService.initIsSubscribedToCommunity(this.properties, this.communityId);
|
|
|
|
this.isSubscribed();
|
2019-11-08 12:04:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (this.communityId) {
|
2020-07-17 01:25:23 +02:00
|
|
|
this.subs.push(this._subscribeService.getNumberOfCommunitySubscribers(this.properties, this.communityId).subscribe(
|
2019-11-08 12:04:10 +01:00
|
|
|
res => {
|
2020-04-14 11:35:25 +02:00
|
|
|
this.subscribers = (res && res.value) ? res.value : 0;//(res && res.subscribers && res.subscribers.length) ? res.subscribers.length : 0;
|
2020-05-06 18:08:49 +02:00
|
|
|
this.countSubscribersEvent.emit({
|
|
|
|
value: this.subscribers
|
|
|
|
});
|
2019-11-08 12:04:10 +01:00
|
|
|
},
|
|
|
|
error => {
|
|
|
|
this.handleError("Error getting community subscribers for community with id: " + this.communityId, error);
|
2020-07-17 01:25:23 +02:00
|
|
|
}));
|
2019-11-08 12:04:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this.communityId) {
|
|
|
|
this.emailToInformManagers = {body: "", subject: "", recipients: []};
|
|
|
|
|
2020-07-17 01:25:23 +02:00
|
|
|
this.subs.push(this._communityService.getCommunityByState(this.properties, this.properties.communityAPI + this.communityId).subscribe(
|
2019-11-08 12:04:10 +01:00
|
|
|
community => {
|
|
|
|
this.community = community;
|
|
|
|
},
|
|
|
|
error => {
|
|
|
|
//console.log('System error retrieving community profile', error)
|
|
|
|
this.handleError("Error getting community with id: " + this.communityId, error);
|
|
|
|
}
|
2020-07-17 01:25:23 +02:00
|
|
|
));
|
2019-11-08 12:04:10 +01:00
|
|
|
}
|
2019-09-13 09:47:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
subscribe() {
|
2019-11-08 13:08:15 +01:00
|
|
|
var email = (this.user) ? this.user.email : null;
|
2019-09-13 09:47:29 +02:00
|
|
|
if (email == null) {
|
|
|
|
this.subscribed = false;
|
|
|
|
// this.showLoginAlert = true;
|
|
|
|
this.router.navigate(['/user-info'], {
|
|
|
|
queryParams: {
|
|
|
|
"errorCode": LoginErrorCodes.ACTION_REQUIRES_LOGIN,
|
|
|
|
"redirectUrl": this.router.url
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.loading = true;
|
|
|
|
this.showLoginAlert = false;
|
2020-07-17 01:25:23 +02:00
|
|
|
this.subs.push(this._subscribeService.subscribeToCommunity(this.properties, this.communityId).subscribe(
|
2019-09-13 09:47:29 +02:00
|
|
|
res => {
|
|
|
|
this.loading = false;
|
|
|
|
if (res.status && res.status != 200) {
|
2019-04-15 16:08:37 +02:00
|
|
|
this.subscribeEvent.emit({
|
|
|
|
value: "error"
|
|
|
|
});
|
2019-01-23 09:55:08 +01:00
|
|
|
UIkit.notification({
|
2019-09-13 09:47:29 +02:00
|
|
|
message: '<strong>An error occured. Please try again!<strong>',
|
|
|
|
status: 'warning',
|
|
|
|
timeout: 3000,
|
|
|
|
pos: 'top-center'
|
2019-01-23 09:55:08 +01:00
|
|
|
});
|
2019-09-13 09:47:29 +02:00
|
|
|
} else {
|
|
|
|
if (!this.subscribed) {
|
|
|
|
this.subscribed = true;
|
|
|
|
this.subscribeEvent.emit({
|
|
|
|
value: "ok"
|
|
|
|
});
|
2020-07-17 01:25:23 +02:00
|
|
|
this.subs.push(this._emailService.notifyForNewManagers(this.properties, this.communityId, Composer.composeEmailToInformManagers(this.community.title, this.communityId, this.community.managers,
|
2020-05-08 17:04:39 +02:00
|
|
|
email, this.properties.adminPortalURL)).subscribe(
|
2019-09-13 09:47:29 +02:00
|
|
|
res => {
|
|
|
|
//console.log("The email has been sent successfully!")
|
|
|
|
},
|
|
|
|
error => {
|
|
|
|
//console.log(error)
|
|
|
|
this.handleError("Error notifying managers about new subscribers for community with id: " + this.communityId, error);
|
|
|
|
}
|
2020-07-17 01:25:23 +02:00
|
|
|
));
|
2019-09-13 09:47:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
error => {
|
|
|
|
this.loading = false;
|
|
|
|
this.subscribeEvent.emit({
|
|
|
|
value: "error"
|
2018-03-06 13:35:29 +01:00
|
|
|
});
|
2019-09-13 09:47:29 +02:00
|
|
|
UIkit.notification({
|
|
|
|
message: '<strong>An error occured. Please try again!<strong>',
|
|
|
|
status: 'warning',
|
|
|
|
timeout: 3000,
|
|
|
|
pos: 'top-center'
|
|
|
|
});
|
|
|
|
//console.log(error)
|
|
|
|
this.handleError("Error subscribing email: " + email + " to community with id: " + this.communityId, error);
|
2020-07-17 01:25:23 +02:00
|
|
|
}));
|
2018-03-06 13:35:29 +01:00
|
|
|
}
|
2019-09-13 09:47:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
unsubscribe() {
|
2019-11-08 13:08:15 +01:00
|
|
|
var email = (this.user) ? this.user.email : null;
|
2019-09-13 09:47:29 +02:00
|
|
|
if (email == null) {
|
|
|
|
this.subscribed = false;
|
|
|
|
} else {
|
|
|
|
this.loading = true;
|
|
|
|
//this.properties.adminToolsAPIURL
|
2020-07-17 01:25:23 +02:00
|
|
|
this.subs.push(this._subscribeService.unSubscribeToCommunity(this.properties, this.communityId).subscribe(
|
2019-09-13 09:47:29 +02:00
|
|
|
res => {
|
|
|
|
this.loading = false;
|
|
|
|
if (res.status && res.status != 200) {
|
2019-01-23 09:55:08 +01:00
|
|
|
UIkit.notification({
|
2019-09-13 09:47:29 +02:00
|
|
|
message: '<strong>An error occured. Please try again!<strong>',
|
|
|
|
status: 'warning',
|
|
|
|
timeout: 3000,
|
|
|
|
pos: 'top-center'
|
2019-01-23 09:55:08 +01:00
|
|
|
});
|
2019-09-13 09:47:29 +02:00
|
|
|
} else {
|
|
|
|
//console.log(res);
|
|
|
|
if (this.subscribed) {
|
|
|
|
this.subscribed = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
error => {
|
|
|
|
this.loading = false;
|
|
|
|
UIkit.notification({
|
|
|
|
message: '<strong>An error occured. Please try again!<strong>',
|
|
|
|
status: 'warning',
|
|
|
|
timeout: 3000,
|
|
|
|
pos: 'top-center'
|
2018-03-06 13:35:29 +01:00
|
|
|
});
|
2019-09-13 09:47:29 +02:00
|
|
|
//console.log(error)
|
|
|
|
this.handleError("Error unsubscribing email: " + email + " from community with id: " + this.communityId, error);
|
2020-07-17 01:25:23 +02:00
|
|
|
}));
|
2018-03-06 13:35:29 +01:00
|
|
|
}
|
2019-09-13 09:47:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
confirmOpen() {
|
|
|
|
|
|
|
|
this.alert.cancelButton = true;
|
|
|
|
this.alert.okButton = true;
|
2020-04-24 18:38:35 +02:00
|
|
|
//this.alert.alertTitle = "Unsubscribe community ";
|
|
|
|
//this.alert.message = "Do you want to proceed? ";
|
|
|
|
this.alert.message = "You are subscribed to the Community Gateway. Do you want to unsubscribe?";
|
|
|
|
this.alert.okButtonText = "UNSUBSCRIBE";
|
|
|
|
this.alert.cancelButtonText = "CANCEL";
|
2019-09-13 09:47:29 +02:00
|
|
|
this.alert.open();
|
|
|
|
}
|
|
|
|
|
|
|
|
confirmClose(data) {
|
|
|
|
this.unsubscribe();
|
|
|
|
}
|
|
|
|
|
|
|
|
private handleError(message: string, error) {
|
|
|
|
console.error("Subscribe (component): " + message, error);
|
|
|
|
}
|
2018-03-06 13:35:29 +01:00
|
|
|
}
|