Project

General

Profile

1
import {Component, OnDestroy, OnInit, ViewChild} from '@angular/core';
2
import {UserRegistryService} from '../../openaireLibrary/services/user-registry.service';
3
import {Subscription} from 'rxjs/Rx';
4
import {ActivatedRoute} from '@angular/router';
5
import {AlertModal} from '../../openaireLibrary/utils/modal/alert';
6
import {FormBuilder, FormControl, Validators} from '@angular/forms';
7
import {CommunityService} from '../../openaireLibrary/connect/community/community.service';
8
import {properties} from '../../../environments/environment';
9

    
10
@Component({
11
  selector: 'managers',
12
  templateUrl: 'managers.component.html'
13
})
14
export class ManagersComponent implements OnInit, OnDestroy {
15

    
16
  public managers: any[];
17
  public pending: any[];
18
  public communityId: string;
19
  public showManagers: boolean = true;
20
  public subs: any[] = [];
21
  public loadManagers: boolean = true;
22
  public loadPending: boolean = true;
23
  public error: string;
24
  public selectedUser: string = null;
25
  public invited: FormControl;
26
  public communityName: string;
27
  @ViewChild('inviteManagerModal') inviteManagerModal: AlertModal;
28
  @ViewChild('deleteManagerModal') deleteManagerModal: AlertModal;
29
  @ViewChild('deletePendingModal') deletePendingModal: AlertModal;
30

    
31
  constructor(private userRegistryService: UserRegistryService,
32
              private communityService: CommunityService,
33
              private fb: FormBuilder,
34
              private route: ActivatedRoute) {
35
  }
36

    
37
  ngOnInit() {
38
    this.subs.push(this.route.queryParams.subscribe(params => {
39
      if (params && params['communityId']) {
40
        this.communityId = params['communityId'];
41
        this.subs.push(this.communityService.getCommunity(properties, properties.communityAPI + this.communityId).subscribe(community => {
42
          this.communityName = community.title;
43
        }));
44
        this.subs.push(this.userRegistryService.getManagersEmail('community', this.communityId).subscribe(managers => {
45
          this.managers = managers;
46
          this.loadManagers = false;
47
        }, error => {
48
          this.managers = [];
49
          this.error = error.error.response;
50
          this.loadManagers = false;
51
        }));
52
        this.subs.push(this.userRegistryService.getPendingManagers('community', this.communityId).subscribe(pending => {
53
          this.pending = pending;
54
          this.loadPending = false;
55
        }, error => {
56
          this.managers = [];
57
          this.error = error.error.response;
58
          this.loadManagers = false;
59
        }));
60
      }
61
    }));
62
  }
63

    
64
  ngOnDestroy() {
65
    this.subs.forEach(sub => {
66
      if (sub instanceof Subscription) {
67
        sub.unsubscribe();
68
      }
69
    });
70
  }
71

    
72
  openDeleteModal(item: any) {
73
    if(this.showManagers) {
74
      this.selectedUser = item.email;
75
      this.deleteManagerModal.alertTitle = 'Delete ' + item.email + ' from managers';
76
      this.deleteManagerModal.open();
77
    } else {
78
      this.selectedUser = item;
79
      this.deletePendingModal.alertTitle = 'Delete ' + item + ' from managers';
80
      this.deletePendingModal.open();
81
    }
82
  }
83

    
84
  openInviteModal() {
85
    this.inviteManagerModal.alertTitle = 'Invite user to be manager';
86
    this.inviteManagerModal.okButtonLeft = false;
87
    this.invited = this.fb.control('', [Validators.required, Validators.email]);
88
    this.inviteManagerModal.open();
89
  }
90

    
91
  deleteManager() {
92
    this.loadManagers = true;
93
    this.userRegistryService.removeManagerRole('community', this.communityId, this.selectedUser).subscribe(() => {
94
      this.managers = this.managers.filter(manager => manager.email != this.selectedUser);
95
      this.loadManagers = false;
96
      this.error = null;
97
    },error => {
98
      this.error = error.error.response;
99
      this.loadManagers = false;
100
    });
101
  }
102

    
103
  deletePendingManager() {
104
    this.loadPending = true;
105
    this.userRegistryService.cancelInvitation('community', this.communityId, this.selectedUser).subscribe(() => {
106
      this.pending = this.pending.filter(manager => manager != this.selectedUser);
107
      this.error = null;
108
      this.loadPending = false;
109
    },error => {
110
      this.error = error.error.response;
111
      this.loadPending = false;
112
    });
113
  }
114

    
115
  inviteManager() {
116
    this.loadManagers = true;
117
    let details = {
118
      name: this.communityName,
119
      link: this.getURL()
120
    }
121
    this.userRegistryService.invite('community', this.communityId, this.invited.value, details).subscribe(invitation => {
122
      this.error = null;
123
      if(!this.pending.includes(this.invited.value)) {
124
        this.pending.push(this.invited.value);
125
      }
126
      this.loadManagers = false;
127
    },error => {
128
      this.error = error.error.response;
129
      this.loadManagers = false;
130
    })
131
  }
132

    
133
  private getURL(): string {
134
    if(properties.environment != "production") {
135
      return 'https://beta.' + this.communityId + ".openaire.eu/verify";
136
    } else {
137
      return 'https://' + this.communityId + ".openaire.eu/verify";
138
    }
139
  }
140
}
(3-3/4)