Project

General

Profile

1
import {Component, ViewChild} from '@angular/core';
2
import {ActivatedRoute} from '@angular/router';
3

    
4
import {UserComponent} from '../openaireLibrary/login/user.component';
5
import {EmailService} from "../openaireLibrary/utils/email/email.service";
6

    
7
import {Session} from '../openaireLibrary/login/utils/helper.class';
8
import {EnvProperties} from '../openaireLibrary/utils/properties/env-properties';
9
import {SubscribeComponent} from '../utils/subscribe/subscribe.component';
10
import {ConnectHelper} from '../openaireLibrary/connect/connectHelper';
11
import {properties} from "../../environments/environment";
12
import {Subscriber} from "rxjs";
13

    
14
@Component({
15
  selector: 'openaire-user',
16
  template: `
17
    <div id="tm-main" class="uk-section tm-middle">
18
      <div class="uk-margin-small-top">
19
        <user [mainComponent]=false></user>
20
        <div *ngIf="!server" class=" uk-container uk-container-small uk-position-relative">
21

    
22
          <div *ngIf="usercomponent.errorCode == '6' && !isSubscribed" class="uk-alert uk-alert-warning">
23
            <span *ngIf="!isSubscribed ">For this action you have to <span *ngIf="!loggedIn">login and</span> subscribe to the research community.
24
              <span *ngIf="subscribe && !subscribeLoading "> <span *ngIf="!loggedIn">Login and </span>Subscribe <a
25
                [class]="'' + (subscribe.loading ? ' uk-disabled' : '')" (click)="subscribeTo()"> here</a>.</span>
26
            </span>
27
            <span *ngIf="subscribeLoading ">Subscribing to community....</span>
28
            <span *ngIf="subscribeError ">An error occured while trying to subscribe to community....</span>
29

    
30
          </div>
31
          <div *ngIf="usercomponent.errorCode == '7'" class="uk-alert uk-alert-warning">
32
            This action requires authentication.
33
            <span *ngIf="!loggedIn">
34
              Please  <a class="" (click)="login()"> sign in</a> to continue.
35
             </span>
36
          </div>
37
        </div>
38
      </div>
39
    </div>
40
    <subscribe [communityId]="communityId" [showTemplate]=false class=""
41
               (subscribeEvent)="afterSubscibeEvent($event)"></subscribe>
42
  `
43
})
44

    
45
export class OpenaireUserComponent {
46
  @ViewChild(UserComponent, { static: true }) usercomponent: UserComponent;
47
  @ViewChild(SubscribeComponent, { static: true }) subscribe: SubscribeComponent;
48
  properties: EnvProperties;
49
  communityId = null;
50
  subscribeLoading: boolean = false;
51
  subscribeError: boolean = false;
52
  isSubscribed: boolean = false;
53
  public server: boolean = true;
54
  loggedIn: boolean = false;
55
  sub;
56
  constructor(private _emailService: EmailService, private route: ActivatedRoute) {
57
  }
58
  ngOnDestroy() {
59
    if (this.sub instanceof Subscriber) {
60
      this.sub.unsubscribe();
61
    }
62
  }
63
  public ngOnInit() {
64
    if (typeof document !== 'undefined') {
65
      this.server = false;
66
      this.loggedIn = Session.isLoggedIn();
67
    }
68
    this.properties = properties;
69
    this.sub = this.route.queryParams.subscribe(
70
      communityId => {
71
        this.communityId = ConnectHelper.getCommunityFromDomain(this.properties.domain);
72
        if (!this.communityId) {
73
          this.communityId = communityId['communityId'];
74
        }
75
        if (this.subscribe.subscribed) {
76
          this.usercomponent.redirect();
77
        }
78
      });
79
  }
80

    
81
  login() {
82
    this.usercomponent.logIn();
83

    
84
  }
85

    
86
  subscribeTo() {
87
    if (this.subscribe && this.communityId) {
88
      this.subscribeLoading = true;
89
      this.subscribe.subscribe();
90
    }
91
  }
92

    
93
  afterSubscibeEvent($event) {
94
    var res = $event.value;
95
    this.subscribeLoading = false;
96
    this.isSubscribed = this.subscribe.subscribed;
97
    if (res == "ok") {
98
      this.isSubscribed = true;
99
      this.usercomponent.redirect();
100
    } else {
101
      this.subscribeError = true;
102
    }
103

    
104
  }
105

    
106
}
(3-3/3)