File
Methods
Async
executeActions
|
executeActions()
|
|
|
parseActions
|
parseActions()
|
|
|
actionsControl
|
Default value : new FormControl(
`[
{
"type": "REPLACE_ME"
}
]`,
{ nonNullable: true, validators: [Validators.required], updateOn: 'blur' }
)
|
|
Optional
executeActionsButton
|
Type : ElementRef<HTMLButtonElement>
|
Decorators :
@ViewChild('executeActionsButton', {static: true, read: ElementRef})
|
|
isExecuting
|
Default value : false
|
|
registeredActions
|
Type : string[]
|
Default value : []
|
|
import { PfeActionConfig, PfeActionsService } from '@allianz/ngx-pfe';
import { CommonModule } from '@angular/common';
import { Component, DestroyRef, ElementRef, ViewChild, inject } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { AbstractControl, FormControl, FormsModule, ReactiveFormsModule, ValidationErrors, Validators } from '@angular/forms';
import { NxButtonModule } from '@aposin/ng-aquila/button';
import { NxCopytextModule } from '@aposin/ng-aquila/copytext';
import { NxFormfieldModule } from '@aposin/ng-aquila/formfield';
import { NxGridModule } from '@aposin/ng-aquila/grid';
import { NxLinkModule } from '@aposin/ng-aquila/link';
import { NxMessageModule, NxMessageToastService } from '@aposin/ng-aquila/message';
function createActionsValidator(knownActions: string[]) {
return (control: AbstractControl): ValidationErrors | null => {
try {
const parsedJson = JSON.parse(control.value) as PfeActionConfig[];
if (parsedJson.length === 0) {
return { noActions: true };
}
for (const actionConfig of parsedJson) {
if (!actionConfig.type) {
return { missingType: true };
}
if (!knownActions.includes(actionConfig.type)) {
return { invalidAction: actionConfig.type };
}
}
return null;
} catch (e) {
return { invalidJson: true };
}
};
}
@Component({
selector: 'pfe-devtools-actions',
imports: [
CommonModule,
NxGridModule,
NxMessageModule,
NxLinkModule,
FormsModule,
ReactiveFormsModule,
NxButtonModule,
NxFormfieldModule,
NxCopytextModule,
],
templateUrl: './actions.component.html',
styleUrl: './actions.component.scss',
})
export class DevToolsActionsComponent {
actionsControl = new FormControl(
`[
{
"type": "REPLACE_ME"
}
]`,
{ nonNullable: true, validators: [Validators.required], updateOn: 'blur' }
);
registeredActions: string[] = [];
isExecuting = false;
@ViewChild('executeActionsButton', { static: true, read: ElementRef }) executeActionsButton?: ElementRef<HTMLButtonElement>;
private pfeActionsService = inject(PfeActionsService);
private nxMessageService = inject(NxMessageToastService);
private destroyRef = inject(DestroyRef);
constructor() {
this.pfeActionsService.registeredActions$.pipe(takeUntilDestroyed(this.destroyRef)).subscribe((actions) => {
this.registeredActions = actions;
this.actionsControl.clearValidators();
this.actionsControl.setValidators([createActionsValidator(this.registeredActions), Validators.required]);
this.actionsControl.updateValueAndValidity();
});
}
async executeActions() {
if (!this.actionsControl.valid) {
this.actionsControl.markAsTouched();
this.executeActionsButton?.nativeElement.classList.add('shake');
// remove the class roughly after the animation completes
setTimeout(() => {
this.executeActionsButton?.nativeElement.classList.remove('shake');
}, 1000);
return;
}
this.isExecuting = true;
try {
const parsedJSON = JSON.parse(this.actionsControl.value);
await this.pfeActionsService.executeActions(parsedJSON);
this.nxMessageService.open('Action(s) successfully executed', { context: 'success' });
} catch (e) {
this.nxMessageService.open(
`Error executing the Action(s). Error: "${(e as Error).message}". Please check the browser console for details.`,
{
context: 'info',
}
);
throw e;
} finally {
this.isExecuting = false;
}
}
parseActions() {
if (!this.actionsControl.valid) {
return;
}
const formattedJSON = JSON.stringify(JSON.parse(this.actionsControl.value), null, 2);
this.actionsControl.setValue(formattedJSON);
}
}
<div nxRow>
<div nxCol="12">
<h2 class="headline">PFE Actions</h2>
<nx-message context="info" context="info"
>Take a look at the
<nx-link
><a
target="_blank"
href="https://ngx-pfe.frameworks.allianz.io/assets/docs/additional-documentation/flow-and-backend-integration/pfe-actions.html"
>PFE documentation</a
></nx-link
>
to learn more about Actions.
</nx-message>
</div>
</div>
<div nxRow class="nx-margin-top-m">
<div nxCol="12">
<label>Action configuration:</label><br />
<textarea
[formControl]="actionsControl"
(blur)="parseActions()"
name="actionsControl"
data-testid="execute-actions-textarea"
></textarea>
@if (actionsControl.touched && actionsControl.errors !== null && actionsControl.value !== '') {
@if (actionsControl.hasError('invalidJson')) {
<nx-error>The Action configuration(s) has to be valid JSON</nx-error>
} @else if (actionsControl.hasError('invalidAction')) {
<nx-error
>No registered Action of type "{{ actionsControl.getError('invalidAction') }}" found. You can only use registered Actions (see
below).</nx-error
>
} @else if (actionsControl.hasError('noActions')) {
<nx-error>Please add an Action configuration</nx-error>
} @else if (actionsControl.hasError('missingType')) {
<nx-error><i>type</i> is missing in action config</nx-error>
}
}
<p>
<button
#executeActionsButton
nxButton="primary small"
data-testid="execute-actions-button"
tabindex="0"
[disabled]="isExecuting"
(click)="executeActions()"
>
Execute Action(s)
</button>
</p>
</div>
</div>
<div nxRow>
<div nxCol="12">
<h2 class="headline">Registered Actions</h2>
<ul data-testid="registered-actions">
<li *ngFor="let action of registeredActions">{{ action }}</li>
</ul>
</div>
</div>
Legend
Html element with directive