Files
eatvio-infra/app/Livewire/Plan/Comment/Upvote.php

72 lines
1.7 KiB
PHP

<?php
namespace App\Livewire\Plan\Comment;
use App\Models\PlanComment;
use Livewire\Component;
use Illuminate\View\View;
class Upvote extends Component
{
public PlanComment $comment;
public function mount(PlanComment $comment): void
{
$this->comment = $comment;
}
public function render(): View
{
$upvotes = $this->comment->upvotes()->forPlanCommentsUpVote($this->comment)->count();
$downvotes = $this->comment->upvotes()->forPlanCommentsDownVote($this->comment)->count();
$sum = $upvotes - $downvotes;
return view(
'livewire.plan.comment.upvote',
[
'sum' => $sum,
'isUpVoted' => $this->comment->isUpVoted(),
'isDownVoted' => $this->comment->isDownVoted(),
]
);
}
public function upvote(): void
{
if ($this->comment->isUpVoted()) {
$this->comment->removeUpVote();
return;
}
if ($this->comment->isDownVoted()) {
$this->comment->removeDownVote();
}
$this->comment->upvotes()->create([
'user_id' => auth()->id(),
'comment_id' => $this->comment->id,
'type' => 'upvote',
]);
}
public function downvote(): void
{
if ($this->comment->isDownVoted()) {
$this->comment->removeDownVote();
return;
}
if ($this->comment->isUpVoted()) {
$this->comment->removeUpVote();
}
$this->comment->upvotes()->create([
'user_id' => auth()->id(),
'comment_id' => $this->comment->id,
'type' => 'downvote',
]);
}
}