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, User} 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
import {UserManagementService} from "../../openaireLibrary/services/user-management.service";
15

    
16
declare var UIkit: any;
17

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

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

    
33
    <span *ngIf="showNumbers && subscribers !=null && subscribers > 0  && showTemplate">
34
      <span class="lowOpacityColor">  Members</span> {{subscribers}}
35
    </span>
36
    <modal-alert (alertOutput)="confirmClose($event)">
37
    </modal-alert>
38
  `
39
})
40

    
41
export class SubscribeComponent {
42
  // @Input() showSubscribe:boolean = true;
43
  @Input() showNumbers: boolean;
44
  @Input() communityId: string;
45
  @Input() showTemplate: boolean = true;
46
  @Output() subscribeEvent = new EventEmitter();
47

    
48
  public community = null;
49
  public emailToInformManagers: Email;
50

    
51
  loading: boolean = false;
52
  subscribed: boolean = null;
53
  properties: EnvProperties;
54
  subscribers: number = null;
55
  showLoginAlert: Boolean = false;
56
  @ViewChild(AlertModal) alert;
57
  private user: User;
58

    
59
  constructor(private route: ActivatedRoute,
60
              private _subscribeService: SubscribeService,
61
              private _emailService: EmailService,
62
              private _communityService: CommunityService,
63
              private router: Router,
64
              private userManagementService: UserManagementService
65
  ) {
66
  }
67

    
68
  public ngOnInit() {
69
    this.route.data
70
      .subscribe((data: { envSpecific: EnvProperties }) => {
71
        this.properties = data.envSpecific;
72
        this.userManagementService.getUserInfo(this.properties.userInfoUrl).subscribe( user => {
73
          this.user = user;
74
          if (!this.showNumbers) {
75
            let email = (this.user)?this.user.email:null;
76
            if (email == null) {
77
              this.subscribed = false;
78
            } else {
79
              if (this.communityId) {
80
                this._subscribeService.isSubscribedToCommunity(this.properties, this.communityId, email).subscribe(
81
                  res => {
82
                    this.subscribed = res;
83
                    if (this.subscribed) {
84
                      this.subscribeEvent.emit({
85
                        value: "ok"
86
                      });
87
                    }
88
                  },
89
                  error => {
90
                    this.handleError("Error getting response if email: " + email + " is subscribed to community with id: " + this.communityId, error);
91
                  });
92
              }
93
            }
94
          } else {
95
            if (this.communityId) {
96
              this._subscribeService.getCommunitySubscribers(this.properties, this.communityId).subscribe(
97
                res => {
98
                  this.subscribers = (res && res.subscribers && res.subscribers.length) ? res.subscribers.length : 0;
99
                },
100
                error => {
101
                  this.handleError("Error getting community subscribers for community with id: " + this.communityId, error);
102
                });
103
            }
104
          }
105
          if (this.communityId) {
106
            this.emailToInformManagers = {body: "", subject: "", recipients: []};
107

    
108
            this._communityService.getCommunity(this.properties, this.properties.communityAPI + this.communityId).subscribe(
109
              community => {
110
                this.community = community;
111
              },
112
              error => {
113
                //console.log('System error retrieving community profile', error)
114
                this.handleError("Error getting community with id: " + this.communityId, error);
115
              }
116
            );
117
          }
118
        });
119
      });
120
  }
121

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

    
184
  unsubscribe() {
185
    var email = (this.user)?this.user.email:null;
186
    if (email == null) {
187
      this.subscribed = false;
188
    } else {
189
      this.loading = true;
190
      //this.properties.adminToolsAPIURL
191
      this._subscribeService.unSubscribeToCommunity(this.communityId, email, this.properties.adminToolsAPIURL).subscribe(
192
        res => {
193
          this.loading = false;
194
          if (res.status && res.status != 200) {
195
            UIkit.notification({
196
              message: '<strong>An error occured. Please try again!<strong>',
197
              status: 'warning',
198
              timeout: 3000,
199
              pos: 'top-center'
200
            });
201
          } else {
202
            //console.log(res);
203
            if (this.subscribed) {
204
              console.log('here')
205
              this.subscribed = false;
206
            }
207
          }
208
        },
209
        error => {
210
          this.loading = false;
211
          UIkit.notification({
212
            message: '<strong>An error occured. Please try again!<strong>',
213
            status: 'warning',
214
            timeout: 3000,
215
            pos: 'top-center'
216
          });
217
          //console.log(error)
218
          this.handleError("Error unsubscribing email: " + email + " from community with id: " + this.communityId, error);
219
        });
220
    }
221
  }
222

    
223
  confirmOpen() {
224

    
225
    this.alert.cancelButton = true;
226
    this.alert.okButton = true;
227
    this.alert.alertTitle = "Unsubscribe community ";
228
    this.alert.message = "Do you want to proceed? ";
229
    this.alert.okButtonText = "Yes";
230
    this.alert.cancelButtonText = "No";
231
    this.alert.open();
232
  }
233

    
234
  confirmClose(data) {
235
    this.unsubscribe();
236
  }
237

    
238
  private handleError(message: string, error) {
239
    console.error("Subscribe (component): " + message, error);
240
  }
241
}
(1-1/2)