Project

General

Profile

1
import {Component, Input, OnChanges, OnDestroy, OnInit, SimpleChanges, ViewChild} from '@angular/core';
2
import {FormBuilder, FormControl, FormGroup, Validators} from '@angular/forms';
3
import {AlertModal} from "../../../utils/modal/alert";
4
import {UserRegistryService} from "../../../services/user-registry.service";
5
import {EnvProperties} from "../../../utils/properties/env-properties";
6
import {properties} from "../../../../../environments/environment";
7
import {Role, Session, User} from "../../../login/utils/helper.class";
8
import {UserManagementService} from "../../../services/user-management.service";
9
import {Router} from "@angular/router";
10
import {StringUtils} from "../../../utils/string-utils.class";
11
import {NotificationService} from "../../../notifications/notification.service";
12
import {Subscription} from "rxjs";
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
  @Input()
39
  public inviteDisableMessage: string;
40
  public user: User = null;
41
  public active: any[];
42
  public showActive: any[] = [];
43
  public managers: any[];
44
  public pending: any[];
45
  public showPending: any[] = [];
46
  public showCurrent: boolean = true;
47
  public subs: any[] = [];
48
  public loadActive: boolean = true;
49
  public loadPending: boolean = true;
50
  public selectedUser: string = null;
51
  public invited: FormControl;
52
  public properties: EnvProperties = properties;
53
  public exists: boolean = true;
54
  public roleFb: FormGroup;
55
  /** Paging */
56
  activePage: number = 1;
57
  pendingPage: number = 1;
58
  pageSize: number = 5;
59
  /** Search */
60
  filterForm: FormGroup;
61
  @ViewChild('inviteModal') inviteModal: AlertModal;
62
  @ViewChild('deleteModal') deleteModal: AlertModal;
63
  @ViewChild('deletePendingModal') deletePendingModal: AlertModal;
64
  @ViewChild('createRoleModal') createRoleModal: AlertModal;
65
  
66
  constructor(private userRegistryService: UserRegistryService,
67
              private userManagementService: UserManagementService,
68
              private notificationService: NotificationService,
69
              private router: Router,
70
              private fb: FormBuilder) {
71
  }
72
  
73
  ngOnInit() {
74
    this.initForm();
75
    this.updateLists();
76
    this.userManagementService.getUserInfo().subscribe(user => {
77
      this.user = user;
78
    });
79
  }
80
  
81
  ngOnChanges(changes: SimpleChanges) {
82
    if (changes.role) {
83
      this.unsubscribe();
84
      this.initForm();
85
      this.updateLists();
86
    }
87
  }
88
  
89
  ngOnDestroy() {
90
    this.unsubscribe();
91
  }
92
  
93
  unsubscribe() {
94
    this.subs.forEach(sub => {
95
      if (sub instanceof Subscription) {
96
        sub.unsubscribe();
97
      }
98
    });
99
  }
100
  
101
  initForm() {
102
    this.filterForm = this.fb.group({
103
      active: this.fb.control(''),
104
      pending: this.fb.control('')
105
    });
106
    this.subs.push(this.filterForm.get('active').valueChanges.subscribe(value => {
107
      this.filterActiveBySearch(value);
108
    }));
109
    this.subs.push(this.filterForm.get('pending').valueChanges.subscribe(value => {
110
      this.filterPendingBySearch(value);
111
    }));
112
  }
113
  
