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';
|
|
|
|
|
|
|
|
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';
|
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-08-25 16:10:17 +02:00
|
|
|
@ViewChild('policyForm', { read: ViewContainerRef, static: true }) policyFormRef: ViewContainerRef;
|
|
|
|
|
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;
|
2020-01-20 14:57:55 +01:00
|
|
|
|
2021-08-25 16:10:17 +02:00
|
|
|
private policyResponse: PolicyResponse;
|
2020-01-20 14:57:55 +01:00
|
|
|
|
|
|
|
constructor(private apiService: ApiService, private i18nService: I18nService,
|
2021-08-25 16:10:17 +02:00
|
|
|
private toasterService: ToasterService, private componentFactoryResolver: ComponentFactoryResolver,
|
|
|
|
private cdr: ChangeDetectorRef) {
|
2020-03-03 22:37:54 +01:00
|
|
|
}
|
2020-01-20 14:57:55 +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-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;
|
|
|
|
|
|
|
|
this.cdr.detectChanges();
|
2020-01-20 14:57:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async load() {
|
|
|
|
try {
|
2021-08-25 16:10:17 +02:00
|
|
|
this.policyResponse = await this.apiService.getPolicy(this.organizationId, this.policy.type);
|
2020-01-20 14:57:55 +01:00
|
|
|
} catch (e) {
|
|
|
|
if (e.statusCode === 404) {
|
2021-08-25 16:10:17 +02:00
|
|
|
this.policyResponse = new PolicyResponse({Enabled: false});
|
2020-01-20 14:57:55 +01:00
|
|
|
} else {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async submit() {
|
2021-08-25 16:10:17 +02:00
|
|
|
let request: PolicyRequest;
|
|
|
|
try {
|
|
|
|
request = await this.policyComponent.buildRequest(this.policiesEnabledMap);
|
|
|
|
} catch (e) {
|
|
|
|
this.toasterService.pop('error', null, e);
|
|
|
|
return;
|
2020-10-26 17:56:02 +01:00
|
|
|
}
|
2021-01-13 00:13:59 +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;
|
|
|
|
this.toasterService.popAsync('success', null, this.i18nService.t('editedPolicyId', this.i18nService.t(this.policy.name)));
|
|
|
|
this.onSavedPolicy.emit();
|
|
|
|
} catch {}
|
2020-01-20 14:57:55 +01:00
|
|
|
}
|
|
|
|
}
|