Skip to content

Commit

Permalink
fix: ignoring questions from feed
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro committed Apr 6, 2024
1 parent 728cb15 commit 0af6d35
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 14 deletions.
14 changes: 14 additions & 0 deletions app/Livewire/Questions/Show.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion resources/views/livewire/feed.blade.php
Original file line number Diff line number Diff line change
@@ -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
Expand Down
16 changes: 9 additions & 7 deletions resources/views/livewire/questions/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 17 additions & 6 deletions tests/Unit/Livewire/Questions/ShowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit 0af6d35

Please sign in to comment.