Files
smartguide/webapp/src/app/modules/jobs/task-edit-dialog/task-edit-dialog.component.ts
T

174 lines
5.1 KiB
TypeScript

import { CommonModule } from '@angular/common';
import { Component, OnInit, computed, inject, signal } from '@angular/core';
import { toSignal } from '@angular/core/rxjs-interop';
import {
FormControl,
FormGroup,
ReactiveFormsModule,
Validators,
} from '@angular/forms';
import { MatAutocompleteModule } from '@angular/material/autocomplete';
import { MatButtonModule } from '@angular/material/button';
import {
MAT_DIALOG_DATA,
MatDialogModule,
MatDialogRef,
} from '@angular/material/dialog';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatIconModule } from '@angular/material/icon';
import { MatInputModule } from '@angular/material/input';
import { MatTooltipModule } from '@angular/material/tooltip';
import { BackgroundJobTask, BackgroundJobsService } from '../../../api';
import { INFO_OUTLINE_ICON, SAVE_ICON } from '../../../shared/ui/icons';
const ENV_LINE_PATTERN = /^[A-Za-z_][A-Za-z0-9_]*=.*$/;
export interface TaskEditDialogData {
task: BackgroundJobTask;
}
@Component({
selector: 'app-task-edit-dialog',
imports: [
CommonModule,
ReactiveFormsModule,
MatAutocompleteModule,
MatButtonModule,
MatDialogModule,
MatFormFieldModule,
MatIconModule,
MatInputModule,
MatTooltipModule,
],
templateUrl: './task-edit-dialog.component.html',
styleUrl: './task-edit-dialog.component.scss',
standalone: true,
})
export class TaskEditDialogComponent implements OnInit {
readonly INFO_OUTLINE_ICON = INFO_OUTLINE_ICON;
readonly SAVE_ICON = SAVE_ICON;
private readonly dialogRef =
inject<
MatDialogRef<TaskEditDialogComponent, BackgroundJobTask | undefined>
>(MatDialogRef);
private readonly backgroundJobsService = inject(BackgroundJobsService);
protected readonly data = inject<TaskEditDialogData>(MAT_DIALOG_DATA);
protected readonly saving = signal(false);
protected readonly serverError = signal('');
protected readonly images = signal<string[]>([]);
protected readonly form = new FormGroup({
name: new FormControl(this.data.task.name, {
nonNullable: true,
validators: [Validators.required],
}),
description: new FormControl(this.data.task.description, {
nonNullable: true,
}),
targetIndex: new FormControl(this.data.task.target_index, {
nonNullable: true,
}),
command: new FormControl(this.data.task.command ?? '', {
nonNullable: true,
}),
image: new FormControl(this.data.task.image ?? '', {
nonNullable: true,
validators: [Validators.required],
}),
env: new FormControl(this.envToText(this.data.task.env), {
nonNullable: true,
}),
});
private readonly imageQuery = toSignal(
this.form.controls.image.valueChanges,
{
initialValue: this.form.controls.image.value,
},
);
protected readonly filteredImages = computed(() => {
const query = (this.imageQuery() ?? '').toLowerCase().trim();
const all = this.images();
if (!query) return all;
return all.filter((image) => image.toLowerCase().includes(query));
});
ngOnInit(): void {
this.backgroundJobsService
.getBackgroundJobAvailableImages()
.subscribe({ next: (result) => this.images.set(result.images ?? []) });
}
protected submit(): void {
this.serverError.set('');
const value = this.form.getRawValue();
const name = value.name.trim();
if (!name) {
this.form.controls.name.markAsTouched();
return;
}
const image = value.image.trim();
if (!image) {
this.form.controls.image.markAsTouched();
return;
}
const env = this.parseEnv(value.env);
if (env === undefined) return;
this.saving.set(true);
this.backgroundJobsService
.updateBackgroundJobTask(this.data.task.id, {
name,
description: value.description.trim(),
target_index: value.targetIndex.trim(),
image,
command: value.command.trim() || null,
env,
})
.subscribe({
next: (task) => this.dialogRef.close(task),
error: (error) => {
this.saving.set(false);
const detail = error?.error?.detail;
this.serverError.set(
typeof detail === 'string'
? detail
: 'Der Job konnte nicht gespeichert werden.',
);
},
});
}
protected close(): void {
this.dialogRef.close();
}
private envToText(env: { [key: string]: string }): string {
return Object.entries(env)
.map(([key, value]) => `${key}=${value}`)
.join('\n');
}
private parseEnv(text: string): { [key: string]: string } | undefined {
const env: { [key: string]: string } = {};
for (const line of text.split('\n')) {
const trimmed = line.trim();
if (!trimmed) continue;
if (!ENV_LINE_PATTERN.test(trimmed)) {
this.form.controls.env.setErrors({ invalidEnv: true });
this.form.controls.env.markAsTouched();
return undefined;
}
const separatorIndex = trimmed.indexOf('=');
env[trimmed.slice(0, separatorIndex)] = trimmed.slice(separatorIndex + 1);
}
return env;
}
}