Skip to content

Commit

Permalink
Add documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Autive committed Jan 16, 2024
1 parent 6aa6906 commit e56bd35
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
33 changes: 30 additions & 3 deletions app/Livewire/Changelog/Vote.php
Original file line number Diff line number Diff line change
@@ -1,34 +1,61 @@
<?php
/**
* Class Vote
*
* This class represents a Livewire component for voting on a changelog.
*
* @since 2.13.0
*/

namespace App\Livewire\Changelog;

use Livewire\Component;
use App\Models\Changelog;
use AllowDynamicProperties;
use Illuminate\Contracts\View\View;
use Illuminate\Contracts\View\Factory;
use Illuminate\Foundation\Application;

/**
* Class Vote
*
*/
#[AllowDynamicProperties]
class Vote extends Component
{
public Changelog $changelog;

public $votes;

/**
* Mount the component.
*
* @param Changelog $changelog The Changelog instance to be mounted.
*
* @return void
*/
public function mount(Changelog $changelog): void
{
$this->changelog = $changelog;
$this->votes = $changelog->votes;
}

/**
* Toggles the vote for the authenticated user on the changelog.
* Updates the votes count on the changelog.
*
* @return void
*/
public function vote(): void
{
$this->changelog->votes()->toggle(auth()->user());
$this->votes = $this->changelog->votes;
}

public function render(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
/**
* Render the component.
*
* @return \Illuminate\View\View
*/
public function render(): View
{
return view(
'livewire.changelog.vote',
Expand Down
14 changes: 13 additions & 1 deletion app/Models/Changelog.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,24 @@ public function items(): BelongsToMany
return $this->belongsToMany(Item::class);
}

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

public function userVoted($user): bool
/**
* 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();
}
Expand Down
5 changes: 5 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
return new class extends SettingsMigration {
public function up(): void
{
$this->migrator->add('general.show_changelog_like', false);
$this->migrator->add('general.show_changelog_like', false);
}
};

0 comments on commit e56bd35

Please sign in to comment.