Project

General

Profile

1 56052 argiro.kok
import {Component, EventEmitter, Input, Output, ViewChild} from '@angular/core';
2 55571 argiro.kok
import {ActivatedRoute, Router} from '@angular/router';
3 56052 argiro.kok
import {EnvProperties} from '../../openaireLibrary/utils/properties/env-properties';
4 51199 argiro.kok
import {AlertModal} from '../../openaireLibrary/utils/modal/alert';
5 51050 argiro.kok
6 53922 sofia.balt
import {CommunityService} from '../../openaireLibrary/connect/community/community.service';
7 52701 sofia.balt
import {SubscribeService} from '../../openaireLibrary/utils/subscribe/subscribe.service';
8 53922 sofia.balt
import {EmailService} from "../../openaireLibrary/utils/email/email.service";
9 51050 argiro.kok
import {Session} from '../../openaireLibrary/login/utils/helper.class';
10 53349 konstantin
11 53922 sofia.balt
import {Email} from "../../openaireLibrary/utils/email/email";
12 54353 sofia.balt
import {Composer} from "../../openaireLibrary/utils/email/composer";
13 55571 argiro.kok
import {LoginErrorCodes} from '../../openaireLibrary/login/utils/guardHelper.class';
14 53922 sofia.balt
15 53349 konstantin
declare var UIkit: any;
16
17 51050 argiro.kok
@Component({
18
    selector: 'subscribe',
19
    template: `
20
21 55571 argiro.kok
    <span *ngIf="subscribed != null && !showNumbers && showTemplate">
22 51199 argiro.kok
      <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 56052 argiro.kok
      <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 51642 argiro.kok
    </span>
29 51199 argiro.kok
30 55571 argiro.kok
    <span *ngIf="showNumbers && subscribers !=null && subscribers > 0  && showTemplate" >
31 56052 argiro.kok
      <span class="lowOpacityColor">  Members</span> {{subscribers}}
32 51199 argiro.kok
    </span>
33
    <modal-alert (alertOutput)="confirmClose($event)">
34
    </modal-alert>
35 51050 argiro.kok
    `
36
})
37
38
export class SubscribeComponent {
39
  // @Input() showSubscribe:boolean = true;
40
  @Input() showNumbers:boolean;
41
  @Input() communityId:string;
42 55571 argiro.kok
  @Input() showTemplate:boolean = true;
43
  @Output() subscribeEvent = new EventEmitter();
44 51050 argiro.kok
45 53922 sofia.balt
  public community = null;
46
  public emailToInformManagers: Email;
47
48 53349 konstantin
  loading: boolean = false;
49 51050 argiro.kok
  subscribed:boolean = null;
50
  properties:EnvProperties;
51 51199 argiro.kok
  subscribers:number= null;
52
  showLoginAlert:Boolean = false;
53
  @ViewChild(AlertModal) alert;
54 51050 argiro.kok
  constructor (private route: ActivatedRoute,
55 53922 sofia.balt
      private _subscribeService: SubscribeService,
56
      private _emailService: EmailService,
57 55571 argiro.kok
      private _communityService: CommunityService, private router: Router
58 51050 argiro.kok
    ) {
59
    }
60
61
    public ngOnInit() {
62
      this.route.data
63
          .subscribe((data: { envSpecific: EnvProperties }) => {
64
             this.properties = data.envSpecific;
65 51199 argiro.kok
             if(!this.showNumbers){
66 56052 argiro.kok
               let email = Session.getUserEmail();
67 51199 argiro.kok
               if(email == null){
68
                 this.subscribed = false;
69
               }else{
70 55571 argiro.kok
                 if(this.communityId){
71 56052 argiro.kok
                   this._subscribeService.isSubscribedToCommunity(this.properties, this.communityId, email).subscribe (
72 55571 argiro.kok
                     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 56052 argiro.kok
                 this._subscribeService.getCommunitySubscribers(this.properties, this.communityId).subscribe (
83 51199 argiro.kok
                   res => {
84 55571 argiro.kok
                      this.subscribers = (res && res.subscribers && res.subscribers.length )?res.subscribers.length:0;
85 54863 konstantin
                   },
86
                   error => {
87 55571 argiro.kok
                     this.handleError("Error getting community subscribers for community with id: "+this.communityId, error);
88 51199 argiro.kok
                   });
89 55571 argiro.kok
                 }
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 54863 konstantin
                 },
98
                 error => {
99 55571 argiro.kok
                   //console.log('System error retrieving community profile', error)
100
                   this.handleError("Error getting community with id: "+this.communityId, error);
101
                 }
102
               );
103 51050 argiro.kok
             }
104
           });
105
106 51199 argiro.kok
107 51050 argiro.kok
    }
108
109
    subscribe(){
110
      var email = Session.getUserEmail();
111
      if(email == null){
112
        this.subscribed = false;
113 55571 argiro.kok
         // this.showLoginAlert = true;
114
         this.router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.ACTION_REQUIRES_LOGIN, "redirectUrl":  this.router.url } });
