2021-02-02 17:48:48 +01:00
|
|
|
import {
|
|
|
|
ChangeDetectorRef,
|
2022-06-28 20:13:40 +02:00
|
|
|
Component, EventEmitter,
|
2021-02-02 17:48:48 +01:00
|
|
|
Input,
|
|
|
|
OnChanges,
|
|
|
|
OnDestroy,
|
2022-06-28 20:13:40 +02:00
|
|
|
OnInit, Output,
|
2021-02-02 17:48:48 +01:00
|
|
|
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;
|
|
|
|
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;
|
2022-06-28 20:13:40 +02:00
|
|
|
pageSize: number = 10;
|
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 => {
|
2022-06-28 20:13:40 +02:00
|
|
|
if(this.subscribers) {
|
|
|
|
this.filterBySearch(value);
|
|
|
|
}
|
2021-01-21 16:15:53 +01:00
|
|
|
}));
|
|
|
|
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;
|
2024-01-24 19:13:05 +01:00
|
|
|
this.subs.push( this.userRegistryService.getActive(this.type, this.id, 'member', true).subscribe(users => {
|
2021-09-17 19:13:35 +02:00
|
|
|
this.subscribers = users;
|
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;
|
2022-06-28 20:13:40 +02:00
|
|
|
this.deleteModal.alertTitle = 'Delete member';
|
2021-01-21 16:15:53 +01:00
|
|
|
this.deleteModal.open();
|
|
|
|
}
|
|
|
|
|
|
|
|
openInviteModal() {
|
|
|
|
if(this.subscriberInvite && !this.subscriberInvite.loading) {
|
2022-06-28 20:13:40 +02:00
|
|
|
this.inviteModal.alertTitle = 'Invite users to join ' + this.name;
|
2021-01-21 16:15:53 +01:00
|
|
|
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-09-17 19:13:35 +02:00
|
|
|
description: this.fb.control(Role.mapType(this.type) + ' ' + this.id, Validators.required)
|
2021-01-21 16:15:53 +01:00
|
|
|
});
|
|
|
|
setTimeout(() => {
|
|
|
|
this.roleFb.get('name').disable();
|
2021-09-17 19:13:35 +02:00
|
|
|
this.roleFb.get('description').disable();
|
2021-01-21 16:15:53 +01:00
|
|
|
}, 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();
|
2022-09-27 10:11:25 +02:00
|
|
|
UIkit.notification(this.selectedUser + ' <b>is no longer</b> a member of ' + this.name + ' Dashboard', {
|
2021-01-21 16:15:53 +01:00
|
|
|
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;
|
2021-09-17 19:13:35 +02:00
|
|
|
this.userRegistryService.createRole(this.type, this.id).subscribe(() => {
|
2021-01-21 16:15:53 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|