From bb70c23ca3ebd434c279f7ad62f90ed8ae3259d9 Mon Sep 17 00:00:00 2001 From: faissaloux Date: Wed, 3 Jan 2024 23:46:07 +0100 Subject: [PATCH 01/24] get article likers --- app/Concerns/HasLikes.php | 10 ++++++++++ app/Livewire/LikeArticle.php | 18 ++++++++++++++++++ .../views/livewire/like-article.blade.php | 7 ++++++- 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/app/Concerns/HasLikes.php b/app/Concerns/HasLikes.php index ad6ccfe96..860ca1a22 100644 --- a/app/Concerns/HasLikes.php +++ b/app/Concerns/HasLikes.php @@ -37,6 +37,16 @@ public function dislikedBy(User $user) $this->unsetRelation('likesRelation'); } + public function likers() + { + return $this->likersRelation; + } + + public function likersRelation() + { + return $this->belongsToMany(User::class, Like::class, 'likeable_id'); + } + /** * It's important to name the relationship the same as the method because otherwise * eager loading of the polymorphic relationship will fail on queued jobs. diff --git a/app/Livewire/LikeArticle.php b/app/Livewire/LikeArticle.php index b114663d3..77f593e9d 100644 --- a/app/Livewire/LikeArticle.php +++ b/app/Livewire/LikeArticle.php @@ -14,11 +14,24 @@ final class LikeArticle extends Component public $isSidebar = true; + public $showLikers = true; + + private $likersLimit = 10; + + public $likers = []; + protected $listeners = ['likeToggled']; public function mount(Article $article): void { $this->article = $article; + + $likers = $this->getLikers(); + $this->likers = join(", ", $likers); + + if (count($likers) > $this->likersLimit) { + $this->likers .= " and more"; + } } public function toggleLike(): void @@ -40,4 +53,9 @@ public function likeToggled() { return $this->article; } + + public function getLikers(): array + { + return $this->article->likers()->pluck('username')->toArray(); + } } diff --git a/resources/views/livewire/like-article.blade.php b/resources/views/livewire/like-article.blade.php index bfb845c34..1afc67db2 100644 --- a/resources/views/livewire/like-article.blade.php +++ b/resources/views/livewire/like-article.blade.php @@ -1,4 +1,9 @@ -
+
+ @if ($showLikers) +
+ {{ $likers }} liked this article +
+ @endif @guest
From 9309d126871e53f3cdced9d157d896fc5ffd8dab Mon Sep 17 00:00:00 2001 From: faissaloux Date: Wed, 3 Jan 2024 22:46:44 +0000 Subject: [PATCH 02/24] Fix code styling --- app/Livewire/LikeArticle.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Livewire/LikeArticle.php b/app/Livewire/LikeArticle.php index 77f593e9d..8ca9f61f3 100644 --- a/app/Livewire/LikeArticle.php +++ b/app/Livewire/LikeArticle.php @@ -27,10 +27,10 @@ public function mount(Article $article): void $this->article = $article; $likers = $this->getLikers(); - $this->likers = join(", ", $likers); + $this->likers = implode(', ', $likers); if (count($likers) > $this->likersLimit) { - $this->likers .= " and more"; + $this->likers .= ' and more'; } } From c21f4853106dd1acb286757b023cdb9f5d53cdde Mon Sep 17 00:00:00 2001 From: faissaloux Date: Thu, 4 Jan 2024 00:12:51 +0100 Subject: [PATCH 03/24] add tooltip bottom arrow --- resources/views/livewire/like-article.blade.php | 1 + 1 file changed, 1 insertion(+) diff --git a/resources/views/livewire/like-article.blade.php b/resources/views/livewire/like-article.blade.php index 1afc67db2..446a38c62 100644 --- a/resources/views/livewire/like-article.blade.php +++ b/resources/views/livewire/like-article.blade.php @@ -1,6 +1,7 @@
@if ($showLikers)
+
{{ $likers }} liked this article
@endif From 0a7768bf506d200f92fb7f730b4aef2306c03c62 Mon Sep 17 00:00:00 2001 From: faissaloux Date: Thu, 4 Jan 2024 00:13:52 +0100 Subject: [PATCH 04/24] reduce font size --- resources/views/livewire/like-article.blade.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/views/livewire/like-article.blade.php b/resources/views/livewire/like-article.blade.php index 446a38c62..64519165b 100644 --- a/resources/views/livewire/like-article.blade.php +++ b/resources/views/livewire/like-article.blade.php @@ -1,6 +1,6 @@
@if ($showLikers) -
+
{{ $likers }} liked this article
From ee45265b75ee78a6955d9e8f75232acc278014d0 Mon Sep 17 00:00:00 2001 From: faissaloux Date: Thu, 4 Jan 2024 00:16:44 +0100 Subject: [PATCH 05/24] adjust padding --- resources/views/livewire/like-article.blade.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/views/livewire/like-article.blade.php b/resources/views/livewire/like-article.blade.php index 64519165b..1fd6ba9bf 100644 --- a/resources/views/livewire/like-article.blade.php +++ b/resources/views/livewire/like-article.blade.php @@ -1,6 +1,6 @@
@if ($showLikers) -
+
{{ $likers }} liked this article
From 72661c63283bc313a4c9cf797e79befbbc0e6565 Mon Sep 17 00:00:00 2001 From: faissaloux Date: Thu, 4 Jan 2024 00:52:47 +0100 Subject: [PATCH 06/24] show likers when hover on like icon --- app/Livewire/LikeArticle.php | 13 ++++++++++--- resources/views/livewire/like-article.blade.php | 4 ++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/app/Livewire/LikeArticle.php b/app/Livewire/LikeArticle.php index 8ca9f61f3..924df9058 100644 --- a/app/Livewire/LikeArticle.php +++ b/app/Livewire/LikeArticle.php @@ -14,13 +14,13 @@ final class LikeArticle extends Component public $isSidebar = true; - public $showLikers = true; + public $showLikers = false; private $likersLimit = 10; - public $likers = []; + public $likers = ""; - protected $listeners = ['likeToggled']; + protected $listeners = ['likeToggled', 'toggleLikers']; public function mount(Article $article): void { @@ -54,6 +54,13 @@ public function likeToggled() return $this->article; } + public function toggleLikers(): void + { + if (strlen($this->likers)) { + $this->showLikers = ! $this->showLikers; + } + } + public function getLikers(): array { return $this->article->likers()->pluck('username')->toArray(); diff --git a/resources/views/livewire/like-article.blade.php b/resources/views/livewire/like-article.blade.php index 1fd6ba9bf..0919c0bb8 100644 --- a/resources/views/livewire/like-article.blade.php +++ b/resources/views/livewire/like-article.blade.php @@ -7,14 +7,14 @@ @endif @guest
- + {{ count($this->article->likes()) }}
@else