authorize('jobs.view-job', $jobId); $this->jobId = $jobId; $this->taskId = $taskId; $this->taskTitle = $taskTitle; } /** * @return list */ #[Computed] public function comments(): array { return app(TaskCommentQueries::class)->commentsOf($this->jobId, $this->taskId); } public function add(AddTaskComment $addComment): void { $this->authorize('jobs.update-task-status', $this->jobId); $this->resetErrorBag(); if (trim($this->newComment) === '') { $this->addError('newComment', __('jobs.tasks.comments.errors.body_required')); return; } try { $addComment->execute($this->jobId, $this->taskId, (int) auth()->id(), $this->newComment); } catch (InvalidTaskCommentBody) { $this->addError('newComment', __('jobs.tasks.comments.errors.invalid_body')); return; } catch (TaskNotFound) { $this->notFound(); return; } $this->reset('newComment'); $this->changed(__('jobs.tasks.comments.added')); } public function startEdit(int $commentId): void { $this->authorize('jobs.update-task-status', $this->jobId); $comment = $this->comment($commentId); if ($comment === null) { return; } if (! $this->isOwn($comment)) { $this->message = __('jobs.tasks.comments.errors.not_author'); return; } $this->editingCommentId = $comment->id; $this->editingBody = $comment->body; $this->resetErrorBag(); $this->js("document.getElementById('comment-edit-{$comment->id}')?.focus()"); } public function saveEdit(EditTaskComment $editComment): void { $this->authorize('jobs.update-task-status', $this->jobId); $this->resetErrorBag(); if ($this->editingCommentId === null) { return; } if (trim($this->editingBody) === '') { $this->addError('editingBody', __('jobs.tasks.comments.errors.body_required')); return; } try { $editComment->execute($this->jobId, $this->taskId, $this->editingCommentId, (int) auth()->id(), $this->editingBody); } catch (InvalidTaskCommentBody) { $this->addError('editingBody', __('jobs.tasks.comments.errors.invalid_body')); return; } catch (NotTaskCommentAuthor) { $this->cancelEdit(); $this->message = __('jobs.tasks.comments.errors.not_author'); return; } catch (TaskNotFound|TaskCommentNotFound) { $this->reset('editingCommentId', 'editingBody'); $this->notFound(); return; } $this->cancelEdit(); $this->changed(__('jobs.tasks.comments.saved')); } public function cancelEdit(): void { $commentId = $this->editingCommentId; $this->reset('editingCommentId', 'editingBody'); $this->resetErrorBag(); if ($commentId !== null) { $this->js("document.getElementById('comment-edit-button-{$commentId}')?.focus()"); } } public function confirmRemoval(int $commentId): void { $this->authorize('jobs.update-task-status', $this->jobId); $comment = $this->comment($commentId); if ($comment === null) { return; } if (! $this->mayRemove($comment)) { $this->message = __('jobs.tasks.comments.errors.not_author'); return; } $this->removalCommentId = $comment->id; $this->js("document.getElementById('remove-comment-dialog-{$this->taskId}').showModal()"); } public function remove(RemoveTaskComment $removeComment): void { $this->authorize('jobs.update-task-status', $this->jobId); if ($this->removalCommentId === null) { return; } $commentId = $this->removalCommentId; $this->reset('removalCommentId'); $this->js("document.getElementById('remove-comment-dialog-{$this->taskId}').close()"); try { $removeComment->execute($this->jobId, $this->taskId, $commentId, (int) auth()->id(), Gate::allows('jobs.remove-any-task-comment')); } catch (NotTaskCommentAuthor) { $this->message = __('jobs.tasks.comments.errors.not_author'); return; } catch (TaskNotFound|TaskCommentNotFound) { $this->notFound(); return; } if ($this->editingCommentId === $commentId) { $this->reset('editingCommentId', 'editingBody'); } $this->changed(__('jobs.tasks.comments.removed')); // The removed comment's buttons are gone: move focus to the field for a new comment. $this->js("document.getElementById('comment-new-{$this->taskId}')?.focus()"); } public function isOwn(TaskCommentSummary $comment): bool { return $comment->authorId !== null && $comment->authorId === (int) auth()->id(); } public function mayRemove(TaskCommentSummary $comment): bool { return $this->isOwn($comment) || Gate::allows('jobs.remove-any-task-comment'); } /** * "25.11.2026 klo 14.05" from "2026-11-25 14:05". */ public function formatTime(string $moment): string { $time = CarbonImmutable::createFromFormat('Y-m-d H:i', $moment); return __('jobs.tasks.comments.at', ['date' => $time->format('j.n.Y'), 'time' => $time->format('H.i')]); } public function authorName(TaskCommentSummary $comment): string { return $comment->authorName === '' ? __('jobs.tasks.comments.unknown_author') : $comment->authorName; } private function comment(int $commentId): ?TaskCommentSummary { foreach ($this->comments as $comment) { if ($comment->id === $commentId) { return $comment; } } $this->notFound(); return null; } /** * Refreshes the list and tells the job page, whose task cards show the * number of comments. */ private function changed(string $message): void { unset($this->comments); $this->message = $message; $this->dispatch('task-comments-changed'); } private function notFound(): void { $this->changed(__('jobs.tasks.comments.not_found')); } }; ?> {{-- Named by the task, so the comments of a job's tasks are distinguishable landmarks. --}}
@php($canComment = Gate::allows('jobs.update-task-status', $jobId))

{{ __('jobs.tasks.comments.title') }} @if ($this->comments !== []) ({{ count($this->comments) }}) @endif

{{ $message }}

@if ($this->comments === [])

{{ __('jobs.tasks.comments.empty') }}

@else
    @foreach ($this->comments as $comment) @php($at = $this->formatTime($comment->createdAt)) @php($named = ['author' => $this->authorName($comment), 'at' => $at])
  1. {{ $this->authorName($comment) }} @if ($comment->editedAt !== null) ({{ __('jobs.tasks.comments.edited', ['at' => $this->formatTime($comment->editedAt)]) }}) @endif

    @if ($editingCommentId === $comment->id)
    @else

    {{ $comment->body }}

    @if ($canComment && ($this->isOwn($comment) || $this->mayRemove($comment)))
    @if ($this->isOwn($comment)) @endif
    @endif @endif
  2. @endforeach
@endif @if ($canComment)

{{ __('jobs.tasks.comments.remove_question') }}

@endif