Skip to content

Commit

Permalink
feat: added follow button in users component
Browse files Browse the repository at this point in the history
  • Loading branch information
MrPunyapal committed Sep 20, 2024
1 parent e36870f commit bbb49da
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 12 deletions.
25 changes: 24 additions & 1 deletion app/Livewire/Home/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Livewire\Home;

use App\Livewire\Concerns\Followable;
use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
Expand All @@ -14,6 +15,8 @@

final class Users extends Component
{
use Followable;

/**
* The component's search query.
*/
Expand Down Expand Up @@ -51,6 +54,16 @@ private function usersByQuery(): Collection
$query->whereNotNull('answer');
}])
->orderBy('answered_questions_count', 'desc')
->when(auth()->check(), function (Builder $query): void {
$query->withExists([
'following as is_follower' => function (Builder $query): void {
$query->where('user_id', auth()->id());
},
'followers as is_following' => function (Builder $query): void {
$query->where('follower_id', auth()->id());
},
]);
})
->limit(10)
->get();
}
Expand All @@ -67,7 +80,17 @@ private function defaultUsers(): Collection
return $this->famousUsers($verifiedUsers)
->merge($verifiedUsers)
->shuffle()
->load('links');
->load('links')
->when(auth()->check(), function (Collection $users): void {
$users->loadExists([
'following as is_follower' => function (Builder $query): void {
$query->where('user_id', auth()->id());
},
'followers as is_following' => function (Builder $query): void {
$query->where('follower_id', auth()->id());
},
]);
});
}

/**
Expand Down
33 changes: 22 additions & 11 deletions resources/views/livewire/home/users.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,27 @@ class="w-full mx-1 !rounded-2xl dark:!bg-slate-950 !bg-slate-50 !bg-opacity-80 p
<section class="max-w-2xl">
<ul class="flex flex-col gap-2">
@foreach ($users as $user)
<li>
<a
href="{{ route('profile.show', ['username' => $user->username]) }}"
class="group flex items-center gap-3 rounded-2xl border dark:border-slate-900 border-slate-200 dark:bg-slate-950 bg-slate-50 dark:bg-opacity-80 p-4 transition-colors dark:hover:bg-slate-900 hover:bg-slate-100"
wire:navigate
>
<li
data-parent=true
x-data="clickHandler"
x-on:click="handleNavigation($event)"
>
<div class="group flex items-center gap-3 rounded-2xl border dark:border-slate-900 border-slate-200 dark:bg-slate-950 bg-slate-50 dark:bg-opacity-80 p-4 transition-colors dark:hover:bg-slate-900 hover:bg-slate-100">
<figure class="{{ $user->is_company_verified ? 'rounded-md' : 'rounded-full' }} h-12 w-12 flex-shrink-0 overflow-hidden bg-slate-800 transition-opacity group-hover:opacity-90">
<img
class="{{ $user->is_company_verified ? 'rounded-md' : 'rounded-full' }} h-12 w-12"
src="{{ $user->avatar_url }}"
alt="{{ $user->username }}"
/>
</figure>
<div class="flex flex-col overflow-hidden text-sm">
<div class="flex items-center space-x-2">
<p class="truncate font-medium dark:text-white text-black">
<div class="flex flex-col overflow-hidden text-sm text-left">
<a
class="flex items-center space-x-2"
href="{{ route('profile.show', ['username' => $user->username]) }}"
wire:navigate
x-ref="parentLink"
>
<p class="text-wrap truncate font-medium dark:text-white text-black">
{{ $user->name }}
</p>

Expand All @@ -53,12 +58,18 @@ class="size-4"
class="size-4"
/>
@endif
</div>
</a>
<p class="truncate text-slate-500 transition-colors group-hover:text-slate-400">
{{ '@'.$user->username }}
</p>
</div>
</a>
<x-follow-button
:id="$user->id"
:isFollower="auth()->check() && $user->is_follower"
:isFollowing="auth()->check() && $user->is_following"
class="ml-auto"
/>
</div>
</li>
@endforeach
</ul>
Expand Down
51 changes: 51 additions & 0 deletions tests/Unit/Livewire/Home/UsersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,54 @@
$this->assertNotEquals($famousUsers->pluck('id')->toArray(), $CachedFamousUsers);
$this->assertEquals($newFamousUsers->pluck('id')->toArray(), $CachedFamousUsers);
});

test('users has is_follower and is_following attributes only when authenticated', function () {

User::factory(10)
->hasLinks(1, function (array $attributes, User $user) {
return ['url' => "https://twitter.com/{$user->username}"];
})
->hasQuestionsReceived(2, ['answer' => 'this is an answer'])
->create();

User::factory()
->hasLinks(1, function (array $attributes, User $user) {
return ['url' => "https://twitter.com/{$user->username}"];
})
->sequence([
'name' => 'Nuno Maduro',
], [
'name' => 'Punyapal Shah',
])
->create();

$component = Livewire::test(Users::class);

$component->viewData('users')->each(function (User $user): void {
expect($user)->not->toHaveKey('is_follower');
expect($user)->not->toHaveKey('is_following');
});

$component->set('query', 'un');

$component->viewData('users')->each(function (User $user): void {
expect($user)->not->toHaveKey('is_follower');
expect($user)->not->toHaveKey('is_following');
});

$component->actingAs(User::factory()->create());

$component->set('query', '');

$component->viewData('users')->each(function (User $user): void {
expect($user->is_follower)->toBeBool();
expect($user->is_following)->toBeBool();
});

$component->set('query', 'un');

$component->viewData('users')->each(function (User $user): void {
expect($user->is_follower)->toBeBool();
expect($user->is_following)->toBeBool();
});
});

0 comments on commit bbb49da

Please sign in to comment.