Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix variables on CreateServer page #558

Merged
merged 1 commit into from
Aug 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading