Skip to content

Commit

Permalink
Feature - Prevent delete action on resources with childs (#1532)
Browse files Browse the repository at this point in the history
Co-authored-by: Alec Ritson <[email protected]>
Co-authored-by: Glenn Jacobs <[email protected]>
  • Loading branch information
3 people authored Feb 15, 2024
1 parent 02d8e89 commit 1366268
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 5 deletions.
7 changes: 7 additions & 0 deletions packages/admin/resources/lang/en/attributegroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,11 @@
],
],

'action' => [
'delete' => [
'notification' => [
'error_protected' => 'This attribute group can not be deleted as there are attributes associated.',
],
],
],
];
7 changes: 7 additions & 0 deletions packages/admin/resources/lang/en/brand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,11 @@
],
],

'action' => [
'delete' => [
'notification' => [
'error_protected' => 'This brand can not be deleted as there are products associated.',
],
],
],
];
7 changes: 7 additions & 0 deletions packages/admin/resources/lang/en/collectiongroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,11 @@
],
],

'action' => [
'delete' => [
'notification' => [
'error_protected' => 'This collection group can not be deleted as there are collections associated.',
],
],
],
];
7 changes: 7 additions & 0 deletions packages/admin/resources/lang/en/customergroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,11 @@
],
],

'action' => [
'delete' => [
'notification' => [
'error_protected' => 'This customer group can not be deleted as there are customers associated.',
],
],
],
];
8 changes: 8 additions & 0 deletions packages/admin/resources/lang/en/producttype.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,12 @@
'no_attributes' => 'There are no attributes available.',
],

'action' => [
'delete' => [
'notification' => [
'error_protected' => 'This product type can not be deleted as there are products associated.',
],
],
],

];
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Lunar\Admin\Filament\Resources\AttributeGroupResource\Pages;

use Filament\Actions;
use Filament\Notifications\Notification;
use Lunar\Admin\Filament\Resources\AttributeGroupResource;
use Lunar\Admin\Support\Pages\BaseEditRecord;

Expand All @@ -13,7 +14,16 @@ class EditAttributeGroup extends BaseEditRecord
protected function getDefaultHeaderActions(): array
{
return [
Actions\DeleteAction::make(),
Actions\DeleteAction::make()
->before(function ($record, Actions\DeleteAction $action) {
if ($record->attributes->count() > 0) {
Notification::make()
->warning()
->body(__('lunarpanel::attributegroup.action.delete.notification.error_protected'))
->send();
$action->cancel();
}
}),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Lunar\Admin\Filament\Resources\BrandResource\Pages;

use Filament\Actions;
use Filament\Notifications\Notification;
use Lunar\Admin\Filament\Resources\BrandResource;
use Lunar\Admin\Support\Pages\BaseEditRecord;

Expand All @@ -13,7 +14,16 @@ class EditBrand extends BaseEditRecord
protected function getDefaultHeaderActions(): array
{
return [
Actions\DeleteAction::make(),
Actions\DeleteAction::make()
->before(function ($record, Actions\DeleteAction $action) {
if ($record->products->count() > 0) {
Notification::make()
->warning()
->body(__('lunarpanel::brand.action.delete.notification.error_protected'))
->send();
$action->cancel();
}
}),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Lunar\Admin\Filament\Resources\CollectionGroupResource\Pages;

use Filament\Actions;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\EditRecord;
use Lunar\Admin\Filament\Resources\CollectionGroupResource;
use Lunar\Admin\Filament\Resources\CollectionGroupResource\Widgets;
Expand All @@ -14,7 +15,16 @@ class EditCollectionGroup extends EditRecord
protected function getHeaderActions(): array
{
return [
Actions\DeleteAction::make(),
Actions\DeleteAction::make()
->before(function ($record, Actions\DeleteAction $action) {
if ($record->collections->count() > 0) {
Notification::make()
->warning()
->body(__('lunarpanel::collectiongroup.action.delete.notification.error_protected'))
->send();
$action->cancel();
}
}),
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Lunar\Admin\Filament\Resources\CustomerGroupResource\Pages;

use Filament\Actions;
use Filament\Notifications\Notification;
use Lunar\Admin\Filament\Resources\CustomerGroupResource;
use Lunar\Admin\Support\Pages\BaseEditRecord;

Expand All @@ -13,7 +14,16 @@ class EditCustomerGroup extends BaseEditRecord
protected function getDefaultHeaderActions(): array
{
return [
Actions\DeleteAction::make(),
Actions\DeleteAction::make()
->before(function ($record, Actions\DeleteAction $action) {
if ($record->customers->count() > 0) {
Notification::make()
->warning()
->body(__('lunarpanel::customergroup.action.delete.notification.error_protected'))
->send();
$action->cancel();
}
}),
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Lunar\Admin\Filament\Resources\ProductTypeResource\Pages;

use Filament\Actions;
use Filament\Notifications\Notification;
use Lunar\Admin\Filament\Resources\ProductTypeResource;
use Lunar\Admin\Support\Pages\BaseEditRecord;

Expand All @@ -13,7 +14,16 @@ class EditProductType extends BaseEditRecord
protected function getDefaultHeaderActions(): array
{
return [
Actions\DeleteAction::make(),
Actions\DeleteAction::make()
->before(function ($record, Actions\DeleteAction $action) {
if ($record->products->count() > 0) {
Notification::make()
->warning()
->body(__('lunarpanel::producttype.action.delete.notification.error_protected'))
->send();
$action->cancel();
}
}),
];
}

Expand Down

0 comments on commit 1366268

Please sign in to comment.