1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Usamamuneerchaudhary\Commentify\Http\Livewire; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Illuminate\Auth\Access\AuthorizationException; |
7
|
|
|
use Illuminate\Contracts\View\Factory; |
8
|
|
|
use Illuminate\Contracts\View\View; |
9
|
|
|
use Illuminate\Foundation\Application; |
10
|
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests; |
11
|
|
|
use Illuminate\Support\Str; |
12
|
|
|
use Livewire\Attributes\On; |
13
|
|
|
use Livewire\Component; |
14
|
|
|
use Usamamuneerchaudhary\Commentify\Models\User; |
15
|
|
|
|
16
|
|
|
class Comment extends Component |
17
|
|
|
{ |
18
|
|
|
use AuthorizesRequests; |
19
|
|
|
|
20
|
|
|
public $comment; |
21
|
|
|
|
22
|
|
|
public $users = []; |
23
|
|
|
|
24
|
|
|
public $isReplying = false; |
25
|
|
|
public $hasReplies = false; |
26
|
|
|
|
27
|
|
|
public $showOptions = false; |
28
|
|
|
|
29
|
|
|
public $isEditing = false; |
30
|
|
|
|
31
|
|
|
public $replyState = [ |
32
|
|
|
'body' => '' |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
public $editState = [ |
36
|
|
|
'body' => '' |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
protected $validationAttributes = [ |
40
|
|
|
'replyState.body' => 'Reply', |
41
|
|
|
'editState.body' => 'Reply' |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param $isEditing |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
|
|
public function updatedIsEditing($isEditing): void |
50
|
|
|
{ |
51
|
|
|
if (!$isEditing) { |
52
|
|
|
return; |
53
|
|
|
} |
54
|
|
|
$this->editState = [ |
55
|
|
|
'body' => $this->comment->body |
56
|
|
|
]; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return void |
61
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
62
|
|
|
*/ |
63
|
|
|
public function editComment(): void |
64
|
|
|
{ |
65
|
|
|
$this->authorize('update', $this->comment); |
66
|
|
|
$this->validate([ |
67
|
|
|
'editState.body' => 'required|min:2' |
68
|
|
|
]); |
69
|
|
|
$this->comment->update($this->editState); |
70
|
|
|
$this->isEditing = false; |
71
|
|
|
$this->showOptions = false; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return void |
76
|
|
|
* @throws AuthorizationException |
77
|
|
|
*/ |
78
|
|
|
#[On('refresh')] |
79
|
|
|
public function deleteComment(): void |
80
|
|
|
{ |
81
|
|
|
$this->authorize('destroy', $this->comment); |
82
|
|
|
$this->comment->delete(); |
83
|
|
|
$this->showOptions = false; |
84
|
|
|
$this->dispatch('refresh'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return Factory|Application|View|\Illuminate\Contracts\Foundation\Application|null |
89
|
|
|
*/ |
90
|
|
|
public function render(): \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\Contracts\View\View|\Illuminate\Contracts\Foundation\Application|null |
91
|
|
|
{ |
92
|
|
|
return view('commentify::livewire.comment'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return void |
97
|
|
|
*/ |
98
|
|
|
#[On('refresh')] |
99
|
|
|
public function postReply(): void |
100
|
|
|
{ |
101
|
|
|
if (config('commentify.read_only')) { |
102
|
|
|
session()->flash('message', __('commentify::commentify.comments.read_only_message')); |
103
|
|
|
session()->flash('alertType', 'warning'); |
104
|
|
|
return; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$this->authorize('create', \Usamamuneerchaudhary\Commentify\Models\Comment::class); |
108
|
|
|
|
109
|
|
|
if (!$this->comment->isParent()) { |
110
|
|
|
return; |
111
|
|
|
} |
112
|
|
|
$this->validate([ |
113
|
|
|
'replyState.body' => 'required' |
114
|
|
|
]); |
115
|
|
|
$reply = $this->comment->children()->make($this->replyState); |
116
|
|
|
$reply->user()->associate(auth()->user()); |
117
|
|
|
$reply->commentable()->associate($this->comment->commentable); |
118
|
|
|
$reply->save(); |
119
|
|
|
|
120
|
|
|
$this->replyState = [ |
121
|
|
|
'body' => '' |
122
|
|
|
]; |
123
|
|
|
$this->isReplying = false; |
124
|
|
|
$this->showOptions = false; |
125
|
|
|
$this->dispatch('refresh')->self(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param $userName |
130
|
|
|
* @return void |
131
|
|
|
*/ |
132
|
|
|
public function selectUser($userName): void |
133
|
|
|
{ |
134
|
|
|
if ($this->replyState['body']) { |
135
|
|
|
$this->replyState['body'] = preg_replace('/@(\w+)$/', '@' . str_replace(' ', '_', Str::lower($userName)) . ' ', |
136
|
|
|
$this->replyState['body']); |
137
|
|
|
// $this->replyState['body'] =$userName; |
138
|
|
|
$this->users = []; |
139
|
|
|
} elseif ($this->editState['body']) { |
140
|
|
|
$this->editState['body'] = preg_replace('/@(\w+)$/', '@' . str_replace(' ', '_', Str::lower($userName)) . ' ', |
141
|
|
|
$this->editState['body']); |
142
|
|
|
$this->users = []; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param $searchTerm |
149
|
|
|
* @return void |
150
|
|
|
*/ |
151
|
|
|
#[On('getUsers')] |
152
|
|
|
public function getUsers($searchTerm): void |
153
|
|
|
{ |
154
|
|
|
if (!empty($searchTerm)) { |
155
|
|
|
$this->users = User::where('name', 'like', '%' . $searchTerm . '%')->take(5)->get(); |
156
|
|
|
} else { |
157
|
|
|
$this->users = []; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
} |
162
|
|
|
|