Skip to content

Commit

Permalink
Save Variables after update & off click
Browse files Browse the repository at this point in the history
Variables update when the user clicks off the input.
  • Loading branch information
notAreYouScared committed Aug 14, 2024
1 parent 1e0bb00 commit e0ee02b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
37 changes: 35 additions & 2 deletions app/Filament/App/Pages/Startup.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function form(Form $form): Form
continue;
}

$text = TextInput::make('var_' . $serverVariable->variable->name)
$text = TextInput::make($serverVariable->variable->name)
->hidden(fn (Component $component) => $this->shouldHideComponent($serverVariable, $component))
->readOnly(fn () => !$serverVariable->variable->user_editable)
->required(fn () => in_array('required', explode('|', $serverVariable->variable->rules)))
Expand All @@ -109,7 +109,7 @@ public function form(Form $form): Form
},
]);

$select = Select::make('var_' . $serverVariable->variable->name)
$select = Select::make($serverVariable->variable->name)
->hidden(fn (Component $component) => $this->shouldHideComponent($serverVariable, $component))
->disabled(fn () => !$serverVariable->variable->user_editable) // TODO: find better way, doesn't look so nice
->options(fn () => $this->getSelectOptionsFromRules($serverVariable))
Expand All @@ -120,6 +120,9 @@ public function form(Form $form): Form
foreach ($components as &$component) {
$component = $component
->live(onBlur: true)
->afterStateUpdated(function ($state) use ($serverVariable) {
$this->update($state, $serverVariable->variable->env_variable);
})
->hintIcon('tabler-code')
->label(fn () => $serverVariable->variable->name)
->default(fn () => $serverVariable->variable_value ?? $serverVariable->variable->default_value)
Expand Down Expand Up @@ -182,4 +185,34 @@ private function getSelectOptionsFromRules(ServerVariable $serverVariable): arra
->mapWithKeys(fn ($value) => [$value => $value])
->all();
}

public function update($state, string $var): null
{
/** @var Server $server */
$server = Filament::getTenant();
$variable = $server->variables()->where('env_variable', $var)->first();
$original = $variable->server_value;

//$this->validate($state, $variable->rules); //TODO: Setup Rule Validation

ServerVariable::query()->updateOrCreate([
'server_id' => $server->id,
'variable_id' => $variable->id,
], [
'variable_value' => $state ?? '',
]);

if ($variable->env_variable !== $var) {
Activity::event('server:startup.edit')

Check failure on line 206 in app/Filament/App/Pages/Startup.php

View workflow job for this annotation

GitHub Actions / PHPStan

Call to static method event() on an unknown class App\Filament\App\Pages\Activity.
->subject($variable)
->property([
'variable' => $variable->env_variable,
'old' => $original,
'new' => $state,
])
->log();
}

return null;
}
}
12 changes: 4 additions & 8 deletions app/Filament/Resources/ServerResource/Pages/EditServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -454,23 +454,19 @@ public function form(Form $form): Form
->label('Startup Command')
->required()
->columnSpan(6)
->rows(function ($state) {
return str($state)->explode("\n")->reduce(
fn (int $carry, $line) => $carry + floor(strlen($line) / 125),
0
);
}),
->autosize(),

Forms\Components\Textarea::make('defaultStartup')
->hintAction(CopyAction::make())
->label('Default Startup Command')
->disabled()
->autosize()
->columnSpan(6)
->formatStateUsing(function ($state, Get $get, Set $set) {
$egg = Egg::query()->find($get('egg_id'));

return $egg->startup;
})
->columnSpan(6),
}),

Forms\Components\Repeater::make('server_variables')
->relationship('serverVariables')
Expand Down

0 comments on commit e0ee02b

Please sign in to comment.