114
  updateLists() {
115
    this.loadActive = true;
116
    this.loadPending = true;
117
    this.subs.push(this.userRegistryService.getActiveEmail(this.type, this.id, this.role).subscribe(users => {
118
      if (this.role === 'member') {
119
        this.subs.push(this.userRegistryService.getActiveEmail(this.type, this.id, 'manager').subscribe(managers => {
120
          this.managers = managers;
121
          this.active = users;
122
          this.active.forEach(user => {
123
            user['isManager'] = this.managers.find(manager => manager.email === user.email);
124
          });
125
          this.filterActiveBySearch(this.filterForm.value.active);
126
          this.loadActive = false;
127
          this.exists = true;
128
        }))
129
      } else {
130
        this.active = users;
131
        this.filterActiveBySearch(this.filterForm.value.active);
132
        this.loadActive = false;
133
        this.exists = true;
134
      }
135
    }, error => {
136
      this.active = [];
137
      if (error.status === 404) {
138
        this.exists = false;
139
      }
140
      this.loadActive = false;
141
    }));
142
    this.subs.push(this.userRegistryService.getPending(this.type, this.id, this.role).subscribe(users => {
143
      this.pending = users;
144
      this.filterPendingBySearch(this.filterForm.value.pending);
145
      this.loadPending = false;
146
    }, error => {
147
      this.pending = [];
148
      this.loadPending = false;
149
    }));
150
  }
151
  
152
  get currentActivePage(): any[] {
153
    if (this.active) {
154
      return this.active.slice((this.activePage - 1) * this.pageSize, this.activePage * this.pageSize);
155
    } else {
156
      return [];
157
    }
158
  }
159
  
160
  get currentPendingPage(): any[] {
161
    if (this.pending) {
162
      return this.pending.slice((this.pendingPage - 1) * this.pageSize, this.pendingPage * this.pageSize);
163
    } else {
164
      return [];
165
    }
166
  }
167
  
168
  openDeleteModal(item: any) {
169
    if (this.showCurrent) {
170
      this.selectedUser = item.email;
171
      this.deleteModal.alertTitle = 'Delete ' + this.role;
172
      this.deleteModal.open();
173
    } else {
174
      this.selectedUser = item;
175
      this.deletePendingModal.alertTitle = 'Cancel invitation';
176
      this.deletePendingModal.open();
177
    }
178
  }
179
  
180
  openInviteModal() {
181
    this.inviteModal.alertTitle = 'Invite ' + this.role;
182
    this.inviteModal.okButtonLeft = false;
183
    this.inviteModal.okButtonText = 'Send';
184
    this.invited = this.fb.control('', [Validators.required, Validators.email]);
185
    this.inviteModal.open();
186
  }
187
  
188
  openCreateRoleModal() {
189
    this.createRoleModal.alertTitle = 'Create group';
190
    this.createRoleModal.okButtonLeft = false;
191
    this.createRoleModal.okButtonText = 'Create';
192
    this.roleFb = this.fb.group({
193
      name: this.fb.control(Role.mapType(this.type, (this.role === 'manager')) + '.' + this.id, Validators.required),
194
      description: this.fb.control('', Validators.required)
195
    });
196
    setTimeout(() => {
197
      this.roleFb.get('name').disable();
198
    }, 0);
199
    this.createRoleModal.open();
200
  }
201
  
202
  deleteActive() {
203
    this.loadActive = true;
204
    this.subs.push(this.userRegistryService.remove(this.type, this.id, this.selectedUser, this.role).subscribe(() => {
205
      this.active = this.active.filter(user => user.email != this.selectedUser);
206
      if(this.currentActivePage.length === 0) {
207
        this.activePage = 1;
208
      }
209
      this.filterActiveBySearch(this.filterForm.value.active);
210
      this.userManagementService.updateUserInfo();
211
      UIkit.notification(this.selectedUser + ' <b>is no longer</b> ' + this.role + ' of ' + this.name + ' Dashboard', {
212
        status: 'success',
213
        timeout: 6000,
214
        pos: 'bottom-right'
215
      });
216
      this.loadActive = false;
217
    }, error => {
218
      UIkit.notification('An error has occurred. Please try again later', {
219
        status: 'danger',
220
        timeout: 6000,
221
        pos: 'bottom-right'
222
      });
223
      this.loadActive = false;
224
    }));
225
  }
226
  
