236 lines
7.5 KiB
TypeScript
236 lines
7.5 KiB
TypeScript
import {Component, Input, OnChanges, OnDestroy, OnInit, SimpleChanges, ViewChild} from '@angular/core';
|
|
import {Subscription} from 'rxjs/Rx';
|
|
import {FormBuilder, FormControl, FormGroup, Validators} from '@angular/forms';
|
|
import {AlertModal} from "../../../utils/modal/alert";
|
|
import {UserRegistryService} from "../../../services/user-registry.service";
|
|
import {EnvProperties} from "../../../utils/properties/env-properties";
|
|
import {properties} from "../../../../../environments/environment";
|
|
import {Session} from "../../../login/utils/helper.class";
|
|
import {UserManagementService} from "../../../services/user-management.service";
|
|
import {Router} from "@angular/router";
|
|
import {LoginErrorCodes} from "../../../login/utils/guardHelper.class";
|
|
import {Composer} from "../../../utils/email/composer";
|
|
import {StringUtils} from "../../../utils/string-utils.class";
|
|
|
|
declare var UIkit;
|
|
|
|
@Component({
|
|
selector: 'role-users',
|
|
templateUrl: 'role-users.component.html'
|
|
})
|
|
export class RoleUsersComponent implements OnInit, OnDestroy, OnChanges {
|
|
|
|
@Input()
|
|
public id: string;
|
|
@Input()
|
|
public type: string;
|
|
@Input()
|
|
public name: string;
|
|
@Input()
|
|
public link: string;
|
|
@Input()
|
|
public isPortalAdmin: boolean = false;
|
|
@Input()
|
|
public role: "member" | "manager" = "manager";
|
|
@Input()
|
|
public messages: Map<"member" | "manager", string> = null;
|
|
public active: any[];
|
|
public pending: any[];
|
|
public showActive: boolean = true;
|
|
public subs: any[] = [];
|
|
public loadActive: boolean = true;
|
|
public loadPending: boolean = true;
|
|
public selectedUser: string = null;
|
|
public invited: FormControl;
|
|
public properties: EnvProperties = properties;
|
|
public exists: boolean = true;
|
|
public roleFb: FormGroup;
|
|
@ViewChild('inviteModal') inviteModal: AlertModal;
|
|
@ViewChild('deleteModal') deleteModal: AlertModal;
|
|
@ViewChild('deletePendingModal') deletePendingModal: AlertModal;
|
|
@ViewChild('createRoleModal') createRoleModal: AlertModal;
|
|
|
|
constructor(private userRegistryService: UserRegistryService,
|
|
private userManagementService: UserManagementService,
|
|
private router: Router,
|
|
private fb: FormBuilder) {
|
|
}
|
|
|
|
ngOnInit() {
|
|
this.updateLists();
|
|
this.userManagementService.getUserInfo().subscribe(user => {
|
|
if(user) {
|
|
if(!Session.isPortalAdministrator(user) && !Session.isCurator(this.type, user) && !Session.isManager(this.type, this.id, user)) {
|
|
this.router.navigate(['/user-info'], {
|
|
queryParams: {
|
|
"errorCode": LoginErrorCodes.NOT_AUTHORIZED,
|
|
"redirectUrl": this.router.url
|
|
}
|
|
});
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
ngOnChanges(changes: SimpleChanges) {
|
|
if(changes.role) {
|
|
this.updateLists();
|
|
}
|
|
}
|
|
|
|
ngOnDestroy() {
|
|
this.subs.forEach(sub => {
|
|
if (sub instanceof Subscription) {
|
|
sub.unsubscribe();
|
|
}
|
|
});
|
|
}
|
|
|
|
updateLists() {
|
|
this.loadActive = true;
|
|
this.loadPending = true;
|
|
this.subs.push(this.userRegistryService.getActiveEmail(this.type, this.id, this.role).subscribe(users => {
|
|
this.active = users;
|
|
this.loadActive = false;
|
|
this.exists = true;
|
|
}, error => {
|
|
this.active = [];
|
|
if(error.status === 404) {
|
|
this.exists = false;
|
|
}
|
|
this.loadActive = false;
|
|
}));
|
|
this.subs.push(this.userRegistryService.getPending(this.type, this.id, this.role).subscribe(users => {
|
|
this.pending = users;
|
|
this.loadPending = false;
|
|
}, error => {
|
|
this.active = [];
|
|
this.loadPending = false;
|
|
}));
|
|
}
|
|
|
|
openDeleteModal(item: any) {
|
|
if (this.showActive) {
|
|
this.selectedUser = item.email;
|
|
this.deleteModal.alertTitle = 'Delete ' + this.role;
|
|
this.deleteModal.open();
|
|
} else {
|
|
this.selectedUser = item;
|
|
this.deletePendingModal.alertTitle = 'Cancel invitation';
|
|
this.deletePendingModal.open();
|
|
}
|
|
}
|
|
|
|
openInviteModal() {
|
|
this.inviteModal.alertTitle = 'Invite ' + this.role;
|
|
this.inviteModal.okButtonLeft = false;
|
|
this.inviteModal.okButtonText = 'Send';
|
|
this.invited = this.fb.control('', [Validators.required, Validators.email]);
|
|
this.inviteModal.open();
|
|
}
|
|
|
|
openCreateRoleModal() {
|
|
this.createRoleModal.alertTitle = 'Create group';
|
|
this.createRoleModal.okButtonLeft = false;
|
|
this.createRoleModal.okButtonText = 'create';
|
|
this.roleFb = this.fb.group({
|
|
name: this.fb.control(Session.mapType(this.type) + '.' + this.id, Validators.required),
|
|
description: this.fb.control('', Validators.required)
|
|
});
|
|
setTimeout(() => {
|
|
this.roleFb.get('name').disable();
|
|
}, 0);
|
|
this.createRoleModal.open();
|
|
}
|
|
|
|
deleteActive() {
|
|
this.loadActive = true;
|
|
this.userRegistryService.remove(this.type, this.id, this.selectedUser, this.role).subscribe(() => {
|
|
this.active = this.active.filter(user => user.email != this.selectedUser);
|
|
this.userManagementService.updateUserInfo();
|
|
UIkit.notification(this.selectedUser + ' is no longer ' + this.role + ' of ' + this.name + ' Dashboard', {
|
|
status: 'success',
|
|
timeout: 3000,
|
|
pos: 'top-left'
|
|
});
|
|
this.loadActive = false;
|
|
}, error => {
|
|
UIkit.notification('An error has occurred. Please try again later', {
|
|
status: 'danger',
|
|
timeout: 3000,
|
|
pos: 'top-left'
|
|
});
|
|
this.loadActive = false;
|
|
});
|
|
}
|
|
|
|
deletePending() {
|
|
this.loadPending = true;
|
|
this.userRegistryService.cancelInvitation(this.type, this.id, this.selectedUser, this.role).subscribe(() => {
|
|
this.pending = this.pending.filter(user => user != this.selectedUser);
|
|
UIkit.notification(StringUtils.capitalize(this.role) + ' invitation to ' + this.selectedUser + ' has been canceled', {
|
|
status: 'success',
|
|
timeout: 3000,
|
|
pos: 'top-left'
|
|
});
|
|
this.loadPending = false;
|
|
}, error => {
|
|
UIkit.notification('An error has occurred. Please try again later', {
|
|
status: 'danger',
|
|
timeout: 3000,
|
|
pos: 'top-left'
|
|
});
|
|
this.loadPending = false;
|
|
});
|
|
}
|
|
|
|
invite() {
|
|
this.showActive = false;
|
|
this.loadPending = true;
|
|
let details = {
|
|
link: this.link,
|
|
email: Composer.composeEmailForMonitorDashboard(this.name, this.invited.value, this.role)
|
|
}
|
|
this.userRegistryService.invite(this.type, this.id, details, this.role).subscribe(() => {
|
|
if (!this.pending.includes(this.invited.value)) {
|
|
this.pending.push(this.invited.value);
|
|
}
|
|
UIkit.notification(StringUtils.capitalize(this.role) + ' invitation to ' + this.selectedUser + ' has been sent', {
|
|
status: 'success',
|
|
timeout: 3000,
|
|
pos: 'top-left'
|
|
});
|
|
this.loadPending = false;
|
|
}, error => {
|
|
UIkit.notification('An error has occurred. Please try again later', {
|
|
status: 'danger',
|
|
timeout: 3000,
|
|
pos: 'top-left'
|
|
});
|
|
this.loadActive = false;
|
|
})
|
|
}
|
|
|
|
createGroup() {
|
|
this.loadActive = true;
|
|
this.loadPending = true;
|
|
this.roleFb.get('name').enable();
|
|
this.userRegistryService.createRole(this.type, this.id, this.roleFb.value).subscribe(() => {
|
|
UIkit.notification('Group has been created', {
|
|
status: 'success',
|
|
timeout: 3000,
|
|
pos: 'top-left'
|
|
});
|
|
this.updateLists();
|
|
}, error => {
|
|
UIkit.notification('An error has occurred. Please try again later', {
|
|
status: 'danger',
|
|
timeout: 3000,
|
|
pos: 'top-left'
|
|
});
|
|
this.loadActive = false;
|
|
this.loadPending = false;
|
|
});
|
|
}
|
|
}
|