2020-01-20 14:57:55 +01:00
|
|
|
import {
|
2021-08-25 16:10:17 +02:00
|
|
|
ChangeDetectorRef,
|
2020-01-20 14:57:55 +01:00
|
|
|
Component,
|
2021-08-25 16:10:17 +02:00
|
|
|
ComponentFactoryResolver,
|
2020-01-20 14:57:55 +01:00
|
|
|
EventEmitter,
|
|
|
|
Input,
|
|
|
|
Output,
|
2021-08-25 16:10:17 +02:00
|
|
|
ViewChild,
|
|
|
|
ViewContainerRef,
|
2020-01-20 14:57:55 +01:00
|
|
|
} from "@angular/core";
|
|
|
|
|
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-12-07 20:41:45 +01:00
|
|
|
import { PlatformUtilsService } from "jslib-common/abstractions/platformUtils.service";
|
2020-01-20 14:57:55 +01:00
|
|
|
|
2021-06-07 20:13:58 +02:00
|
|
|
import { PolicyType } from "jslib-common/enums/policyType";
|
2020-01-20 14:57:55 +01:00
|
|
|
|
2021-06-07 20:13:58 +02:00
|
|
|
import { PolicyRequest } from "jslib-common/models/request/policyRequest";
|
2020-01-20 14:57:55 +01:00
|
|
|
|
2021-06-07 20:13:58 +02:00
|
|
|
import { PolicyResponse } from "jslib-common/models/response/policyResponse";
|
2020-01-20 14:57:55 +01:00
|
|
|
|
2021-08-25 16:10:17 +02:00
|
|
|
import { BasePolicy, BasePolicyComponent } from "../policies/base-policy.component";
|
|
|
|
|
2020-01-20 14:57:55 +01:00
|
|
|
@Component({
|
|
|
|
selector: "app-policy-edit",
|
|
|
|
templateUrl: "policy-edit.component.html",
|
|
|
|
})
|
2021-08-25 16:10:17 +02:00
|
|
|
export class PolicyEditComponent {
|
|
|
|
@Input() policy: BasePolicy;
|
2020-01-20 14:57:55 +01:00
|
|
|
@Input() organizationId: string;
|
2020-10-26 17:56:02 +01:00
|
|
|
@Input() policiesEnabledMap: Map<PolicyType, boolean> = new Map<PolicyType, boolean>();
|
2020-01-20 14:57:55 +01:00
|
|
|
@Output() onSavedPolicy = new EventEmitter();
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2021-08-25 16:10:17 +02:00
|
|
|
@ViewChild("policyForm", { read: ViewContainerRef, static: true })
|
|
|
|
policyFormRef: ViewContainerRef;
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2020-01-29 23:49:20 +01:00
|
|
|
policyType = PolicyType;
|
2020-01-20 14:57:55 +01:00
|
|
|
loading = true;
|
|
|
|
enabled = false;
|
|
|
|
formPromise: Promise<any>;
|
2020-03-11 16:35:12 +01:00
|
|
|
defaultTypes: any[];
|
2021-08-25 16:10:17 +02:00
|
|
|
policyComponent: BasePolicyComponent;
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2021-08-25 16:10:17 +02:00
|
|
|
private policyResponse: PolicyResponse;
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2020-01-20 14:57:55 +01:00
|
|
|
constructor(
|
|
|
|
private apiService: ApiService,
|
|
|
|
private i18nService: I18nService,
|
2021-12-07 20:41:45 +01:00
|
|
|
private platformUtilsService: PlatformUtilsService,
|
|
|
|
private componentFactoryResolver: ComponentFactoryResolver,
|
2021-10-20 18:30:04 +02:00
|
|
|
private cdr: ChangeDetectorRef,
|
|
|
|
private logService: LogService
|
2021-12-17 15:57:11 +01:00
|
|
|
) {}
|
|
|
|
|
2021-08-25 16:10:17 +02:00
|
|
|
async ngAfterViewInit() {
|
2020-01-20 14:57:55 +01:00
|
|
|
await this.load();
|
|
|
|
this.loading = false;
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2021-08-25 16:10:17 +02:00
|
|
|
const factory = this.componentFactoryResolver.resolveComponentFactory(this.policy.component);
|
|
|
|
this.policyComponent = this.policyFormRef.createComponent(factory)
|
|
|
|
.instance as BasePolicyComponent;
|
|
|
|
this.policyComponent.policy = this.policy;
|
|
|
|
this.policyComponent.policyResponse = this.policyResponse;
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2021-08-25 16:10:17 +02:00
|
|
|
this.cdr.detectChanges();
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
|
2020-01-20 14:57:55 +01:00
|
|
|
async load() {
|
2021-12-17 15:57:11 +01:00
|
|
|
try {
|
2021-08-25 16:10:17 +02:00
|
|
|
this.policyResponse = await this.apiService.getPolicy(this.organizationId, this.policy.type);
|
2021-10-20 18:30:04 +02:00
|
|
|
} catch (e) {
|
2020-01-20 14:57:55 +01:00
|
|
|
if (e.statusCode === 404) {
|
2021-08-25 16:10:17 +02:00
|
|
|
this.policyResponse = new PolicyResponse({ Enabled: false });
|
2021-12-17 15:57:11 +01:00
|
|
|
} else {
|
|
|
|
throw e;
|
|
|
|
}
|
2020-03-03 22:37:54 +01:00
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
|
2021-08-25 16:10:17 +02:00
|
|
|
async submit() {
|
|
|
|
let request: PolicyRequest;
|
|
|
|
try {
|
|
|
|
request = await this.policyComponent.buildRequest(this.policiesEnabledMap);
|
2020-01-20 14:57:55 +01:00
|
|
|
} catch (e) {
|
2021-08-25 16:10:17 +02:00
|
|
|
this.platformUtilsService.showToast("error", null, e);
|
|
|
|
return;
|
2020-01-20 14:57:55 +01:00
|
|
|
}
|
|
|
|
|
2021-08-25 16:10:17 +02:00
|
|
|
try {
|
|
|
|
this.formPromise = this.apiService.putPolicy(this.organizationId, this.policy.type, request);
|
|
|
|
await this.formPromise;
|
2021-12-07 20:41:45 +01:00
|
|
|
this.platformUtilsService.showToast(
|
|
|
|
"success",
|
|
|
|
null,
|
|
|
|
this.i18nService.t("editedPolicyId", this.i18nService.t(this.policy.name))
|
|
|
|
);
|
2021-08-25 16:10:17 +02:00
|
|
|
this.onSavedPolicy.emit();
|
2021-10-20 18:30:04 +02:00
|
|
|
} catch (e) {
|
|
|
|
this.logService.error(e);
|
2020-01-20 14:57:55 +01:00
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2020-01-20 14:57:55 +01:00
|
|
|
}
|