Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

For you to follow 453 #600

Merged
merged 14 commits into from
Sep 19, 2024
33 changes: 33 additions & 0 deletions app/Livewire/Home/QuestionsFollowing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace App\Livewire\Home;

use App\Jobs\IncrementViews;
use App\Livewire\Concerns\HasLoadMore;
use App\Models\User;
use App\Queries\Feeds\QuestionsFollowingFeed;
use Illuminate\View\View;
use Livewire\Component;

final class QuestionsFollowing extends Component
{
use HasLoadMore;

/**
* Renders the component.
*/
public function render(): View
{
$user = type(auth()->user())->as(User::class);

$questions = (new QuestionsFollowingFeed($user))->builder()->simplePaginate($this->perPage);

IncrementViews::dispatchUsingSession($questions->getCollection());

return view('livewire.home.questions-following', [
'followingQuestions' => $questions,
]);
}
}
30 changes: 0 additions & 30 deletions app/Livewire/Home/QuestionsForYou.php

This file was deleted.

36 changes: 36 additions & 0 deletions app/Queries/Feeds/QuestionsFollowingFeed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace App\Queries\Feeds;

use App\Models\Question;
use App\Models\User;
use Illuminate\Database\Eloquent\Builder;

final readonly class QuestionsFollowingFeed
{
/**
* Create a new instance Following feed.
*/
public function __construct(
private User $user,
) {}

/**
* Get the query builder for the feed.
*
* @return Builder<Question>
*/
public function builder(): Builder
{
return Question::query()
->whereHas('to', function (Builder $toQuery): void {
$toQuery->whereIn('id', $this->user->following()->select('users.id'));
})
->orderByDesc('updated_at')
->whereNotNull('answer')
->where('is_reported', false)
->where('is_ignored', false);
}
}
51 changes: 0 additions & 51 deletions app/Queries/Feeds/QuestionsForYouFeed.php

This file was deleted.

8 changes: 4 additions & 4 deletions resources/views/components/home-menu.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ class="h-6 w-6 xsm:mr-2" />
</a>

<a
href="{{ route('home.for_you') }}"
class="{{ request()->routeIs('home.for_you') ? 'bg-pink-600 text-slate-100' : 'text-slate-500 hover:text-slate-100 bg-slate-900 ' }} inline-flex flex-1 items-center justify-center whitespace-nowrap rounded-md border border-transparent px-3 py-2 text-sm font-medium leading-4 transition duration-150 ease-in-out focus:outline-none"
title="{{ __('For you') }}"
href="{{ route('home.following') }}"
class="{{ request()->routeIs('home.following') ? 'bg-pink-600 text-slate-100' : 'text-slate-500 hover:text-slate-100 bg-slate-900 ' }} inline-flex flex-1 items-center justify-center whitespace-nowrap rounded-md border border-transparent px-3 py-2 text-sm font-medium leading-4 transition duration-150 ease-in-out focus:outline-none"
title="{{ __('Following') }}"
wire:navigate
wire:transition
>
<x-heroicon-o-heart
class="h-6 w-6 xsm:mr-2" />
<span class="hidden xsm:inline">{{ __('For you') }}</span>
<span class="hidden xsm:inline">{{ __('Following') }}</span>
</a>

<a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<x-home-menu></x-home-menu>

@auth
<livewire:home.questions-for-you :focus-input="true" />
<livewire:home.questions-following :focus-input="true"/>
@else
<div class="mb-4">
<div class="mb-4">Log in or sign up to access personalized content.</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<div class="mb-12 w-full text-slate-200">
@if ($forYouQuestions->isEmpty())
@if ($followingQuestions->isEmpty())
<section class="rounded-lg">
<p class="my-8 text-center text-lg text-slate-500">
We haven't found any questions that may interest you based on the activity you've done on Pinkary.
</p>
</section>
@else
<section class="mb-12 min-h-screen space-y-10">
@foreach ($forYouQuestions as $question)
@foreach ($followingQuestions as $question)
<livewire:questions.show
:questionId="$question->id"
:key="'question-' . $question->id"
Expand All @@ -18,7 +18,7 @@

<x-load-more-button
:perPage="$perPage"
:paginator="$forYouQuestions"
:paginator="$followingQuestions"
message="There are no more questions to load, or you have scrolled too far."
/>
</section>
Expand Down
3 changes: 2 additions & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
Route::view('/about', 'about')->name('about');

Route::view('/', 'home/feed')->name('home.feed');
Route::view('/for-you', 'home/questions-for-you')->name('home.for_you');
Route::get('/for-you', fn () => redirect()->route('home.following'))->name('home.for_you');
msamgan marked this conversation as resolved.
Show resolved Hide resolved
Route::view('/following', 'home/following')->name('home.following');
Route::view('/trending', 'home/trending-questions')->name('home.trending');
Route::view('/users', 'home/users')->name('home.users');

