Project

General

Profile

1
import {Component, Input, OnChanges, OnDestroy, OnInit, SimpleChanges, ViewChild} from '@angular/core';
2
import {Subscription} from 'rxjs/Rx';
3
import {FormBuilder, FormControl, FormGroup, Validators} from '@angular/forms';
4
import {AlertModal} from "../../../utils/modal/alert";
5
import {UserRegistryService} from "../../../services/user-registry.service";
6
import {EnvProperties} from "../../../utils/properties/env-properties";
7
import {properties} from "../../../../../environments/environment";
8
import {Role, Session, User} from "../../../login/utils/helper.class";
9
import {UserManagementService} from "../../../services/user-management.service";
10
import {Router} from "@angular/router";
11
import {StringUtils} from "../../../utils/string-utils.class";
12
import {NotificationService} from "../../../notifications/notification.service";
13

    
14
declare var UIkit;
15

    
16
@Component({
17
  selector: 'role-users',
18
  templateUrl: 'role-users.component.html'
19
})
20
export class RoleUsersComponent implements OnInit, OnDestroy, OnChanges {
21
  
22
  @Input()
23
  public id: string;
24
  @Input()
25
  public type: string;
26
  @Input()
27
  public name: string;
28
  @Input()
29
  public link: string;
30
  @Input()
31
  public role: "member" | "manager" = "manager";
32
  @Input()
33
  public message: string = null;
34
  @Input()
35
  public emailComposer: Function;
36
  @Input()
37
  public notificationFn: Function;
38
  public user: User = null;
39
  public active: any[];
40
  public pending: any[];
41
  public showActive: boolean = true;
42
  public subs: any[] = [];
43
  public loadActive: boolean = true;
44
  public loadPending: boolean = true;
45
  public selectedUser: string = null;
46
  public invited: FormControl;
47
  public properties: EnvProperties = properties;
48
  public exists: boolean = true;
49
  public roleFb: FormGroup;
50
  @ViewChild('inviteModal') inviteModal: AlertModal;
51
  @ViewChild('deleteModal') deleteModal: AlertModal;
52
  @ViewChild('deletePendingModal') deletePendingModal: AlertModal;
53
  @ViewChild('createRoleModal') createRoleModal: AlertModal;
54
  
55
  constructor(private userRegistryService: UserRegistryService,
56
              private userManagementService: UserManagementService,
57
              private notificationService: NotificationService,
58
              private router: Router,
59
              private fb: FormBuilder) {
60
  }
61
  
62
  ngOnInit() {
63
    this.updateLists();
64
    this.userManagementService.getUserInfo().subscribe(user => {
65
      this.user = user;
66
    });
67
  }
68
  
69
  ngOnChanges(changes: SimpleChanges) {
70
    if(changes.role) {
71
      this.unsubscribe();
72
      this.updateLists();
73
    }
74
  }
75
  
76
  ngOnDestroy() {
77
    this.unsubscribe();
78
  }
79
  
80
  unsubscribe() {
81
    this.subs.forEach(sub => {
82
      if (sub instanceof Subscription) {
83
        sub.unsubscribe();
84
      }
85
    });
86
  }
87
  
88
  
89
  updateLists() {
90
    this.loadActive = true;
91
    this.loadPending = true;
92
    this.subs.push(this.userRegistryService.getActiveEmail(this.type, this.id, this.role).subscribe(users => {
93
      this.active = users;
94
      this.loadActive = false;
95
      this.exists = true;
96
    }, error => {
97
      this.active = [];
98
      if(error.status === 404) {
99
        this.exists = false;
100
      }
101
      this.loadActive = false;
102
    }));
103
    this.subs.push(this.userRegistryService.getPending(this.type, this.id, this.role).subscribe(users => {
104
      this.pending = users;
105
      this.loadPending = false;
106
    }, error => {
107
      this.active = [];
108
      this.loadPending = false;
109
    }));
110
  }
111
  
112
  openDeleteModal(item: any) {
113
    if (this.showActive) {
114
      this.selectedUser = item.email;
115
      this.deleteModal.alertTitle = 'Delete ' + this.role;
116
      this.deleteModal.open();
117
    } else {
118
      this.selectedUser = item;
119
      this.deletePendingModal.alertTitle = 'Cancel invitation';
120
      this.deletePendingModal.open();
121
    }
122
  }
123
  
124
  openInviteModal() {
125
    this.inviteModal.alertTitle = 'Invite ' + this.role;
126
    this.inviteModal.okButtonLeft = false;
127
    this.inviteModal.okButtonText = 'Send';
128
    this.invited = this.fb.control('', [Validators.required, Validators.email]);
129
    this.inviteModal.open();
130
  }
131
  
132
  openCreateRoleModal() {
133
    this.createRoleModal.alertTitle = 'Create group';
134
    this.createRoleModal.okButtonLeft = false;
135
    this.createRoleModal.okButtonText = 'Create';
136
    this.roleFb = this.fb.group({
137
      name: this.fb.control(Role.mapType(this.type, (this.role === 'manager')) + '.' + this.id, Validators.required),
138
      description: this.fb.control('', Validators.required)
139
    });
140
    setTimeout(() => {
141
      this.roleFb.get('name').disable();
142
    }, 0);
143
    this.createRoleModal.open();
144
  }
145
  
146
  deleteActive() {
147
    this.loadActive = true;
148
    this.subs.push(this.userRegistryService.remove(this.type, this.id, this.selectedUser, this.role).subscribe(() => {
149
      this.active = this.active.filter(user => user.email != this.selectedUser);
150
      this.userManagementService.updateUserInfo();
151
      UIkit.notification(this.selectedUser + ' <b>is no longer</b> ' + this.role + ' of ' + this.name + ' Dashboard', {
152
        status: 'success',
153
        timeout: 6000,
154
        pos: 'bottom-right'
155
      });
156
      this.loadActive = false;
157
    }, error => {
158
      UIkit.notification('An error has occurred. Please try again later', {
159
        status: 'danger',
160
        timeout: 6000,
161
        pos: 'bottom-right'
162
      });
163
      this.loadActive = false;
164
    }));
165
  }
166
  
167
  deletePending() {
168
    this.loadPending = true;
169
    this.subs.push(this.userRegistryService.cancelInvitation(this.type, this.id, this.selectedUser, this.role).subscribe(() => {
170
      this.pending = this.pending.filter(user => user != this.selectedUser);
171
      UIkit.notification(StringUtils.capitalize(this.role) + ' invitation to ' + this.selectedUser + ' has been <b>canceled</b>', {
172
        status: 'success',
173
        timeout: 6000,
174
        pos: 'bottom-right'
175
      });
176
      this.loadPending = false;
177
    }, error => {
178
      UIkit.notification('An error has occurred. Please try again later', {
179
        status: 'danger',
180
        timeout: 6000,
181
        pos: 'bottom-right'
182
      });
183
      this.loadPending = false;
184
    }));
185
  }
186
  
187
  invite() {
188
    this.showActive = false;
189
    this.loadPending = true;
190
    this.selectedUser = this.invited.value;
191
    let details = {
192
      link: this.link,
193
      email: this.emailComposer(this.name, this.invited.value, this.role)
194
    }
195
    this.subs.push(this.userRegistryService.invite(this.type, this.id, details, this.role).subscribe(invitation => {
196
      if (!this.pending.includes(this.invited.value)) {
197
        this.pending.push(this.invited.value);
198
      }
199
      if(this.notificationFn) {
200
        this.subs.push(this.notificationService.sendNotification(this.notificationFn(this.name, this.invited.value, this.role, invitation)).subscribe(notification => {
201
          UIkit.notification('A notification has been <b>sent</b> successfully', {
202
            status: 'success',
203
            timeout: 6000,
204
            pos: 'bottom-right'
205
          });
206
        }, error => {
207
          UIkit.notification('An error has occurred. Please try again later', {
208
            status: 'danger',
209
            timeout: 6000,
210
            pos: 'bottom-right'
211
          });
212
        }));
213
      }
214
      UIkit.notification(StringUtils.capitalize(this.role) + ' invitation to ' + this.selectedUser + ' has been <b>sent</b>', {
215
        status: 'success',
216
        timeout: 6000,
217
        pos: 'bottom-right'
218
      });
219
      this.loadPending = false;
220
    }, error => {
221
      UIkit.notification('An error has occurred. Please try again later', {
222
        status: 'danger',
223
        timeout: 6000,
224
        pos: 'bottom-right'
225
      });
226
      this.loadPending = false;
227
    }));
228
  }
229
  
230
  createGroup() {
231
    this.loadActive = true;
232
    this.loadPending = true;
233
    this.roleFb.get('name').enable();
234
    this.userRegistryService.createRole(this.type, this.id, this.roleFb.value).subscribe(() => {
235
      UIkit.notification('Group has been <b> successfully created</b>', {
236
        status: 'success',
237
        timeout: 6000,
238
        pos: 'bottom-right'
239
      });
240
      this.updateLists();
241
    }, error => {
242
      UIkit.notification('An error has occurred. Please try again later', {
243
        status: 'danger',
244
        timeout: 6000,
245
        pos: 'bottom-right'
246
      });
247
      this.loadActive = false;
248
      this.loadPending = false;
249
    });
250
  }
251
  
252
  public get isCurator(): boolean {
253
    return this.isPortalAdmin || !!Session.isCurator(this.type, this.user);
254
  }
255
  
256
  public get isPortalAdmin(): boolean {
257
    return !!Session.isPortalAdministrator(this.user);
258
  }
259
}
(2-2/3)