Skip to content

Commit

Permalink
Fix variables on CreateServer page (#558)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy132 authored Aug 20, 2024
1 parent c2b1a98 commit 20b06b7
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions app/Filament/Resources/ServerResource/Pages/CreateServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use App\Models\Allocation;
use App\Models\Egg;
use App\Models\Node;
use App\Models\ServerVariable;
use App\Models\User;
use App\Services\Allocations\AssignmentService;
use App\Services\Servers\RandomWordService;
Expand Down Expand Up @@ -444,7 +443,7 @@ public function form(Form $form): Form

$text = Forms\Components\TextInput::make('variable_value')
->hidden($this->shouldHideComponent(...))
->required(fn (ServerVariable $serverVariable) => $serverVariable->variable->getRequiredAttribute())
->required(fn (Forms\Get $get) => in_array('required', $get('rules')))
->rules(
fn (Forms\Get $get): Closure => function (string $attribute, $value, Closure $fail) use ($get) {
$validator = Validator::make(['validatorkey' => $value], [
Expand All @@ -471,7 +470,7 @@ public function form(Form $form): Form
->live(onBlur: true)
->hintIcon('tabler-code')
->label(fn (Forms\Get $get) => $get('name'))
->hintIconTooltip(fn (Forms\Get $get) => $get('rules'))
->hintIconTooltip(fn (Forms\Get $get) => implode('|', $get('rules')))
->prefix(fn (Forms\Get $get) => '{{' . $get('env_variable') . '}}')
->helperText(fn (Forms\Get $get) => empty($get('description')) ? '' : $get('description'))
->afterStateUpdated(function (Forms\Set $set, Forms\Get $get, $state) {
Expand Down Expand Up @@ -806,9 +805,11 @@ protected function handleRecordCreation(array $data): Model
return $service->handle($data);
}

private function shouldHideComponent(ServerVariable $serverVariable, Forms\Components\Component $component): bool
private function shouldHideComponent(Forms\Get $get, Forms\Components\Component $component): bool
{
$containsRuleIn = array_first($serverVariable->variable->rules, fn ($value) => str($value)->startsWith('in:'), false);
$containsRuleIn = collect($get('rules'))->reduce(
fn ($result, $value) => $result === true && !str($value)->startsWith('in:'), true
);

if ($component instanceof Forms\Components\Select) {
return $containsRuleIn;
Expand All @@ -821,9 +822,11 @@ private function shouldHideComponent(ServerVariable $serverVariable, Forms\Compo
throw new \Exception('Component type not supported: ' . $component::class);
}

private function getSelectOptionsFromRules(ServerVariable $serverVariable): array
private function getSelectOptionsFromRules(Forms\Get $get): array
{
$inRule = array_first($serverVariable->variable->rules, fn ($value) => str($value)->startsWith('in:'));
$inRule = collect($get('rules'))->reduce(
fn ($result, $value) => str($value)->startsWith('in:') ? $value : $result, ''
);

return str($inRule)
->after('in:')
Expand Down

0 comments on commit 20b06b7

Please sign in to comment.