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-reverse 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()">
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
  }
121
  
122
  private isSubscribed() {
123
    this.subscribed = Session.isSubscribedTo('community', this.communityId, this.user);
124
  }
125
  
126
  private init() {
127
    if (!this.showNumbers) {
128
      let email = (this.user) ? this.user.email : null;
129
      if (email == null) {
130
        this.subscribed = false;
131
      } else {
132
        if (this.communityId) {
133
          this.isSubscribed();
134
        }
135
      }
136
    } else {
137
      if (this.communityId) {
138
        this.subscribeService.setLoading(true);
139
        this.subs.push(this.userRegistryService.getMembersCount('community', this.communityId).subscribe(res => {
140
            this.subscribeService.setMembers((res && res.response) ? res.response : 0);
141
            this.subscribeService.setLoading(false);
142
          },
143
          error => {
144
            this.handleError("Error getting community subscribers for community with id: " + this.communityId, error);
145
          }));
146
      }
147
    }
148
    if (this.communityId) {
149
      this.emailToInformManagers = {body: "", subject: "", recipients: []};
150
      this.subs.push(this._communityService.getCommunityAsObservable().subscribe(
151
        community => {
152
          this.community = community;
153
        },
154
        error => {
155
          //console.log('System error retrieving community profile', error)
156
          this.handleError("Error getting community with id: " + this.communityId, error);
157
        }
158
      ));
159
    }
160
  }
161
  
162
  private successfulSubscribe(email) {
163
    if (!this.subscribed) {
164
      this.subscribed = true;
165
      this.subscribeEvent.emit({
166
        value: "ok"
167
      });
168
      this.subs.push(this._emailService.notifyForNewManagers(this.properties, this.communityId, Composer.composeEmailToInformManagers(this.community.title, this.communityId, this.community.managers,
169
        email, this.properties.adminPortalURL)).subscribe(
170
        res => {
171
          //console.log("The email has been sent successfully!")
172
        },
173
        error => {
174
          //console.log(error)
175
          this.handleError("Error notifying managers about new subscribers for community with id: " + this.communityId, error);
176
        }
177
      ));
178
    }
179
  }
180
  
181
  subscribe() {
182
    if (!this.user) {
183
      this.subscribed = false;
184
      // this.showLoginAlert = true;
185
      this.router.navigate(['/user-info'], {
186
        queryParams: {
187
          "errorCode": LoginErrorCodes.ACTION_REQUIRES_LOGIN,
188
          "redirectUrl": this.router.url
189
        }
190
      });
191
    } else {
192
      this.subscribeService.setLoading(true);
193
      this.showLoginAlert = false;
194
      this.subs.push(this.userRegistryService.subscribeTo('community', this.communityId).subscribe(res => {
195
        this.userManagementService.updateUserInfo();
196
        this.subscribeService.setMembers(this.members + 1);
197
        this.subscribeService.setLoading(false);
198
        this.successfulSubscribe(this.user.email);
199
      }, error => {
200
        this.subscribeService.setLoading(false);
201
        UIkit.notification({
202
          message: '<strong>An error occurred. Please try again!<strong>',
203
          status: 'warning',
204
          timeout: 3000,
205
          pos: 'top-center'
206
        });
207
        //console.log(error)
208
        this.handleError("Error subscribing email: " + this.user.email + " from community with id: " + this.communityId, error);
209
      }));
210
    }
211
  }
212
  
213
  unsubscribe() {
214
    var email = (this.user) ? this.user.email : null;
215
    if (email == null) {
216
      this.subscribed = false;
217
    } else {
218
      this.subscribeService.setLoading(true);
219
      this.subs.push(this.userRegistryService.unsubscribeFrom('community', this.communityId).subscribe(res => {
220
        this.userManagementService.updateUserInfo();
221
        this.subscribeService.setMembers(this.members - 1);
222
        this.subscribeService.setLoading(false);
223
        this.subscribed = false;
224
      }, error => {
225
        this.subscribeService.setLoading(false);
226
        UIkit.notification({
227
          message: '<strong>An error occurred. Please try again!<strong>',
228
          status: 'warning',
229
          timeout: 3000,
230
          pos: 'top-center'
231
        });
232
        //console.log(error)
233
        this.handleError("Error unsubscribing email: " + email + " from community with id: " + this.communityId, error);
234
      }));
235
    }
236
  }
237
  
238
  confirmOpen() {
239
    
240
    this.alert.cancelButton = true;
241
    this.alert.okButton = true;
242
    //this.alert.alertTitle = "Unsubscribe community ";
243
    //this.alert.message = "Do you want to proceed? ";
244
    this.alert.message = "You are subscribed to the Community Gateway. Do you want to unsubscribe?";
245
    this.alert.okButtonText = "UNSUBSCRIBE";
246
    this.alert.cancelButtonText = "CANCEL";
247
    this.alert.open();
248
  }
249
  
250
  confirmClose(data) {
251
    this.unsubscribe();
252
  }
253
  
254
  private handleError(message: string, error) {
255
    console.error("Subscribe (component): " + message, error);
256
  }
257
}
(1-1/2)