finished the upgrade to filament 4. completely revamped the frontend with codex, now it looks great!
This commit is contained in:
@@ -1,14 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources;
|
||||
namespace App\Filament\Resources\ApiProviders;
|
||||
|
||||
use App\Filament\Resources\ApiProviderResource\Pages;
|
||||
use App\Filament\Resources\ApiProviderResource\RelationManagers;
|
||||
use App\Filament\Resources\ApiProviders\Pages;
|
||||
use App\Filament\Resources\ApiProviders\RelationManagers;
|
||||
use App\Models\ApiProvider;
|
||||
use Filament\Forms;
|
||||
use Filament\Forms\Form;
|
||||
use Filament\Schemas;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Tables;
|
||||
use Filament\Actions\BulkActionGroup;
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Actions\DeleteBulkAction;
|
||||
use Filament\Actions\EditAction;
|
||||
use Filament\Tables\Table;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\SoftDeletingScope;
|
||||
@@ -19,210 +23,102 @@ use Illuminate\Support\Facades\File;
|
||||
use App\Api\Plugins\ApiPluginInterface;
|
||||
use Filament\Forms\Components\Toggle;
|
||||
use Filament\Tables\Columns\IconColumn;
|
||||
use Filament\Forms\Components\Actions;
|
||||
use Filament\Forms\Components\Actions\Action;
|
||||
use Filament\Forms\Components\ViewField;
|
||||
use Filament\Notifications\Notification;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Blade;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use App\Api\Plugins\PluginLoader;
|
||||
use BackedEnum;
|
||||
|
||||
class ApiProviderResource extends Resource
|
||||
{
|
||||
protected static ?string $model = ApiProvider::class;
|
||||
|
||||
protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
|
||||
protected static BackedEnum|string|null $navigationIcon = 'heroicon-o-rectangle-stack';
|
||||
|
||||
public static function form(Form $form): Form
|
||||
public static function form(Schema $schema): Schema
|
||||
{
|
||||
$plugins = self::getAvailablePlugins();
|
||||
$livewire = $form->getLivewire();
|
||||
return $form
|
||||
->schema([
|
||||
Forms\Components\Section::make()
|
||||
->schema([
|
||||
TextInput::make('name')
|
||||
->label(__('filament.resource.api_provider.form.name'))
|
||||
->required()
|
||||
->maxLength(255),
|
||||
Toggle::make('enabled')
|
||||
->label(__('filament.resource.api_provider.form.enabled'))
|
||||
->default(true),
|
||||
TextInput::make('api_url')
|
||||
->label(__('filament.resource.api_provider.form.api_url'))
|
||||
->required()
|
||||
->url()
|
||||
->maxLength(255),
|
||||
TextInput::make('username')
|
||||
->label(__('filament.resource.api_provider.form.username'))
|
||||
->nullable()
|
||||
->maxLength(255),
|
||||
TextInput::make('password')
|
||||
->label(__('filament.resource.api_provider.form.password'))
|
||||
->password()
|
||||
->nullable()
|
||||
->maxLength(255),
|
||||
TextInput::make('token')
|
||||
->label(__('filament.resource.api_provider.form.token'))
|
||||
->nullable()
|
||||
->maxLength(255),
|
||||
Select::make('plugin')
|
||||
->options($plugins)
|
||||
->nullable()
|
||||
->label(__('filament.resource.api_provider.form.plugin')),
|
||||
Actions::make([
|
||||
Action::make('test_connection')
|
||||
->label(__('filament.resource.api_provider.action.test_connection'))
|
||||
->icon(function (\Livewire\Component $livewire) {
|
||||
return match ($livewire->testResultState) {
|
||||
'success' => 'heroicon-o-check-circle',
|
||||
'failed' => 'heroicon-o-x-circle',
|
||||
default => 'heroicon-o-link',
|
||||
};
|
||||
})
|
||||
->color(function (\Livewire\Component $livewire) {
|
||||
return match ($livewire->testResultState) {
|
||||
'success' => 'success',
|
||||
'failed' => 'danger',
|
||||
default => 'gray',
|
||||
};
|
||||
})
|
||||
->action(function (array $data, Forms\Components\Component $component, \Livewire\Component $livewire) {
|
||||
$formData = $component->getLivewire()->form->getState();
|
||||
$apiUrl = str_replace('127.0.0.1', 'localhost', $formData['api_url'] ?? null);
|
||||
$pluginName = $formData['plugin'] ?? null;
|
||||
$token = $formData['token'] ?? null;
|
||||
|
||||
if (!$pluginName) {
|
||||
Notification::make()
|
||||
->title(__('filament.resource.api_provider.notification.connection_failed'))
|
||||
->body('Please select a plugin first.')
|
||||
->danger()
|
||||
->send();
|
||||
$component->getLivewire()->dispatch('testConnectionFinished', result: 'failed');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// Create a dummy ApiProvider model for the test
|
||||
$dummyApiProvider = new \App\Models\ApiProvider();
|
||||
$dummyApiProvider->api_url = $apiUrl;
|
||||
$dummyApiProvider->token = $token;
|
||||
|
||||
// Load the specific plugin using the PluginLoader
|
||||
$pluginInstance = PluginLoader::getPlugin($pluginName, $dummyApiProvider);
|
||||
|
||||
// Call the testConnection method of the plugin
|
||||
$testResult = $pluginInstance->testConnection([
|
||||
'api_url' => $apiUrl,
|
||||
'token' => $token,
|
||||
'username' => $formData['username'] ?? null,
|
||||
'password' => $formData['password'] ?? null,
|
||||
]);
|
||||
|
||||
if ($testResult) {
|
||||
Notification::make()
|
||||
->title(__('filament.resource.api_provider.notification.connection_successful'))
|
||||
->success()
|
||||
->send();
|
||||
$component->getLivewire()->dispatch('testConnectionFinished', result: 'success');
|
||||
} else {
|
||||
Notification::make()
|
||||
->title(__('filament.resource.api_provider.notification.connection_failed'))
|
||||
->body('Plugin reported connection failed. Check logs for details.')
|
||||
->danger()
|
||||
->send();
|
||||
$component->getLivewire()->dispatch('testConnectionFinished', result: 'failed');
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
Log::error('Plugin test connection failed: An unexpected error occurred.', [
|
||||
'api_url' => $apiUrl,
|
||||
'plugin' => $pluginName,
|
||||
'error_message' => $e->getMessage(),
|
||||
]);
|
||||
Notification::make()
|
||||
->title(__('filament.resource.api_provider.notification.connection_failed'))
|
||||
->body('An unexpected error occurred during plugin test: ' . $e->getMessage())
|
||||
->danger()
|
||||
->send();
|
||||
$component->getLivewire()->dispatch('testConnectionFinished', result: 'failed');
|
||||
}
|
||||
}),
|
||||
]),
|
||||
])
|
||||
return $schema
|
||||
->components([
|
||||
TextInput::make('name')
|
||||
->required()
|
||||
->maxLength(255),
|
||||
Toggle::make('enabled')
|
||||
->default(true),
|
||||
TextInput::make('api_url')
|
||||
->required()
|
||||
->url()
|
||||
->maxLength(255),
|
||||
TextInput::make('username')
|
||||
->nullable()
|
||||
->maxLength(255),
|
||||
TextInput::make('password')
|
||||
->password()
|
||||
->nullable()
|
||||
->maxLength(255),
|
||||
TextInput::make('token')
|
||||
->nullable()
|
||||
->maxLength(255),
|
||||
Select::make('plugin')
|
||||
->options($plugins)
|
||||
->nullable(),
|
||||
]);
|
||||
}
|
||||
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->columns([
|
||||
TextColumn::make('name')->label(__('filament.resource.api_provider.table.name'))->searchable()->sortable(),
|
||||
TextColumn::make('name')->searchable()->sortable(),
|
||||
IconColumn::make('enabled')
|
||||
->label(__('filament.resource.api_provider.table.enabled'))
|
||||
->boolean(),
|
||||
TextColumn::make('api_url')->label(__('filament.resource.api_provider.table.api_url'))->searchable(),
|
||||
TextColumn::make('plugin')->label(__('filament.resource.api_provider.table.plugin'))->searchable()->sortable(),
|
||||
TextColumn::make('api_url')->searchable(),
|
||||
TextColumn::make('plugin')->searchable()->sortable(),
|
||||
])
|
||||
->filters([
|
||||
//
|
||||
])
|
||||
->actions([
|
||||
Tables\Actions\EditAction::make(),
|
||||
EditAction::make(),
|
||||
])
|
||||
->bulkActions([
|
||||
Tables\Actions\BulkActionGroup::make([
|
||||
Tables\Actions\DeleteBulkAction::make(),
|
||||
Tables\Actions\BulkAction::make('enable')
|
||||
->label(__('filament.resource.api_provider.action.enable_selected'))
|
||||
->icon('heroicon-o-check-circle')
|
||||
->action(function (\Illuminate\Support\Collection $records) {
|
||||
$records->each->update(['enabled' => true]);
|
||||
}),
|
||||
Tables\Actions\BulkAction::make('disable')
|
||||
->label(__('filament.resource.api_provider.action.disable_selected'))
|
||||
->icon('heroicon-o-x-circle')
|
||||
->action(function (\Illuminate\Support\Collection $records) {
|
||||
$records->each->update(['enabled' => false]);
|
||||
}),
|
||||
BulkActionGroup::make([
|
||||
DeleteBulkAction::make(),
|
||||
]),
|
||||
])
|
||||
->emptyStateActions([
|
||||
Tables\Actions\CreateAction::make(),
|
||||
CreateAction::make(),
|
||||
]);
|
||||
}
|
||||
public static function getRelations(): array
|
||||
{
|
||||
return [
|
||||
//
|
||||
];
|
||||
|
||||
public static function getRelations(): array
|
||||
{
|
||||
return [
|
||||
//
|
||||
];
|
||||
}
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => Pages\ListApiProviders::route('/'),
|
||||
'create' => Pages\CreateApiProvider::route('/create'),
|
||||
'edit' => Pages\EditApiProvider::route('/{record}/edit'),
|
||||
];
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => Pages\ListApiProviders::route('/'),
|
||||
'create' => Pages\CreateApiProvider::route('/create'),
|
||||
'edit' => Pages\EditApiProvider::route('/{record}/edit'),
|
||||
];
|
||||
}
|
||||
protected static function getAvailablePlugins(): array
|
||||
{
|
||||
$plugins = [];
|
||||
$path = app_path('Api/Plugins');
|
||||
$files = File::files($path);
|
||||
|
||||
foreach ($files as $file) {
|
||||
$filename = $file->getFilenameWithoutExtension();
|
||||
if (in_array($filename, ['ApiPluginInterface', 'PluginLoader'])) {
|
||||
continue;
|
||||
}
|
||||
$class = "App\Api\Plugins\\" . $filename;
|
||||
if (class_exists($class) && in_array(ApiPluginInterface::class, class_implements($class))) {
|
||||
// Do not instantiate here, just get identifier and name if possible statically or by convention
|
||||
// For now, we'll use filename as identifier and name
|
||||
$plugins[$filename] = $filename;
|
||||
|
||||
protected static function getAvailablePlugins(): array
|
||||
{
|
||||
$plugins = [];
|
||||
$path = app_path('Api/Plugins');
|
||||
$files = File::files($path);
|
||||
|
||||
foreach ($files as $file) {
|
||||
$filename = $file->getFilenameWithoutExtension();
|
||||
if (in_array($filename, ['ApiPluginInterface', 'PluginLoader'])) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
return $plugins;
|
||||
}
|
||||
}
|
||||
$class = "App\Api\Plugins\\" . $filename;
|
||||
if (class_exists($class) && in_array(ApiPluginInterface::class, class_implements($class))) {
|
||||
$plugins[$filename] = $filename;
|
||||
}
|
||||
}
|
||||
return $plugins;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\ApiProviderResource\Pages;
|
||||
namespace App\Filament\Resources\ApiProviders\Pages;
|
||||
|
||||
use App\Filament\Resources\ApiProviderResource;
|
||||
use App\Filament\Resources\ApiProviders\ApiProviderResource;
|
||||
use Filament\Actions;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
use Livewire\Attributes\On;
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\ApiProviderResource\Pages;
|
||||
namespace App\Filament\Resources\ApiProviders\Pages;
|
||||
|
||||
use App\Filament\Resources\ApiProviderResource;
|
||||
use App\Filament\Resources\ApiProviders\ApiProviderResource;
|
||||
use Filament\Actions;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
use Livewire\Attributes\On;
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\ApiProviderResource\Pages;
|
||||
namespace App\Filament\Resources\ApiProviders\Pages;
|
||||
|
||||
use App\Filament\Resources\ApiProviderResource;
|
||||
use App\Filament\Resources\ApiProviders\ApiProviderResource;
|
||||
use Filament\Actions;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
use Illuminate\Contracts\Pagination\Paginator;
|
||||
|
||||
Reference in New Issue
Block a user