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 {EmailService} from "../../openaireLibrary/utils/email/email.service";
8
import {Session, User} from '../../openaireLibrary/login/utils/helper.class';
9

    
10
import {Email} from "../../openaireLibrary/utils/email/email";
11
import {Composer} from "../../openaireLibrary/utils/email/composer";
12
import {LoginErrorCodes} from '../../openaireLibrary/login/utils/guardHelper.class';
13
import {UserManagementService} from "../../openaireLibrary/services/user-management.service";
14
import {Subscriber, Subscription} from "rxjs";
15
import {properties} from "../../../environments/environment";
16
import {UserRegistryService} from "../../openaireLibrary/services/user-registry.service";
17
import {SubscribeService} from "../../openaireLibrary/utils/subscribe/subscribe.service";
18

    
19
declare var UIkit: any;
20

    
21
@Component({
22
  selector: 'subscribe',
23
  template: `
24
    <span *ngIf="subscribed != null && !showNumbers && showTemplate">
25
      <div *ngIf="!subscribed && showLoginAlert" class="uk-alert-warning uk-animation-slide-bottom" uk-alert="">
26
        <a class="uk-alert-close" uk-close></a>
27
        <p>Please login first to subscribe</p>
28
      </div>
29
      <button *ngIf="loading" class="uk-button portal-button uk-button-small uk-width-1-1">
30
        <span class="uk-icon"><loading [top_margin]="false" [size]="'small'" [color]="null"></loading></span>
31
      </button>
32
      <button *ngIf="!subscribed && !loading" class="uk-button portal-button uk-button-small uk-width-1-1"
33
              (click)="subscribe()">
34
        <span class="uk-icon uk-flex uk-flex-middle">
35
          <svg height="24" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
36
            <path d="M0 0h24v24H0z" fill="none"></path>
37
            <path
38
                d="M16 11c1.66 0 2.99-1.34 2.99-3S17.66 5 16 5c-1.66 0-3 1.34-3 3s1.34 3 3 3zm-8 0c1.66 0 2.99-1.34 2.99-3S9.66 5 8 5C6.34 5 5 6.34 5 8s1.34 3 3 3zm0 2c-2.33 0-7 1.17-7 3.5V19h14v-2.5c0-2.33-4.67-3.5-7-3.5zm8 0c-.29 0-.62.02-.97.05 1.16.84 1.97 1.97 1.97 3.45V19h6v-2.5c0-2.33-4.67-3.5-7-3.5z"></path>
39
          </svg>
40
          <span class="space">Subscribe</span>
41
        </span>
42
      </button>
43
      <button *ngIf="subscribed && !loading" class="subscribed-button uk-button uk-button-small uk-width-1-1"
44
              (click)="confirmOpen()" [disabled]="isManager" [attr.uk-tooltip]="isManager?'You cannot unsubscribe because you are a manager':null">
45
        <span class="uk-icon uk-flex uk-flex-middle">
46
          <svg height="24" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
47
            <path d="M0 0h24v24H0z" fill="none"></path>
48
            <path
49
                d="M16 11c1.66 0 2.99-1.34 2.99-3S17.66 5 16 5c-1.66 0-3 1.34-3 3s1.34 3 3 3zm-8 0c1.66 0 2.99-1.34 2.99-3S9.66 5 8 5C6.34 5 5 6.34 5 8s1.34 3 3 3zm0 2c-2.33 0-7 1.17-7 3.5V19h14v-2.5c0-2.33-4.67-3.5-7-3.5zm8 0c-.29 0-.62.02-.97.05 1.16.84 1.97 1.97 1.97 3.45V19h6v-2.5c0-2.33-4.67-3.5-7-3.5z"></path>
50
          </svg>
51
          <span class="space">Subscribed</span>
52
        </span>
53
      </button>
54
    </span>
55
    <span *ngIf="showNumbers && members > 0  && showTemplate" class="uk-display-inline-block">
56
      <span class="lowOpacityColor uk-text-muted">Members: </span> {{members}}
57
    </span>
58
    <modal-alert (alertOutput)="confirmClose($event)">
59
    </modal-alert>
60
  `
61
})
62
export class SubscribeComponent {
63
  // @Input() showSubscribe:boolean = true;
64
  @Input() showNumbers: boolean;
65
  @Input() communityId: string;
66
  @Input() showTemplate: boolean = true;
67
  @Output() subscribeEvent = new EventEmitter();
68
  
69
  public community = null;
70
  public emailToInformManagers: Email;
71
  
72
  loading: boolean = false;
73
  subscribed: boolean = null;
74
  @Input() properties: EnvProperties = properties;
75
  members: number = 0;
76
  @Output() countSubscribersEvent = new EventEmitter();
77
  showLoginAlert: Boolean = false;
78
  @ViewChild(AlertModal) alert;
79
  private user: User;
80
  
81
  subs: Subscription[] = [];
82
  
83
  constructor(private route: ActivatedRoute,
84
              private _emailService: EmailService,
85
              private _communityService: CommunityService,
86
              private router: Router,
87
              private subscribeService: SubscribeService,
88
              private userManagementService: UserManagementService,
89
              private userRegistryService: UserRegistryService) {
90
  }
91
  
92
  public ngOnInit() {
93
    this.subs.push(this.subscribeService.getLoading().subscribe(loading => {
94
      this.loading = loading;
95
    }));
96
    this.subs.push(this.subscribeService.getMembers().subscribe(members => {
97
      this.members = members;
98
      this.countSubscribersEvent.emit({
99
        value: this.members
100
      });
101
    }));
102
    if (!this.showNumbers) {
103
      this.subs.push(this.userManagementService.getUserInfo().subscribe(
104
        user => {
105
          this.user = user;
106
          this.init();
107
        }
108
      ));
109
    } else {
110
      this.init();
111
    }
112
  }
113
  
114
  public ngOnDestroy() {
115
    for (let sub of this.subs) {
116
      if (sub instanceof Subscriber) {
117
        sub.unsubscribe();
118
      }
119
    }
120
    this.subscribeService.setLoading(false);
121
  }
122
  
123
  private isSubscribed() {
124
    this.subscribed = Session.isSubscribedTo('community', this.communityId, this.user);
125
  }
126
  
127
  private get isManager() {
128
    return Session.isManager('community', this.communityId, this.user);
129
  }
130
  
131
  private init() {
132
    if (!this.showNumbers) {
133
      let email = (this.user) ? this.user.email : null;
134
      if (email == null) {
135
        this.subscribed = false;
136
      } else {
137
        if (this.communityId) {
138
          this.isSubscribed();
139
        }
140
      }
141
    } else {
142
      if (this.communityId) {
143
        this.subscribeService.setLoading(true);
144
        this.subs.push(this.userRegistryService.getMembersCount('community', this.communityId).subscribe(res => {
145
            this.subscribeService.setMembers((res && res.response) ? res.response : 0);
146
            this.subscribeService.setLoading(false);
147
          },
148
          error => {
149
            this.handleError("Error getting community subscribers for community with id: " + this.communityId, error);
150
          }));
151
      }
152
    }
153
    if (this.communityId) {
154
      this.emailToInformManagers = {body: "", subject: "", recipients: []};
155
      this.subs.push(this._communityService.getCommunityAsObservable().subscribe(
156
        community => {
157
          this.community = community;
158
        },
159
        error => {
160
          //console.log('System error retrieving community profile', error)
161
          this.handleError("Error getting community with id: " + this.communityId, error);
162
        }
163
      ));
164
    }
165
  }
166
  
167
  private successfulSubscribe() {
168
    if (!this.subscribed) {
169
      this.subscribed = true;
170
      this.subscribeEvent.emit({
171
        value: "ok"
172
      });
173
      this.subs.push(this._emailService.notifyManagers(this.communityId, 'subscriber',
174
        Composer.composeEmailToInformManagers(this.community.title, this.communityId, this.user.fullname)).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
  subscribe() {
187
    if (!this.user) {
188
      this.subscribed = false;
189
      // this.showLoginAlert = true;
190
      this.router.navigate(['/user-info'], {
191
        queryParams: {
192
          "errorCode": LoginErrorCodes.ACTION_REQUIRES_LOGIN,
193
          "redirectUrl": this.router.url
194
        }
195
      });
196
    } else {
197
      this.subscribeService.setLoading(true);
198
      this.showLoginAlert = false;
199
      this.subs.push(this.userRegistryService.subscribeTo('community', this.communityId).subscribe(res => {
200
        this.userManagementService.updateUserInfo();
201
        this.subscribeService.setMembers(this.members + 1);
202
        this.subscribeService.setLoading(false);
203
        this.successfulSubscribe();
204
      }, error => {
205
        this.subscribeService.setLoading(false);
206
        UIkit.notification({
207
          message: '<strong>An error occurred. Please try again!<strong>',
208
          status: 'warning',
209
          timeout: 3000,
210
          pos: 'top-center'
211
        });
212
        //console.log(error)
213
        this.handleError("Error subscribing email: " + this.user.email + " from community with id: " + this.communityId, error);
214
      }));
215
    }
216
  }
217
  
218
  unsubscribe() {
219
    var email = (this.user) ? this.user.email : null;
220
    if (email == null) {
221
      this.subscribed = false;
222
    } else {
223
      this.subscribeService.setLoading(true);
224
      this.subs.push(this.userRegistryService.unsubscribeFrom('community', this.communityId).subscribe(res => {
225
        this.userManagementService.updateUserInfo();
226
        this.subscribeService.setMembers(this.members - 1);
227
        this.subscribeService.setLoading(false);
228
        this.subscribed = false;
229
      }, error => {
230
        this.subscribeService.setLoading(false);
231
        UIkit.notification({
232
          message: '<strong>An error occurred. Please try again!<strong>',
233
          status: 'warning',
234
          timeout: 3000,
235
          pos: 'top-center'
236
        });
237
        //console.log(error)
238
        this.handleError("Error unsubscribing email: " + email + " from community with id: " + this.communityId, error);
239
      }));
240
    }
241
  }
242
  
243
  confirmOpen() {
244
    
245
    this.alert.cancelButton = true;
246
    this.alert.okButton = true;
247
    //this.alert.alertTitle = "Unsubscribe community ";
248
    //this.alert.message = "Do you want to proceed? ";
249
    this.alert.message = "You are subscribed to the Community Gateway. Do you want to unsubscribe?";
250
    this.alert.okButtonText = "UNSUBSCRIBE";
251
    this.alert.cancelButtonText = "CANCEL";
252
    this.alert.open();
253
  }
254
  
255
  confirmClose(data) {
256
    this.unsubscribe();
257
  }
258
  
259
  private handleError(message: string, error) {
260
    console.error("Subscribe (component): " + message, error);
261
  }
262
}
(1-1/2)