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

    
14
declare var UIkit: any;
15

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

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

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

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

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

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

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

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

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

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

    
91

    
92
    }
93

    
94
    subscribe(){
95
      var email = Session.getUserEmail();
96
      if(email == null){
97
        this.subscribed = false;
98
         this.showLoginAlert = true;
99
      }else{
100
        this.loading = true;
101
        this.showLoginAlert = false;
102
        this._subscribeService.subscribeToCommunity(this.communityId, email, this.properties.adminToolsAPIURL).subscribe (
103
          res => {
104
            this.loading = false;
105
             console.log(res);
106
             if(res.status && res.status != 200) {
107
               UIkit.notification({
108
                   message : '<strong>There was an error in your subscription. Please try again!<strong>',
109
                   status  : 'warning',
110
                   timeout : 3000,
111
                   pos     : 'top-center'
112
               });
113
             } else {
114
               if(!this.subscribed){
115
                 this.subscribed = true;
116
                 this._emailService.sendEmail(this.properties.notifyForNewSubscribers + this.communityId, this.composeEmailToInformManagers(this.community.title, this.communityId, this.community.managers)).subscribe(
117
                 // this._emailService.sendEmail("http://duffy.di.uoa.gr:8080/uoa-admin-tools/notifyForNewSubscribers/"+ this.communityId, this.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
    // TODO find the right place to write it
130
    composeEmailToInformManagers(communityName: string, communityId: string, managers: any): Email {
131
      this.emailToInformManagers.subject = "[OpenAIRE-Connect]  " + communityName + ": New subscriber notification";
132
      this.emailToInformManagers.body = "<div style='font-size:14px;'>"
133
                                        +"<p>There is a new subscriber for \"" + communityName + "\" community. Click  "
134
                                        + "<a href='https://beta.admin.connect.openaire.eu/manage-subscribers?communityId="
135
                                        + communityId + "'>here</a> to manage the subscibers list. </p>"
136
                                        + "<p>OpenAIRE team<br> <a href='https://www.openaire.eu/'>www.openaire.eu</a></p><br>"
137
                                        + "<p style='font-size:11px;'>You are receiving this e-mail as manager of the community "
138
                                        + "<a href='https://beta." + communityId + ".openaire.eu/'>" + communityName + "</a>. "
139
                                        + "If you are not responsible for this community, please "
140
                                        + "<a href='mailto:openaire.test@gmail.com'>contact us</a>."
141
                                        + "<br>"
142
                                        + "Click  <a href='https://beta.admin.connect.openaire.eu/manage-user-notifications?communityId=" + communityId
143
                                        + "'>here</a> to manage your notification settings. </p>"
144
                                        + "</div>";
145

    
146
      this.emailToInformManagers.recipients = managers;
147
      console.log(this.emailToInformManagers);
148

    
149
      return this.emailToInformManagers;
150
    }
151

    
152
    unsubscribe(){
153
      var email = Session.getUserEmail();
154
      if(email == null){
155
        this.subscribed = false;
156
      }else{
157
        this.loading = true;
158
        //this.properties.adminToolsAPIURL
159
        this._subscribeService.unSubscribeToCommunity(this.communityId, email,this.properties.adminToolsAPIURL).subscribe (
160
          res => {
161
            this.loading = false;
162
            if(res.status && res.status != 200) {
163
              UIkit.notification({
164
                  message : '<strong>There was an error in your unsubscription. Please try again!<strong>',
165
                  status  : 'warning',
166
                  timeout : 3000,
167
                  pos     : 'top-center'
168
              });
169
            } else {
170
             console.log(res);
171
             if(this.subscribed){
172
               this.subscribed = false;
173
             }
174
           }
175
          });
176
      }
177
    }
178

    
179
    confirmOpen(){
180

    
181
        this.alert.cancelButton = true;
182
        this.alert.okButton = true;
183
        this.alert.alertTitle = "Unsubscribe community ";
184
        this.alert.message = "Do you want to proceed? ";
185
        this.alert.okButtonText = "Yes";
186
        this.alert.cancelButtonText = "No";
187
        this.alert.open();
188
      }
189
      confirmClose(data){
190
       this.unsubscribe();
191
     }
192
}
(1-1/2)