2018-07-09 22:27:54 +02:00
|
|
|
import { Component, EventEmitter, Input, OnInit, Output } from "@angular/core";
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2021-06-07 20:13:58 +02:00
|
|
|
import { ApiService } from "jslib-common/abstractions/api.service";
|
|
|
|
import { CollectionService } from "jslib-common/abstractions/collection.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";
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2021-06-07 20:13:58 +02:00
|
|
|
import { CollectionData } from "jslib-common/models/data/collectionData";
|
|
|
|
import { Collection } from "jslib-common/models/domain/collection";
|
|
|
|
import { GroupRequest } from "jslib-common/models/request/groupRequest";
|
|
|
|
import { SelectionReadOnlyRequest } from "jslib-common/models/request/selectionReadOnlyRequest";
|
|
|
|
import { CollectionDetailsResponse } from "jslib-common/models/response/collectionResponse";
|
|
|
|
import { CollectionView } from "jslib-common/models/view/collectionView";
|
2018-07-09 22:27:54 +02:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: "app-group-add-edit",
|
|
|
|
templateUrl: "group-add-edit.component.html",
|
|
|
|
})
|
|
|
|
export class GroupAddEditComponent implements OnInit {
|
|
|
|
@Input() groupId: string;
|
|
|
|
@Input() organizationId: string;
|
|
|
|
@Output() onSavedGroup = new EventEmitter();
|
|
|
|
@Output() onDeletedGroup = new EventEmitter();
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2018-07-09 22:27:54 +02:00
|
|
|
loading = true;
|
|
|
|
editMode: boolean = false;
|
|
|
|
title: string;
|
|
|
|
name: string;
|
|
|
|
externalId: string;
|
2018-07-10 20:46:13 +02:00
|
|
|
access: "all" | "selected" = "selected";
|
2018-07-09 22:27:54 +02:00
|
|
|
collections: CollectionView[] = [];
|
|
|
|
formPromise: Promise<any>;
|
|
|
|
deletePromise: Promise<any>;
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2018-07-09 22:27:54 +02:00
|
|
|
constructor(
|
|
|
|
private apiService: ApiService,
|
|
|
|
private i18nService: I18nService,
|
2021-12-07 20:41:45 +01:00
|
|
|
private collectionService: CollectionService,
|
2021-10-20 18:30:04 +02:00
|
|
|
private platformUtilsService: PlatformUtilsService,
|
|
|
|
private logService: LogService
|
|
|
|
) {}
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2018-07-09 22:27:54 +02:00
|
|
|
async ngOnInit() {
|
|
|
|
this.editMode = this.loading = this.groupId != null;
|
|
|
|
await this.loadCollections();
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2018-07-09 22:27:54 +02:00
|
|
|
if (this.editMode) {
|
|
|
|
this.editMode = true;
|
|
|
|
this.title = this.i18nService.t("editGroup");
|
|
|
|
try {
|
|
|
|
const group = await this.apiService.getGroupDetails(this.organizationId, this.groupId);
|
|
|
|
this.access = group.accessAll ? "all" : "selected";
|
|
|
|
this.name = group.name;
|
|
|
|
this.externalId = group.externalId;
|
|
|
|
if (group.collections != null && this.collections != null) {
|
2021-02-03 18:41:33 +01:00
|
|
|
group.collections.forEach((s) => {
|
|
|
|
const collection = this.collections.filter((c) => c.id === s.id);
|
2018-07-09 22:27:54 +02:00
|
|
|
if (collection != null && collection.length > 0) {
|
|
|
|
(collection[0] as any).checked = true;
|
|
|
|
collection[0].readOnly = s.readOnly;
|
2020-05-21 15:55:58 +02:00
|
|
|
collection[0].hidePasswords = s.hidePasswords;
|
2021-10-20 18:30:04 +02:00
|
|
|
}
|
2018-07-09 22:27:54 +02:00
|
|
|
});
|
|
|
|
}
|
2021-10-20 18:30:04 +02:00
|
|
|
} catch (e) {
|
2018-07-09 22:27:54 +02:00
|
|
|
this.logService.error(e);
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2018-07-09 22:27:54 +02:00
|
|
|
} else {
|
|
|
|
this.title = this.i18nService.t("addGroup");
|
|
|
|
}
|
|
|
|
|
|
|
|
this.loading = false;
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
|
2018-07-09 22:27:54 +02:00
|
|
|
async loadCollections() {
|
|
|
|
const response = await this.apiService.getCollections(this.organizationId);
|
2021-02-03 18:41:33 +01:00
|
|
|
const collections = response.data.map(
|
2018-07-09 22:27:54 +02:00
|
|
|
(r) => new Collection(new CollectionData(r as CollectionDetailsResponse))
|
|
|
|
);
|
|
|
|
this.collections = await this.collectionService.decryptMany(collections);
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
|
2018-07-09 22:27:54 +02:00
|
|
|
check(c: CollectionView, select?: boolean) {
|
|
|
|
(c as any).checked = select == null ? !(c as any).checked : select;
|
|
|
|
if (!(c as any).checked) {
|
|
|
|
c.readOnly = false;
|
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
|
2018-07-09 22:27:54 +02:00
|
|
|
selectAll(select: boolean) {
|
|
|
|
this.collections.forEach((c) => this.check(c, select));
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
|
2018-07-09 22:27:54 +02:00
|
|
|
async submit() {
|
|
|
|
const request = new GroupRequest();
|
|
|
|
request.name = this.name;
|
|
|
|
request.externalId = this.externalId;
|
|
|
|
request.accessAll = this.access === "all";
|
|
|
|
if (!request.accessAll) {
|
2021-02-03 18:41:33 +01:00
|
|
|
request.collections = this.collections
|
2018-07-09 22:27:54 +02:00
|
|
|
.filter((c) => (c as any).checked)
|
|
|
|
.map((c) => new SelectionReadOnlyRequest(c.id, !!c.readOnly, !!c.hidePasswords));
|
|
|
|
}
|
|
|
|
|
2021-12-17 15:57:11 +01:00
|
|
|
try {
|
2018-07-09 22:27:54 +02:00
|
|
|
if (this.editMode) {
|
2021-02-03 18:41:33 +01:00
|
|
|
this.formPromise = this.apiService.putGroup(this.organizationId, this.groupId, request);
|
|
|
|
} else {
|
2018-07-09 22:27:54 +02:00
|
|
|
this.formPromise = this.apiService.postGroup(this.organizationId, request);
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2018-07-09 22:27:54 +02:00
|
|
|
await this.formPromise;
|
2021-12-07 20:41:45 +01:00
|
|
|
this.platformUtilsService.showToast(
|
2021-12-17 15:57:11 +01:00
|
|
|
"success",
|
2021-02-03 18:41:33 +01:00
|
|
|
null,
|
|
|
|
this.i18nService.t(this.editMode ? "editedGroupId" : "createdGroupId", this.name)
|
2021-12-17 15:57:11 +01:00
|
|
|
);
|
2018-07-09 22:27:54 +02:00
|
|
|
this.onSavedGroup.emit();
|
2021-02-03 18:41:33 +01:00
|
|
|
} catch (e) {
|
2021-10-20 18:30:04 +02:00
|
|
|
this.logService.error(e);
|
2018-07-09 22:27:54 +02:00
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2018-07-09 22:27:54 +02:00
|
|
|
|
|
|
|
async delete() {
|
|
|
|
if (!this.editMode) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const confirmed = await this.platformUtilsService.showDialog(
|
|
|
|
this.i18nService.t("deleteGroupConfirmation"),
|
|
|
|
this.name,
|
|
|
|
this.i18nService.t("yes"),
|
|
|
|
this.i18nService.t("no"),
|
|
|
|
"warning"
|
|
|
|
);
|
|
|
|
if (!confirmed) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
this.deletePromise = this.apiService.deleteGroup(this.organizationId, this.groupId);
|
|
|
|
await this.deletePromise;
|
2021-12-07 20:41:45 +01:00
|
|
|
this.platformUtilsService.showToast(
|
|
|
|
"success",
|
|
|
|
null,
|
|
|
|
this.i18nService.t("deletedGroupId", this.name)
|
|
|
|
);
|
2018-07-09 22:27:54 +02:00
|
|
|
this.onDeletedGroup.emit();
|
2021-10-20 18:30:04 +02:00
|
|
|
} catch (e) {
|
|
|
|
this.logService.error(e);
|
2018-07-09 22:27:54 +02:00
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2018-07-09 22:27:54 +02:00
|
|
|
}
|