1
|
import { Component, Input, ViewChild } from '@angular/core';
|
2
|
import { Location } from '@angular/common';
|
3
|
import {ActivatedRoute} from '@angular/router';
|
4
|
import { EnvProperties} from '../../openaireLibrary/utils/properties/env-properties';
|
5
|
import {AlertModal} from '../../openaireLibrary/utils/modal/alert';
|
6
|
|
7
|
import {SubscribeService} from '../../openaireLibrary/utils/subscribe/subscribe.service';
|
8
|
import {Session} from '../../openaireLibrary/login/utils/helper.class';
|
9
|
@Component({
|
10
|
selector: 'subscribe',
|
11
|
template: `
|
12
|
|
13
|
<span *ngIf="subscribed != null && !showNumbers">
|
14
|
<div *ngIf="!subscribed && showLoginAlert" class="uk-alert-warning uk-animation-slide-bottom" uk-alert="" >
|
15
|
<a class="uk-alert-close" uk-close></a>
|
16
|
<p>Please login first to subscribe</p>
|
17
|
</div>
|
18
|
<a *ngIf="!subscribed" class="uk-button uk-button-primary" (click)="subscribe()"> Subscribe</a>
|
19
|
<a *ngIf="subscribed" class="uk-button uk-button-danger" (click)="confirmOpen()"> Unsubscribe</a>
|
20
|
</span>
|
21
|
|
22
|
<span *ngIf="showNumbers && subscribers !=null && subscribers > 0" >
|
23
|
Members: {{subscribers}}
|
24
|
</span>
|
25
|
<modal-alert (alertOutput)="confirmClose($event)">
|
26
|
</modal-alert>
|
27
|
`
|
28
|
})
|
29
|
|
30
|
export class SubscribeComponent {
|
31
|
// @Input() showSubscribe:boolean = true;
|
32
|
@Input() showNumbers:boolean;
|
33
|
@Input() communityId:string;
|
34
|
|
35
|
|
36
|
subscribed:boolean = null;
|
37
|
properties:EnvProperties;
|
38
|
subscribers:number= null;
|
39
|
showLoginAlert:Boolean = false;
|
40
|
@ViewChild(AlertModal) alert;
|
41
|
constructor (private route: ActivatedRoute,
|
42
|
private _subscribeService: SubscribeService
|
43
|
) {
|
44
|
}
|
45
|
|
46
|
public ngOnInit() {
|
47
|
this.route.data
|
48
|
.subscribe((data: { envSpecific: EnvProperties }) => {
|
49
|
this.properties = data.envSpecific;
|
50
|
if(!this.showNumbers){
|
51
|
var email = Session.getUserEmail();
|
52
|
if(email == null){
|
53
|
this.subscribed = false;
|
54
|
}else{
|
55
|
this._subscribeService.isSubscribedToCommunity(this.communityId, email,this.properties.adminToolsAPIURL).subscribe (
|
56
|
res => {
|
57
|
this.subscribed = res;
|
58
|
});
|
59
|
}
|
60
|
}else{
|
61
|
this._subscribeService.getCommunitySubscribers(this.communityId, this.properties.adminToolsAPIURL).subscribe (
|
62
|
res => {
|
63
|
|
64
|
this.subscribers = (res && res.subscribers && res.subscribers.length )?res.subscribers.length:0;
|
65
|
|
66
|
});
|
67
|
}
|
68
|
});
|
69
|
|
70
|
|
71
|
}
|
72
|
|
73
|
subscribe(){
|
74
|
var email = Session.getUserEmail();
|
75
|
if(email == null){
|
76
|
this.subscribed = false;
|
77
|
this.showLoginAlert = true;
|
78
|
}else{
|
79
|
this.showLoginAlert = false;
|
80
|
this._subscribeService.subscribeToCommunity(this.communityId, email, this.properties.adminToolsAPIURL).subscribe (
|
81
|
res => {
|
82
|
console.log(res);
|
83
|
if(!this.subscribed){
|
84
|
this.subscribed = true;
|
85
|
}
|
86
|
});
|
87
|
}
|
88
|
}
|
89
|
|
90
|
unsubscribe(){
|
91
|
var email = Session.getUserEmail();
|
92
|
if(email == null){
|
93
|
this.subscribed = false;
|
94
|
}else{
|
95
|
//this.properties.adminToolsAPIURL
|
96
|
this._subscribeService.unSubscribeToCommunity(this.communityId, email,this.properties.adminToolsAPIURL).subscribe (
|
97
|
res => {
|
98
|
console.log(res);
|
99
|
if(this.subscribed){
|
100
|
this.subscribed = false;
|
101
|
}
|
102
|
});
|
103
|
}
|
104
|
}
|
105
|
|
106
|
confirmOpen(){
|
107
|
|
108
|
this.alert.cancelButton = true;
|
109
|
this.alert.okButton = true;
|
110
|
this.alert.alertTitle = "Unsubscribe community ";
|
111
|
this.alert.message = "Do you want to proceed? ";
|
112
|
this.alert.okButtonText = "Yes";
|
113
|
this.alert.cancelButtonText = "No";
|
114
|
this.alert.open();
|
115
|
}
|
116
|
confirmClose(data){
|
117
|
this.unsubscribe();
|
118
|
}
|
119
|
}
|