227
  deletePending() {
228
    this.loadPending = true;
229
    this.subs.push(this.userRegistryService.cancelInvitation(this.type, this.id, this.selectedUser, this.role).subscribe(() => {
230
      this.pending = this.pending.filter(user => user != this.selectedUser);
231
      this.filterPendingBySearch(this.filterForm.value.pending);
232
      if(this.currentPendingPage.length === 0) {
233
        this.pendingPage = 1;
234
      }
235
      UIkit.notification(StringUtils.capitalize(this.role) + ' invitation to ' + this.selectedUser + ' has been <b>canceled</b>', {
236
        status: 'success',
237
        timeout: 6000,
238
        pos: 'bottom-right'
239
      });
240
      this.loadPending = false;
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.loadPending = false;
248
    }));
249
  }
250
  
251
  invite() {
252
    this.showCurrent = false;
253
    this.loadPending = true;
254
    this.selectedUser = this.invited.value;
255
    let details = {
256
      link: this.link,
257
      email: this.emailComposer(this.name, this.invited.value, this.role)
258
    }
259
    this.subs.push(this.userRegistryService.invite(this.type, this.id, details, this.role).subscribe(invitation => {
260
      if (!this.pending.includes(this.invited.value)) {
261
        this.pending.push(this.invited.value);
262
        this.pendingPage =  Math.ceil(this.pending.length/this.pageSize);
263
        this.filterPendingBySearch(this.filterForm.value.pending);
264
      }
265
      if (this.notificationFn) {
266
        this.subs.push(this.notificationService.sendNotification(this.notificationFn(this.name, this.invited.value, this.role, invitation)).subscribe(notification => {
267
          UIkit.notification('A notification has been <b>sent</b> successfully', {
268
            status: 'success',
269
            timeout: 6000,
270
            pos: 'bottom-right'
271
          });
272
        }, error => {
273
          UIkit.notification('An error has occurred. Please try again later', {
274
            status: 'danger',
275
            timeout: 6000,
276
            pos: 'bottom-right'
277
          });
278
        }));
279
      }
280
      UIkit.notification(StringUtils.capitalize(this.role) + ' invitation to ' + this.selectedUser + ' has been <b>sent</b>', {
281
        status: 'success',
282
        timeout: 6000,
283
        pos: 'bottom-right'
284
      });
285
      this.loadPending = false;
286
    }, error => {
287
      UIkit.notification('An error has occurred. Please try again later', {
288
        status: 'danger',
289
        timeout: 6000,
290
        pos: 'bottom-right'
291
      });
292
      this.loadPending = false;
293
    }));
294
  }
295
  
296
  createGroup() {
297
    this.loadActive = true;
298
    this.loadPending = true;
299
    this.roleFb.get('name').enable();
300
    this.userRegistryService.createRole(this.type, this.id, this.roleFb.value).subscribe(() => {
301
      UIkit.notification('Group has been <b> successfully created</b>', {
302
        status: 'success',
303
        timeout: 6000,
304
        pos: 'bottom-right'
305
      });
306
      this.updateLists();
307
    }, error => {
308
      UIkit.notification('An error has occurred. Please try again later', {
309
        status: 'danger',
310
        timeout: 6000,
311
        pos: 'bottom-right'
312
      });
313
      this.loadActive = false;
314
      this.loadPending = false;
315
    });
316
  }
317
  
318
  public get isCurator(): boolean {
319
    return this.isPortalAdmin || !!Session.isCurator(this.type, this.user);
320
  }
321
  
322
  public get isPortalAdmin(): boolean {
323
    return !!Session.isPortalAdministrator(this.user);
324
  }
325
  
326
  updateActivePage(event: any) {
327
    this.activePage = event.value;
328
  }
329
  
330
  updatePendingPage(event: any) {
331
    this.pendingPage = event.value;
332
  }
333
  
334
  private filterActiveBySearch(value: any) {
335
    this.showActive = this.active.filter(active => !value || active.email.includes(value));
336
    this.activePage = 1;
337
  }
338
  
339
  private filterPendingBySearch(value: any) {
340
    this.showPending = this.pending.filter(pending => !value || pending.includes(value));
341
    this.pendingPage = 1;
342
  }
343
}
(2-2/3)