2021-02-02 17:48:48 +01:00
|
|
|
import {
|
|
|
|
ChangeDetectorRef,
|
|
|
|
Component,
|
|
|
|
Input,
|
|
|
|
OnChanges,
|
|
|
|
OnDestroy,
|
|
|
|
OnInit,
|
|
|
|
SimpleChanges,
|
|
|
|
ViewChild
|
|
|
|
} from '@angular/core';
|
2021-04-08 16:35:39 +02:00
|
|
|
import {AbstractControl, FormBuilder, FormGroup, Validators} from '@angular/forms';
|
2021-01-21 16:15:53 +01:00
|
|
|
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";
|
2021-02-04 14:36:29 +01:00
|
|
|
import {Role, Session, User} from "../../../login/utils/helper.class";
|
2021-01-21 16:15:53 +01:00
|
|
|
import {UserManagementService} from "../../../services/user-management.service";
|
|
|
|
import {Router} from "@angular/router";
|
|
|
|
import {SubscriberInviteComponent} from "../../../sharedComponents/subscriber-invite/subscriber-invite.component";
|
2021-07-14 13:19:57 +02:00
|
|
|
import {Subscription, zip} from "rxjs";
|
2021-01-21 16:15:53 +01:00
|
|
|
|
|
|
|
declare var UIkit;
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'subscribers',
|
|
|
|
templateUrl: 'subscribers.component.html'
|
|
|
|
})
|
|
|
|
export class SubscribersComponent implements OnInit, OnDestroy, OnChanges {
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
public id: string;
|
|
|
|
@Input()
|
|
|
|
public type: string;
|
|
|
|
@Input()
|
|
|
|
public name: string;
|
|
|
|
@Input()
|
|
|
|
public link: string;
|
|
|
|
@Input()
|
|
|
|
public message: string = null;
|
2021-05-17 13:28:04 +02:00
|
|
|
@Input()
|
|
|
|
public inviteDisableMessage: string;
|
2021-01-21 16:15:53 +01:00
|
|
|
public user: User = null;
|
2021-03-01 18:32:21 +01:00
|
|
|
public managers: any[];
|
2021-01-21 16:15:53 +01:00
|
|
|
public subscribers: any[];
|
|
|
|
public showSubscribers: any[];
|
|
|
|
public subs: any[] = [];
|
|
|
|
public loading: boolean = true;
|
|
|
|
public selectedUser: string = null;
|
|
|
|
public properties: EnvProperties = properties;
|
|
|
|
public exists: boolean = true;
|
|
|
|
public roleFb: FormGroup;
|
2021-03-26 10:25:39 +01:00
|
|
|
/** Paging */
|
2021-01-21 16:15:53 +01:00
|
|
|
page: number = 1;
|
2021-03-04 18:11:36 +01:00
|
|
|
pageSize: number = 5;
|
2021-03-26 10:25:39 +01:00
|
|
|
/** Search */
|
2021-01-21 16:15:53 +01:00
|
|
|
filterForm: FormGroup;
|
|
|
|
@ViewChild('inviteModal') inviteModal: AlertModal;
|
|
|
|
@ViewChild('deleteModal') deleteModal: AlertModal;
|
|
|
|
@ViewChild('createRoleModal') createRoleModal: AlertModal;
|
|
|
|
@ViewChild('subscriberInvite') subscriberInvite: SubscriberInviteComponent;
|
|
|
|
|
|
|
|
constructor(private userRegistryService: UserRegistryService,
|
|
|
|
private userManagementService: UserManagementService,
|
|
|
|
private router: Router,
|
2021-02-02 17:48:48 +01:00
|
|
|
private cdr: ChangeDetectorRef,
|
2021-01-21 16:15:53 +01:00
|
|
|
private fb: FormBuilder) {
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
this.filterForm = this.fb.group({
|
|
|
|
keyword: this.fb.control('')
|
|
|
|
});
|
|
|
|
this.subs.push(this.filterForm.get('keyword').valueChanges.subscribe(value => {
|
|
|
|
this.filterBySearch(value);
|
|
|
|
}));
|
|
|
|
this.updateList();
|
|
|
|
this.userManagementService.getUserInfo().subscribe(user => {
|
|
|
|
this.user = user;
|
2021-02-02 17:48:48 +01:00
|
|
|
this.cdr.detectChanges();
|
2021-01-21 16:15:53 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnChanges(changes: SimpleChanges) {
|
|
|
|
if (changes.role) {
|
|
|
|
this.updateList();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnDestroy() {
|
|
|
|
this.subs.forEach(sub => {
|
|
|
|
if (sub instanceof Subscription) {
|
|
|
|
sub.unsubscribe();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-08 16:35:39 +02:00
|
|
|
get currentPage(): any[] {
|
|
|
|
if (this.showSubscribers) {
|
|
|
|
return this.showSubscribers.slice((this.page - 1) * this.pageSize, this.page * this.pageSize);
|
|
|
|
} else {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-21 16:15:53 +01:00
|
|
|
updateList() {
|
|
|
|
this.loading = true;
|
2021-03-01 18:32:21 +01:00
|
|
|
let data = zip(this.userRegistryService.getActiveEmail(this.type, this.id, 'manager'),
|
|
|
|
this.userRegistryService.getActiveEmail(this.type, this.id, 'member'));
|
|
|
|
this.subs.push(data.subscribe(users => {
|
|
|
|
this.managers = users[0];
|
|
|
|
this.subscribers = users[1];
|
|
|
|
this.subscribers.forEach(subscriber => {
|
|
|
|
subscriber['isManager'] = this.managers.find(manager => manager.email === subscriber.email);
|
|
|
|
});
|
2021-01-21 16:15:53 +01:00
|
|
|
this.filterBySearch(this.filterForm.value.keyword);
|
|
|
|
this.loading = false;
|
|
|
|
this.exists = true;
|
|
|
|
}, error => {
|
|
|
|
this.subscribers = [];
|
|
|
|
this.showSubscribers = [];
|
|
|
|
if (error.status === 404) {
|
|
|
|
this.exists = false;
|
|
|
|
}
|
|
|
|
this.loading = false;
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
openDeleteModal(item: any) {
|
|
|
|
this.selectedUser = item.email;
|
|
|
|
this.deleteModal.alertTitle = 'Delete subscriber';
|
|
|
|
this.deleteModal.open();
|
|
|
|
}
|
|
|
|
|
|
|
|
openInviteModal() {
|
|
|
|
if(this.subscriberInvite && !this.subscriberInvite.loading) {
|
|
|
|
this.inviteModal.alertTitle = 'Invite users to subscribe';
|
|
|
|
this.inviteModal.okButtonLeft = false;
|
|
|
|
this.inviteModal.okButtonText = 'Send';
|
|
|
|
this.subscriberInvite.reset();
|
|
|
|
this.inviteModal.open();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
openCreateRoleModal() {
|
|
|
|
this.createRoleModal.alertTitle = 'Create group';
|
|
|
|
this.createRoleModal.okButtonLeft = false;
|
|
|
|
this.createRoleModal.okButtonText = 'create';
|
|
|
|
this.roleFb = this.fb.group({
|
2021-02-04 14:36:29 +01:00
|
|
|
name: this.fb.control(Role.mapType(this.type) + '.' + this.id, Validators.required),
|
2021-01-21 16:15:53 +01:00
|
|
|
description: this.fb.control('', Validators.required)
|
|
|
|
});
|
|
|
|
setTimeout(() => {
|
|
|
|
this.roleFb.get('name').disable();
|
|
|
|
}, 0);
|
|
|
|
this.createRoleModal.open();
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteSubscriber() {
|
|
|
|
this.loading = true;
|
|
|
|
this.userRegistryService.remove(this.type, this.id, this.selectedUser, 'member').subscribe(() => {
|
|
|
|
this.subscribers = this.subscribers.filter(user => user.email != this.selectedUser);
|
|
|
|
this.filterBySearch(this.filterForm.value.keyword);
|
2021-04-08 16:35:39 +02:00
|
|
|
if(this.currentPage.length === 0) {
|
|
|
|
this.page = 1;
|
|
|
|
}
|
2021-01-21 16:15:53 +01:00
|
|
|
this.userManagementService.updateUserInfo();
|
|
|
|
UIkit.notification(this.selectedUser + ' <b>is no longer</b> subscribed to ' + this.name + ' Dashboard', {
|
|
|
|
status: 'success',
|
|
|
|
timeout: 6000,
|
|
|
|
pos: 'bottom-right'
|
|
|
|
});
|
|
|
|
this.loading = false;
|
|
|
|
}, error => {
|
|
|
|
UIkit.notification('An error has occurred. Please try again later', {
|
|
|
|
status: 'danger',
|
|
|
|
timeout: 6000,
|
|
|
|
pos: 'bottom-right'
|
|
|
|
});
|
|
|
|
this.loading = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
createGroup() {
|
|
|
|
this.loading = true;
|
|
|
|
this.roleFb.get('name').enable();
|
|
|
|
this.userRegistryService.createRole(this.type, this.id, this.roleFb.value).subscribe(() => {
|
|
|
|
UIkit.notification('Group has been <b> successfully created</b>', {
|
|
|
|
status: 'success',
|
|
|
|
timeout: 6000,
|
|
|
|
pos: 'bottom-right'
|
|
|
|
});
|
|
|
|
this.updateList();
|
|
|
|
}, error => {
|
|
|
|
UIkit.notification('An error has occurred. Please try again later', {
|
|
|
|
status: 'danger',
|
|
|
|
timeout: 6000,
|
|
|
|
pos: 'bottom-right'
|
|
|
|
});
|
|
|
|
this.loading = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public get isPortalAdmin() {
|
|
|
|
return Session.isPortalAdministrator(this.user) || Session.isCurator(this.type, this.user);
|
|
|
|
}
|
|
|
|
|
2021-03-28 13:07:48 +02:00
|
|
|
public updatePage(event) {
|
|
|
|
this.page = event.value;
|
2021-01-21 16:15:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private filterBySearch(value: any) {
|
|
|
|
this.showSubscribers = this.subscribers.filter(subscriber => !value || subscriber.email.includes(value));
|
2021-03-26 10:25:39 +01:00
|
|
|
this.page = 1;
|
2021-01-21 16:15:53 +01:00
|
|
|
}
|
|
|
|
}
|