Expand Down
9 changes: 9 additions & 0 deletions tests/Http/ForYouRedirectTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

test('/for-you redirects to /following', function () {
msamgan marked this conversation as resolved.
Show resolved Hide resolved
$response = $this->get('/for-you');

$response->assertRedirect('/following');
});
34 changes: 34 additions & 0 deletions tests/Http/Home/FollowingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

use App\Jobs\IncrementViews;
use App\Livewire\Home\QuestionsFollowing;
use App\Models\User;
use Illuminate\Support\Facades\Queue;

it('can see the "following" view', function () {
$user = User::factory()->create();

$response = $this->actingAs($user)->get(route('home.following'));

$response->assertOk()
->assertSee('Following')
->assertSeeLivewire(QuestionsFollowing::class);
});

it('guest can see the "following" view', function () {
$response = $this->get(route('home.following'));

$response->assertOk()
->assertSee('Log in or sign up to access personalized content');
});

it('does increment views', function () {
Queue::fake(IncrementViews::class);

$this->actingAs(User::factory()->create());
$this->get(route('home.following'));

Queue::assertPushed(IncrementViews::class);
msamgan marked this conversation as resolved.
Show resolved Hide resolved
});
23 changes: 0 additions & 23 deletions tests/Http/Home/ForYouTest.php

This file was deleted.

14 changes: 7 additions & 7 deletions tests/Unit/Feeds/QuestionsForYouTest.php
msamgan marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use App\Models\Like;
use App\Models\Question;
use App\Models\User;
use App\Queries\Feeds\QuestionsForYouFeed;
use App\Queries\Feeds\QuestionsFollowingFeed;
use Illuminate\Database\Eloquent\Builder;

it('render questions with right conditions', function () {
Expand All @@ -30,7 +30,7 @@
'is_reported' => false,
]);

$builder = (new QuestionsForYouFeed($likerUser))->builder();
$builder = (new QuestionsFollowingFeed($likerUser))->builder();
msamgan marked this conversation as resolved.
Show resolved Hide resolved

expect($builder->count())->toBe(2);
});
Expand Down Expand Up @@ -58,7 +58,7 @@
'is_reported' => false,
]);

$builder = (new QuestionsForYouFeed($likerUser))->builder();
$builder = (new QuestionsFollowingFeed($likerUser))->builder();

expect($builder->count())->toBe(0);
});
Expand Down Expand Up @@ -87,7 +87,7 @@
'answer' => null,
]);

$builder = (new QuestionsForYouFeed($likerUser))->builder();
$builder = (new QuestionsFollowingFeed($likerUser))->builder();

expect($builder->where('answer', $answer)->count())->toBe(1);
});
Expand All @@ -104,7 +104,7 @@
'answer' => 'Answer',
]);

$builder = (new QuestionsForYouFeed($follower))->builder();
$builder = (new QuestionsFollowingFeed($follower))->builder();

expect($builder->count())->toBe(1);
});
Expand All @@ -131,13 +131,13 @@
'is_reported' => true,
]);

$builder = (new QuestionsForYouFeed($likerUser))->builder();
$builder = (new QuestionsFollowingFeed($likerUser))->builder();

expect($builder->where('is_reported', false)->count())->toBe(1);
});

it('builder returns Eloquent\Builder instance', function () {
$builder = (new QuestionsForYouFeed(User::factory()->create()))->builder();
$builder = (new QuestionsFollowingFeed(User::factory()->create()))->builder();

expect($builder)->toBeInstanceOf(Builder::class);
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

use App\Livewire\Home\QuestionsForYou;
use App\Livewire\Home\QuestionsFollowing;
use App\Models\Like;
use App\Models\Question;
use App\Models\User;
Expand All @@ -26,7 +26,7 @@

$question->likes()->save($like);
msamgan marked this conversation as resolved.
Show resolved Hide resolved

$component = Livewire::actingAs($user)->test(QuestionsForYou::class);
$component = Livewire::actingAs($user)->test(QuestionsFollowing::class);

$component
->assertDontSee('We haven\'t found any questions that may interest you based on the activity you\'ve done on Pinkary.')
Expand All @@ -45,7 +45,7 @@
'to_id' => $user->id,
]);

$component = Livewire::actingAs($user)->test(QuestionsForYou::class);
$component = Livewire::actingAs($user)->test(QuestionsFollowing::class);

$component
->assertSee('found any questions that may interest you based on the activity')
Expand All @@ -57,7 +57,7 @@

Question::factory(120)->create();

$component = Livewire::actingAs($user)->test(QuestionsForYou::class);
$component = Livewire::actingAs($user)->test(QuestionsFollowing::class);

$component->call('loadMore');
$component->assertSet('perPage', 10);
Expand Down