Generate name automatically for server if egg is selected

This commit is contained in:
Lance Pioch 2024-05-04 13:39:57 -04:00
parent 17787fee18
commit 48c97ee1cc
2 changed files with 12 additions and 2 deletions

View File

@ -252,8 +252,8 @@ class CreateServer extends CreateRecord
->searchable()
->preload()
->live()
->afterStateUpdated(function ($state, Forms\Set $set) {
$egg = Egg::find($state);
->afterStateUpdated(function ($state, Forms\Set $set, Forms\Get $get, $old) {
$egg = Egg::query()->find($state);
$set('startup', $egg->startup);
$variables = $egg->variables ?? [];
@ -271,6 +271,11 @@ class CreateServer extends CreateRecord
}
$set('environment', $variables);
$previousEgg = Egg::query()->find($old);
if (!$get('name') || $previousEgg?->getKebabName() === $get('name')) {
$set('name', $egg->getKebabName());
}
})
->required(),

View File

@ -307,4 +307,9 @@ class Egg extends Model
{
return $this->belongsTo(self::class, 'config_from');
}
public function getKebabName(): string
{
return str($this->name)->kebab()->lower()->trim()->split('/[^\w\-]/')->join('');
}
}