- 2.x - scripts

This commit is contained in:
Saeed Vaziry
2024-10-06 20:49:59 +02:00
parent c24b4b7333
commit 8b2338cb41
32 changed files with 936 additions and 79 deletions

View File

@ -0,0 +1,65 @@
<?php
namespace App\Web\Pages\Settings\NotificationChannels\Actions;
use App\Actions\NotificationChannels\AddChannel;
use Exception;
use Filament\Forms\Components\Checkbox;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Get;
use Filament\Notifications\Notification;
class Create
{
public static function form(): array
{
return [
Select::make('provider')
->options(
collect(config('core.notification_channels_providers'))
->mapWithKeys(fn ($provider) => [$provider => $provider])
)
->live()
->reactive()
->rules(fn (Get $get) => AddChannel::rules($get())['provider']),
TextInput::make('label')
->rules(fn (Get $get) => AddChannel::rules($get())['label']),
TextInput::make('webhook_url')
->label('Webhook URL')
->validationAttribute('Webhook URL')
->visible(fn (Get $get) => AddChannel::rules($get())['webhook_url'] ?? false)
->rules(fn (Get $get) => AddChannel::rules($get())['webhook_url']),
TextInput::make('email')
->visible(fn (Get $get) => AddChannel::rules($get())['email'] ?? false)
->rules(fn (Get $get) => AddChannel::rules($get())['email']),
TextInput::make('bot_token')
->label('Bot Token')
->visible(fn (Get $get) => AddChannel::rules($get())['bot_token'] ?? false)
->rules(fn (Get $get) => AddChannel::rules($get())['bot_token']),
TextInput::make('chat_id')
->label('Chat ID')
->visible(fn (Get $get) => AddChannel::rules($get())['chat_id'] ?? false)
->rules(fn (Get $get) => AddChannel::rules($get())['chat_id']),
Checkbox::make('global')
->label('Is Global (Accessible in all projects)'),
];
}
/**
* @throws Exception
*/
public static function action(array $data): void
{
try {
app(AddChannel::class)->add(auth()->user(), $data);
} catch (Exception $e) {
Notification::make()
->title($e->getMessage())
->danger()
->send();
throw $e;
}
}
}

View File

@ -0,0 +1,27 @@
<?php
namespace App\Web\Pages\Settings\NotificationChannels\Actions;
use App\Actions\NotificationChannels\EditChannel;
use App\Models\NotificationChannel;
use Filament\Forms\Components\Checkbox;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Get;
class Edit
{
public static function form(): array
{
return [
TextInput::make('label')
->rules(fn (Get $get) => EditChannel::rules($get())['label']),
Checkbox::make('global')
->label('Is Global (Accessible in all projects)'),
];
}
public static function action(NotificationChannel $channel, array $data): void
{
app(EditChannel::class)->edit($channel, auth()->user(), $data);
}
}

View File

@ -0,0 +1,52 @@
<?php
namespace App\Web\Pages\Settings\NotificationChannels;
use App\Models\NotificationChannel;
use App\Web\Components\Page;
use Filament\Actions\Action;
use Filament\Support\Enums\MaxWidth;
class Index extends Page
{
protected static ?string $navigationGroup = 'Settings';
protected static ?string $slug = 'settings/notification-channels';
protected static ?string $title = 'Notification Channels';
protected static ?string $navigationIcon = 'heroicon-o-bell';
protected static ?int $navigationSort = 8;
public static function canAccess(): bool
{
return auth()->user()?->can('viewAny', NotificationChannel::class) ?? false;
}
public function getWidgets(): array
{
return [
[Widgets\NotificationChannelsList::class],
];
}
protected function getHeaderActions(): array
{
return [
Action::make('add')
->label('Add new Channel')
->icon('heroicon-o-plus')
->modalHeading('Add a new Channel')
->modalSubmitActionLabel('Add')
->form(Actions\Create::form())
->authorize('create', NotificationChannel::class)
->modalWidth(MaxWidth::Large)
->action(function (array $data) {
Actions\Create::action($data);
$this->dispatch('$refresh');
}),
];
}
}

View File

@ -0,0 +1,77 @@
<?php
namespace App\Web\Pages\Settings\NotificationChannels\Widgets;
use App\Models\NotificationChannel;
use App\Web\Pages\Settings\NotificationChannels\Actions\Edit;
use Filament\Support\Enums\MaxWidth;
use Filament\Tables\Actions\DeleteAction;
use Filament\Tables\Actions\EditAction;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Filament\Widgets\TableWidget as Widget;
use Illuminate\Database\Eloquent\Builder;
class NotificationChannelsList extends Widget
{
protected $listeners = [
'$refresh' => 'refreshTable',
];
protected function getTableQuery(): Builder
{
return NotificationChannel::getByProjectId(auth()->user()->current_project_id);
}
protected function getTableColumns(): array
{
return [
IconColumn::make('provider')
->icon(fn (NotificationChannel $record) => 'icon-'.$record->provider)
->width(24),
TextColumn::make('label')
->default(fn (NotificationChannel $record) => $record->label)
->searchable()
->sortable(),
TextColumn::make('id')
->label('Global')
->badge()
->color(fn (NotificationChannel $record) => $record->project_id ? 'gray' : 'success')
->formatStateUsing(function (NotificationChannel $record) {
return $record->project_id ? 'No' : 'Yes';
}),
TextColumn::make('created_at')
->label('Created At')
->formatStateUsing(fn (NotificationChannel $record) => $record->created_at_by_timezone)
->searchable()
->sortable(),
];
}
public function getTable(): Table
{
return $this->table
->heading('')
->actions([
EditAction::make('edit')
->modalHeading('Edit Notification Channel')
->mutateRecordDataUsing(function (array $data, NotificationChannel $record) {
return [
'label' => $record->label,
'global' => ! $record->project_id,
];
})
->form(Edit::form())
->authorize(fn (NotificationChannel $record) => auth()->user()->can('update', $record))
->using(fn (array $data, NotificationChannel $record) => Edit::action($record, $data))
->modalWidth(MaxWidth::Medium),
DeleteAction::make('delete')
->modalHeading('Delete Notification Channel')
->authorize(fn (NotificationChannel $record) => auth()->user()->can('delete', $record))
->using(function (array $data, NotificationChannel $record) {
//
}),
]);
}
}