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

152 lines
4.6 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 { 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 { ADD_ICON, INFO_OUTLINE_ICON } from '../../../shared/ui/icons';
const ENV_LINE_PATTERN = /^[A-Za-z_][A-Za-z0-9_]*=.*$/;
@Component({
selector: 'app-task-create-dialog',
imports: [
CommonModule,
ReactiveFormsModule,
MatAutocompleteModule,
MatButtonModule,
MatDialogModule,
MatFormFieldModule,
MatIconModule,
MatInputModule,
MatTooltipModule,
],
templateUrl: './task-create-dialog.component.html',
styleUrl: './task-create-dialog.component.scss',
standalone: true,
})
export class TaskCreateDialogComponent implements OnInit {
readonly INFO_OUTLINE_ICON = INFO_OUTLINE_ICON;
readonly ADD_ICON = ADD_ICON;
private readonly dialogRef =
inject<
MatDialogRef<TaskCreateDialogComponent, BackgroundJobTask | undefined>
>(MatDialogRef);
private readonly backgroundJobsService = inject(BackgroundJobsService);
protected readonly creating = signal(false);
protected readonly serverError = signal('');
protected readonly images = signal<string[]>([]);
protected readonly form = new FormGroup({
name: new FormControl('', {
nonNullable: true,
validators: [Validators.required],
}),
description: new FormControl('', { nonNullable: true }),
targetIndex: new FormControl('', { nonNullable: true }),
command: new FormControl('', { nonNullable: true }),
image: new FormControl('', {
nonNullable: true,
validators: [Validators.required],
}),
env: new FormControl('', { 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 command = value.command.trim() || null;
const env = this.parseEnv(value.env);
if (env === undefined) return;
this.creating.set(true);
this.backgroundJobsService
.createBackgroundJobTask({
name,
description: value.description.trim(),
target_index: value.targetIndex.trim(),
image,
command,
env,
})
.subscribe({
next: (task) => this.dialogRef.close(task),
error: (error) => {
this.creating.set(false);
const detail = error?.error?.detail;
this.serverError.set(
typeof detail === 'string'
? detail
: 'Der Job konnte nicht angelegt werden.',
);
},
});
}
protected close(): void {
this.dialogRef.close();
}
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;
}
}