130 lines
4.0 KiB
TypeScript
130 lines
4.0 KiB
TypeScript
import {Component, Input, OnDestroy, OnInit, ViewChild} from '@angular/core';
|
|
import {Subscription} from 'rxjs/Rx';
|
|
import {UserRegistryService} from "../../../services/user-registry.service";
|
|
import {EnvProperties} from "../../../utils/properties/env-properties";
|
|
import {properties} from "../../../../../environments/environment";
|
|
import {FormBuilder, FormControl, Validators} from "@angular/forms";
|
|
import {AlertModal} from "../../../utils/modal/alert";
|
|
|
|
@Component({
|
|
selector: 'members',
|
|
templateUrl: 'members.component.html'
|
|
})
|
|
export class MembersComponent implements OnInit, OnDestroy {
|
|
|
|
@Input()
|
|
public id: string;
|
|
@Input()
|
|
public type: string;
|
|
@Input()
|
|
public name: string;
|
|
@Input()
|
|
public link: string;
|
|
public members: any[];
|
|
public pending: any[];
|
|
public showMembers: boolean = true;
|
|
public subs: any[] = [];
|
|
public loadMembers: boolean = true;
|
|
public loadPending: boolean = true;
|
|
public error: string;
|
|
public selectedUser: string = null;
|
|
public invited: FormControl;
|
|
public properties: EnvProperties = properties;
|
|
@ViewChild('inviteMemberModal') inviteMemberModal: AlertModal;
|
|
@ViewChild('deleteMemberModal') deleteMemberModal: AlertModal;
|
|
@ViewChild('deletePendingModal') deletePendingModal: AlertModal;
|
|
|
|
constructor(private userRegistryService: UserRegistryService,
|
|
private fb: FormBuilder) {
|
|
}
|
|
|
|
ngOnInit() {
|
|
this.subs.push(this.userRegistryService.getMembersEmail(this.type, this.id).subscribe(members => {
|
|
this.members = members;
|
|
this.loadMembers = false;
|
|
}, error => {
|
|
this.members = [];
|
|
this.error = error.error.response;
|
|
this.loadMembers = false;
|
|
}));
|
|
this.subs.push(this.userRegistryService.getPendingMembers(this.type, this.id).subscribe(pending => {
|
|
this.pending = pending;
|
|
this.loadPending = false;
|
|
}, error => {
|
|
this.members = [];
|
|
this.error = error.error.response;
|
|
this.loadMembers = false;
|
|
}));
|
|
}
|
|
|
|
ngOnDestroy() {
|
|
this.subs.forEach(sub => {
|
|
if (sub instanceof Subscription) {
|
|
sub.unsubscribe();
|
|
}
|
|
});
|
|
}
|
|
|
|
openDeleteModal(item: any) {
|
|
if (this.showMembers) {
|
|
this.selectedUser = item.email;
|
|
this.deleteMemberModal.alertTitle = 'Delete member';
|
|
this.deleteMemberModal.open();
|
|
} else {
|
|
this.selectedUser = item;
|
|
this.deletePendingModal.alertTitle = 'Cancel invitation';
|
|
this.deletePendingModal.open();
|
|
}
|
|
}
|
|
|
|
openInviteModal() {
|
|
this.inviteMemberModal.alertTitle = 'Invite member';
|
|
this.inviteMemberModal.okButtonText = 'Send';
|
|
this.inviteMemberModal.okButtonLeft = false;
|
|
this.invited = this.fb.control('', [Validators.required, Validators.email]);
|
|
this.inviteMemberModal.open();
|
|
}
|
|
|
|
deleteMember() {
|
|
this.loadMembers = true;
|
|
this.userRegistryService.removeMember(this.type, this.id, this.selectedUser).subscribe(() => {
|
|
this.members = this.members.filter(manager => manager.email != this.selectedUser);
|
|
this.loadMembers = false;
|
|
this.error = null;
|
|
}, error => {
|
|
this.error = error.error.response;
|
|
this.loadMembers = false;
|
|
});
|
|
}
|
|
|
|
deletePendingMember() {
|
|
this.loadPending = true;
|
|
this.userRegistryService.cancelMemberInvitation(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;
|
|
});
|
|
}
|
|
|
|
inviteMember() {
|
|
this.loadMembers = true;
|
|
let details = {
|
|
name: this.name,
|
|
link: this.link
|
|
}
|
|
this.userRegistryService.inviteMember(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.loadMembers = false;
|
|
}, error => {
|
|
this.error = error.error.response;
|
|
this.loadMembers = false;
|
|
})
|
|
}
|
|
}
|