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 portal-button uk-button-small uk-width-1-1 ' + (loading ? ' uk-disabled' : '')"
28
              (click)="subscribe()"> Subscribe</button>
29
      <button *ngIf="subscribed" [class]="'subscribed-button uk-button uk-button-small uk-width-1-1 ' + (loading ? ' uk-disabled' : '')"
30
              (click)="confirmOpen()"> Subscribed</button>
31
    </span>
32

    
33
    <span *ngIf="showNumbers && subscribers !=null && subscribers > 0  && showTemplate">
34
      <span class="lowOpacityColor uk-text-muted">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
  @Input() 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
    console.log("subscribe constructor");
67
  }
68

    
69
  public ngOnInit() {
70
    this.route.data.subscribe((data: { envSpecific: EnvProperties }) => {
71
      if(!this.properties) {
72
        this.properties = data.envSpecific;
73
      }
74
      console.log(this.properties);
75
      if(!this.showNumbers) {
76
        this.userManagementService.getUserInfo().subscribe(
77
          user => {
78
            this.user = user;
79
            this.init();
80
          },
81
          error => {},
82
          () => {
83
            this.init();
84
          }
85
        );
86
      } else {
87
        this.init();
88
      }
89
    });
90
    //this.init();
91
  }
92

    
93
  private init() {
94
    if (!this.showNumbers) {
95
      let email = (this.user) ? this.user.email : null;
96
      if (email == null) {
97
        this.subscribed = false;
98
      } else {
99
        if (this.communityId) {
100
          this._subscribeService.isSubscribedToCommunity(this.properties, this.communityId).subscribe(
101
            res => {
102
              this.subscribed = res;
103
              if (this.subscribed) {
104
                this.subscribeEvent.emit({
105
                  value: "ok"
106
                });
107
              }
108
            },
109
            error => {
110
              this.handleError("Error getting response if email: " + email + " is subscribed to community with id: " + this.communityId, error);
111
            });
112
        }
113
      }
114
    } else {
115
      if (this.communityId) {
116
        this._subscribeService.getNumberOfCommunitySubscribers(this.properties, this.communityId).subscribe(
117
          res => {
118
            this.subscribers = (res && res.value) ? res.value : 0;//(res && res.subscribers && res.subscribers.length) ? res.subscribers.length : 0;
119
          },
120
          error => {
121
            this.handleError("Error getting community subscribers for community with id: " + this.communityId, error);
122
          });
123
      }
124
    }
125
    if (this.communityId) {
126
      this.emailToInformManagers = {body: "", subject: "", recipients: []};
127

    
128
      this._communityService.getCommunity(this.properties, this.properties.communityAPI + this.communityId).subscribe(
129
        community => {
130
          this.community = community;
131
        },
132
        error => {
133
          //console.log('System error retrieving community profile', error)
134
          this.handleError("Error getting community with id: " + this.communityId, error);
135
        }
136
      );
137
    }
138
  }
139

    
140
  subscribe() {
141
    var email = (this.user) ? this.user.email : null;
142
    if (email == null) {
143
      this.subscribed = false;
144
      // this.showLoginAlert = true;
145
      console.log("subscribe before navigate");
146
      this.router.navigate(['/user-info'], {
147
        queryParams: {
148
          "errorCode": LoginErrorCodes.ACTION_REQUIRES_LOGIN,
149
          "redirectUrl": this.router.url
150
        }
151
      });
152
    } else {
153
      this.loading = true;
154
      this.showLoginAlert = false;
155
      this._subscribeService.subscribeToCommunity(this.properties, this.communityId).subscribe(
156
        res => {
157
          this.loading = false;
158
          if (res.status && res.status != 200) {
159
            this.subscribeEvent.emit({
160
              value: "error"
161
            });
162
            UIkit.notification({
163
              message: '<strong>An error occured. Please try again!<strong>',
164
              status: 'warning',
165
              timeout: 3000,
166
              pos: 'top-center'
167
            });
168
          } else {
169
            if (!this.subscribed) {
170
              this.subscribed = true;
171
              this.subscribeEvent.emit({
172
                value: "ok"
173
              });
174
              this._emailService.notifyForNewManagers(this.properties, this.communityId, Composer.composeEmailToInformManagers(this.community.title, this.communityId, this.community.managers, email)).subscribe(
175
                res => {
176
                  //console.log("The email has been sent successfully!")
177
                },
178
                error => {
179
                  //console.log(error)
180
                  this.handleError("Error notifying managers about new subscribers for community with id: " + this.communityId, error);
181
                }
182
              );
183
            }
184
          }
185
        },
186
        error => {
187
          this.loading = false;
188
          this.subscribeEvent.emit({
189
            value: "error"
190
          });
191
          UIkit.notification({
192
            message: '<strong>An error occured. Please try again!<strong>',
193
            status: 'warning',
194
            timeout: 3000,
195
            pos: 'top-center'
196
          });
197
          //console.log(error)
198
          this.handleError("Error subscribing email: " + email + " to community with id: " + this.communityId, error);
199
        });
200
    }
201
  }
202

    
203
  unsubscribe() {
204
    var email = (this.user) ? this.user.email : null;
205
    if (email == null) {
206
      this.subscribed = false;
207
    } else {
208
      this.loading = true;
209
      //this.properties.adminToolsAPIURL
210
      this._subscribeService.unSubscribeToCommunity(this.properties, this.communityId).subscribe(
211
        res => {
212
          this.loading = false;
213
          if (res.status && res.status != 200) {
214
            UIkit.notification({
215
              message: '<strong>An error occured. Please try again!<strong>',
216
              status: 'warning',
217
              timeout: 3000,
218
              pos: 'top-center'
219
            });
220
          } else {
221
            //console.log(res);
222
            if (this.subscribed) {
223
              console.log('here')
224
              this.subscribed = false;
225
            }
226
          }
227
        },
228
        error => {
229
          this.loading = false;
230
          UIkit.notification({
231
            message: '<strong>An error occured. Please try again!<strong>',
232
            status: 'warning',
233
            timeout: 3000,
234
            pos: 'top-center'
235
          });
236
          //console.log(error)
237
          this.handleError("Error unsubscribing email: " + email + " from community with id: " + this.communityId, error);
238
        });
239
    }
240
  }
241

    
242
  confirmOpen() {
243

    
244
    this.alert.cancelButton = true;
245
    this.alert.okButton = true;
246
    //this.alert.alertTitle = "Unsubscribe community ";
247
    //this.alert.message = "Do you want to proceed? ";
248
    this.alert.message = "You are subscribed to the Community Gateway. Do you want to unsubscribe?";
249
    this.alert.okButtonText = "UNSUBSCRIBE";
250
    this.alert.cancelButtonText = "CANCEL";
251
    this.alert.open();
252
  }
253

    
254
  confirmClose(data) {
255
    this.unsubscribe();
256
  }
257

    
258
  private handleError(message: string, error) {
259
    console.error("Subscribe (component): " + message, error);
260
  }
261
}
(1-1/2)