From 5d1f98bf93fedc77c08740f06d77590283baa24e Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Tue, 20 Aug 2024 15:38:43 -0700 Subject: [PATCH] Simplify by directly wire:modeling --- .../CustomFieldSetDefaultValuesForModel.php | 20 ++++------- ...eld-set-default-values-for-model.blade.php | 34 +++++++------------ 2 files changed, 19 insertions(+), 35 deletions(-) diff --git a/app/Livewire/CustomFieldSetDefaultValuesForModel.php b/app/Livewire/CustomFieldSetDefaultValuesForModel.php index 756d87ef97..41c97c45eb 100644 --- a/app/Livewire/CustomFieldSetDefaultValuesForModel.php +++ b/app/Livewire/CustomFieldSetDefaultValuesForModel.php @@ -2,7 +2,6 @@ namespace App\Livewire; -use App\Models\CustomField; use Illuminate\Support\Collection; use Livewire\Attributes\Computed; use Livewire\Component; @@ -18,6 +17,7 @@ class CustomFieldSetDefaultValuesForModel extends Component public $model_id; public Collection $cachedValues; + public array $selectedValues = []; public function mount($model_id = null) { @@ -25,23 +25,15 @@ class CustomFieldSetDefaultValuesForModel extends Component $this->fieldset_id = $this->model?->fieldset_id; $this->add_default_values = ($this->model?->defaultValues->count() > 0); - $this->cachedValues = collect(); - $this->fields->each(function ($field) { - $this->cachedValues->put($field->db_column, $field->defaultValue($this->model_id)); + if ($field->element === 'checkbox') { + $this->selectedValues[$field->db_column] = explode(', ', $field->defaultValue($this->model_id)); + } else { + $this->selectedValues[$field->db_column] = $field->defaultValue($this->model_id); + } }); } - public function getFieldValue(CustomField $field) - { - return $this->cachedValues->get($field->db_column); - } - - public function updateFieldValue($dbColumn, $updatedValue): void - { - $this->cachedValues->put($dbColumn, $updatedValue); - } - #[Computed] public function model() { diff --git a/resources/views/livewire/custom-field-set-default-values-for-model.blade.php b/resources/views/livewire/custom-field-set-default-values-for-model.blade.php index d825192756..0110cd5fea 100644 --- a/resources/views/livewire/custom-field-set-default-values-for-model.blade.php +++ b/resources/views/livewire/custom-field-set-default-values-for-model.blade.php @@ -41,14 +41,9 @@ @@ -60,23 +55,18 @@ style="width: 100%;" id="default-value{{ $field->id }}" name="default_values[{{ $field->id }}]" - x-on:change="(e) => $wire.$call( - 'updateFieldValue', - '{{ $field->db_column }}', - e.target.value - )" - >{{ $this->getFieldValue($field) }} + wire:model="selectedValues.{{ $field->db_column }}" + > @elseif($field->element == "listbox") - @foreach(explode("\r\n", $field->field_values) as $field_value)