Add update egg bulk action (#1122)

* add update egg bulk action

* make phpstan happy

* use `before`
This commit is contained in:
Boy132 2025-03-18 17:42:04 +01:00 committed by GitHub
parent a05e330b19
commit 2dbb9a5f9b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 102 additions and 11 deletions

View File

@ -7,6 +7,7 @@ use App\Filament\Components\Actions\ImportEggAction as ImportEggHeaderAction;
use App\Filament\Components\Tables\Actions\ExportEggAction;
use App\Filament\Components\Tables\Actions\ImportEggAction;
use App\Filament\Components\Tables\Actions\UpdateEggAction;
use App\Filament\Components\Tables\Actions\UpdateEggBulkAction;
use App\Filament\Components\Tables\Filters\TagsFilter;
use App\Models\Egg;
use Filament\Actions\CreateAction as CreateHeaderAction;
@ -17,6 +18,7 @@ use Filament\Tables\Actions\EditAction;
use Filament\Tables\Actions\ReplicateAction;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Str;
class ListEggs extends ListRecords
@ -28,7 +30,6 @@ class ListEggs extends ListRecords
return $table
->searchable(true)
->defaultPaginationPageOption(25)
->checkIfRecordIsSelectableUsing(fn (Egg $egg) => $egg->servers_count <= 0)
->columns([
TextColumn::make('id')
->label('Id')
@ -69,7 +70,16 @@ class ListEggs extends ListRecords
->successRedirectUrl(fn (Egg $replica) => EditEgg::getUrl(['record' => $replica])),
])
->groupedBulkActions([
DeleteBulkAction::make(),
DeleteBulkAction::make()
->before(fn (DeleteBulkAction $action, Collection $records) => $action->records($records->filter(function ($egg) {
/** @var Egg $egg */
return $egg->servers_count <= 0;
}))),
UpdateEggBulkAction::make()
->before(fn (UpdateEggBulkAction $action, Collection $records) => $action->records($records->filter(function ($egg) {
/** @var Egg $egg */
return cache()->get("eggs.$egg->uuid.update", false);
}))),
])
->emptyStateIcon('tabler-eggs')
->emptyStateDescription('')

View File

@ -20,7 +20,7 @@ class UpdateEggAction extends Action
{
parent::setUp();
$this->label(trans('admin/egg.update'));
$this->label(trans_choice('admin/egg.update', 1));
$this->icon('tabler-cloud-download');
@ -28,9 +28,9 @@ class UpdateEggAction extends Action
$this->requiresConfirmation();
$this->modalHeading(trans('admin/egg.update_question'));
$this->modalHeading(trans_choice('admin/egg.update_question', 1));
$this->modalDescription(trans('admin/egg.update_description'));
$this->modalDescription(trans_choice('admin/egg.update_description', 1));
$this->modalIconColor('danger');
@ -54,7 +54,7 @@ class UpdateEggAction extends Action
}
Notification::make()
->title(trans('admin/egg.updated'))
->title(trans_choice('admin/egg.updated', 1))
->body($egg->name)
->success()
->send();

View File

@ -0,0 +1,80 @@
<?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\BulkAction;
use Illuminate\Database\Eloquent\Collection;
class UpdateEggBulkAction extends BulkAction
{
public static function getDefaultName(): ?string
{
return 'update';
}
protected function setUp(): void
{
parent::setUp();
$this->label(trans_choice('admin/egg.update', 2));
$this->icon('tabler-cloud-download');
$this->color('success');
$this->requiresConfirmation();
$this->modalHeading(trans_choice('admin/egg.update_question', 2));
$this->modalDescription(trans_choice('admin/egg.update_description', 2));
$this->modalIconColor('danger');
$this->modalSubmitAction(fn (StaticAction $action) => $action->color('danger'));
$this->action(function (Collection $records, EggImporterService $eggImporterService) {
if ($records->count() === 0) {
Notification::make()
->title(trans('admin/egg.no_updates'))
->warning()
->send();
return;
}
$success = 0;
$failed = 0;
/** @var Egg $egg */
foreach ($records as $egg) {
try {
$eggImporterService->fromUrl($egg->update_url, $egg);
$success++;
cache()->forget("eggs.$egg->uuid.update");
} catch (Exception $exception) {
$failed++;
report($exception);
}
}
Notification::make()
->title(trans_choice('admin/egg.updated', 2, ['count' => $success, 'total' => $records->count()]))
->body($failed > 0 ? trans('admin/egg.updated_failed', ['count' => $failed]) : null)
->status($failed > 0 ? 'warning' : 'success')
->persistent()
->send();
});
$this->authorize(fn () => auth()->user()->can('import egg'));
$this->deselectRecordsAfterCompletion();
}
}

View File

@ -79,9 +79,10 @@ return [
'no_servers' => 'No Servers',
'no_servers_help' => 'No Servers are assigned to this Egg.',
'update' => 'Update',
'updated' => 'Egg updated',
'update_failed' => 'Egg Update Failed',
'update_question' => 'Are you sure you want to update this egg?',
'update_description' => 'If you made any changes to the egg they will be overwritten!',
'update' => 'Update|Update selected',
'updated' => 'Egg updated|:count/:total Eggs updated',
'updated_failed' => ':count failed',
'update_question' => 'Are you sure you want to update this egg?|Are you sure you want to update the selected eggs?',
'update_description' => 'If you made any changes to the egg they will be overwritten!|If you made any changes to the eggs they will be overwritten!',
'no_updates' => 'No updates for the selected eggs available',
];