|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\Livewire; |
| 6 | + |
| 7 | +use App\Actions\ReportSpamAction; |
| 8 | +use App\Models\SpamReport; |
| 9 | +use Illuminate\Support\Facades\Auth; |
| 10 | +use Livewire\Component; |
| 11 | + |
| 12 | +final class ReportSpam extends Component |
| 13 | +{ |
| 14 | + public $recordId; |
| 15 | + |
| 16 | + public $recordType; |
| 17 | + |
| 18 | + public $reason; |
| 19 | + |
| 20 | + public $alreadyReported; |
| 21 | + |
| 22 | + public $isModalOpen = false; |
| 23 | + |
| 24 | + public function mount($recordId, $recordType): void |
| 25 | + { |
| 26 | + $this->recordId = $recordId; |
| 27 | + $this->recordType = $recordType; |
| 28 | + $this->alreadyReported = SpamReport::where('user_id', Auth::id()) |
| 29 | + ->where('reportable_id', $recordId) |
| 30 | + ->where('reportable_type', $recordType) |
| 31 | + ->exists(); |
| 32 | + } |
| 33 | + |
| 34 | + public function openModal(): void |
| 35 | + { |
| 36 | + $this->isModalOpen = true; |
| 37 | + } |
| 38 | + |
| 39 | + public function closeModal(): void |
| 40 | + { |
| 41 | + $this->isModalOpen = false; |
| 42 | + $this->reset('reason'); |
| 43 | + } |
| 44 | + |
| 45 | + public function report(): void |
| 46 | + { |
| 47 | + $user = Auth::user(); |
| 48 | + |
| 49 | + if (! $user) { |
| 50 | + abort(403, 'Vous devez être connecté pour signaler du contenu.'); |
| 51 | + } |
| 52 | + |
| 53 | + if (! $user->hasVerifiedEmail()) { |
| 54 | + abort(403, 'Vous devez avoir un email vérifié pour signaler du contenu'); |
| 55 | + } |
| 56 | + |
| 57 | + try { |
| 58 | + app(ReportSpamAction::class)->execute([ |
| 59 | + 'user' => $user, |
| 60 | + 'recordId' => $this->recordId, |
| 61 | + 'recordType' => $this->recordType, |
| 62 | + 'reason' => $this->reason, |
| 63 | + ]); |
| 64 | + |
| 65 | + $this->reset('reason'); |
| 66 | + |
| 67 | + session()->flash('message', 'Signalement créé avec succès'); |
| 68 | + |
| 69 | + } catch (\Exception $e) { |
| 70 | + if ($e->getMessage() === 'already_reported') { |
| 71 | + $this->addError('alreadyReported', 'Vous avez déjà signalé ce contenu.'); |
| 72 | + } else { |
| 73 | + session()->flash('error', 'Une erreur est survenue lors du signalement.'); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + public function render() |
| 79 | + { |
| 80 | + return view('livewire.components.spam-report'); |
| 81 | + } |
| 82 | +} |
0 commit comments