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