Add likes to comments
This commit is contained in:
@@ -1,10 +1,17 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace App\Livewire;
|
||||
|
||||
use App\Models\User;
|
||||
|
||||
use Livewire\Component;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
||||
|
||||
use Maize\Markable\Models\Like;
|
||||
|
||||
class Comment extends Component
|
||||
{
|
||||
use AuthorizesRequests;
|
||||
@@ -13,6 +20,10 @@ class Comment extends Component
|
||||
|
||||
public $isReplying = false;
|
||||
|
||||
public $likeCount = 0;
|
||||
|
||||
public $liked = false;
|
||||
|
||||
public $replyState = [
|
||||
'body' => ''
|
||||
];
|
||||
@@ -85,6 +96,34 @@ class Comment extends Component
|
||||
$this->dispatch('refresh')->self();
|
||||
}
|
||||
|
||||
public function like()
|
||||
{
|
||||
if (! Auth::check()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Like::toggle($this->comment, User::where('id', Auth::user()->id)->firstOrFail());
|
||||
|
||||
Cache::forget('commentLikes'.$this->comment->id);
|
||||
|
||||
if ($this->liked) {
|
||||
$this->liked = false;
|
||||
$this->likeCount--;
|
||||
return;
|
||||
}
|
||||
|
||||
$this->liked = true;
|
||||
$this->likeCount++;
|
||||
}
|
||||
|
||||
public function mount()
|
||||
{
|
||||
if (Auth::check()) {
|
||||
$this->likeCount = $this->comment->likeCount();
|
||||
$this->liked = Like::has($this->comment, User::where('id', Auth::user()->id)->firstOrFail());
|
||||
}
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.comment');
|
||||
|
||||
Reference in New Issue
Block a user