diff --git a/app/Filament/Admin/Resources/EggResource/Pages/EditEgg.php b/app/Filament/Admin/Resources/EggResource/Pages/EditEgg.php index 9ae02c08b6..d1f064b5c8 100644 --- a/app/Filament/Admin/Resources/EggResource/Pages/EditEgg.php +++ b/app/Filament/Admin/Resources/EggResource/Pages/EditEgg.php @@ -5,17 +5,14 @@ use AbdelhamidErrahmouni\FilamentMonacoEditor\MonacoEditor; use App\Filament\Admin\Resources\EggResource; use App\Filament\Admin\Resources\EggResource\RelationManagers\ServersRelationManager; +use App\Filament\Components\Actions\ExportEggAction; +use App\Filament\Components\Actions\ImportEggAction; use App\Models\Egg; -use App\Services\Eggs\Sharing\EggExporterService; -use App\Services\Eggs\Sharing\EggImporterService; -use Exception; -use Filament\Actions; +use Filament\Actions\DeleteAction; use Filament\Forms\Components\Checkbox; use Filament\Forms\Components\Fieldset; -use Filament\Forms\Components\FileUpload; use Filament\Forms\Components\Hidden; use Filament\Forms\Components\KeyValue; -use Filament\Forms\Components\Placeholder; use Filament\Forms\Components\Repeater; use Filament\Forms\Components\Select; use Filament\Forms\Components\Tabs; @@ -26,7 +23,6 @@ use Filament\Forms\Components\Toggle; use Filament\Forms\Form; use Filament\Forms\Set; -use Filament\Notifications\Notification; use Filament\Resources\Pages\EditRecord; class EditEgg extends EditRecord @@ -242,83 +238,11 @@ public function form(Form $form): Form protected function getHeaderActions(): array { return [ - Actions\DeleteAction::make('deleteEgg') + DeleteAction::make() ->disabled(fn (Egg $egg): bool => $egg->servers()->count() > 0) ->label(fn (Egg $egg): string => $egg->servers()->count() <= 0 ? 'Delete' : 'In Use'), - Actions\Action::make('exportEgg') - ->label('Export') - ->color('primary') - ->action(fn (EggExporterService $service, Egg $egg) => response()->streamDownload(function () use ($service, $egg) { - echo $service->handle($egg->id); - }, 'egg-' . $egg->getKebabName() . '.json')) - ->authorize(fn () => auth()->user()->can('export egg')), - Actions\Action::make('importEgg') - ->label('Import') - ->form([ - Placeholder::make('warning') - ->label('This will overwrite the current egg to the one you upload.'), - Tabs::make('Tabs') - ->tabs([ - Tab::make('From File') - ->icon('tabler-file-upload') - ->schema([ - FileUpload::make('egg') - ->label('Egg') - ->hint('eg. minecraft.json') - ->acceptedFileTypes(['application/json']) - ->storeFiles(false), - ]), - Tab::make('From URL') - ->icon('tabler-world-upload') - ->schema([ - TextInput::make('url') - ->label('URL') - ->default(fn (Egg $egg): ?string => $egg->update_url) - ->hint('Link to the egg file (eg. minecraft.json)') - ->url(), - ]), - ]) - ->contained(false), - - ]) - ->action(function (array $data, Egg $egg, EggImporterService $eggImportService): void { - if (!empty($data['egg'])) { - try { - $eggImportService->fromFile($data['egg'], $egg); - } catch (Exception $exception) { - Notification::make() - ->title('Import Failed') - ->body($exception->getMessage()) - ->danger() // Will Robinson - ->send(); - - report($exception); - - return; - } - } elseif (!empty($data['url'])) { - try { - $eggImportService->fromUrl($data['url'], $egg); - } catch (Exception $exception) { - Notification::make() - ->title('Import Failed') - ->body($exception->getMessage()) - ->danger() - ->send(); - - report($exception); - - return; - } - } - - $this->refreshForm(); - Notification::make() - ->title('Import Success') - ->success() - ->send(); - }) - ->authorize(fn () => auth()->user()->can('import egg')), + ExportEggAction::make(), + ImportEggAction::make(), $this->getSaveFormAction()->formId('form'), ]; } diff --git a/app/Filament/Admin/Resources/EggResource/Pages/ListEggs.php b/app/Filament/Admin/Resources/EggResource/Pages/ListEggs.php index e504b4b8f3..4414e8d737 100644 --- a/app/Filament/Admin/Resources/EggResource/Pages/ListEggs.php +++ b/app/Filament/Admin/Resources/EggResource/Pages/ListEggs.php @@ -3,24 +3,19 @@ namespace App\Filament\Admin\Resources\EggResource\Pages; use App\Filament\Admin\Resources\EggResource; +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\Models\Egg; -use App\Services\Eggs\Sharing\EggExporterService; -use App\Services\Eggs\Sharing\EggImporterService; -use Exception; -use Filament\Actions; -use Filament\Forms\Components\FileUpload; -use Filament\Forms\Components\Tabs; -use Filament\Forms\Components\Tabs\Tab; -use Filament\Forms\Components\TextInput; -use Filament\Notifications\Notification; +use Filament\Actions\CreateAction as CreateHeaderAction; use Filament\Resources\Pages\ListRecords; -use Filament\Tables\Actions\Action; use Filament\Tables\Actions\BulkActionGroup; +use Filament\Tables\Actions\CreateAction; use Filament\Tables\Actions\DeleteBulkAction; use Filament\Tables\Actions\EditAction; use Filament\Tables\Columns\TextColumn; use Filament\Tables\Table; -use Livewire\Features\SupportFileUploads\TemporaryUploadedFile; class ListEggs extends ListRecords { @@ -49,132 +44,31 @@ public function table(Table $table): Table ]) ->actions([ EditAction::make(), - Action::make('export') - ->icon('tabler-download') - ->label('Export') - ->color('primary') - ->action(fn (EggExporterService $service, Egg $egg) => response()->streamDownload(function () use ($service, $egg) { - echo $service->handle($egg->id); - }, 'egg-' . $egg->getKebabName() . '.json')) - ->authorize(fn () => auth()->user()->can('export egg')), - Action::make('update') - ->icon('tabler-cloud-download') - ->label('Update') - ->color('success') - ->requiresConfirmation() - ->modalHeading('Are you sure you want to update this egg?') - ->modalDescription('If you made any changes to the egg they will be overwritten!') - ->modalIconColor('danger') - ->modalSubmitAction(fn (Actions\StaticAction $action) => $action->color('danger')) - ->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') - ->success() - ->send(); - }) - ->authorize(fn () => auth()->user()->can('import egg')) - ->visible(fn (Egg $egg) => cache()->get("eggs.{$egg->uuid}.update", false)), + ExportEggAction::make(), + UpdateEggAction::make(), ]) ->bulkActions([ BulkActionGroup::make([ DeleteBulkAction::make() ->authorize(fn () => auth()->user()->can('delete egg')), ]), + ]) + ->emptyStateIcon('tabler-eggs') + ->emptyStateDescription('') + ->emptyStateHeading('No Eggs') + ->emptyStateActions([ + CreateAction::make() + ->label('Create Egg'), + ImportEggAction::make(), ]); } protected function getHeaderActions(): array { return [ - Actions\CreateAction::make('create')->label('Create Egg'), - - Actions\Action::make('import') - ->label('Import') - ->form([ - Tabs::make('Tabs') - ->tabs([ - Tab::make('From File') - ->icon('tabler-file-upload') - ->schema([ - FileUpload::make('egg') - ->label('Egg') - ->hint('This should be the json file ( egg-minecraft.json )') - ->acceptedFileTypes(['application/json']) - ->storeFiles(false) - ->multiple(), - ]), - Tab::make('From URL') - ->icon('tabler-world-upload') - ->schema([ - TextInput::make('url') - ->label('URL') - ->hint('This URL should point to a single json file') - ->url(), - ]), - ]) - ->contained(false), - - ]) - ->action(function (array $data, EggImporterService $eggImportService): void { - if (!empty($data['egg'])) { - /** @var TemporaryUploadedFile[] $eggFile */ - $eggFile = $data['egg']; - - foreach ($eggFile as $file) { - try { - $eggImportService->fromFile($file); - } catch (Exception $exception) { - Notification::make() - ->title('Import Failed') - ->body($exception->getMessage()) - ->danger() - ->send(); - - report($exception); - - return; - } - } - } - - if (!empty($data['url'])) { - try { - $eggImportService->fromUrl($data['url']); - } catch (Exception $exception) { - Notification::make() - ->title('Import Failed') - ->body($exception->getMessage()) - ->danger() - ->send(); - - report($exception); - - return; - } - } - - Notification::make() - ->title('Import Success') - ->success() - ->send(); - }) - ->authorize(fn () => auth()->user()->can('import egg')), + ImportEggHeaderAction::make(), + CreateHeaderAction::make() + ->label('Create Egg'), ]; } } diff --git a/app/Filament/Components/Actions/ExportEggAction.php b/app/Filament/Components/Actions/ExportEggAction.php new file mode 100644 index 0000000000..57588d7858 --- /dev/null +++ b/app/Filament/Components/Actions/ExportEggAction.php @@ -0,0 +1,28 @@ +label('Export'); + + $this->authorize(fn () => auth()->user()->can('export egg')); + + $this->action(fn (EggExporterService $service, Egg $egg) => response()->streamDownload(function () use ($service, $egg) { + echo $service->handle($egg->id); + }, 'egg-' . $egg->getKebabName() . '.json')); + } +} diff --git a/app/Filament/Components/Actions/ImportEggAction.php b/app/Filament/Components/Actions/ImportEggAction.php new file mode 100644 index 0000000000..f3026caed7 --- /dev/null +++ b/app/Filament/Components/Actions/ImportEggAction.php @@ -0,0 +1,87 @@ +label('Import'); + + $this->authorize(fn () => auth()->user()->can('import egg')); + + $this->form([ + Tabs::make('Tabs') + ->contained(false) + ->tabs([ + Tab::make('From File') + ->icon('tabler-file-upload') + ->schema([ + FileUpload::make('egg') + ->label('Egg') + ->hint('This should be the json file ( egg-minecraft.json )') + ->acceptedFileTypes(['application/json']) + ->storeFiles(false) + ->multiple(), + ]), + Tab::make('From URL') + ->icon('tabler-world-upload') + ->schema([ + TextInput::make('url') + ->label('URL') + ->hint('This URL should point to a single json file') + ->url(), + ]), + ]), + ]); + + $this->action(function (array $data, EggImporterService $eggImportService): void { + try { + if (!empty($data['egg'])) { + /** @var TemporaryUploadedFile[] $eggFile */ + $eggFile = $data['egg']; + + foreach ($eggFile as $file) { + $eggImportService->fromFile($file); + } + } + + if (!empty($data['url'])) { + $eggImportService->fromUrl($data['url']); + } + } catch (Exception $exception) { + Notification::make() + ->title('Import Failed') + ->body($exception->getMessage()) + ->danger() + ->send(); + + report($exception); + + return; + } + + Notification::make() + ->title('Import Success') + ->success() + ->send(); + }); + } +} diff --git a/app/Filament/Components/Tables/Actions/ExportEggAction.php b/app/Filament/Components/Tables/Actions/ExportEggAction.php new file mode 100644 index 0000000000..2df4a9d9dd --- /dev/null +++ b/app/Filament/Components/Tables/Actions/ExportEggAction.php @@ -0,0 +1,30 @@ +label('Export'); + + $this->icon('tabler-download'); + + $this->authorize(fn () => auth()->user()->can('export egg')); + + $this->action(fn (EggExporterService $service, Egg $egg) => response()->streamDownload(function () use ($service, $egg) { + echo $service->handle($egg->id); + }, 'egg-' . $egg->getKebabName() . '.json')); + } +} diff --git a/app/Filament/Components/Tables/Actions/ImportEggAction.php b/app/Filament/Components/Tables/Actions/ImportEggAction.php new file mode 100644 index 0000000000..e8a82a3013 --- /dev/null +++ b/app/Filament/Components/Tables/Actions/ImportEggAction.php @@ -0,0 +1,87 @@ +label('Import'); + + $this->authorize(fn () => auth()->user()->can('import egg')); + + $this->form([ + Tabs::make('Tabs') + ->contained(false) + ->tabs([ + Tab::make('From File') + ->icon('tabler-file-upload') + ->schema([ + FileUpload::make('egg') + ->label('Egg') + ->hint('This should be the json file ( egg-minecraft.json )') + ->acceptedFileTypes(['application/json']) + ->storeFiles(false) + ->multiple(), + ]), + Tab::make('From URL') + ->icon('tabler-world-upload') + ->schema([ + TextInput::make('url') + ->label('URL') + ->hint('This URL should point to a single json file') + ->url(), + ]), + ]), + ]); + + $this->action(function (array $data, EggImporterService $eggImportService): void { + try { + if (!empty($data['egg'])) { + /** @var TemporaryUploadedFile[] $eggFile */ + $eggFile = $data['egg']; + + foreach ($eggFile as $file) { + $eggImportService->fromFile($file); + } + } + + if (!empty($data['url'])) { + $eggImportService->fromUrl($data['url']); + } + } catch (Exception $exception) { + Notification::make() + ->title('Import Failed') + ->body($exception->getMessage()) + ->danger() + ->send(); + + report($exception); + + return; + } + + Notification::make() + ->title('Import Success') + ->success() + ->send(); + }); + } +} diff --git a/app/Filament/Components/Tables/Actions/UpdateEggAction.php b/app/Filament/Components/Tables/Actions/UpdateEggAction.php new file mode 100644 index 0000000000..5f0af2b1dd --- /dev/null +++ b/app/Filament/Components/Tables/Actions/UpdateEggAction.php @@ -0,0 +1,67 @@ +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)); + } +}