From f6c1642bf231ca69fa7a2b328957050dcf39af18 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Tue, 13 Aug 2024 17:01:35 -0700 Subject: [PATCH 01/26] Use computed fields in CustomFieldSetDefaultValuesForModel component --- .../CustomFieldSetDefaultValuesForModel.php | 34 ++++++++----------- ...eld-set-default-values-for-model.blade.php | 6 ++-- 2 files changed, 17 insertions(+), 23 deletions(-) diff --git a/app/Livewire/CustomFieldSetDefaultValuesForModel.php b/app/Livewire/CustomFieldSetDefaultValuesForModel.php index a4a9f9fe71..db9f557c67 100644 --- a/app/Livewire/CustomFieldSetDefaultValuesForModel.php +++ b/app/Livewire/CustomFieldSetDefaultValuesForModel.php @@ -2,6 +2,7 @@ namespace App\Livewire; +use Livewire\Attributes\Computed; use Livewire\Component; use App\Models\CustomFieldset; @@ -15,30 +16,23 @@ class CustomFieldSetDefaultValuesForModel extends Component public $fields; public $model_id; - public function mount() + public function mount($model_id = null) { - if(is_null($this->model_id)){ - return; - } - - $this->model = AssetModel::find($this->model_id); // It's possible to do some clever route-model binding here, but let's keep it simple, shall we? - $this->fieldset_id = $this->model->fieldset_id; - - $this->fields = null; - - if ($fieldset = CustomFieldset::find($this->fieldset_id)) { - $this->fields = CustomFieldset::find($this->fieldset_id)->fields; - } - - $this->add_default_values = ($this->model->defaultValues->count() > 0); + $this->model_id = $model_id; + $this->fieldset_id = $this->model?->fieldset_id; + $this->add_default_values = ($this->model?->defaultValues->count() > 0); } - public function updatedFieldsetId() + #[Computed] + public function model() { - if (CustomFieldset::find($this->fieldset_id)) { - $this->fields = CustomFieldset::find($this->fieldset_id)->fields; - } - + return AssetModel::find($this->model_id); + } + + #[Computed] + public function customFields() + { + return CustomFieldset::find($this->fieldset_id)?->fields; } public function render() 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 7f61db8cea..7234e5144f 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 @@ -16,10 +16,10 @@ - @if ($this->add_default_values ) {{-- 'if the checkbox is enabled *AND* there are more than 0 fields in the fieldsset' --}} - @if ($fields) + @if ($add_default_values ) {{-- 'if the checkbox is enabled *AND* there are more than 0 fields in the fieldsset' --}} + @if ($this->customFields) - @foreach ($fields as $field) + @foreach ($this->customFields as $field)
From 038e86954b10fda8100d3a6cf5c87f48880e69e4 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Tue, 13 Aug 2024 17:30:53 -0700 Subject: [PATCH 02/26] Improve property name --- app/Livewire/CustomFieldSetDefaultValuesForModel.php | 3 +-- .../custom-field-set-default-values-for-model.blade.php | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/app/Livewire/CustomFieldSetDefaultValuesForModel.php b/app/Livewire/CustomFieldSetDefaultValuesForModel.php index db9f557c67..23c747c2e2 100644 --- a/app/Livewire/CustomFieldSetDefaultValuesForModel.php +++ b/app/Livewire/CustomFieldSetDefaultValuesForModel.php @@ -13,7 +13,6 @@ class CustomFieldSetDefaultValuesForModel extends Component public $add_default_values; public $fieldset_id; - public $fields; public $model_id; public function mount($model_id = null) @@ -30,7 +29,7 @@ class CustomFieldSetDefaultValuesForModel extends Component } #[Computed] - public function customFields() + public function fields() { return CustomFieldset::find($this->fieldset_id)?->fields; } 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 7234e5144f..a022caa496 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 @@ -17,9 +17,9 @@
@if ($add_default_values ) {{-- 'if the checkbox is enabled *AND* there are more than 0 fields in the fieldsset' --}} - @if ($this->customFields) + @if ($this->fields) - @foreach ($this->customFields as $field) + @foreach ($this->fields as $field)
From ab63a19affed7fe30f2ba13db4e17b391342414a Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Wed, 14 Aug 2024 11:01:51 -0700 Subject: [PATCH 03/26] Initialize concept of cached values --- .../CustomFieldSetDefaultValuesForModel.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/app/Livewire/CustomFieldSetDefaultValuesForModel.php b/app/Livewire/CustomFieldSetDefaultValuesForModel.php index 23c747c2e2..d9531fce82 100644 --- a/app/Livewire/CustomFieldSetDefaultValuesForModel.php +++ b/app/Livewire/CustomFieldSetDefaultValuesForModel.php @@ -2,6 +2,7 @@ namespace App\Livewire; +use Illuminate\Support\Collection; use Livewire\Attributes\Computed; use Livewire\Component; @@ -15,11 +16,19 @@ class CustomFieldSetDefaultValuesForModel extends Component public $fieldset_id; public $model_id; + public Collection $cachedValues; + public function mount($model_id = null) { $this->model_id = $model_id; $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)); + }); } #[Computed] @@ -31,7 +40,13 @@ class CustomFieldSetDefaultValuesForModel extends Component #[Computed] public function fields() { - return CustomFieldset::find($this->fieldset_id)?->fields; + $customFieldset = CustomFieldset::find($this->fieldset_id); + + if ($customFieldset) { + return $customFieldset?->fields; + } + + return collect(); } public function render() From 6ab976f353b67b03824ed2b29ea78155dd294b4b Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 15 Aug 2024 10:32:33 -0700 Subject: [PATCH 04/26] Use cached values for text input --- .../CustomFieldSetDefaultValuesForModel.php | 11 +++++++++++ ...eld-set-default-values-for-model.blade.php | 19 +++++++++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/app/Livewire/CustomFieldSetDefaultValuesForModel.php b/app/Livewire/CustomFieldSetDefaultValuesForModel.php index d9531fce82..f4d4c30cdf 100644 --- a/app/Livewire/CustomFieldSetDefaultValuesForModel.php +++ b/app/Livewire/CustomFieldSetDefaultValuesForModel.php @@ -2,6 +2,7 @@ namespace App\Livewire; +use App\Models\CustomField; use Illuminate\Support\Collection; use Livewire\Attributes\Computed; use Livewire\Component; @@ -31,6 +32,16 @@ class CustomFieldSetDefaultValuesForModel extends Component }); } + public function getValueForField(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 a022caa496..3dea89cf6e 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 @@ -20,7 +20,7 @@ @if ($this->fields) @foreach ($this->fields as $field) -
+
@@ -38,7 +38,18 @@ @elseif ($field->element == "text") - + @elseif($field->element == "textarea") @@ -80,11 +91,11 @@ Unknown field element: {{ $field->element }} @endif -
+
@endforeach - + @endif @endif From e7c9c799bbd90320ada1da9343cb8539bc23ecf1 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 15 Aug 2024 10:40:23 -0700 Subject: [PATCH 05/26] Add wire:keys --- ...stom-field-set-default-values-for-model.blade.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) 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 3dea89cf6e..4209179476 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 @@ -64,7 +64,13 @@ @@ -72,7 +78,7 @@ @elseif($field->element == "radio") @foreach(explode("\r\n", $field->field_values) as $field_value) -