-
Notifications
You must be signed in to change notification settings - Fork 16
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 #143 from P3D-Legacy/blog-post-comments
Blog Post Comments
- Loading branch information
Showing
17 changed files
with
362 additions
and
8 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,39 @@ | ||
<?php | ||
|
||
namespace App\Http\Livewire\Blog; | ||
|
||
use App\Models\Comment; | ||
use Livewire\Component; | ||
|
||
class CommentLike extends Component | ||
{ | ||
public Comment $comment; | ||
public int $count; | ||
public $user; | ||
public bool $liked; | ||
|
||
public function mount(Comment $comment) | ||
{ | ||
$this->comment = $comment; | ||
$this->user = auth()->user(); | ||
$this->count = $this->comment->likers()->count(); | ||
$this->liked = $this->user ? $this->comment->isLikedBy($this->user) : false; | ||
} | ||
|
||
public function like() | ||
{ | ||
// Redirect guest to login page | ||
if (auth()->guest()) { | ||
return redirect()->route('login'); | ||
} | ||
// else if user; like | ||
$this->user->toggleLike($this->comment); | ||
$this->count = $this->comment->likers()->count(); | ||
$this->liked = $this->comment->isLikedBy($this->user); | ||
} | ||
|
||
public function render() | ||
{ | ||
return view('livewire.blog.comment-like'); | ||
} | ||
} |
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,47 @@ | ||
<?php | ||
|
||
namespace App\Http\Livewire\Blog; | ||
|
||
use App\Models\Post; | ||
use LivewireUI\Modal\ModalComponent; | ||
|
||
class CommentModal extends ModalComponent | ||
{ | ||
public int|Post $post; | ||
public $body; | ||
public $parentComment = null; | ||
|
||
public function mount(int|Post $post, $parentComment) | ||
{ | ||
$this->post = Post::find($post); | ||
$this->body = ''; | ||
$this->parentComment = $parentComment; | ||
} | ||
|
||
public function save() | ||
{ | ||
$this->validate([ | ||
'body' => ['required', 'string', 'min:10', 'max:255'], | ||
]); | ||
|
||
$commentData = [ | ||
'title' => null, | ||
'body' => $this->body, | ||
]; | ||
|
||
if ($this->parentComment) { | ||
$this->parentComment = \AliBayat\LaravelCommentable\Comment::find($this->parentComment); | ||
} | ||
|
||
$this->post->comment($commentData, auth()->user(), $this->parentComment); | ||
|
||
$this->emit('commentAdded'); | ||
|
||
$this->closeModal(); | ||
} | ||
|
||
public function render() | ||
{ | ||
return view('livewire.blog.comment-modal'); | ||
} | ||
} |
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,36 @@ | ||
<?php | ||
|
||
namespace App\Http\Livewire\Blog; | ||
|
||
use App\Models\Post; | ||
use Livewire\Component; | ||
|
||
class CommentSection extends Component | ||
{ | ||
public int|Post $post; | ||
public $comments; | ||
|
||
protected $listeners = [ | ||
'commentAdded' => 'update', | ||
]; | ||
|
||
public function mount(int|Post $post) | ||
{ | ||
if ($post instanceof Post) { | ||
$this->post = $post; | ||
} else { | ||
$this->post = Post::find($post); | ||
} | ||
$this->comments = $this->post->comments; | ||
} | ||
|
||
public function update() | ||
{ | ||
$this->comments = $this->post->comments; | ||
} | ||
|
||
public function render() | ||
{ | ||
return view('livewire.blog.comment-section'); | ||
} | ||
} |
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,25 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Overtrue\LaravelLike\Traits\Likeable; | ||
use Spatie\Activitylog\LogOptions; | ||
use Spatie\Activitylog\Traits\LogsActivity; | ||
|
||
class Comment extends \AliBayat\LaravelCommentable\Comment | ||
{ | ||
use LogsActivity; | ||
use Likeable; | ||
|
||
/** | ||
* The attributes that should be logged for the user. | ||
* | ||
* @return array | ||
*/ | ||
public function getActivitylogOptions(): LogOptions | ||
{ | ||
return LogOptions::defaults() | ||
->logFillable() | ||
->logOnlyDirty(); | ||
} | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Laravel Commentable Package by Ali Bayat. | ||
*/ | ||
|
||
return [ | ||
/* | ||
|-------------------------------------------------------------------------- | ||
| Eloquent Model | ||
|-------------------------------------------------------------------------- | ||
*/ | ||
|
||
'model' => \App\Models\Comment::class, | ||
]; |
33 changes: 33 additions & 0 deletions
33
database/migrations/2022_04_03_125632_create_comments_table.php
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,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Laravel Commentable Package by Ali Bayat. | ||
*/ | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Kalnoy\Nestedset\NestedSet; | ||
|
||
class CreateCommentsTable extends Migration | ||
{ | ||
public function up() | ||
{ | ||
Schema::create('comments', function (Blueprint $table) { | ||
$table->increments('id'); | ||
$table->string('title')->nullable(); | ||
$table->boolean('active')->default(false); | ||
$table->text('body'); | ||
$table->morphs('commentable'); | ||
$table->morphs('creator'); | ||
NestedSet::columns($table); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
public function down() | ||
{ | ||
Schema::dropIfExists('comments'); | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"/js/app.js": "/js/app.js?id=b9b4c7cb5c903535c15cb23812e23dda", | ||
"/css/app.css": "/css/app.css?id=bd2475c578a1582bed4a14b802932d12" | ||
"/css/app.css": "/css/app.css?id=6ccef9c7f3126ce34563c22ae7535914" | ||
} |
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
Oops, something went wrong.