Skip to content

Commit 399db86

Browse files
committed
Add isDeletable property and refresh logic for field and section deletion in ManageCustomField components
1 parent a692504 commit 399db86

File tree

4 files changed

+19
-8
lines changed

4 files changed

+19
-8
lines changed

resources/views/filament/pages/custom-fields-next.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
class="flex flex-col gap-y-6"
1616
>
1717
@foreach ($this->sections as $section)
18-
@livewire('manage-custom-field-section', ['entityType' => $this->currentEntityType, 'section' => $section], key($section->id . str()->random(16)))
18+
@livewire('manage-custom-field-section', ['entityType' => $this->currentEntityType, 'section' => $section, 'isDeletable' => $this->sections()->count() > 1], key($section->id . str()->random(16)))
1919
@endforeach
2020

2121
{{ $this->createSectionAction }}

src/Filament/Pages/CustomFields.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Relaticle\CustomFields\Filament\Pages;
44

55
use Illuminate\Support\Collection;
6+
use Livewire\Attributes\On;
67
use Relaticle\CustomFields\Enums\CustomFieldSectionType;
78
use Relaticle\CustomFields\Filament\FormSchemas\SectionForm;
89
use Relaticle\CustomFields\Models\CustomFieldSection;
@@ -50,11 +51,8 @@ public function sections(): Collection
5051
->orderBy('sort_order');
5152
}
5253
])
53-
->orderBy('sort_order') // Adjust as necessary based on your sorting preference
54-
->get()
55-
->map(function ($section) {
56-
return $section;
57-
});
54+
->orderBy('sort_order')
55+
->get();
5856
}
5957

6058
#[Computed]
@@ -141,6 +139,11 @@ private function storeSection(array $data): CustomFieldSection
141139
return CustomFieldSection::create($data);
142140
}
143141

142+
#[On('section-deleted')]
143+
public function sectionDeleted(): void
144+
{
145+
$this->sections = $this->sections->filter(fn($section) => $section->exists);
146+
}
144147

145148
public static function getCluster(): ?string
146149
{

src/Livewire/ManageCustomField.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function deleteAction(): Action
7878
->model(CustomField::class)
7979
->record($this->field)
8080
->visible(fn(CustomField $record): bool => !$record->isActive() && !$record->isSystemDefined())
81-
->action(fn() => $this->field->delete());
81+
->action(fn() => $this->field->delete() && $this->dispatch('field-deleted'));
8282
}
8383

8484
public function setWidth(int $fieldId, int $width): void

src/Livewire/ManageCustomFieldSection.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class ManageCustomFieldSection extends Component implements HasForms, HasActions
2626

2727
public string $entityType;
2828
public CustomFieldSection $section;
29+
public bool $isDeletable = true;
2930

3031
#[Computed]
3132
public function fields()
@@ -43,6 +44,12 @@ public function fieldWidthUpdated(int $fieldId, int $width): void
4344
$this->section->refresh();
4445
}
4546

47+
#[On('field-deleted')]
48+
public function fieldDeleted(): void
49+
{
50+
$this->section->refresh();
51+
}
52+
4653
public function updateFieldsOrder($sectionId, $fields): void
4754
{
4855
foreach ($fields as $index => $field) {
@@ -105,7 +112,8 @@ public function deleteAction(): Action
105112
->model(CustomFieldSection::class)
106113
->record($this->section)
107114
->visible(fn(CustomFieldSection $record): bool => !$record->isActive() && !$record->isSystemDefined())
108-
->action(fn() => $this->section->delete());
115+
->disabled(fn(CustomFieldSection $record): bool => !$this->isDeletable)
116+
->action(fn() => $this->section->delete() && $this->dispatch('section-deleted'));
109117
}
110118

111119
public function createFieldAction(): Action

0 commit comments

Comments
 (0)