|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Xetaravel\Livewire\Blog; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Auth\Access\AuthorizationException; |
|
8
|
|
|
use Illuminate\Contracts\Database\Query\Builder; |
|
9
|
|
|
use Illuminate\Contracts\View\View; |
|
10
|
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests; |
|
11
|
|
|
use Illuminate\Pagination\LengthAwarePaginator; |
|
12
|
|
|
use Illuminate\Support\Facades\Auth; |
|
13
|
|
|
use Livewire\Component; |
|
14
|
|
|
use Masmerise\Toaster\Toastable; |
|
15
|
|
|
use Xetaravel\Events\Blog\CommentWasCreatedEvent; |
|
16
|
|
|
use Xetaravel\Livewire\Forms\CommentForm; |
|
17
|
|
|
use Xetaravel\Livewire\Traits\WithCachedRows; |
|
18
|
|
|
use Xetaravel\Livewire\Traits\WithPerPagePagination; |
|
19
|
|
|
use Xetaravel\Livewire\Traits\WithSorting; |
|
20
|
|
|
use Xetaravel\Models\BlogArticle; |
|
21
|
|
|
use Xetaravel\Models\BlogComment; |
|
22
|
|
|
use Xetaravel\Models\User; |
|
23
|
|
|
|
|
24
|
|
|
class Comment extends Component |
|
25
|
|
|
{ |
|
26
|
|
|
use AuthorizesRequests; |
|
27
|
|
|
use Toastable; |
|
28
|
|
|
use WithCachedRows; |
|
29
|
|
|
use WithPerPagePagination; |
|
30
|
|
|
use WithSorting; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* The article where the comments belong to. |
|
34
|
|
|
* |
|
35
|
|
|
* @var BlogArticle |
|
36
|
|
|
*/ |
|
37
|
|
|
public BlogArticle $article; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* The form used to create a comment. |
|
41
|
|
|
* |
|
42
|
|
|
* @var CommentForm |
|
43
|
|
|
*/ |
|
44
|
|
|
public CommentForm $form; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Number of rows displayed on a page. |
|
48
|
|
|
* |
|
49
|
|
|
* @var int |
|
50
|
|
|
*/ |
|
51
|
|
|
public int $perPage = 10; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* The field to sort by. |
|
55
|
|
|
* |
|
56
|
|
|
* @var string |
|
57
|
|
|
*/ |
|
58
|
|
|
public string $sortField = 'created_at'; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* The direction of the ordering. |
|
62
|
|
|
* |
|
63
|
|
|
* @var string |
|
64
|
|
|
*/ |
|
65
|
|
|
public string $sortDirection = 'desc'; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Array of allowed fields. |
|
69
|
|
|
* |
|
70
|
|
|
* @var array |
|
71
|
|
|
*/ |
|
72
|
|
|
public array $allowedFields = [ |
|
73
|
|
|
'created_at' |
|
74
|
|
|
]; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* The modal used to delete a comment. |
|
78
|
|
|
* |
|
79
|
|
|
* @var bool |
|
80
|
|
|
*/ |
|
81
|
|
|
public bool $deleteCommentModal = false; |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* The comment id to delete. |
|
85
|
|
|
* |
|
86
|
|
|
* @var int|null |
|
87
|
|
|
*/ |
|
88
|
|
|
public ?int $deleteCommentId = null; |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* The number of comment for this article. |
|
92
|
|
|
* |
|
93
|
|
|
* @var int|null |
|
94
|
|
|
*/ |
|
95
|
|
|
public ?int $blogCommentCount = null; |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* The listeners. |
|
99
|
|
|
* |
|
100
|
|
|
* @var array |
|
101
|
|
|
*/ |
|
102
|
|
|
protected $listeners = [ |
|
103
|
|
|
'deleted-event' => '$refresh' |
|
104
|
|
|
]; |
|
105
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
public function mount(BlogArticle $article): void |
|
108
|
|
|
{ |
|
109
|
|
|
$this->article = $article; |
|
110
|
|
|
$this->form->blog_article_id = $article->id; |
|
111
|
|
|
$this->blogCommentCount = $article->blog_comment_count; |
|
112
|
|
|
$this->perPage = config('xetaravel.pagination.blog.comment_per_page'); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function render(): View |
|
116
|
|
|
{ |
|
117
|
|
|
return view('livewire.blog.comment', [ |
|
118
|
|
|
'comments' => $this->rows |
|
|
|
|
|
|
119
|
|
|
]); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Create and return the query for the items. |
|
124
|
|
|
* |
|
125
|
|
|
* @return Builder |
|
126
|
|
|
*/ |
|
127
|
|
|
public function getRowsQueryProperty(): Builder |
|
128
|
|
|
{ |
|
129
|
|
|
$query = BlogComment::query() |
|
130
|
|
|
->with('user', 'user.account') |
|
131
|
|
|
->where('blog_article_id', $this->article->id); |
|
132
|
|
|
|
|
133
|
|
|
return $this->applySorting($query); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Build the query or get it from the cache and paginate it. |
|
138
|
|
|
* |
|
139
|
|
|
* @return LengthAwarePaginator |
|
140
|
|
|
*/ |
|
141
|
|
|
public function getRowsProperty(): LengthAwarePaginator |
|
142
|
|
|
{ |
|
143
|
|
|
return $this->cache(function () { |
|
144
|
|
|
return $this->applyPagination($this->rowsQuery); |
|
|
|
|
|
|
145
|
|
|
}); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Validate and create the model. |
|
150
|
|
|
* |
|
151
|
|
|
* @return void |
|
152
|
|
|
* |
|
153
|
|
|
* @throws AuthorizationException |
|
154
|
|
|
*/ |
|
155
|
|
|
public function create(): void |
|
156
|
|
|
{ |
|
157
|
|
|
$this->authorize('create', BlogComment::class); |
|
158
|
|
|
|
|
159
|
|
|
$this->validate(); |
|
160
|
|
|
|
|
161
|
|
|
if (BlogComment::isFlooding('xetaravel.flood.blog.comment')) { |
|
162
|
|
|
$this->error('Wow, keep calm bro, and try to not flood !'); |
|
163
|
|
|
|
|
164
|
|
|
return; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
$comment = $this->form->store(); |
|
168
|
|
|
|
|
169
|
|
|
// We must find the user else we won't see the updated blog_comment_count. |
|
170
|
|
|
event(new CommentWasCreatedEvent(User::find(Auth::id()), $comment)); |
|
171
|
|
|
|
|
172
|
|
|
$this->form->content = ""; |
|
173
|
|
|
$this->blogCommentCount++; |
|
174
|
|
|
|
|
175
|
|
|
$this->success('Your comment has been posted successfully !'); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Delete a comment. |
|
180
|
|
|
* |
|
181
|
|
|
* @return void |
|
182
|
|
|
*/ |
|
183
|
|
|
public function delete(): void |
|
184
|
|
|
{ |
|
185
|
|
|
$comment = BlogComment::findOrFail($this->deleteCommentId); |
|
186
|
|
|
|
|
187
|
|
|
$this->authorize('delete', [$comment, $this->article]); |
|
188
|
|
|
|
|
189
|
|
|
if ($comment->delete()) { |
|
190
|
|
|
$this->success("Your comment has been deleted successfully !"); |
|
191
|
|
|
$this->blogCommentCount--; |
|
192
|
|
|
$this->deleteCommentModal = false; |
|
193
|
|
|
$this->dispatch('deleted-event'); |
|
194
|
|
|
|
|
195
|
|
|
return; |
|
196
|
|
|
} |
|
197
|
|
|
$this->error('Whoops, looks like something went wrong !'); |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|