diff --git a/app/Livewire/Questions/Show.php b/app/Livewire/Questions/Show.php index 9a54794ca..7de0f8987 100644 --- a/app/Livewire/Questions/Show.php +++ b/app/Livewire/Questions/Show.php @@ -68,6 +68,20 @@ public function redirectToProfile(): void */ public function ignore(): void { + if (! auth()->check()) { + to_route('login'); + + return; + } + + if ($this->inIndex) { + $this->dispatch('notification.created', 'Question ignored.'); + + $this->dispatch('question.ignore', questionId: $this->questionId); + + return; + } + $question = Question::findOrFail($this->questionId); $this->authorize('ignore', $question); diff --git a/resources/views/livewire/feed.blade.php b/resources/views/livewire/feed.blade.php index 4f62cccea..2863beee4 100644 --- a/resources/views/livewire/feed.blade.php +++ b/resources/views/livewire/feed.blade.php @@ -1,7 +1,7 @@ <div> <section class="mb-12 min-h-screen space-y-10"> @forelse ($questions as $question) - <livewire:questions.show :questionId="$question->id" :key="'question-' . $question->id" :inIndex="true" /> + <livewire:questions.show :questionId="$question->id" :key="'question-' . $question->id" :inIndex="false" /> @empty <div class="text-center text-slate-400">There are no questions to show.</div> @endforelse diff --git a/resources/views/livewire/questions/show.blade.php b/resources/views/livewire/questions/show.blade.php index 9051e6cf6..911a8a3ee 100644 --- a/resources/views/livewire/questions/show.blade.php +++ b/resources/views/livewire/questions/show.blade.php @@ -98,13 +98,15 @@ class="h-10 w-10 rounded-full" <span>Unpin</span> </x-dropdown-button> @endif - <x-dropdown-button - wire:click="ignore" - wire:confirm="Are you sure you want to delete this question?" - class="flex items-center gap-1.5"> - <x-icons.trash class="h-4 w-4" /> - <span>Delete</span> - </x-dropdown-button> + @if (! $question->is_ignored && auth()->user()->can('ignore', $question)) + <x-dropdown-button + wire:click="ignore" + wire:confirm="Are you sure you want to delete this question?" + class="flex items-center gap-1.5"> + <x-icons.trash class="h-4 w-4" /> + <span>Delete</span> + </x-dropdown-button> + @endif </x-slot> </x-dropdown> @endif diff --git a/tests/Unit/Livewire/Questions/ShowTest.php b/tests/Unit/Livewire/Questions/ShowTest.php index 633529e50..6bfe5b011 100644 --- a/tests/Unit/Livewire/Questions/ShowTest.php +++ b/tests/Unit/Livewire/Questions/ShowTest.php @@ -82,27 +82,38 @@ $component = Livewire::actingAs($user)->test(Show::class, [ 'questionId' => $question->id, + 'inIndex' => false, ]); - $component->dispatch('question.ignore', $question->id); + $component->call('ignore'); expect($question->fresh()->is_ignored)->toBeTrue(); $component->assertRedirect(route('profile.show', ['username' => $question->to->username])); + + $question = Question::factory()->create(); + $user = User::find($question->to_id); + + $component = Livewire::actingAs($user)->test(Show::class, [ + 'questionId' => $question->id, + 'inIndex' => true, + ]); + + $component->call('ignore'); + $component->assertDispatched('notification.created', 'Question ignored.'); + $component->assertDispatched('question.ignore'); }); test('ignore auth', function () { $question = Question::factory()->create(); - $user = User::factory()->create(); - - $component = Livewire::actingAs($user)->test(Show::class, [ + $component = Livewire::test(Show::class, [ 'questionId' => $question->id, ]); - $component->dispatch('question.ignore', $question->id); + $component->call('ignore'); - $component->assertStatus(403); + $component->assertRedirect(route('login')); }); test('like', function () {