Project

General

Profile

1
import {
2
  ChangeDetectorRef,
3
  Component,
4
  Input,
5
  OnChanges,
6
  OnDestroy,
7
  OnInit,
8
  SimpleChanges,
9
  ViewChild
10
} from '@angular/core';
11
import {AbstractControl, FormBuilder, FormGroup, Validators} from '@angular/forms';
12
import {AlertModal} from "../../../utils/modal/alert";
13
import {UserRegistryService} from "../../../services/user-registry.service";
14
import {EnvProperties} from "../../../utils/properties/env-properties";
15
import {properties} from "../../../../../environments/environment";
16
import {Role, Session, User} from "../../../login/utils/helper.class";
17
import {UserManagementService} from "../../../services/user-management.service";
18
import {Router} from "@angular/router";
19
import {SubscriberInviteComponent} from "../../../sharedComponents/subscriber-invite/subscriber-invite.component";
20
import {Subscription, zip} from "rxjs";
21

    
22
declare var UIkit;
23

    
24
@Component({
25
  selector: 'subscribers',
26
  templateUrl: 'subscribers.component.html'
27
})
28
export class SubscribersComponent implements OnInit, OnDestroy, OnChanges {
29
  
30
  @Input()
31
  public id: string;
32
  @Input()
33
  public type: string;
34
  @Input()
35
  public name: string;
36
  @Input()
37
  public link: string;
38
  @Input()
39
  public message: string = null;
40
  @Input()
41
  public inviteDisableMessage: string;
42
  public user: User = null;
43
  public managers: any[];
44
  public subscribers: any[];
45
  public showSubscribers: any[];
46
  public subs: any[] = [];
47
  public loading: boolean = true;
48
  public selectedUser: string = null;
49
  public properties: EnvProperties = properties;
50
  public exists: boolean = true;
51
  public roleFb: FormGroup;
52
  /** Paging */
53
  page: number = 1;
54
  pageSize: number = 5;
55
  /** Search */
56
  filterForm: FormGroup;
57
  @ViewChild('inviteModal') inviteModal: AlertModal;
58
  @ViewChild('deleteModal') deleteModal: AlertModal;
59
  @ViewChild('createRoleModal') createRoleModal: AlertModal;
60
  @ViewChild('subscriberInvite') subscriberInvite: SubscriberInviteComponent;
61
  
62
  constructor(private userRegistryService: UserRegistryService,
63
              private userManagementService: UserManagementService,
64
              private router: Router,
65
              private cdr: ChangeDetectorRef,
66
              private fb: FormBuilder) {
67
  }
68
  
69
  ngOnInit() {
70
    this.filterForm = this.fb.group({
71
      keyword: this.fb.control('')
72
    });
73
    this.subs.push(this.filterForm.get('keyword').valueChanges.subscribe(value => {
74
      this.filterBySearch(value);
75
    }));
76
    this.updateList();
77
    this.userManagementService.getUserInfo().subscribe(user => {
78
      this.user = user;
79
      this.cdr.detectChanges();
80
    });
81
  }
82
  
83
  ngOnChanges(changes: SimpleChanges) {
84
    if (changes.role) {
85
      this.updateList();
86
    }
87
  }
88
  
89
  ngOnDestroy() {
90
    this.subs.forEach(sub => {
91
      if (sub instanceof Subscription) {
92
        sub.unsubscribe();
93
      }
94
    });
95
  }
96
  
97
  get currentPage(): any[] {
98
    if (this.showSubscribers) {
99
      return this.showSubscribers.slice((this.page - 1) * this.pageSize, this.page * this.pageSize);
100
    } else {
101
      return [];
102
    }
103
  }
104
  
105
  updateList() {
106
    this.loading = true;
107
    let data = zip(this.userRegistryService.getActiveEmail(this.type, this.id, 'manager'),
108
                  this.userRegistryService.getActiveEmail(this.type, this.id, 'member'));
109
    this.subs.push(data.subscribe(users => {
110
      this.managers = users[0];
111
      this.subscribers = users[1];
112
      this.subscribers.forEach(subscriber => {
113
        subscriber['isManager'] = this.managers.find(manager => manager.email === subscriber.email);
114
      });
115
      this.filterBySearch(this.filterForm.value.keyword);
116
      this.loading = false;
117
      this.exists = true;
118
    }, error => {
119
      this.subscribers = [];
120
      this.showSubscribers = [];
121
      if (error.status === 404) {
122
        this.exists = false;
123
      }
124
      this.loading = false;
125
    }));
126
  }
127
  
128
  openDeleteModal(item: any) {
129
    this.selectedUser = item.email;
130
    this.deleteModal.alertTitle = 'Delete subscriber';
131
    this.deleteModal.open();
132
  }
133
  
134
  openInviteModal() {
135
    if(this.subscriberInvite && !this.subscriberInvite.loading) {
136
      this.inviteModal.alertTitle = 'Invite users to subscribe';
137
      this.inviteModal.okButtonLeft = false;
138
      this.inviteModal.okButtonText = 'Send';
139
      this.subscriberInvite.reset();
140
      this.inviteModal.open();
141
    }
142
  }
143
  
144
  openCreateRoleModal() {
145
    this.createRoleModal.alertTitle = 'Create group';
146
    this.createRoleModal.okButtonLeft = false;
147
    this.createRoleModal.okButtonText = 'create';
148
    this.roleFb = this.fb.group({
149
      name: this.fb.control(Role.mapType(this.type) + '.' + this.id, Validators.required),
150
      description: this.fb.control('', Validators.required)
151
    });
152
    setTimeout(() => {
153
      this.roleFb.get('name').disable();
154
    }, 0);
155
    this.createRoleModal.open();
156
  }
157
  
158
  deleteSubscriber() {
159
    this.loading = true;
160
    this.userRegistryService.remove(this.type, this.id, this.selectedUser, 'member').subscribe(() => {
161
      this.subscribers = this.subscribers.filter(user => user.email != this.selectedUser);
162
      this.filterBySearch(this.filterForm.value.keyword);
163
      if(this.currentPage.length === 0) {
164
        this.page = 1;
165
      }
166
      this.userManagementService.updateUserInfo();
167
      UIkit.notification(this.selectedUser + ' <b>is no longer</b> subscribed to ' + this.name + ' Dashboard', {
168
        status: 'success',
169
        timeout: 6000,
170
        pos: 'bottom-right'
171
      });
172
      this.loading = false;
173
    }, error => {
174
      UIkit.notification('An error has occurred. Please try again later', {
175
        status: 'danger',
176
        timeout: 6000,
177
        pos: 'bottom-right'
178
      });
179
      this.loading = false;
180
    });
181
  }
182
  
183
  createGroup() {
184
    this.loading = true;
185
    this.roleFb.get('name').enable();
186
    this.userRegistryService.createRole(this.type, this.id, this.roleFb.value).subscribe(() => {
187
      UIkit.notification('Group has been <b> successfully created</b>', {
188
        status: 'success',
189
        timeout: 6000,
190
        pos: 'bottom-right'
191
      });
192
      this.updateList();
193
    }, error => {
194
      UIkit.notification('An error has occurred. Please try again later', {
195
        status: 'danger',
196
        timeout: 6000,
197
        pos: 'bottom-right'
198
      });
199
      this.loading = false;
200
    });
201
  }
202
  
203
  public get isPortalAdmin() {
204
    return Session.isPortalAdministrator(this.user) || Session.isCurator(this.type, this.user);
205
  }
206
  
207
  public updatePage(event) {
208
    this.page = event.value;
209
  }
210
  
211
  private filterBySearch(value: any) {
212
    this.showSubscribers = this.subscribers.filter(subscriber => !value || subscriber.email.includes(value));
213
    this.page = 1;
214
  }
215
}
(2-2/3)