|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\Livewire\Components\User; |
| 6 | + |
| 7 | +use App\Actions\Discussion\DeleteDiscussionAction; |
| 8 | +use App\Models\Discussion; |
| 9 | +use Filament\Actions\Action; |
| 10 | +use Filament\Actions\Concerns\InteractsWithActions; |
| 11 | +use Filament\Actions\Contracts\HasActions; |
| 12 | +use Filament\Forms\Concerns\InteractsWithForms; |
| 13 | +use Filament\Forms\Contracts\HasForms; |
| 14 | +use Filament\Notifications\Notification; |
| 15 | +use Illuminate\Contracts\Pagination\LengthAwarePaginator; |
| 16 | +use Illuminate\Contracts\View\View; |
| 17 | +use Illuminate\Support\Facades\Auth; |
| 18 | +use Livewire\Attributes\Computed; |
| 19 | +use Livewire\Attributes\Lazy; |
| 20 | +use Livewire\Component; |
| 21 | +use Livewire\WithoutUrlPagination; |
| 22 | +use Livewire\WithPagination; |
| 23 | + |
| 24 | +#[Lazy] |
| 25 | +final class Discussions extends Component implements HasActions, HasForms |
| 26 | +{ |
| 27 | + use InteractsWithActions; |
| 28 | + use InteractsWithForms; |
| 29 | + use WithoutUrlPagination; |
| 30 | + use WithPagination; |
| 31 | + |
| 32 | + #[Computed] |
| 33 | + public function discussions(): LengthAwarePaginator |
| 34 | + { |
| 35 | + return Discussion::with('user') |
| 36 | + ->where('user_id', Auth::id()) |
| 37 | + ->latest() |
| 38 | + ->paginate(10); |
| 39 | + } |
| 40 | + |
| 41 | + public function placeholder(): View |
| 42 | + { |
| 43 | + return view('components.skeletons.discussions'); |
| 44 | + } |
| 45 | + |
| 46 | + public function editAction(): Action |
| 47 | + { |
| 48 | + return Action::make('edit') |
| 49 | + ->label(__('actions.edit')) |
| 50 | + ->color('gray') |
| 51 | + ->badge() |
| 52 | + ->action( |
| 53 | + fn (array $arguments) => $this->dispatch( |
| 54 | + 'openPanel', |
| 55 | + component: 'components.slideovers.discussion-form', |
| 56 | + arguments: ['discussionId' => $arguments['id']] |
| 57 | + ) |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + public function deleteAction(): Action |
| 62 | + { |
| 63 | + return Action::make('delete') |
| 64 | + ->label(__('actions.delete')) |
| 65 | + ->color('danger') |
| 66 | + ->badge() |
| 67 | + ->requiresConfirmation() |
| 68 | + ->action(function (array $arguments): void { |
| 69 | + /** @var Discussion $discussion */ |
| 70 | + $discussion = Discussion::query()->find($arguments['id']); |
| 71 | + |
| 72 | + $this->authorize('delete', $discussion); |
| 73 | + |
| 74 | + app(DeleteDiscussionAction::class)->execute($discussion); |
| 75 | + |
| 76 | + Notification::make() |
| 77 | + ->success() |
| 78 | + ->title(__('notifications.discussion.deleted')) |
| 79 | + ->send(); |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + public function render(): View |
| 84 | + { |
| 85 | + return view('livewire.components.user.discussions'); |
| 86 | + } |
| 87 | +} |
0 commit comments