1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Usamamuneerchaudhary\Commentify\Http\Livewire; |
4
|
|
|
|
5
|
|
|
use Illuminate\Auth\Access\AuthorizationException; |
6
|
|
|
use Illuminate\Contracts\View\Factory; |
7
|
|
|
use Illuminate\Contracts\View\View; |
8
|
|
|
use Illuminate\Foundation\Application; |
9
|
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests; |
10
|
|
|
use Livewire\Attributes\On; |
11
|
|
|
use Livewire\Component; |
12
|
|
|
use Livewire\WithPagination; |
13
|
|
|
use Illuminate\Database\Eloquent\Model; |
14
|
|
|
|
15
|
|
|
class Comments extends Component |
16
|
|
|
{ |
17
|
|
|
use WithPagination, AuthorizesRequests; |
18
|
|
|
|
19
|
|
|
public Model $model; |
20
|
|
|
|
21
|
|
|
public $users = []; |
22
|
|
|
|
23
|
|
|
public $showDropdown = false; |
24
|
|
|
|
25
|
|
|
protected $numberOfPaginatorsRendered = []; |
26
|
|
|
|
27
|
|
|
public $newCommentState = [ |
28
|
|
|
'body' => '' |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
protected $listeners = [ |
32
|
|
|
'refresh' => '$refresh' |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
protected $validationAttributes = [ |
36
|
|
|
'newCommentState.body' => 'comment' |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
public function mount(Model $model) |
40
|
|
|
{ |
41
|
|
|
$this->model = $model; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return Factory|Application|View|\Illuminate\Contracts\Foundation\Application|null |
46
|
|
|
*/ |
47
|
|
|
public function render( |
48
|
|
|
): \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\Contracts\View\View|\Illuminate\Contracts\Foundation\Application|null |
49
|
|
|
{ |
50
|
|
|
$comments = $this->model |
51
|
|
|
->comments() |
52
|
|
|
->with('user', 'children.user', 'children.children') |
53
|
|
|
->parent() |
54
|
|
|
->latest() |
55
|
|
|
->paginate(config('commentify.pagination_count',10)); |
56
|
|
|
return view('commentify::livewire.comments', [ |
57
|
|
|
'comments' => $comments |
58
|
|
|
]); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return void |
63
|
|
|
*/ |
64
|
|
|
#[On('refresh')] |
65
|
|
|
public function postComment(): void |
66
|
|
|
{ |
67
|
|
|
if (config('commentify.read_only')) { |
68
|
|
|
session()->flash('message', __('commentify::commentify.comments.read_only_message')); |
69
|
|
|
session()->flash('alertType', 'warning'); |
70
|
|
|
return; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// Authorize using the CommentPolicy@create method |
74
|
|
|
try { |
75
|
|
|
$this->authorize('create', \Usamamuneerchaudhary\Commentify\Models\Comment::class); |
76
|
|
|
} catch (AuthorizationException $e) { |
77
|
|
|
session()->flash('message', __('commentify::commentify.comments.banned_message')); |
78
|
|
|
session()->flash('alertType', 'error'); |
79
|
|
|
return; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$this->validate([ |
83
|
|
|
'newCommentState.body' => 'required' |
84
|
|
|
]); |
85
|
|
|
|
86
|
|
|
$comment = $this->model->comments()->make($this->newCommentState); |
87
|
|
|
$comment->user()->associate(auth()->user()); |
88
|
|
|
$comment->save(); |
89
|
|
|
|
90
|
|
|
$this->newCommentState = [ |
91
|
|
|
'body' => '' |
92
|
|
|
]; |
93
|
|
|
$this->users = []; |
94
|
|
|
$this->showDropdown = false; |
95
|
|
|
|
96
|
|
|
$this->resetPage(); |
97
|
|
|
session()->flash('message', 'Comment Posted Successfully!'); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|