2018-07-06 21:01:23 +02:00
|
|
|
import {
|
|
|
|
Component,
|
2018-07-10 20:46:13 +02:00
|
|
|
ComponentFactoryResolver,
|
2018-07-06 21:01:23 +02:00
|
|
|
OnInit,
|
2018-07-10 20:46:13 +02:00
|
|
|
ViewChild,
|
|
|
|
ViewContainerRef,
|
2018-07-06 21:01:23 +02:00
|
|
|
} from '@angular/core';
|
|
|
|
import { ActivatedRoute } from '@angular/router';
|
|
|
|
|
2018-07-10 20:46:13 +02:00
|
|
|
import { ToasterService } from 'angular2-toaster';
|
|
|
|
import { Angulartics2 } from 'angulartics2';
|
|
|
|
|
2018-07-06 21:01:23 +02:00
|
|
|
import { ApiService } from 'jslib/abstractions/api.service';
|
|
|
|
import { I18nService } from 'jslib/abstractions/i18n.service';
|
2018-07-10 20:46:13 +02:00
|
|
|
import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service';
|
2018-07-06 21:01:23 +02:00
|
|
|
|
|
|
|
import { OrganizationUserUserDetailsResponse } from 'jslib/models/response/organizationUserResponse';
|
|
|
|
|
2018-07-06 21:45:35 +02:00
|
|
|
import { OrganizationUserStatusType } from 'jslib/enums/organizationUserStatusType';
|
|
|
|
import { OrganizationUserType } from 'jslib/enums/organizationUserType';
|
|
|
|
|
2018-07-06 21:01:23 +02:00
|
|
|
import { Utils } from 'jslib/misc/utils';
|
2018-07-06 16:21:08 +02:00
|
|
|
|
2018-07-10 20:46:13 +02:00
|
|
|
import { ModalComponent } from '../../modal.component';
|
|
|
|
import { UserAddEditComponent } from './user-add-edit.component';
|
|
|
|
|
2018-07-06 16:21:08 +02:00
|
|
|
@Component({
|
|
|
|
selector: 'app-org-people',
|
|
|
|
templateUrl: 'people.component.html',
|
|
|
|
})
|
2018-07-06 21:01:23 +02:00
|
|
|
export class PeopleComponent implements OnInit {
|
2018-07-10 20:46:13 +02:00
|
|
|
@ViewChild('addEdit', { read: ViewContainerRef }) addEditModalRef: ViewContainerRef;
|
|
|
|
@ViewChild('groupsTemplate', { read: ViewContainerRef }) groupsModalRef: ViewContainerRef;
|
|
|
|
|
2018-07-06 21:01:23 +02:00
|
|
|
loading = true;
|
|
|
|
organizationId: string;
|
|
|
|
users: OrganizationUserUserDetailsResponse[];
|
|
|
|
searchText: string;
|
2018-07-06 21:45:35 +02:00
|
|
|
organizationUserType = OrganizationUserType;
|
|
|
|
organizationUserStatusType = OrganizationUserStatusType;
|
2018-07-06 21:01:23 +02:00
|
|
|
|
2018-07-10 20:46:13 +02:00
|
|
|
private modal: ModalComponent = null;
|
|
|
|
|
2018-07-06 21:01:23 +02:00
|
|
|
constructor(private apiService: ApiService, private route: ActivatedRoute,
|
2018-07-10 20:46:13 +02:00
|
|
|
private i18nService: I18nService, private componentFactoryResolver: ComponentFactoryResolver,
|
|
|
|
private platformUtilsService: PlatformUtilsService, private analytics: Angulartics2,
|
|
|
|
private toasterService: ToasterService) { }
|
2018-07-06 21:01:23 +02:00
|
|
|
|
|
|
|
async ngOnInit() {
|
|
|
|
this.route.parent.parent.params.subscribe(async (params) => {
|
|
|
|
this.organizationId = params.organizationId;
|
2018-07-07 05:08:10 +02:00
|
|
|
await this.load();
|
2018-07-06 21:01:23 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async load() {
|
|
|
|
const response = await this.apiService.getOrganizationUsers(this.organizationId);
|
|
|
|
const users = response.data != null && response.data.length > 0 ? response.data : [];
|
|
|
|
users.sort(Utils.getSortFunction(this.i18nService, 'email'));
|
|
|
|
this.users = users;
|
|
|
|
this.loading = false;
|
|
|
|
}
|
2018-07-09 23:07:13 +02:00
|
|
|
|
|
|
|
edit(user: OrganizationUserUserDetailsResponse) {
|
2018-07-10 20:46:13 +02:00
|
|
|
if (this.modal != null) {
|
|
|
|
this.modal.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
const factory = this.componentFactoryResolver.resolveComponentFactory(ModalComponent);
|
|
|
|
this.modal = this.addEditModalRef.createComponent(factory).instance;
|
|
|
|
const childComponent = this.modal.show<UserAddEditComponent>(
|
|
|
|
UserAddEditComponent, this.addEditModalRef);
|
|
|
|
|
|
|
|
childComponent.name = user != null ? user.name || user.email : null;
|
|
|
|
childComponent.organizationId = this.organizationId;
|
|
|
|
childComponent.organizationUserId = user != null ? user.id : null;
|
|
|
|
childComponent.onSavedUser.subscribe(() => {
|
|
|
|
this.modal.close();
|
|
|
|
this.load();
|
|
|
|
});
|
|
|
|
childComponent.onDeletedUser.subscribe(() => {
|
|
|
|
this.modal.close();
|
|
|
|
this.removeUser(user);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.modal.onClosed.subscribe(() => {
|
|
|
|
this.modal = null;
|
|
|
|
});
|
2018-07-09 23:07:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
invite() {
|
2018-07-10 20:46:13 +02:00
|
|
|
this.edit(null);
|
2018-07-09 23:07:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async remove(user: OrganizationUserUserDetailsResponse) {
|
2018-07-10 20:46:13 +02:00
|
|
|
const confirmed = await this.platformUtilsService.showDialog(
|
|
|
|
this.i18nService.t('removeUserConfirmation'), user.name || user.email,
|
|
|
|
this.i18nService.t('yes'), this.i18nService.t('no'), 'warning');
|
|
|
|
if (!confirmed) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await this.apiService.deleteOrganizationUser(this.organizationId, user.id);
|
|
|
|
this.analytics.eventTrack.next({ action: 'Deleted User' });
|
|
|
|
this.toasterService.popAsync('success', null, this.i18nService.t('removedUserId', user.name || user.email));
|
|
|
|
this.removeUser(user);
|
|
|
|
} catch { }
|
|
|
|
}
|
|
|
|
|
|
|
|
private removeUser(user: OrganizationUserUserDetailsResponse) {
|
|
|
|
const index = this.users.indexOf(user);
|
|
|
|
if (index > -1) {
|
|
|
|
this.users.splice(index, 1);
|
|
|
|
}
|
2018-07-09 23:07:13 +02:00
|
|
|
}
|
2018-07-06 21:01:23 +02:00
|
|
|
}
|