connect-admin/src/app/pages/managers/managers.component.ts

141 lines
4.8 KiB
TypeScript

import {Component, OnDestroy, OnInit, ViewChild} from '@angular/core';
import {UserRegistryService} from '../../openaireLibrary/services/user-registry.service';
import {Subscription} from 'rxjs/Rx';
import {ActivatedRoute} from '@angular/router';
import {AlertModal} from '../../openaireLibrary/utils/modal/alert';
import {FormBuilder, FormControl, Validators} from '@angular/forms';
import {CommunityService} from '../../openaireLibrary/connect/community/community.service';
import {properties} from '../../../environments/environment';
@Component({
selector: 'managers',
templateUrl: 'managers.component.html'
})
export class ManagersComponent implements OnInit, OnDestroy {
public managers: any[];
public pending: any[];
public communityId: string;
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 communityName: string;
@ViewChild('inviteManagerModal') inviteManagerModal: AlertModal;
@ViewChild('deleteManagerModal') deleteManagerModal: AlertModal;
@ViewChild('deletePendingModal') deletePendingModal: AlertModal;
constructor(private userRegistryService: UserRegistryService,
private communityService: CommunityService,
private fb: FormBuilder,
private route: ActivatedRoute) {
}
ngOnInit() {
this.subs.push(this.route.queryParams.subscribe(params => {
if (params && params['communityId']) {
this.communityId = params['communityId'];
this.subs.push(this.communityService.getCommunity(properties, properties.communityAPI + this.communityId).subscribe(community => {
this.communityName = community.title;
}));
this.subs.push(this.userRegistryService.getManagersEmail('community', this.communityId).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('community', this.communityId).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 = 'Delete ' + item + ' from managers';
this.deletePendingModal.open();
}
}
openInviteModal() {
this.inviteManagerModal.alertTitle = 'Invite user to be manager';
this.inviteManagerModal.okButtonLeft = false;
this.invited = this.fb.control('', [Validators.required, Validators.email]);
this.inviteManagerModal.open();
}
deleteManager() {
this.loadManagers = true;
this.userRegistryService.removeManagerRole('community', this.communityId, 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.cancelInvitation('community', this.communityId, 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.communityName,
link: this.getURL()
}
this.userRegistryService.invite('community', this.communityId, this.invited.value, details).subscribe(invitation => {
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;
})
}
private getURL(): string {
if(properties.environment != "production") {
return 'https://beta.' + this.communityId + ".openaire.eu/verify/";
} else {
return 'https://' + this.communityId + ".openaire.eu/verify/";
}
}
}