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

127 lines
3.9 KiB
TypeScript

import { CommonModule } from '@angular/common';
import { Component, inject, signal } from '@angular/core';
import { FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms';
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';
const ENV_LINE_PATTERN = /^[A-Za-z_][A-Za-z0-9_]*=.*$/;
const DEFAULT_JOB_IMAGE = 'docker.nes.uni-due.de/inse/background-jobs:latest';
export interface TaskEditDialogData {
task: BackgroundJobTask;
}
@Component({
selector: 'app-task-edit-dialog',
imports: [
CommonModule,
ReactiveFormsModule,
MatButtonModule,
MatDialogModule,
MatFormFieldModule,
MatIconModule,
MatInputModule,
MatTooltipModule,
],
templateUrl: './task-edit-dialog.component.html',
styleUrl: './task-edit-dialog.component.scss',
standalone: true,
})
export class TaskEditDialogComponent {
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 form = new FormGroup({
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 }),
env: new FormControl(this.envToText(this.data.task.env), {
nonNullable: true,
}),
});
protected submit(): void {
this.serverError.set('');
const value = this.form.getRawValue();
const env = this.parseEnv(value.env);
if (env === undefined) return;
// The default image is stored as null so the job keeps following the
// backend-configured image if it ever changes.
const image = value.image.trim();
this.saving.set(true);
this.backgroundJobsService
.updateBackgroundJobTask(this.data.task.id, {
description: value.description.trim(),
target_index: value.targetIndex.trim(),
image: !image || image === DEFAULT_JOB_IMAGE ? null : 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;
}
}