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 | use Throwable; |
||
24 | |||
25 | class Comment extends Component |
||
26 | { |
||
27 | use AuthorizesRequests; |
||
28 | use Toastable; |
||
29 | use WithCachedRows; |
||
30 | use WithPerPagePagination; |
||
31 | use WithSorting; |
||
32 | |||
33 | /** |
||
34 | * The article where the comments belong to. |
||
35 | * |
||
36 | * @var BlogArticle |
||
37 | */ |
||
38 | public BlogArticle $article; |
||
39 | |||
40 | /** |
||
41 | * The form used to create a comment. |
||
42 | * |
||
43 | * @var CommentForm |
||
44 | */ |
||
45 | public CommentForm $form; |
||
46 | |||
47 | /** |
||
48 | * Number of rows displayed on a page. |
||
49 | * |
||
50 | * @var int |
||
51 | */ |
||
52 | public int $perPage = 10; |
||
53 | |||
54 | /** |
||
55 | * The field to sort by. |
||
56 | * |
||
57 | * @var string |
||
58 | */ |
||
59 | public string $sortField = 'created_at'; |
||
60 | |||
61 | /** |
||
62 | * The direction of the ordering. |
||
63 | * |
||
64 | * @var string |
||
65 | */ |
||
66 | public string $sortDirection = 'desc'; |
||
67 | |||
68 | /** |
||
69 | * Array of allowed fields. |
||
70 | * |
||
71 | * @var array |
||
72 | */ |
||
73 | public array $allowedFields = [ |
||
74 | 'created_at' |
||
75 | ]; |
||
76 | |||
77 | /** |
||
78 | * The modal used to delete a comment. |
||
79 | * |
||
80 | * @var bool |
||
81 | */ |
||
82 | public bool $deleteCommentModal = false; |
||
83 | |||
84 | /** |
||
85 | * The comment id to delete. |
||
86 | * |
||
87 | * @var int|null |
||
88 | */ |
||
89 | public ?int $deleteCommentId = null; |
||
90 | |||
91 | /** |
||
92 | * The number of comment for this article. |
||
93 | * |
||
94 | * @var int|null |
||
95 | */ |
||
96 | public ?int $blogCommentCount = null; |
||
97 | |||
98 | /** |
||
99 | * The listeners. |
||
100 | * |
||
101 | * @var array |
||
102 | */ |
||
103 | protected $listeners = [ |
||
104 | 'deleted-event' => '$refresh' |
||
105 | ]; |
||
106 | |||
107 | |||
108 | public function mount(BlogArticle $article): void |
||
109 | { |
||
110 | $this->article = $article; |
||
111 | $this->form->blog_article_id = $article->id; |
||
112 | $this->blogCommentCount = $article->blog_comment_count; |
||
113 | $this->perPage = config('xetaravel.pagination.blog.comment_per_page'); |
||
114 | } |
||
115 | |||
116 | public function render(): View |
||
117 | { |
||
118 | return view('livewire.blog.comment', [ |
||
119 | 'comments' => $this->rows |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
120 | ]); |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Create and return the query for the items. |
||
125 | * |
||
126 | * @return Builder |
||
127 | */ |
||
128 | public function getRowsQueryProperty(): Builder |
||
129 | { |
||
130 | $query = BlogComment::query() |
||
131 | ->with('user', 'user.account') |
||
132 | ->where('blog_article_id', $this->article->id); |
||
133 | |||
134 | return $this->applySorting($query); |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * Build the query or get it from the cache and paginate it. |
||
139 | * |
||
140 | * @return LengthAwarePaginator |
||
141 | */ |
||
142 | public function getRowsProperty(): LengthAwarePaginator |
||
143 | { |
||
144 | return $this->cache(function () { |
||
145 | return $this->applyPagination($this->rowsQuery); |
||
0 ignored issues
–
show
The property
rowsQuery does not exist on Xetaravel\Livewire\Blog\Comment . Since you implemented __get , consider adding a @property annotation.
![]() |
|||
146 | }); |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * Validate and create the model. |
||
151 | * |
||
152 | * @return void |
||
153 | * |
||
154 | * @throws AuthorizationException|Throwable |
||
155 | */ |
||
156 | public function create(): void |
||
157 | { |
||
158 | $this->authorize('create', [BlogComment::class, $this->article]); |
||
159 | |||
160 | $this->validate(); |
||
161 | |||
162 | if (BlogComment::isFlooding('xetaravel.flood.blog.comment')) { |
||
163 | $this->error('Wow, keep calm bro, and try to not flood !'); |
||
164 | |||
165 | return; |
||
166 | } |
||
167 | $comment = $this->form->create(); |
||
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 |