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

129 lines
4.1 KiB
TypeScript

import {Component, Input, OnDestroy, OnInit, ViewChild} from '@angular/core';
import {Subscription} from 'rxjs/Rx';
import {FormBuilder, FormControl, 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";
@Component({
selector: 'managers',
templateUrl: 'managers.component.html'
})
export class ManagersComponent implements OnInit, OnDestroy {
@Input()
public id: string;
@Input()
public type: string;
@Input()
public name: string;
@Input()
public link: string;
public managers: any[];
public pending: any[];
public showManagers: boolean = true;
public subs: any[] = [];
public loadManagers: boolean = true;
public loadPending: boolean = true;
public error: string;
public selectedUser: string = null;
public invited: FormControl;
public properties: EnvProperties = properties;
@ViewChild('inviteManagerModal') inviteManagerModal: AlertModal;
@ViewChild('deleteManagerModal') deleteManagerModal: AlertModal;
@ViewChild('deletePendingModal') deletePendingModal: AlertModal;
constructor(private userRegistryService: UserRegistryService,
private fb: FormBuilder) {
}
ngOnInit() {
this.subs.push(this.userRegistryService.getManagersEmail(this.type, this.id).subscribe(managers => {
this.managers = managers;
this.loadManagers = false;
}, error => {
this.managers = [];
this.error = error.error.response;
this.loadManagers = false;
}));
this.subs.push(this.userRegistryService.getPendingManagers(this.type, this.id).subscribe(pending => {
this.pending = pending;
this.loadPending = false;
}, error => {
this.managers = [];
this.error = error.error.response;
this.loadManagers = false;
}));
}
ngOnDestroy() {
this.subs.forEach(sub => {
if (sub instanceof Subscription) {
sub.unsubscribe();
}
});
}
openDeleteModal(item: any) {
if (this.showManagers) {
this.selectedUser = item.email;
this.deleteManagerModal.alertTitle = 'Delete ' + item.email + ' from managers';
this.deleteManagerModal.open();
} else {
this.selectedUser = item;
this.deletePendingModal.alertTitle = 'Cancel invitation for user ' + item;
this.deletePendingModal.open();
}
}
openInviteModal() {
this.inviteManagerModal.alertTitle = 'Invite user to join ' + this.name + ' as manager';
this.inviteManagerModal.okButtonLeft = false;
this.invited = this.fb.control('', [Validators.required, Validators.email]);
this.inviteManagerModal.open();
}
deleteManager() {
this.loadManagers = true;
this.userRegistryService.removeManager(this.type, this.id, this.selectedUser).subscribe(() => {
this.managers = this.managers.filter(manager => manager.email != this.selectedUser);
this.loadManagers = false;
this.error = null;
}, error => {
this.error = error.error.response;
this.loadManagers = false;
});
}
deletePendingManager() {
this.loadPending = true;
this.userRegistryService.cancelManagerInvitation(this.type, this.id, this.selectedUser).subscribe(() => {
this.pending = this.pending.filter(manager => manager != this.selectedUser);
this.error = null;
this.loadPending = false;
}, error => {
this.error = error.error.response;
this.loadPending = false;
});
}
inviteManager() {
this.loadManagers = true;
let details = {
name: this.name,
link: this.link
}
this.userRegistryService.inviteManager(this.type, this.id, this.invited.value, details).subscribe(() => {
this.error = null;
if (!this.pending.includes(this.invited.value)) {
this.pending.push(this.invited.value);
}
this.loadManagers = false;
}, error => {
this.error = error.error.response;
this.loadManagers = false;
})
}
}