mirror of
https://github.com/pelican-dev/panel.git
synced 2025-05-20 00:34:44 +02:00
Add url Repeater
to ImportEggHeaderAction
(#1071)
* Add url `Repeater` to `ImportEggAction` * Addtranslation * Requested changes * Only allow `multiple` when not editing `Egg` * Only `deletable` & `grid` if `multiple` * Fix `FileUpload` & Make sure its a json file
This commit is contained in:
parent
98c36c4cc3
commit
ea5914f362
@ -257,7 +257,8 @@ class EditEgg extends EditRecord
|
|||||||
->disabled(fn (Egg $egg): bool => $egg->servers()->count() > 0)
|
->disabled(fn (Egg $egg): bool => $egg->servers()->count() > 0)
|
||||||
->label(fn (Egg $egg): string => $egg->servers()->count() <= 0 ? trans('filament-actions::delete.single.label') : trans('admin/egg.in_use')),
|
->label(fn (Egg $egg): string => $egg->servers()->count() <= 0 ? trans('filament-actions::delete.single.label') : trans('admin/egg.in_use')),
|
||||||
ExportEggAction::make(),
|
ExportEggAction::make(),
|
||||||
ImportEggAction::make(),
|
ImportEggAction::make()
|
||||||
|
->multiple(false),
|
||||||
$this->getSaveFormAction()->formId('form'),
|
$this->getSaveFormAction()->formId('form'),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
@ -75,14 +75,16 @@ class ListEggs extends ListRecords
|
|||||||
->emptyStateHeading(trans('admin/egg.no_eggs'))
|
->emptyStateHeading(trans('admin/egg.no_eggs'))
|
||||||
->emptyStateActions([
|
->emptyStateActions([
|
||||||
CreateAction::make(),
|
CreateAction::make(),
|
||||||
ImportEggAction::make(),
|
ImportEggAction::make()
|
||||||
|
->multiple(),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getHeaderActions(): array
|
protected function getHeaderActions(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
ImportEggHeaderAction::make(),
|
ImportEggHeaderAction::make()
|
||||||
|
->multiple(),
|
||||||
CreateHeaderAction::make(),
|
CreateHeaderAction::make(),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
@ -4,13 +4,16 @@ namespace App\Filament\Components\Actions;
|
|||||||
|
|
||||||
use App\Models\Egg;
|
use App\Models\Egg;
|
||||||
use App\Services\Eggs\Sharing\EggImporterService;
|
use App\Services\Eggs\Sharing\EggImporterService;
|
||||||
|
use Closure;
|
||||||
use Exception;
|
use Exception;
|
||||||
use Filament\Actions\Action;
|
use Filament\Actions\Action;
|
||||||
use Filament\Forms\Components\FileUpload;
|
use Filament\Forms\Components\FileUpload;
|
||||||
|
use Filament\Forms\Components\Repeater;
|
||||||
use Filament\Forms\Components\Tabs;
|
use Filament\Forms\Components\Tabs;
|
||||||
use Filament\Forms\Components\Tabs\Tab;
|
use Filament\Forms\Components\Tabs\Tab;
|
||||||
use Filament\Forms\Components\TextInput;
|
use Filament\Forms\Components\TextInput;
|
||||||
use Filament\Notifications\Notification;
|
use Filament\Notifications\Notification;
|
||||||
|
use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;
|
||||||
|
|
||||||
class ImportEggAction extends Action
|
class ImportEggAction extends Action
|
||||||
{
|
{
|
||||||
@ -27,6 +30,53 @@ class ImportEggAction extends Action
|
|||||||
|
|
||||||
$this->authorize(fn () => auth()->user()->can('import egg'));
|
$this->authorize(fn () => auth()->user()->can('import egg'));
|
||||||
|
|
||||||
|
$this->action(function (array $data, EggImporterService $eggImportService): void {
|
||||||
|
$eggs = array_merge($data['files'], collect($data['urls'])->flatten()->whereNotNull()->unique()->all());
|
||||||
|
if (empty($eggs)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
[$success, $failed] = [collect(), collect()];
|
||||||
|
|
||||||
|
foreach ($eggs as $egg) {
|
||||||
|
if ($egg instanceof TemporaryUploadedFile) {
|
||||||
|
$name = str($egg->getClientOriginalName())->afterLast('egg-')->before('.json')->headline();
|
||||||
|
$method = 'fromFile';
|
||||||
|
} else {
|
||||||
|
$egg = str($egg);
|
||||||
|
$egg = $egg->contains('github.com') ? $egg->replaceFirst('blob', 'raw') : $egg;
|
||||||
|
$name = $egg->afterLast('/egg-')->before('.json')->headline();
|
||||||
|
$method = 'fromUrl';
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
$eggImportService->$method($egg);
|
||||||
|
$success->push($name);
|
||||||
|
} catch (Exception $exception) {
|
||||||
|
$failed->push($name);
|
||||||
|
report($exception);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($failed->count() > 0) {
|
||||||
|
Notification::make()
|
||||||
|
->title(trans('admin/egg.import.import_failed'))
|
||||||
|
->body($failed->join(', '))
|
||||||
|
->danger()
|
||||||
|
->send();
|
||||||
|
}
|
||||||
|
if ($success->count() > 0) {
|
||||||
|
Notification::make()
|
||||||
|
->title(trans('admin/egg.import.import_success'))
|
||||||
|
->body($success->join(', '))
|
||||||
|
->success()
|
||||||
|
->send();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function multiple(bool|Closure $condition = true): static
|
||||||
|
{
|
||||||
|
$isMultiple = (bool) $this->evaluate($condition);
|
||||||
$this->form([
|
$this->form([
|
||||||
Tabs::make('Tabs')
|
Tabs::make('Tabs')
|
||||||
->contained(false)
|
->contained(false)
|
||||||
@ -34,54 +84,40 @@ class ImportEggAction extends Action
|
|||||||
Tab::make(trans('admin/egg.import.file'))
|
Tab::make(trans('admin/egg.import.file'))
|
||||||
->icon('tabler-file-upload')
|
->icon('tabler-file-upload')
|
||||||
->schema([
|
->schema([
|
||||||
FileUpload::make('egg')
|
FileUpload::make('files')
|
||||||
->label('Egg')
|
->label(trans('admin/egg.model_label'))
|
||||||
->hint(trans('admin/egg.import.egg_help'))
|
->hint(trans('admin/egg.import.egg_help'))
|
||||||
->acceptedFileTypes(['application/json'])
|
->acceptedFileTypes(['application/json'])
|
||||||
|
->preserveFilenames()
|
||||||
|
->previewable(false)
|
||||||
->storeFiles(false)
|
->storeFiles(false)
|
||||||
->multiple(),
|
->multiple($isMultiple),
|
||||||
]),
|
]),
|
||||||
Tab::make(trans('admin/egg.import.url'))
|
Tab::make(trans('admin/egg.import.url'))
|
||||||
->icon('tabler-world-upload')
|
->icon('tabler-world-upload')
|
||||||
->schema([
|
->schema([
|
||||||
TextInput::make('url')
|
Repeater::make('urls')
|
||||||
->default(fn (Egg $egg) => $egg->update_url)
|
->itemLabel(fn (array $state) => str($state['url'])->afterLast('/egg-')->before('.json')->headline())
|
||||||
->label(trans('admin/egg.import.url'))
|
|
||||||
->hint(trans('admin/egg.import.url_help'))
|
->hint(trans('admin/egg.import.url_help'))
|
||||||
->url(),
|
->addActionLabel(trans('admin/egg.import.add_url'))
|
||||||
|
->grid($isMultiple ? 2 : null)
|
||||||
|
->reorderable(false)
|
||||||
|
->addable($isMultiple)
|
||||||
|
->deletable(fn (array $state) => count($state) > 1)
|
||||||
|
->schema([
|
||||||
|
TextInput::make('url')
|
||||||
|
->default(fn (Egg $egg) => $egg->update_url)
|
||||||
|
->live()
|
||||||
|
->label(trans('admin/egg.import.url'))
|
||||||
|
->placeholder('https://github.com/pelican-eggs/generic/blob/main/nodejs/egg-node-js-generic.json')
|
||||||
|
->url()
|
||||||
|
->endsWith('.json')
|
||||||
|
->validationAttribute(trans('admin/egg.import.url')),
|
||||||
|
]),
|
||||||
]),
|
]),
|
||||||
]),
|
]),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->action(function (array $data, EggImporterService $eggImportService): void {
|
return $this;
|
||||||
try {
|
|
||||||
if (!empty($data['egg'])) {
|
|
||||||
$eggFile = $data['egg'];
|
|
||||||
|
|
||||||
foreach ($eggFile as $file) {
|
|
||||||
$eggImportService->fromFile($file);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!empty($data['url'])) {
|
|
||||||
$eggImportService->fromUrl($data['url']);
|
|
||||||
}
|
|
||||||
} catch (Exception $exception) {
|
|
||||||
Notification::make()
|
|
||||||
->title(trans('admin/egg.import.import_failed'))
|
|
||||||
->body($exception->getMessage())
|
|
||||||
->danger()
|
|
||||||
->send();
|
|
||||||
|
|
||||||
report($exception);
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Notification::make()
|
|
||||||
->title(trans('admin/egg.import.import_success'))
|
|
||||||
->success()
|
|
||||||
->send();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,14 +2,18 @@
|
|||||||
|
|
||||||
namespace App\Filament\Components\Tables\Actions;
|
namespace App\Filament\Components\Tables\Actions;
|
||||||
|
|
||||||
|
use App\Models\Egg;
|
||||||
use App\Services\Eggs\Sharing\EggImporterService;
|
use App\Services\Eggs\Sharing\EggImporterService;
|
||||||
|
use Closure;
|
||||||
use Exception;
|
use Exception;
|
||||||
use Filament\Forms\Components\FileUpload;
|
use Filament\Forms\Components\FileUpload;
|
||||||
|
use Filament\Forms\Components\Repeater;
|
||||||
use Filament\Forms\Components\Tabs;
|
use Filament\Forms\Components\Tabs;
|
||||||
use Filament\Forms\Components\Tabs\Tab;
|
use Filament\Forms\Components\Tabs\Tab;
|
||||||
use Filament\Forms\Components\TextInput;
|
use Filament\Forms\Components\TextInput;
|
||||||
use Filament\Notifications\Notification;
|
use Filament\Notifications\Notification;
|
||||||
use Filament\Tables\Actions\Action;
|
use Filament\Tables\Actions\Action;
|
||||||
|
use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;
|
||||||
|
|
||||||
class ImportEggAction extends Action
|
class ImportEggAction extends Action
|
||||||
{
|
{
|
||||||
@ -26,60 +30,94 @@ class ImportEggAction extends Action
|
|||||||
|
|
||||||
$this->authorize(fn () => auth()->user()->can('import egg'));
|
$this->authorize(fn () => auth()->user()->can('import egg'));
|
||||||
|
|
||||||
|
$this->action(function (array $data, EggImporterService $eggImportService): void {
|
||||||
|
$eggs = array_merge($data['files'], collect($data['urls'])->flatten()->whereNotNull()->unique()->all());
|
||||||
|
if (empty($eggs)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
[$success, $failed] = [collect(), collect()];
|
||||||
|
|
||||||
|
foreach ($eggs as $egg) {
|
||||||
|
if ($egg instanceof TemporaryUploadedFile) {
|
||||||
|
$name = str($egg->getClientOriginalName())->afterLast('egg-')->before('.json')->headline();
|
||||||
|
$method = 'fromFile';
|
||||||
|
} else {
|
||||||
|
$egg = str($egg);
|
||||||
|
$egg = $egg->contains('github.com') ? $egg->replaceFirst('blob', 'raw') : $egg;
|
||||||
|
$name = $egg->afterLast('/egg-')->before('.json')->headline();
|
||||||
|
$method = 'fromUrl';
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
$eggImportService->$method($egg);
|
||||||
|
$success->push($name);
|
||||||
|
} catch (Exception $exception) {
|
||||||
|
$failed->push($name);
|
||||||
|
report($exception);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($failed->count() > 0) {
|
||||||
|
Notification::make()
|
||||||
|
->title(trans('admin/egg.import.import_failed'))
|
||||||
|
->body($failed->join(', '))
|
||||||
|
->danger()
|
||||||
|
->send();
|
||||||
|
}
|
||||||
|
if ($success->count() > 0) {
|
||||||
|
Notification::make()
|
||||||
|
->title(trans('admin/egg.import.import_success'))
|
||||||
|
->body($success->join(', '))
|
||||||
|
->success()
|
||||||
|
->send();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function multiple(bool|Closure $condition = true): static
|
||||||
|
{
|
||||||
|
$isMultiple = (bool) $this->evaluate($condition);
|
||||||
$this->form([
|
$this->form([
|
||||||
Tabs::make()
|
Tabs::make('Tabs')
|
||||||
->contained(false)
|
->contained(false)
|
||||||
->tabs([
|
->tabs([
|
||||||
Tab::make(trans('admin/egg.import.file'))
|
Tab::make(trans('admin/egg.import.file'))
|
||||||
->icon('tabler-file-upload')
|
->icon('tabler-file-upload')
|
||||||
->schema([
|
->schema([
|
||||||
FileUpload::make('egg')
|
FileUpload::make('files')
|
||||||
->label(trans('admin/egg.model_label'))
|
->label(trans('admin/egg.model_label'))
|
||||||
->hint(trans('admin/egg.import.egg_help'))
|
->hint(trans('admin/egg.import.egg_help'))
|
||||||
->acceptedFileTypes(['application/json'])
|
->acceptedFileTypes(['application/json'])
|
||||||
|
->preserveFilenames()
|
||||||
|
->previewable(false)
|
||||||
->storeFiles(false)
|
->storeFiles(false)
|
||||||
->multiple(),
|
->multiple($isMultiple),
|
||||||
]),
|
]),
|
||||||
Tab::make(trans('admin/egg.import.url'))
|
Tab::make(trans('admin/egg.import.url'))
|
||||||
->icon('tabler-world-upload')
|
->icon('tabler-world-upload')
|
||||||
->schema([
|
->schema([
|
||||||
TextInput::make('url')
|
Repeater::make('urls')
|
||||||
->label(trans('admin/egg.import.url'))
|
->itemLabel(fn (array $state) => str($state['url'])->afterLast('/egg-')->before('.json')->headline())
|
||||||
->hint(trans('admin/egg.import.url_help'))
|
->hint(trans('admin/egg.import.url_help'))
|
||||||
->url(),
|
->addActionLabel(trans('admin/egg.import.add_url'))
|
||||||
|
->grid($isMultiple ? 2 : null)
|
||||||
|
->reorderable(false)
|
||||||
|
->addable($isMultiple)
|
||||||
|
->deletable(fn (array $state) => count($state) > 1)
|
||||||
|
->schema([
|
||||||
|
TextInput::make('url')
|
||||||
|
->default(fn (Egg $egg) => $egg->update_url)
|
||||||
|
->live()
|
||||||
|
->label(trans('admin/egg.import.url'))
|
||||||
|
->placeholder('https://github.com/pelican-eggs/generic/blob/main/nodejs/egg-node-js-generic.json')
|
||||||
|
->url()
|
||||||
|
->endsWith('.json')
|
||||||
|
->validationAttribute(trans('admin/egg.import.url')),
|
||||||
|
]),
|
||||||
]),
|
]),
|
||||||
]),
|
]),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->action(function (array $data, EggImporterService $eggImportService): void {
|
return $this;
|
||||||
try {
|
|
||||||
if (!empty($data['egg'])) {
|
|
||||||
$eggFile = $data['egg'];
|
|
||||||
|
|
||||||
foreach ($eggFile as $file) {
|
|
||||||
$eggImportService->fromFile($file);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!empty($data['url'])) {
|
|
||||||
$eggImportService->fromUrl($data['url']);
|
|
||||||
}
|
|
||||||
} catch (Exception $exception) {
|
|
||||||
Notification::make()
|
|
||||||
->title(trans('admin/egg.import.import_failed'))
|
|
||||||
->body($exception->getMessage())
|
|
||||||
->danger()
|
|
||||||
->send();
|
|
||||||
|
|
||||||
report($exception);
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Notification::make()
|
|
||||||
->title(trans('admin/egg.import.import_success'))
|
|
||||||
->success()
|
|
||||||
->send();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,7 @@ return [
|
|||||||
'url' => 'URL',
|
'url' => 'URL',
|
||||||
'egg_help' => 'This should be the raw .json file ( egg-minecraft.json )',
|
'egg_help' => 'This should be the raw .json file ( egg-minecraft.json )',
|
||||||
'url_help' => 'URLs must point directly to the raw .json file',
|
'url_help' => 'URLs must point directly to the raw .json file',
|
||||||
|
'add_url' => 'New URL',
|
||||||
'import_failed' => 'Import Failed',
|
'import_failed' => 'Import Failed',
|
||||||
'import_success' => 'Import Success',
|
'import_success' => 'Import Success',
|
||||||
],
|
],
|
||||||
|
Loading…
x
Reference in New Issue
Block a user