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.subscribe((data: { envSpecific: EnvProperties }) => {
70
      this.properties = data.envSpecific;
71
      this.userManagementService.getUserInfo(this.properties.userInfoUrl).subscribe(user => {
72
        this.user = user;
73
        this.init();
74
      });
75
    });
76
    this.init();
77
  }
78

    
79
  private init() {
80
    if (!this.showNumbers) {
81
      let email = (this.user) ? this.user.email : null;
82
      if (email == null) {
83
        this.subscribed = false;
84
      } else {
85
        if (this.communityId) {
86
          this._subscribeService.isSubscribedToCommunity(this.properties, this.communityId, email).subscribe(
87
            res => {
88
              this.subscribed = res;
89
              if (this.subscribed) {
90
                this.subscribeEvent.emit({
91
                  value: "ok"
92
                });
93
              }
94
            },
95
            error => {
96
              this.handleError("Error getting response if email: " + email + " is subscribed to community with id: " + this.communityId, error);
97
            });
98
        }
99
      }
100
    } else {
101
      if (this.communityId) {
102
        this._subscribeService.getCommunitySubscribers(this.properties, this.communityId).subscribe(
103
          res => {
104
            this.subscribers = (res && res.subscribers && res.subscribers.length) ? res.subscribers.length : 0;
105
          },
106
          error => {
107
            this.handleError("Error getting community subscribers for community with id: " + this.communityId, error);
108
          });
109
      }
110
    }
111
    if (this.communityId) {
112
      this.emailToInformManagers = {body: "", subject: "", recipients: []};
113

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

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

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

    
227
  confirmOpen() {
228

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

    
238
  confirmClose(data) {
239
    this.unsubscribe();
240
  }
241

    
242
  private handleError(message: string, error) {
243
    console.error("Subscribe (component): " + message, error);
244
  }
245
}
(1-1/2)