Project

General

Profile

1
import {Component, EventEmitter, Input, Output, ViewChild} from '@angular/core';
2
import {ActivatedRoute, Router} from '@angular/router';
3
import {EnvProperties} from '../../openaireLibrary/utils/properties/env-properties';
4
import {AlertModal} from '../../openaireLibrary/utils/modal/alert';
5

    
6
import {CommunityService} from '../../openaireLibrary/connect/community/community.service';
7
import {SubscribeService} from '../../openaireLibrary/utils/subscribe/subscribe.service';
8
import {EmailService} from "../../openaireLibrary/utils/email/email.service";
9
import {Session} from '../../openaireLibrary/login/utils/helper.class';
10

    
11
import {Email} from "../../openaireLibrary/utils/email/email";
12
import {Composer} from "../../openaireLibrary/utils/email/composer";
13
import {LoginErrorCodes} from '../../openaireLibrary/login/utils/guardHelper.class';
14

    
15
declare var UIkit: any;
16

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

    
21
    <span *ngIf="subscribed != null && !showNumbers && showTemplate">
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
      <button *ngIf="!subscribed" [class]="'uk-button uk-button-primary' + (loading ? ' uk-disabled' : '')" (click)="subscribe()"> Subscribe</button>
27
      <button *ngIf="subscribed" [class]="'uk-button uk-button-primary' + (loading ? ' uk-disabled' : '')" (click)="confirmOpen()"> Unsubscribe</button>
28
    </span>
29

    
30
    <span *ngIf="showNumbers && subscribers !=null && subscribers > 0  && showTemplate" >
31
      <span class="lowOpacityColor">  Members</span> {{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
  @Input() showTemplate:boolean = true;
43
  @Output() subscribeEvent = new EventEmitter();
44

    
45
  public community = null;
46
  public emailToInformManagers: Email;
47

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

    
61
    public ngOnInit() {
62
      this.route.data
63
          .subscribe((data: { envSpecific: EnvProperties }) => {
64
             this.properties = data.envSpecific;
65
             if(!this.showNumbers){
66
               let email = Session.getUserEmail();
67
               if(email == null){
68
                 this.subscribed = false;
69
               }else{
70
                 if(this.communityId){
71
                   this._subscribeService.isSubscribedToCommunity(this.properties, this.communityId, email).subscribe (
72
                     res => {
73
                       this.subscribed = res;
74
                     },
75
                     error => {
76
                       this.handleError("Error getting response if email: "+email+" is subscribed to community with id: "+this.communityId, error);
77
                     });
78
                   }
79
               }
80
             }else{
81
               if(this.communityId){
82
                 this._subscribeService.getCommunitySubscribers(this.properties, this.communityId).subscribe (
83
                   res => {
84
                      this.subscribers = (res && res.subscribers && res.subscribers.length )?res.subscribers.length:0;
85
                   },
86
                   error => {
87
                     this.handleError("Error getting community subscribers for community with id: "+this.communityId, error);
88
                   });
89
                 }
90
             }
91
             if(this.communityId){
92
               this.emailToInformManagers = {body: "", subject: "", recipients: []};
93

    
94
               this._communityService.getCommunity(this.properties, this.properties.communityAPI+this.communityId).subscribe (
95
                 community => {
96
                          this.community = community;
97
                 },
98
                 error => {
99
                   //console.log('System error retrieving community profile', error)
100
                   this.handleError("Error getting community with id: "+this.communityId, error);
101
                 }
102
               );
103
             }
104
           });
105

    
106

    
107
    }
108

    
109
    subscribe(){
110
      var email = Session.getUserEmail();
111
      if(email == null){
112
        this.subscribed = false;
113
         // this.showLoginAlert = true;
114
         this.router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.ACTION_REQUIRES_LOGIN, "redirectUrl":  this.router.url } });
115
      }else{
116
        this.loading = true;
117
        this.showLoginAlert = false;
118
        this._subscribeService.subscribeToCommunity(this.communityId, email, this.properties.adminToolsAPIURL).subscribe (
119
          res => {
120
            this.loading = false;
121
             if(res.status && res.status != 200) {
122
               this.subscribeEvent.emit({
123
                 value: "error"
124
               });
125
               UIkit.notification({
126
                   message : '<strong>An error occured. Please try again!<strong>',
127
                   status  : 'warning',
128
                   timeout : 3000,
129
                   pos     : 'top-center'
130
               });
131
             } else {
132
               if(!this.subscribed){
133
                 this.subscribed = true;
134
                 this.subscribeEvent.emit({
135
                   value: "ok"
136
                 });
137
                 this._emailService.sendEmail(this.properties.adminToolsAPIURL + "/notifyForNewSubscribers/" + this.communityId, Composer.composeEmailToInformManagers(this.community.title, this.communityId, this.community.managers, email)).subscribe(
138
                   res => {
139
                     //console.log("The email has been sent successfully!")
140
                   },
141
                   error => {
142
                     //console.log(error)
143
                     this.handleError("Error notifying managers about new subscribers for community with id: "+this.communityId, error);
144
                   }
145
                 );
146
               }
147
            }
148
          },
149
          error => {
150
            this.loading = false;
151
            this.subscribeEvent.emit({
152
              value: "error"
153
            });
154
            UIkit.notification({
155
                message : '<strong>An error occured. Please try again!<strong>',
156
                status  : 'warning',
157
                timeout : 3000,
158
                pos     : 'top-center'
159
            });
160
            //console.log(error)
161
            this.handleError("Error subscribing email: "+email+" to community with id: "+this.communityId, error);
162
          });
163
      }
164
    }
165

    
166
    unsubscribe(){
167
      var email = Session.getUserEmail();
168
      if(email == null){
169
        this.subscribed = false;
170
      }else{
171
        this.loading = true;
172
        //this.properties.adminToolsAPIURL
173
        this._subscribeService.unSubscribeToCommunity(this.communityId, email,this.properties.adminToolsAPIURL).subscribe (
174
          res => {
175
            this.loading = false;
176
            if(res.status && res.status != 200) {
177
              UIkit.notification({
178
                  message : '<strong>An error occured. Please try again!<strong>',
179
                  status  : 'warning',
180
                  timeout : 3000,
181
                  pos     : 'top-center'
182
              });
183
            } else {
184
             //console.log(res);
185
             if(this.subscribed){
186
               this.subscribed = false;
187
             }
188
           }
189
          },
190
          error => {
191
            this.loading = false;
192
            UIkit.notification({
193
                message : '<strong>An error occured. Please try again!<strong>',
194
                status  : 'warning',
195
                timeout : 3000,
196
                pos     : 'top-center'
197
            });
198
            //console.log(error)
199
            this.handleError("Error unsubscribing email: "+email+" from community with id: "+this.communityId, error);
200
          });
201
      }
202
    }
203

    
204
    confirmOpen(){
205

    
206
        this.alert.cancelButton = true;
207
        this.alert.okButton = true;
208
        this.alert.alertTitle = "Unsubscribe community ";
209
        this.alert.message = "Do you want to proceed? ";
210
        this.alert.okButtonText = "Yes";
211
        this.alert.cancelButtonText = "No";
212
        this.alert.open();
213
      }
214
      confirmClose(data){
215
       this.unsubscribe();
216
     }
217

    
218
     private handleError(message: string, error) {
219
       console.error("Subscribe (component): "+message, error);
220
     }
221
}
(1-1/2)