-
-
Notifications
You must be signed in to change notification settings - Fork 332
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #470 from pinkary-project/feat/easy-follow
Feat: easy follow
- Loading branch information
Showing
15 changed files
with
545 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Livewire\Concerns; | ||
|
||
use App\Models\User; | ||
use Livewire\Attributes\Renderless; | ||
|
||
trait Followable | ||
{ | ||
/** | ||
* Follows the given user. | ||
*/ | ||
#[Renderless] | ||
public function follow(int $id): void | ||
{ | ||
if (! auth()->check()) { | ||
$this->redirectRoute('login', navigate: true); | ||
|
||
return; | ||
} | ||
|
||
$user = type(auth()->user())->as(User::class); | ||
$user->following()->attach($id); | ||
|
||
if ($this->shouldHandleFollowingCount()) { | ||
$this->dispatch('following.updated'); | ||
} | ||
|
||
$this->dispatch('user.followed', id: $id); | ||
} | ||
|
||
/** | ||
* Unfollows the given user. | ||
*/ | ||
#[Renderless] | ||
public function unfollow(int $id): void | ||
{ | ||
if (! auth()->check()) { | ||
$this->redirectRoute('login', navigate: true); | ||
|
||
return; | ||
} | ||
|
||
$user = type(auth()->user())->as(User::class); | ||
$user->following()->detach($id); | ||
|
||
if ($this->shouldHandleFollowingCount()) { | ||
$this->dispatch('following.updated'); | ||
} | ||
|
||
$this->dispatch('user.unfollowed', id: $id); | ||
} | ||
|
||
/** | ||
* Indicates if the following count should be handled. | ||
*/ | ||
protected function shouldHandleFollowingCount(): bool | ||
{ | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
const followButton = (id, isFollowing, isFollower, isAuthenticated) => ({ | ||
id, | ||
isFollowing, | ||
isFollower, | ||
isAuthenticated, | ||
buttonText: '', | ||
|
||
init() { | ||
this.setButtonText(); | ||
this.initEventListeners(); | ||
}, | ||
|
||
toggleFollow() { | ||
if (!this.isAuthenticated) { | ||
window.Livewire.navigate('/login'); | ||
return; | ||
} | ||
|
||
if (this.isFollowing) { | ||
this.$wire.unfollow(id); | ||
this.$dispatch('user.unfollowed', { id: id }); | ||
} else { | ||
this.$wire.follow(id); | ||
this.$dispatch('user.followed', { id: id }); | ||
} | ||
}, | ||
|
||
setButtonText() { | ||
this.buttonText = this.isFollowing ? 'Unfollow' : (this.isFollower ? 'Follow Back' : 'Follow'); | ||
}, | ||
|
||
initEventListeners() { | ||
window.addEventListener('user.followed', (event) => { | ||
if (event.detail.id == this.id) { | ||
this.isFollowing = true; | ||
this.setButtonText(); | ||
} | ||
}); | ||
window.addEventListener('user.unfollowed', (event) => { | ||
if (event.detail.id == id) { | ||
this.isFollowing = false; | ||
this.setButtonText(); | ||
} | ||
}); | ||
} | ||
}); | ||
|
||
export { followButton } | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
@props([ | ||
'id', | ||
'isFollower' => false, | ||
'isFollowing' => false, | ||
]) | ||
|
||
@if(auth()->id() !== $id) | ||
<div {{ $attributes }} | ||
x-data="followButton({{ $id }}, @js($isFollowing), @js($isFollower), @js(auth()->check()))" | ||
> | ||
<x-secondary-button | ||
wire:loading.attr="disabled" | ||
data-navigate-ignore="true" | ||
type="button" | ||
x-on:click="toggleFollow()" | ||
class="text-xs md:text-sm text-nowrap" | ||
> | ||
<span x-text='buttonText' :title='buttonText' ></span> | ||
</x-secondary-button> | ||
</div> | ||
@endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.