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 {Session, User} from "../../../login/utils/helper.class";
9
import {UserManagementService} from "../../../services/user-management.service";
10
import {Router} from "@angular/router";
11
import {Composer} from "../../../utils/email/composer";
12
import {SubscriberInviteComponent} from "../../../sharedComponents/subscriber-invite/subscriber-invite.component";
13

    
14
declare var UIkit;
15

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