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

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

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

    
28
  constructor(private userRegistryService: UserRegistryService,
29
              private fb: FormBuilder,
30
              private route: ActivatedRoute) {
31
  }
32

    
33
  ngOnInit() {
34
    this.subs.push(this.route.queryParams.subscribe(params => {
35
      if (params && params['communityId']) {
36
        this.communityId = params['communityId'];
37
        this.subs.push(this.userRegistryService.getManagersEmail('community', this.communityId).subscribe(managers => {
38
          this.managers = managers;
39
          this.loadManagers = false;
40
        }, error => {
41
          this.managers = [];
42
          this.error = error.error.response;
43
          this.loadManagers = false;
44
        }));
45
        this.subs.push(this.userRegistryService.getPendingManagers('community', this.communityId).subscribe(pending => {
46
          this.pending = pending;
47
          this.loadPending = false;
48
        }, error => {
49
          this.managers = [];
50
          this.error = error.error.response;
51
          this.loadManagers = false;
52
        }));
53
      }
54
    }));
55
  }
56

    
57
  ngOnDestroy() {
58
    this.subs.forEach(sub => {
59
      if (sub instanceof Subscription) {
60
        sub.unsubscribe();
61
      }
62
    });
63
  }
64

    
65
  openDeleteModal(item: any) {
66
    if(this.showManagers) {
67
      this.selectedUser = item.email;
68
      this.deleteManagerModal.alertTitle = 'Delete ' + item.email + ' from managers';
69
      this.deleteManagerModal.open();
70
    } else {
71
      this.selectedUser = item;
72
      this.deletePendingModal.alertTitle = 'Delete ' + item + ' from managers';
73
      this.deletePendingModal.open();
74
    }
75
  }
76

    
77
  openInviteModal() {
78
    this.inviteManagerModal.alertTitle = 'Invite user to be manager';
79
    this.inviteManagerModal.okButtonLeft = false;
80
    this.invited = this.fb.control('', [Validators.required, Validators.email]);
81
    this.inviteManagerModal.open();
82
  }
83

    
84
  deleteManager() {
85
    this.loadManagers = true;
86
    this.userRegistryService.removeManagerRole('community', this.communityId, this.selectedUser).subscribe(() => {
87
      this.managers = this.managers.filter(manager => manager.email != this.selectedUser);
88
      this.loadManagers = false;
89
      this.error = null;
90
    },error => {
91
      this.error = error.error.response;
92
      this.loadManagers = false;
93
    });
94
  }
95

    
96
  deletePendingManager() {
97
    this.loadPending = true;
98
    this.userRegistryService.cancelInvitation('community', this.communityId, this.selectedUser).subscribe(() => {
99
      this.pending = this.pending.filter(manager => manager != this.selectedUser);
100
      this.error = null;
101
      this.loadPending = false;
102
    },error => {
103
      this.error = error.error.response;
104
      this.loadPending = false;
105
    });
106
  }
107

    
108
  inviteManager() {
109
    this.loadManagers = true;
110
    this.userRegistryService.invite('community', this.communityId, this.invited.value).subscribe(invitation => {
111
      this.error = null;
112
      if(!this.pending.includes(this.invited.value)) {
113
        this.pending.push(this.invited.value);
114
      }
115
      this.loadManagers = false;
116
    },error => {
117
      this.error = error.error.response;
118
      this.loadManagers = false;
119
    })
120
  }
121
}
(3-3/4)