Skip to content

Commit

Permalink
feat: improves ignoring questions on feed
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro committed Apr 6, 2024
1 parent 0af6d35 commit 8a82b28
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
16 changes: 16 additions & 0 deletions app/Livewire/Feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Models\Question;
use Illuminate\View\View;
use Livewire\Attributes\On;
use Livewire\Component;
use Livewire\WithoutUrlPagination;
use Livewire\WithPagination;
Expand All @@ -27,6 +28,21 @@ public function loadMore(): void
$this->perPage = $this->perPage > 100 ? 100 : ($this->perPage + 5);
}

/**
* Ignore the given question.
*/
#[On('question.ignore')]
public function ignore(string $questionId): void
{
$question = Question::findOrFail($questionId);

$this->authorize('ignore', $question);

$question->update(['is_ignored' => true]);

$this->dispatch('question.ignored');
}

/**
* Render the component.
*/
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="false" />
<livewire:questions.show :questionId="$question->id" :key="'question-' . $question->id" :inIndex="true" />
@empty
<div class="text-center text-slate-400">There are no questions to show.</div>
@endforelse
Expand Down
40 changes: 40 additions & 0 deletions tests/Unit/Livewire/FeedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,46 @@
$component->assertSee('There are no questions to show.');
});

test('ignore', function () {
$user = User::factory()->create();

$question = Question::factory()->create([
'to_id' => $user->id,
]);

$component = Livewire::actingAs($user)->test(Feed::class, [
'userId' => $user->id,
]);

$component->assertSee($question->content);

$component->dispatch('question.ignore', $question->id);

$component->assertDontSee($question->content);

expect($question->fresh()->is_ignored)->toBeTrue();
});

test('ignore auth', function () {
$userA = User::factory()->create();
$userB = User::factory()->create();

$question = Question::factory()->create([
'from_id' => $userA->id,
'to_id' => $userB->id,
]);

$component = Livewire::actingAs($userA)->test(Feed::class, [
'userId' => $userB->id,
]);

$component->dispatch('question.ignore', $question->id);

$component->assertStatus(403);

expect($question->fresh()->is_ignored)->not->toBeTrue();
});

test('load more', function () {
$user = User::factory()->create();

Expand Down

0 comments on commit 8a82b28

Please sign in to comment.