Simplify by directly wire:modeling

This commit is contained in:
Marcus Moore 2024-08-20 15:38:43 -07:00
parent 43b109fac0
commit 5d1f98bf93
No known key found for this signature in database
2 changed files with 19 additions and 35 deletions

View file

@ -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()
{

View file

@ -41,14 +41,9 @@
<input
class="form-control"
type="text"
value="{{ $this->getFieldValue($field) }}"
id="default-value{{ $field->id }}"
name="default_values[{{ $field->id }}]"
x-on:change="(e) => $wire.$call(
'updateFieldValue',
'{{ $field->db_column }}',
e.target.value
)"
wire:model="selectedValues.{{ $field->db_column }}"
/>
@ -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) }}</textarea>
wire:model="selectedValues.{{ $field->db_column }}"
></textarea>
@elseif($field->element == "listbox")
<select class="form-control" name="default_values[{{ $field->id }}]">
<select class="form-control" name="default_values[{{ $field->id }}]" wire:model="selectedValues.{{ $field->db_column }}">
<option value=""></option>
@foreach(explode("\r\n", $field->field_values) as $field_value)
<option
value="{{$field_value}}"
{{ $field->defaultValue($model_id) == $field_value ? 'selected="selected"': '' }}
wire:key="listbox-{{ $field_value }}"
>
{{ $field_value }}
@ -95,12 +85,7 @@
type="radio"
name="default_values[{{ $field->id }}]"
value="{{$field_value}}"
@checked($this->getFieldValue($field) == $field_value)
x-on:change="(e) => $wire.$call(
'updateFieldValue',
'{{ $field->db_column }}',
e.target.value
)"
wire:model="selectedValues.{{ $field->db_column }}"
/>{{ $field_value }}
</label>
@endforeach
@ -109,7 +94,14 @@
@foreach(explode("\r\n", $field->field_values) as $field_value)
<label class="col-md-3 form-control" for="{{ str_slug($field_value) }}" wire:key="checkbox-{{ $field_value }}">
<input id="{{ str_slug($field_value) }}" type="checkbox" aria-label="{{ str_slug($field->name) }}" name="default_values[{{ $field->id }}][]" value="{{ $field_value }}"{{ in_array($field_value, explode(', ',$field->defaultValue($model_id))) ? ' checked="checked"': '' }}> {{ $field_value }}
<input
id="{{ str_slug($field_value) }}"
type="checkbox"
aria-label="{{ str_slug($field->name) }}"
name="default_values[{{ $field->id }}][]"
value="{{ $field_value }}"
wire:model="selectedValues.{{ $field->db_column }}"
> {{ $field_value }}
</label>
@endforeach