115 51050 argiro.kok
      }else{
116 53349 konstantin
        this.loading = true;
117 51199 argiro.kok
        this.showLoginAlert = false;
118 51050 argiro.kok
        this._subscribeService.subscribeToCommunity(this.communityId, email, this.properties.adminToolsAPIURL).subscribe (
119
          res => {
120 53349 konstantin
            this.loading = false;
121
             if(res.status && res.status != 200) {
122 55571 argiro.kok
               this.subscribeEvent.emit({
123
                 value: "error"
124
               });
125 53349 konstantin
               UIkit.notification({
126 54622 argiro.kok
                   message : '<strong>An error occured. Please try again!<strong>',
127 53349 konstantin
                   status  : 'warning',
128
                   timeout : 3000,
129
                   pos     : 'top-center'
130
               });
131
             } else {
132
               if(!this.subscribed){
133
                 this.subscribed = true;
134 55571 argiro.kok
                 this.subscribeEvent.emit({
135
                   value: "ok"
136
                 });
137 54773 argiro.kok
                 this._emailService.sendEmail(this.properties.adminToolsAPIURL + "/notifyForNewSubscribers/" + this.communityId, Composer.composeEmailToInformManagers(this.community.title, this.communityId, this.community.managers, email)).subscribe(
138 53922 sofia.balt
                   res => {
139 54781 konstantin
                     //console.log("The email has been sent successfully!")
140 53922 sofia.balt
                   },
141 54622 argiro.kok
                   error => {
142 54863 konstantin
                     //console.log(error)
143
                     this.handleError("Error notifying managers about new subscribers for community with id: "+this.communityId, error);
144 54622 argiro.kok
                   }
145 53922 sofia.balt
                 );
146 53349 konstantin
               }
147
            }
148 54622 argiro.kok
          },
149
          error => {
150
            this.loading = false;
151 55571 argiro.kok
            this.subscribeEvent.emit({
152
              value: "error"
153
            });
154 54622 argiro.kok
            UIkit.notification({
155
                message : '<strong>An error occured. Please try again!<strong>',
156
                status  : 'warning',
157
                timeout : 3000,
158
                pos     : 'top-center'
159
            });
160 54863 konstantin
            //console.log(error)
161
            this.handleError("Error subscribing email: "+email+" to community with id: "+this.communityId, error);
162 51050 argiro.kok
          });
163
      }
164
    }
165
166
    unsubscribe(){
167
      var email = Session.getUserEmail();
168
      if(email == null){
169
        this.subscribed = false;
170
      }else{
171 53349 konstantin
        this.loading = true;
172 51050 argiro.kok
        //this.properties.adminToolsAPIURL
173 51199 argiro.kok
        this._subscribeService.unSubscribeToCommunity(this.communityId, email,this.properties.adminToolsAPIURL).subscribe (
174 51050 argiro.kok
          res => {
175 53349 konstantin
            this.loading = false;
176
            if(res.status && res.status != 200) {
177
              UIkit.notification({
178 54622 argiro.kok
                  message : '<strong>An error occured. Please try again!<strong>',
179 53349 konstantin
                  status  : 'warning',
180
                  timeout : 3000,
181
                  pos     : 'top-center'
182
              });
183
            } else {
184 54781 konstantin
             //console.log(res);
185 51050 argiro.kok
             if(this.subscribed){
186
               this.subscribed = false;
187
             }
188 53349 konstantin
           }
189 54622 argiro.kok
          },
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 54863 konstantin
            //console.log(error)
199
            this.handleError("Error unsubscribing email: "+email+" from community with id: "+this.communityId, error);
200 51050 argiro.kok
          });
201
      }
202
    }
203 51199 argiro.kok
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 54863 konstantin
218
     private handleError(message: string, error) {
219
       console.error("Subscribe (component): "+message, error);
220
     }
221 51050 argiro.kok
}