2018-07-06 18:49:06 +02:00
|
|
|
import {
|
|
|
|
Component,
|
|
|
|
OnInit,
|
2018-07-09 22:27:54 +02:00
|
|
|
ViewChild,
|
|
|
|
ViewContainerRef,
|
2018-07-06 18:49:06 +02:00
|
|
|
} from '@angular/core';
|
2018-07-10 15:19:29 +02:00
|
|
|
import {
|
|
|
|
ActivatedRoute,
|
|
|
|
Router,
|
|
|
|
} from '@angular/router';
|
2018-07-06 18:49:06 +02:00
|
|
|
|
2021-10-15 00:59:43 +02:00
|
|
|
import { first } from 'rxjs/operators';
|
|
|
|
|
2018-07-09 22:27:54 +02:00
|
|
|
import { ToasterService } from 'angular2-toaster';
|
|
|
|
|
2021-06-07 20:13:58 +02:00
|
|
|
import { ApiService } from 'jslib-common/abstractions/api.service';
|
|
|
|
import { I18nService } from 'jslib-common/abstractions/i18n.service';
|
2021-10-20 18:30:04 +02:00
|
|
|
import { LogService } from 'jslib-common/abstractions/log.service';
|
2021-06-07 20:13:58 +02:00
|
|
|
import { PlatformUtilsService } from 'jslib-common/abstractions/platformUtils.service';
|
|
|
|
import { SearchService } from 'jslib-common/abstractions/search.service';
|
|
|
|
import { UserService } from 'jslib-common/abstractions/user.service';
|
2018-07-06 18:49:06 +02:00
|
|
|
|
2021-08-27 14:50:58 +02:00
|
|
|
import { ModalService } from 'jslib-angular/services/modal.service';
|
|
|
|
|
2021-06-07 20:13:58 +02:00
|
|
|
import { GroupResponse } from 'jslib-common/models/response/groupResponse';
|
2018-07-06 16:21:08 +02:00
|
|
|
|
2021-06-07 20:13:58 +02:00
|
|
|
import { Utils } from 'jslib-common/misc/utils';
|
2018-07-06 21:01:23 +02:00
|
|
|
|
2018-07-10 05:48:26 +02:00
|
|
|
import { EntityUsersComponent } from './entity-users.component';
|
2018-07-09 22:27:54 +02:00
|
|
|
import { GroupAddEditComponent } from './group-add-edit.component';
|
|
|
|
|
2018-07-06 16:21:08 +02:00
|
|
|
@Component({
|
|
|
|
selector: 'app-org-groups',
|
|
|
|
templateUrl: 'groups.component.html',
|
|
|
|
})
|
2018-07-06 18:49:06 +02:00
|
|
|
export class GroupsComponent implements OnInit {
|
2020-08-17 16:04:38 +02:00
|
|
|
@ViewChild('addEdit', { read: ViewContainerRef, static: true }) addEditModalRef: ViewContainerRef;
|
|
|
|
@ViewChild('usersTemplate', { read: ViewContainerRef, static: true }) usersModalRef: ViewContainerRef;
|
2018-07-09 22:27:54 +02:00
|
|
|
|
2018-07-06 18:49:06 +02:00
|
|
|
loading = true;
|
|
|
|
organizationId: string;
|
|
|
|
groups: GroupResponse[];
|
2020-05-22 18:26:43 +02:00
|
|
|
pagedGroups: GroupResponse[];
|
2018-07-06 18:49:06 +02:00
|
|
|
searchText: string;
|
|
|
|
|
2020-05-22 18:26:43 +02:00
|
|
|
protected didScroll = false;
|
|
|
|
protected pageSize = 100;
|
|
|
|
|
|
|
|
private pagedGroupsCount = 0;
|
2018-07-09 22:27:54 +02:00
|
|
|
|
2018-07-06 20:22:20 +02:00
|
|
|
constructor(private apiService: ApiService, private route: ActivatedRoute,
|
2021-08-27 14:50:58 +02:00
|
|
|
private i18nService: I18nService, private modalService: ModalService,
|
2021-04-14 23:43:40 +02:00
|
|
|
private toasterService: ToasterService, private platformUtilsService: PlatformUtilsService,
|
|
|
|
private userService: UserService, private router: Router,
|
2021-10-20 18:30:04 +02:00
|
|
|
private searchService: SearchService, private logService: LogService) { }
|
2018-07-06 18:49:06 +02:00
|
|
|
|
|
|
|
async ngOnInit() {
|
2021-02-03 18:41:33 +01:00
|
|
|
this.route.parent.parent.params.subscribe(async params => {
|
2018-07-06 18:49:06 +02:00
|
|
|
this.organizationId = params.organizationId;
|
2018-07-10 15:19:29 +02:00
|
|
|
const organization = await this.userService.getOrganization(this.organizationId);
|
|
|
|
if (organization == null || !organization.useGroups) {
|
|
|
|
this.router.navigate(['/organizations', this.organizationId]);
|
|
|
|
return;
|
|
|
|
}
|
2018-07-07 05:08:10 +02:00
|
|
|
await this.load();
|
2021-10-15 00:59:43 +02:00
|
|
|
this.route.queryParams.pipe(first()).subscribe(async qParams => {
|
2018-07-11 21:44:40 +02:00
|
|
|
this.searchText = qParams.search;
|
|
|
|
});
|
2018-07-06 18:49:06 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async load() {
|
|
|
|
const response = await this.apiService.getGroups(this.organizationId);
|
2018-07-06 20:22:20 +02:00
|
|
|
const groups = response.data != null && response.data.length > 0 ? response.data : [];
|
2018-07-06 21:01:23 +02:00
|
|
|
groups.sort(Utils.getSortFunction(this.i18nService, 'name'));
|
2018-07-06 20:22:20 +02:00
|
|
|
this.groups = groups;
|
2020-05-22 18:26:43 +02:00
|
|
|
this.resetPaging();
|
2018-07-06 18:49:06 +02:00
|
|
|
this.loading = false;
|
|
|
|
}
|
2018-07-09 22:27:54 +02:00
|
|
|
|
2020-05-22 18:26:43 +02:00
|
|
|
loadMore() {
|
2020-07-28 15:55:59 +02:00
|
|
|
if (!this.groups || this.groups.length <= this.pageSize) {
|
2020-05-22 18:26:43 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const pagedLength = this.pagedGroups.length;
|
|
|
|
let pagedSize = this.pageSize;
|
|
|
|
if (pagedLength === 0 && this.pagedGroupsCount > this.pageSize) {
|
|
|
|
pagedSize = this.pagedGroupsCount;
|
|
|
|
}
|
|
|
|
if (this.groups.length > pagedLength) {
|
|
|
|
this.pagedGroups = this.pagedGroups.concat(this.groups.slice(pagedLength, pagedLength + pagedSize));
|
|
|
|
}
|
|
|
|
this.pagedGroupsCount = this.pagedGroups.length;
|
|
|
|
this.didScroll = this.pagedGroups.length > this.pageSize;
|
|
|
|
}
|
|
|
|
|
2021-08-27 14:50:58 +02:00
|
|
|
async edit(group: GroupResponse) {
|
|
|
|
const [modal] = await this.modalService.openViewRef(GroupAddEditComponent, this.addEditModalRef, comp => {
|
|
|
|
comp.organizationId = this.organizationId;
|
|
|
|
comp.groupId = group != null ? group.id : null;
|
|
|
|
comp.onSavedGroup.subscribe(() => {
|
|
|
|
modal.close();
|
|
|
|
this.load();
|
|
|
|
});
|
|
|
|
comp.onDeletedGroup.subscribe(() => {
|
|
|
|
modal.close();
|
|
|
|
this.removeGroup(group);
|
|
|
|
});
|
2018-07-09 22:27:54 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
add() {
|
|
|
|
this.edit(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
async delete(group: GroupResponse) {
|
|
|
|
const confirmed = await this.platformUtilsService.showDialog(
|
|
|
|
this.i18nService.t('deleteGroupConfirmation'), group.name,
|
|
|
|
this.i18nService.t('yes'), this.i18nService.t('no'), 'warning');
|
|
|
|
if (!confirmed) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await this.apiService.deleteGroup(this.organizationId, group.id);
|
2018-07-10 14:39:05 +02:00
|
|
|
this.toasterService.popAsync('success', null, this.i18nService.t('deletedGroupId', group.name));
|
2018-07-10 16:06:57 +02:00
|
|
|
this.removeGroup(group);
|
2021-10-20 18:30:04 +02:00
|
|
|
} catch (e) {
|
|
|
|
this.logService.error(e);
|
|
|
|
}
|
2018-07-09 22:27:54 +02:00
|
|
|
}
|
2018-07-10 05:48:26 +02:00
|
|
|
|
2021-08-27 14:50:58 +02:00
|
|
|
async users(group: GroupResponse) {
|
|
|
|
const [modal] = await this.modalService.openViewRef(EntityUsersComponent, this.usersModalRef, comp => {
|
|
|
|
comp.organizationId = this.organizationId;
|
|
|
|
comp.entity = 'group';
|
|
|
|
comp.entityId = group.id;
|
|
|
|
comp.entityName = group.name;
|
2018-07-10 05:48:26 +02:00
|
|
|
|
2021-08-27 14:50:58 +02:00
|
|
|
comp.onEditedUsers.subscribe(() => {
|
|
|
|
modal.close();
|
|
|
|
});
|
2018-07-10 05:48:26 +02:00
|
|
|
});
|
|
|
|
}
|
2018-07-10 16:06:57 +02:00
|
|
|
|
2020-05-22 18:26:43 +02:00
|
|
|
async resetPaging() {
|
|
|
|
this.pagedGroups = [];
|
|
|
|
this.loadMore();
|
|
|
|
}
|
|
|
|
|
|
|
|
isSearching() {
|
|
|
|
return this.searchService.isSearchable(this.searchText);
|
|
|
|
}
|
|
|
|
|
|
|
|
isPaging() {
|
|
|
|
const searching = this.isSearching();
|
|
|
|
if (searching && this.didScroll) {
|
|
|
|
this.resetPaging();
|
|
|
|
}
|
2020-07-28 15:55:59 +02:00
|
|
|
return !searching && this.groups && this.groups.length > this.pageSize;
|
2020-05-22 18:26:43 +02:00
|
|
|
}
|
|
|
|
|
2018-07-10 16:06:57 +02:00
|
|
|
private removeGroup(group: GroupResponse) {
|
|
|
|
const index = this.groups.indexOf(group);
|
|
|
|
if (index > -1) {
|
|
|
|
this.groups.splice(index, 1);
|
2020-05-22 18:26:43 +02:00
|
|
|
this.resetPaging();
|
2018-07-10 16:06:57 +02:00
|
|
|
}
|
|
|
|
}
|
2018-07-06 18:49:06 +02:00
|
|
|
}
|