Untitled
unknown
php
3 years ago
3.4 kB
10
Indexable
<?php
namespace App\Http\Livewire\Production\Tasks\Comments;
use App\Models\ProductionAssignmentLinkComment;
use App\Models\ProductionTask;
use App\Models\ProductionTaskComment;
use Livewire\Component;
class Write extends Component
{
public ProductionTask $task;
public ?string $comment = '';
public ProductionTaskComment|ProductionAssignmentLinkComment|null $replyComment = null;
public ?string $optionType = '';
public bool $isLinkComment = false;
public $activeLink;
public array $form = [
'timestamp' => 0,
];
protected $listeners = [
'replyComment', 'activeLink'
];
public function render()
{
return view('production.tasks.comments.write');
}
public function mount()
{
$this->activeLink = $this->task->researchAssignment->links[0]->id;
}
public function activeLink($activeLink)
{
$this->form['timestamp'] = 0;
$this->activeLink = $activeLink['id'];
}
public function replyComment(int $id, string $type)
{
$this->replyComment = $type === 'task_comment'
? $this->task->comments()->findOrFail($id)
: $this->task->researchAssignment->linkComments()->findOrFail($id);
abort_if(!is_null($this->replyComment->parent_id), 403);
}
public function submit()
{
$this->validate([
'comment' => 'required|string|min:3|max:1500'
]);
if ($this->replyComment instanceof ProductionTaskComment) {
return $this->createTaskComment();
} elseif ($this->replyComment instanceof ProductionAssignmentLinkComment) {
return $this->createLinkComment();
}
if ($this->isLinkComment) {
return $this->createLinkComment();
} else {
return $this->createTaskComment();
}
}
protected function createTaskComment()
{
$this->task->comments()->create([
'production_task_id' => $this->task->id,
'user_id' => auth()->id(),
'text' => $this->comment,
'parent_id' => $this->replyComment->id ?? null
]);
$this->resetComment();
$this->emit('refreshComments');
}
protected function createLinkComment()
{
$this->task->researchAssignment->linkComments()->create([
'user_id' => auth()->id(),
'production_assignment_link_id' => $this->replyComment !== null
? $this->replyComment->production_assignment_link_id
: $this->activeLink,
'text' => $this->comment,
'timestamp' => $this->form['timestamp'] ?? null,
'parent_id' => $this->replyComment->id ?? null
]);
$this->resetComment();
$this->emit('refreshComments');
}
public function resetComment()
{
$this->reset('replyComment');
$this->reset('comment');
}
protected function mayCreateNotificationForReply(ProductionTaskComment $taskComment)
{
$parent = $taskComment->parent;
if (!$parent) {
return;
}
$parentUser = $taskComment->parent->user;
if ($parentUser->id == auth()->id()) {
return;
}
auth()->user()->createNotificationFor($taskComment->parent->user)->typeRepliedInTaskComment($taskComment);
}
}Editor is loading...