Project

General

Profile

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 {CommunityService} from '../../openaireLibrary/connect/community/community.service';
8
import {SubscribeService} from '../../openaireLibrary/utils/subscribe/subscribe.service';
9
import {EmailService} from "../../openaireLibrary/utils/email/email.service";
10
import {Session} from '../../openaireLibrary/login/utils/helper.class';
11

    
12
import {Email} from "../../openaireLibrary/utils/email/email";
13
import {Composer} from "../../openaireLibrary/utils/email/composer";
14

    
15
declare var UIkit: any;
16

    
17
@Component({
18
    selector: 'subscribe',
19
    template: `
20

    
21
    <span *ngIf="subscribed != null && !showNumbers">
22
      <div *ngIf="!subscribed && showLoginAlert" class="uk-alert-warning uk-animation-slide-bottom" uk-alert=""  >
23
      <a class="uk-alert-close" uk-close></a>
24
      <p>Please login first to subscribe</p>
25
      </div>
26
      <a *ngIf="!subscribed" [class]="'uk-button portal-button' + (loading ? ' uk-disabled' : '')" (click)="subscribe()"> Subscribe</a>
27
      <a *ngIf="subscribed" [class]="'uk-button uk-button-danger' + (loading ? ' uk-disabled' : '')" (click)="confirmOpen()"> Unsubscribe</a>
28
    </span>
29

    
30
    <span *ngIf="showNumbers && subscribers !=null && subscribers > 0" >
31
        Members: {{subscribers}}
32
    </span>
33
    <modal-alert (alertOutput)="confirmClose($event)">
34
    </modal-alert>
35
    `
36
})
37

    
38
export class SubscribeComponent {
39
  // @Input() showSubscribe:boolean = true;
40
  @Input() showNumbers:boolean;
41
  @Input() communityId:string;
42

    
43
  public community = null;
44
  public emailToInformManagers: Email;
45

    
46
  loading: boolean = false;
47
  subscribed:boolean = null;
48
  properties:EnvProperties;
49
  subscribers:number= null;
50
  showLoginAlert:Boolean = false;
51
  @ViewChild(AlertModal) alert;
52
  constructor (private route: ActivatedRoute,
53
      private _subscribeService: SubscribeService,
54
      private _emailService: EmailService,
55
      private _communityService: CommunityService
56
    ) {
57
    }
58

    
59
    public ngOnInit() {
60
      this.route.data
61
          .subscribe((data: { envSpecific: EnvProperties }) => {
62
             this.properties = data.envSpecific;
63
             if(!this.showNumbers){
64
               var email = Session.getUserEmail();
65
               if(email == null){
66
                 this.subscribed = false;
67
               }else{
68
                 this._subscribeService.isSubscribedToCommunity(this.communityId, email,this.properties.adminToolsAPIURL).subscribe (
69
                   res => {
70
                     this.subscribed = res;
71
                   });
72
               }
73
             }else{
74
               this._subscribeService.getCommunitySubscribers(this.communityId, this.properties.adminToolsAPIURL).subscribe (
75
                 res => {
76

    
77
                    this.subscribers = (res && res.subscribers && res.subscribers.length )?res.subscribers.length:0;
78
                 });
79
             }
80

    
81
             this.emailToInformManagers = {body: "", subject: "", recipients: []};
82

    
83
             this._communityService.getCommunity(this.properties, this.properties.communityAPI+this.communityId).subscribe (
84
               community => {
85
                        this.community = community;
86
                        console.log(this.community);
87
               },
88
               error => console.log('System error retrieving community profile', error)
89
             );
90
           });
91

    
92

    
93
    }
94

    
95
    subscribe(){
96
      var email = Session.getUserEmail();
97
      if(email == null){
98
        this.subscribed = false;
99
         this.showLoginAlert = true;
100
      }else{
101
        this.loading = true;
102
        this.showLoginAlert = false;
103
        this._subscribeService.subscribeToCommunity(this.communityId, email, this.properties.adminToolsAPIURL).subscribe (
104
          res => {
105
            this.loading = false;
106
             console.log(res);
107
             if(res.status && res.status != 200) {
108
               UIkit.notification({
109
                   message : '<strong>There was an error in your subscription. Please try again!<strong>',
110
                   status  : 'warning',
111
                   timeout : 3000,
112
                   pos     : 'top-center'
113
               });
114
             } else {
115
               if(!this.subscribed){
116
                 this.subscribed = true;
117
                 this._emailService.sendEmail(this.properties.adminToolsAPIURL + "/notifyForNewSubscribers/" + this.communityId, Composer.composeEmailToInformManagers(this.community.title, this.communityId, this.community.managers)).subscribe(
118
                   res => {
119
                     console.log("The email has been sent successfully!")
120
                   },
121
                   error => console.log(error)
122
                 );
123
               }
124
            }
125
          });
126
      }
127
    }
128

    
129
    unsubscribe(){
130
      var email = Session.getUserEmail();
131
      if(email == null){
132
        this.subscribed = false;
133
      }else{
134
        this.loading = true;
135
        //this.properties.adminToolsAPIURL
136
        this._subscribeService.unSubscribeToCommunity(this.communityId, email,this.properties.adminToolsAPIURL).subscribe (
137
          res => {
138
            this.loading = false;
139
            if(res.status && res.status != 200) {
140
              UIkit.notification({
141
                  message : '<strong>There was an error in your unsubscription. Please try again!<strong>',
142
                  status  : 'warning',
143
                  timeout : 3000,
144
                  pos     : 'top-center'
145
              });
146
            } else {
147
             console.log(res);
148
             if(this.subscribed){
149
               this.subscribed = false;
150
             }
151
           }
152
          });
153
      }
154
    }
155

    
156
    confirmOpen(){
157

    
158
        this.alert.cancelButton = true;
159
        this.alert.okButton = true;
160
        this.alert.alertTitle = "Unsubscribe community ";
161
        this.alert.message = "Do you want to proceed? ";
162
        this.alert.okButtonText = "Yes";
163
        this.alert.cancelButtonText = "No";
164
        this.alert.open();
165
      }
166
      confirmClose(data){
167
       this.unsubscribe();
168
     }
169
}
(1-1/2)