Boy132 eb819032bc
Add own action classes for egg actions (+ add empty state) (#823)
* add own action classes for egg actions

* add empty state to ListEggs

* put Import before Create
2024-12-12 14:29:02 +01:00

68 lines
1.8 KiB
PHP

<?php
namespace App\Filament\Components\Tables\Actions;
use App\Models\Egg;
use App\Services\Eggs\Sharing\EggImporterService;
use Exception;
use Filament\Actions\StaticAction;
use Filament\Notifications\Notification;
use Filament\Tables\Actions\Action;
class UpdateEggAction extends Action
{
public static function getDefaultName(): ?string
{
return 'update';
}
protected function setUp(): void
{
parent::setUp();
$this->label('Update');
$this->icon('tabler-cloud-download');
$this->color('success');
$this->requiresConfirmation();
$this->modalHeading('Are you sure you want to update this egg?');
$this->modalDescription('If you made any changes to the egg they will be overwritten!');
$this->modalIconColor('danger');
$this->modalSubmitAction(fn (StaticAction $action) => $action->color('danger'));
$this->action(function (Egg $egg, EggImporterService $eggImporterService) {
try {
$eggImporterService->fromUrl($egg->update_url, $egg);
cache()->forget("eggs.$egg->uuid.update");
} catch (Exception $exception) {
Notification::make()
->title('Egg Update failed')
->body($exception->getMessage())
->danger()
->send();
report($exception);
return;
}
Notification::make()
->title('Egg updated')
->body($egg->name)
->success()
->send();
});
$this->authorize(fn () => auth()->user()->can('import egg'));
$this->visible(fn (Egg $egg) => cache()->get("eggs.$egg->uuid.update", false));
}
}