From 39a7640e92f94f3f9580debc43e91e9fe3fcddfd Mon Sep 17 00:00:00 2001 From: Refringe Date: Mon, 30 Sep 2024 22:56:06 -0400 Subject: [PATCH] Reworked Follow Livewire Components Fixes PHPStan errors and makes it a little more performant. Still not good enough. Making way to many queries for what it's doing. --- app/Livewire/User/FollowCard.php | 50 +++++++++++++------ .../views/livewire/user/follow-card.blade.php | 33 ++++++------ 2 files changed, 52 insertions(+), 31 deletions(-) diff --git a/app/Livewire/User/FollowCard.php b/app/Livewire/User/FollowCard.php index 325b585..8aa9679 100644 --- a/app/Livewire/User/FollowCard.php +++ b/app/Livewire/User/FollowCard.php @@ -3,7 +3,6 @@ namespace App\Livewire\User; use App\Models\User; -use Illuminate\Support\Collection; use Illuminate\View\View; use Livewire\Attributes\Locked; use Livewire\Attributes\On; @@ -40,12 +39,16 @@ class FollowCard extends Component public string $dialogTitle; /** - * The users to display in the card. - * - * @var Collection + * The user data to display in the card. */ #[Locked] - public Collection $followUsers; + public array $display = []; + + /** + * The limited user data to display in the card. + */ + #[Locked] + public array $displayLimit = []; /** * The maximum number of users to display on the card. @@ -64,6 +67,12 @@ class FollowCard extends Component #[Locked] public User $profileUser; + /** + * The number of users being displayed. + */ + #[Locked] + public int $followUsersCount; + /** * Called when the component is initialized. */ @@ -130,18 +139,31 @@ class FollowCard extends Component public function populateFollowUsers(): void { // Fetch IDs of all users the authenticated user is following. - $followingIds = auth()->user()->following()->pluck('following_id'); + $followingIds = collect(); + $authUser = auth()->user(); + if ($authUser) { + $followingIds = $authUser->following()->pluck('following_id'); + } - // Load the profile user's followers (or following) and map the follow status. - $this->followUsers = $this->profileUser->{$this->relationship} + // Load the profile user's followers (or following). + $users = $this->profileUser->{$this->relationship}()->with([])->get(); + + // Count the number of users. + $this->followUsersCount = $users->count(); + + // Load the users to display and whether the authenticated user is following each user. + $this->display = $users ->map(function (User $user) use ($followingIds) { - // Add the follow status based on the preloaded IDs. - $user->follows = $followingIds->contains($user->id); + return [ + 'user' => $user, + 'isFollowing' => $followingIds->contains($user->id), + ]; + })->toArray(); - // TODO: The above follows property doesn't exist on the User model. What was I smoking? - - return $user; - }); + // Store limited users for the main view. + $this->displayLimit = collect($this->display) + ->take($this->limit) + ->toArray(); } /** diff --git a/resources/views/livewire/user/follow-card.blade.php b/resources/views/livewire/user/follow-card.blade.php index 2601792..4006f13 100644 --- a/resources/views/livewire/user/follow-card.blade.php +++ b/resources/views/livewire/user/follow-card.blade.php @@ -4,37 +4,37 @@

{{ $title }}

- @if (! $followUsers->count()) + @if ($followUsersCount === 0)
{{ $emptyMessage }}
@else
- @foreach ($followUsers->slice(0, $limit) as $user) + @foreach ($displayLimit as $data) {{-- User Badge --}}
- - {{ $user->name }} + + {{ $data['user']->name }}
- {{ $user->name }} + {{ $data['user']->name }}
@endforeach - @if ($followUsers->count() > $limit) + @if ($followUsersCount > $limit) {{-- Count Badge --}}
- +
- {{ $followUsers->count() }} total + {{ $followUsersCount }} total
@endif
@endif - @if ($followUsers->count() > $limit) + @if ($followUsersCount > $limit) {{-- View All Button --}}
@@ -49,28 +49,27 @@
- @foreach ($followUsers as $user) + @foreach ($display as $data)
- - {{ $user->name }} + + {{ $data['user']->name }}
- {{ $user->name }} + {{ $data['user']->name }} {{ __("Member Since") }} - +
- @if (auth()->check() && auth()->user()->id !== $user->id) - + @if (auth()->check() && auth()->user()->id !== $data['user']->id) + @endif
@endforeach
- {{ __('Close') }}