Add server variables

This commit is contained in:
Lance Pioch 2024-04-12 02:04:22 -04:00
parent 15971aaa94
commit cbc255ddf8
2 changed files with 72 additions and 37 deletions

View File

@ -3,19 +3,19 @@
namespace App\Filament\Resources; namespace App\Filament\Resources;
use App\Filament\Resources\ServerResource\Pages; use App\Filament\Resources\ServerResource\Pages;
use App\Filament\Resources\ServerResource\RelationManagers;
use App\Models\Allocation; use App\Models\Allocation;
use App\Models\Egg; use App\Models\Egg;
use App\Models\Node; use App\Models\Node;
use App\Models\Server; use App\Models\Server;
use App\Services\Allocations\AssignmentService; use App\Services\Allocations\AssignmentService;
use Closure;
use Filament\Forms; use Filament\Forms;
use Filament\Forms\Form; use Filament\Forms\Form;
use Filament\Resources\Resource; use Filament\Resources\Resource;
use Filament\Tables; use Filament\Tables;
use Filament\Tables\Table; use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope; use Illuminate\Support\Facades\Validator;
use Illuminate\Support\HtmlString; use Illuminate\Support\HtmlString;
class ServerResource extends Resource class ServerResource extends Resource
@ -123,7 +123,22 @@ class ServerResource extends Resource
->searchable() ->searchable()
->preload() ->preload()
->live() ->live()
->afterStateUpdated(fn ($state, Forms\Set $set) => $set('startup', Egg::find($state)->startup)) ->afterStateUpdated(function ($state, Forms\Set $set) {
$egg = Egg::find($state);
$set('startup', $egg->startup);
$variables = $egg->variables ?? [];
$serverVariables = collect();
foreach ($variables as $variable) {
$serverVariables->add($variable->toArray());
}
$set($path = 'server_variables', $serverVariables->all());
for ($i = 0; $i < $serverVariables->count(); $i++) {
$set("$path.$i.variable_value", $serverVariables[$i]['default_value']);
$set("$path.$i.variable_id", $serverVariables[$i]['id']);
}
})
->required(), ->required(),
Forms\Components\ToggleButtons::make('skip_scripts') Forms\Components\ToggleButtons::make('skip_scripts')
@ -270,41 +285,56 @@ class ServerResource extends Resource
}) })
->columnSpanFull(), ->columnSpanFull(),
Forms\Components\Repeater::make('s') Forms\Components\Section::make('Egg Variables')
->reorderable(false) ->icon('tabler-eggs')
->addable(false) ->iconColor('primary')
->deletable(false) ->collapsible()
->label('Egg Variables') ->collapsed()
->columnSpanFull()
->grid(2)
->default(function (Forms\Get $get) {
$variables = Egg::find($get('egg_id'))->variables ?? [];
$serverVariables = collect();
foreach ($variables as $variable) {
$serverVariables->add($variable->toArray());
}
return $serverVariables->all();
})
// ->relationship('serverVariables')
// ->default([1, 2, 3])
->name('name')
// ->itemLabel(fn (array $state) => 'asdf')
->schema([ ->schema([
Forms\Components\TextInput::make('value') Forms\Components\Placeholder::make('Select an egg first to show its variables!')
->label(fn (Forms\Get $get) => $get('name')) ->hidden(fn (Forms\Get $get) => !empty($get('server_variables'))),
->helperText(fn (Forms\Get $get) => new HtmlString("
{$get('description')}<br /> Forms\Components\Repeater::make('server_variables')
Access in Startup: <code>{{{$get('env_variable')}}}</code><br /> ->relationship('serverVariables')
Validation Rules: <code>{$get('rules')}</code> ->grid(2)
")) ->reorderable(false)
// ->inlineLabel() ->addable(false)
->maxLength(191), ->deletable(false)
// Forms\Components\Textarea::make('description')->columnSpanFull(), ->default([])
// Forms\Components\TextInput::make('env_variable')->maxLength(191), ->hidden(fn ($state) => empty($state))
// Forms\Components\TextInput::make('default_value')->maxLength(191), ->afterStateUpdated(function () {
// Forms\Components\Textarea::make('rules')->rows(3)->columnSpanFull()->required(),
]) })
->schema([
Forms\Components\TextInput::make('variable_value')
//->rule(0, fn (Forms\Get $get) => str($get('rules'))) // TODO
->rules([
fn (Forms\Get $get): Closure => function (string $attribute, $value, Closure $fail) use ($get) {
$validator = Validator::make(['validatorkey' => $value], [
'validatorkey' => $get('rules'),
]);
if ($validator->fails()) {
$message = str($validator->errors()->first())->replace('validatorkey', $get('name'));
$fail($message);
}
},
])
->label(fn (Forms\Get $get) => $get('name'))
->hint(fn (Forms\Get $get) => $get('rules'))
->prefix(fn (Forms\Get $get) => '{{' . $get('env_variable') . '}}')
->helperText(fn (Forms\Get $get) => empty($get('description')) ? '—' : $get('description'))
->maxLength(191),
Forms\Components\Hidden::make('variable_id')->default(0)
])
->columnSpanFull(),
]),
]); ]);
} }

View File

@ -251,6 +251,11 @@ class Server extends Model
return $this->hasOne(Egg::class, 'id', 'egg_id'); return $this->hasOne(Egg::class, 'id', 'egg_id');
} }
public function eggVariables(): HasMany
{
return $this->hasMany(EggVariable::class, 'egg_id', 'egg_id');
}
/** /**
* Gets information for the egg variables associated with this server. * Gets information for the egg variables associated with this server.
*/ */