openaire-library/dashboard/users/subscribers/subscribers.component.ts

185 lines
5.7 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, User} from "../../../login/utils/helper.class";
import {UserManagementService} from "../../../services/user-management.service";
import {Router} from "@angular/router";
import {Composer} from "../../../utils/email/composer";
import {SubscriberInviteComponent} from "../../../sharedComponents/subscriber-invite/subscriber-invite.component";
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;
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;
/* Paging */
page: number = 1;
pageSize: number = 10;
/* Search */
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,
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;
});
}
ngOnChanges(changes: SimpleChanges) {
if (changes.role) {
this.updateList();
}
}
ngOnDestroy() {
this.subs.forEach(sub => {
if (sub instanceof Subscription) {
sub.unsubscribe();
}
});
}
updateList() {
this.loading = true;
this.subs.push(this.userRegistryService.getActiveEmail(this.type, this.id, 'member').subscribe(users => {
this.subscribers = users;
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({
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();
}
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);
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);
}
public updatePage($event) {
this.page = $event.value;
}
private filterBySearch(value: any) {
this.showSubscribers = this.subscribers.filter(subscriber => !value || subscriber.email.includes(value));
}
}