Skip to content

Commit

Permalink
Move to UpVote trait
Browse files Browse the repository at this point in the history
  • Loading branch information
Autive committed Jan 16, 2024
1 parent e56bd35 commit d89d5bc
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 66 deletions.
2 changes: 1 addition & 1 deletion app/Livewire/Changelog/Vote.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function mount(Changelog $changelog): void
*/
public function vote(): void
{
$this->changelog->votes()->toggle(auth()->user());
$this->changelog->toggleUpvote(auth()->user());
$this->votes = $this->changelog->votes;
}

Expand Down
25 changes: 2 additions & 23 deletions app/Models/Changelog.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use App\Traits\HasUpvote;
use App\Traits\Sluggable;
use App\Traits\HasOgImage;
use Illuminate\Database\Eloquent\Model;
Expand All @@ -12,7 +13,7 @@

class Changelog extends Model
{
use HasFactory, Sluggable, HasOgImage;
use HasFactory, Sluggable, HasOgImage, HasUpvote;

public $fillable = [
'slug',
Expand Down Expand Up @@ -40,26 +41,4 @@ public function items(): BelongsToMany
{
return $this->belongsToMany(Item::class);
}

/**
* Get the votes relationship.
*
* @return BelongsToMany
*/
public function votes(): BelongsToMany
{
return $this->belongsToMany(User::class);
}

/**
* Check if a user has voted on this item.
*
* @param User $user The user to check.
*
* @return bool Returns true if the user has voted on this item, otherwise false.
*/
public function userVoted(User $user): bool
{
return $this->votes()->where('user_id', $user->id)->exists();
}
}
10 changes: 0 additions & 10 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,16 +164,6 @@ public function toggleVoteSubscription(int $id, string $type)
$vote->update(['subscribed' => !$vote->subscribed]);
}

/**
* Returns the relationship between the current model and the Changelog model.
*
* @return BelongsToMany
*/
public function changelogVotes(): BelongsToMany
{
return $this->belongsToMany(Changelog::class);
}

public static function booted()
{
static::creating(function (self $user) {
Expand Down
6 changes: 4 additions & 2 deletions app/Observers/VoteObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ public function deleted(Vote $vote)

protected function updateTotalVotes(Vote $vote)
{
$vote->item->total_votes = $vote->item->votes()->count();
$vote->item->save();
if (isset($vote->item->total_votes)) {
$vote->item->total_votes = $vote->item->votes()->count();
$vote->item->save();
}
}
}
27 changes: 0 additions & 27 deletions database/migrations/2024_01_16_094005_create_changelog_user.php

This file was deleted.

6 changes: 3 additions & 3 deletions resources/views/livewire/changelog/vote.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@foreach($votes as $vote)
<dd>
<img class="h-10 w-10 rounded-full bg-gray-50 ring-2 ring-white"
src="{{ $vote->getGravatar() }}" alt="{{ $vote->name }}">
src="{{ $vote->user->getGravatar() }}" alt="{{ $vote->user->name }}">
</dd>
@php( $i++ )
@if( $i == 4 && $votes->count() > 5 )
Expand Down Expand Up @@ -44,14 +44,14 @@
<button wire:click="vote" type="button" class="
inline-flex items-center justify-center gap-x-1.5 rounded-md px-2.5 py-1.5
text-sm font-semibold text-white shadow-sm
@if( $changelog->userVoted( auth()->user() ) )
@if( $changelog->hasVoted( auth()->user() ) )
bg-red-500 hover:bg-red-700
@else
bg-blue-500 hover:bg-blue-700
@endif
focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-pink-600
">
@if( $changelog->userVoted( auth()->user() ) )
@if( $changelog->hasVoted( auth()->user() ) )
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6">
<path stroke-linecap="round" stroke-linejoin="round" d="m9.75 9.75 4.5 4.5m0-4.5-4.5 4.5M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z" />
</svg>
Expand Down

0 comments on commit d89d5bc

Please sign in to comment.