Everyone who may change a task's status (attached subcontractors too) comments on it on the job page. Authors edit and remove their own comments; admin and management may remove anyone's (jobs.remove-any-task-comment). Edited comments show when they were edited, and task cards show the number of comments. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
366 lines
13 KiB
PHP
366 lines
13 KiB
PHP
<?php
|
|
|
|
use App\Modules\Jobs\Application\Contracts\TaskCommentQueries;
|
|
use App\Modules\Jobs\Application\DTOs\TaskCommentSummary;
|
|
use App\Modules\Jobs\Application\UseCases\AddTaskComment;
|
|
use App\Modules\Jobs\Application\UseCases\EditTaskComment;
|
|
use App\Modules\Jobs\Application\UseCases\RemoveTaskComment;
|
|
use App\Modules\Jobs\Domain\Exceptions\InvalidTaskCommentBody;
|
|
use App\Modules\Jobs\Domain\Exceptions\NotTaskCommentAuthor;
|
|
use App\Modules\Jobs\Domain\Exceptions\TaskCommentNotFound;
|
|
use App\Modules\Jobs\Domain\Exceptions\TaskNotFound;
|
|
use Carbon\CarbonImmutable;
|
|
use Illuminate\Support\Facades\Gate;
|
|
use Livewire\Attributes\Computed;
|
|
use Livewire\Attributes\Locked;
|
|
use Livewire\Component;
|
|
|
|
/**
|
|
* A task's comments on the job page (spec 8.2, K27): everyone who may change
|
|
* the task's status (also attached subcontractors) comments; authors edit
|
|
* and remove their own comments, admin and management remove anyone's.
|
|
*/
|
|
new class extends Component
|
|
{
|
|
#[Locked]
|
|
public int $jobId;
|
|
|
|
#[Locked]
|
|
public int $taskId;
|
|
|
|
#[Locked]
|
|
public string $taskTitle = '';
|
|
|
|
public string $newComment = '';
|
|
|
|
/**
|
|
* The comment being edited, if any.
|
|
*/
|
|
#[Locked]
|
|
public ?int $editingCommentId = null;
|
|
|
|
public string $editingBody = '';
|
|
|
|
/**
|
|
* The comment the remove dialog asks about.
|
|
*/
|
|
#[Locked]
|
|
public ?int $removalCommentId = null;
|
|
|
|
public string $message = '';
|
|
|
|
public function mount(int $jobId, int $taskId, string $taskTitle): void
|
|
{
|
|
$this->authorize('jobs.view-job', $jobId);
|
|
|
|
$this->jobId = $jobId;
|
|
$this->taskId = $taskId;
|
|
$this->taskTitle = $taskTitle;
|
|
}
|
|
|
|
/**
|
|
* @return list<TaskCommentSummary>
|
|
*/
|
|
#[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. --}}
|
|
<section aria-label="{{ __('jobs.tasks.comments.list_of', ['title' => $taskTitle]) }}" class="space-y-2">
|
|
@php($canComment = Gate::allows('jobs.update-task-status', $jobId))
|
|
|
|
<h4 class="font-medium">
|
|
{{ __('jobs.tasks.comments.title') }}
|
|
@if ($this->comments !== [])
|
|
<span class="font-normal">({{ count($this->comments) }})</span>
|
|
@endif
|
|
</h4>
|
|
|
|
<p role="status" aria-live="polite" class="text-sm">{{ $message }}</p>
|
|
|
|
@if ($this->comments === [])
|
|
<p class="text-zinc-600 dark:text-zinc-300">{{ __('jobs.tasks.comments.empty') }}</p>
|
|
@else
|
|
<ol class="space-y-3">
|
|
@foreach ($this->comments as $comment)
|
|
@php($at = $this->formatTime($comment->createdAt))
|
|
@php($named = ['author' => $this->authorName($comment), 'at' => $at])
|
|
<li wire:key="task-comment-{{ $comment->id }}" class="space-y-1 rounded-md bg-zinc-50 p-3 dark:bg-zinc-800">
|
|
<p class="flex flex-wrap gap-x-2">
|
|
<span class="font-medium">{{ $this->authorName($comment) }}</span>
|
|
<span class="text-zinc-600 dark:text-zinc-300">
|
|
<time datetime="{{ str_replace(' ', 'T', $comment->createdAt) }}">{{ $at }}</time>
|
|
@if ($comment->editedAt !== null)
|
|
({{ __('jobs.tasks.comments.edited', ['at' => $this->formatTime($comment->editedAt)]) }})
|
|
@endif
|
|
</span>
|
|
</p>
|
|
|
|
@if ($editingCommentId === $comment->id)
|
|
<form wire:submit="saveEdit" class="space-y-2">
|
|
<x-textarea id="comment-edit-{{ $comment->id }}" wire:model="editingBody" :label="__('jobs.tasks.comments.body')" maxlength="5000" resize-auto />
|
|
<div class="flex flex-wrap gap-2">
|
|
<x-button submit sm icon="check" :text="__('jobs.tasks.comments.save')" />
|
|
<x-button wire:click="cancelEdit" subtle color="black" sm :text="__('jobs.tasks.comments.cancel')" />
|
|
</div>
|
|
</form>
|
|
@else
|
|
<p class="whitespace-pre-line break-words">{{ $comment->body }}</p>
|
|
|
|
@if ($canComment && ($this->isOwn($comment) || $this->mayRemove($comment)))
|
|
<div class="flex flex-wrap items-center gap-2">
|
|
@if ($this->isOwn($comment))
|
|
<x-button
|
|
id="comment-edit-button-{{ $comment->id }}"
|
|
wire:click="startEdit({{ $comment->id }})"
|
|
subtle
|
|
color="black"
|
|
sm
|
|
icon="pencil"
|
|
:text="__('jobs.edit')"
|
|
:aria-label="__('jobs.tasks.comments.edit_named', $named)"
|
|
/>
|
|
@endif
|
|
<x-button
|
|
wire:click="confirmRemoval({{ $comment->id }})"
|
|
subtle
|
|
color="black"
|
|
sm
|
|
icon="trash"
|
|
:text="__('jobs.tasks.comments.remove')"
|
|
:aria-label="__('jobs.tasks.comments.remove_named', $named)"
|
|
/>
|
|
</div>
|
|
@endif
|
|
@endif
|
|
</li>
|
|
@endforeach
|
|
</ol>
|
|
@endif
|
|
|
|
@if ($canComment)
|
|
<form wire:submit="add" class="space-y-2">
|
|
<x-textarea id="comment-new-{{ $taskId }}" wire:model="newComment" :label="__('jobs.tasks.comments.new')" maxlength="5000" resize-auto />
|
|
<x-button submit sm icon="chat-bubble-left" :text="__('jobs.tasks.comments.add')" :aria-label="__('jobs.tasks.comments.add_named', ['title' => $taskTitle])" />
|
|
</form>
|
|
|
|
<x-confirm-dialog id="remove-comment-dialog-{{ $taskId }}" :title="__('jobs.tasks.comments.remove_title')">
|
|
<p>{{ __('jobs.tasks.comments.remove_question') }}</p>
|
|
|
|
<x-slot:actions>
|
|
<x-button id="remove-comment-confirm-{{ $taskId }}" wire:click="remove" color="red" icon="trash" :text="__('jobs.tasks.comments.remove')" />
|
|
</x-slot:actions>
|
|
</x-confirm-dialog>
|
|
@endif
|
|
</section>
|