Add checklists to tasks on the job page
Tasks get an ordered checklist (spec 8.2, phase 3): items are checked done with who and when recorded, also by subcontractors attached to the job; others add, rename, reorder and remove items. Items go with their task. The spec gets a phase 3 chapter (18.1), and open questions move to chapter 19. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -356,6 +356,8 @@ new class extends Component
|
||||
<p class="whitespace-pre-line">{{ $task->description }}</p>
|
||||
@endif
|
||||
|
||||
<livewire:jobs::task-checklist :job-id="$job->id" :task-id="$task->id" :task-title="$task->title" :wire:key="'checklist-'.$task->id" />
|
||||
|
||||
<div class="flex flex-wrap items-end gap-3">
|
||||
@if ($canChangeStatus)
|
||||
<div class="w-full sm:w-56">
|
||||
|
||||
@@ -0,0 +1,364 @@
|
||||
<?php
|
||||
|
||||
use App\Modules\Jobs\Application\Contracts\ChecklistQueries;
|
||||
use App\Modules\Jobs\Application\DTOs\ChecklistItemSummary;
|
||||
use App\Modules\Jobs\Application\UseCases\AddChecklistItem;
|
||||
use App\Modules\Jobs\Application\UseCases\CheckChecklistItem;
|
||||
use App\Modules\Jobs\Application\UseCases\MoveChecklistItem;
|
||||
use App\Modules\Jobs\Application\UseCases\RemoveChecklistItem;
|
||||
use App\Modules\Jobs\Application\UseCases\RenameChecklistItem;
|
||||
use App\Modules\Jobs\Domain\Enums\MoveDirection;
|
||||
use App\Modules\Jobs\Domain\Exceptions\ChecklistItemNotFound;
|
||||
use App\Modules\Jobs\Domain\Exceptions\InvalidChecklistItemText;
|
||||
use App\Modules\Jobs\Domain\Exceptions\TaskNotFound;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Livewire\Attributes\Computed;
|
||||
use Livewire\Attributes\Locked;
|
||||
use Livewire\Component;
|
||||
|
||||
/**
|
||||
* A task's checklist on the job page: checking items (also attached
|
||||
* subcontractors) and adding, renaming, ordering and removing them.
|
||||
*/
|
||||
new class extends Component
|
||||
{
|
||||
#[Locked]
|
||||
public int $jobId;
|
||||
|
||||
#[Locked]
|
||||
public int $taskId;
|
||||
|
||||
#[Locked]
|
||||
public string $taskTitle = '';
|
||||
|
||||
public string $newItem = '';
|
||||
|
||||
/**
|
||||
* The item being renamed, if any.
|
||||
*/
|
||||
#[Locked]
|
||||
public ?int $editingItemId = null;
|
||||
|
||||
public string $editingText = '';
|
||||
|
||||
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<ChecklistItemSummary>
|
||||
*/
|
||||
#[Computed]
|
||||
public function items(): array
|
||||
{
|
||||
return app(ChecklistQueries::class)->checklistOf($this->jobId, $this->taskId);
|
||||
}
|
||||
|
||||
public function check(int $itemId, bool $done, CheckChecklistItem $checkItem): void
|
||||
{
|
||||
$this->authorize('jobs.update-task-status', $this->jobId);
|
||||
|
||||
$item = $this->item($itemId);
|
||||
|
||||
if ($item === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$checkItem->execute($this->jobId, $this->taskId, $itemId, $done, (int) auth()->id());
|
||||
} catch (TaskNotFound|ChecklistItemNotFound) {
|
||||
$this->notFound();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->refreshItems();
|
||||
$this->message = __($done ? 'jobs.tasks.checklist.checked' : 'jobs.tasks.checklist.unchecked', ['text' => $item->text]);
|
||||
}
|
||||
|
||||
public function add(AddChecklistItem $addItem): void
|
||||
{
|
||||
$this->authorize('jobs.manage-tasks', $this->jobId);
|
||||
$this->resetErrorBag();
|
||||
|
||||
if (trim($this->newItem) === '') {
|
||||
$this->addError('newItem', __('jobs.tasks.checklist.errors.text_required'));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$addItem->execute($this->jobId, $this->taskId, $this->newItem);
|
||||
} catch (InvalidChecklistItemText) {
|
||||
$this->addError('newItem', __('jobs.tasks.checklist.errors.invalid_text'));
|
||||
|
||||
return;
|
||||
} catch (TaskNotFound) {
|
||||
$this->notFound();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->refreshItems();
|
||||
$this->message = __('jobs.tasks.checklist.added', ['text' => trim($this->newItem)]);
|
||||
$this->reset('newItem');
|
||||
}
|
||||
|
||||
public function startRename(int $itemId): void
|
||||
{
|
||||
$this->authorize('jobs.manage-tasks', $this->jobId);
|
||||
|
||||
$item = $this->item($itemId);
|
||||
|
||||
if ($item === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->editingItemId = $item->id;
|
||||
$this->editingText = $item->text;
|
||||
$this->resetErrorBag();
|
||||
$this->js("document.getElementById('checklist-edit-{$item->id}')?.focus()");
|
||||
}
|
||||
|
||||
public function saveRename(RenameChecklistItem $renameItem): void
|
||||
{
|
||||
$this->authorize('jobs.manage-tasks', $this->jobId);
|
||||
$this->resetErrorBag();
|
||||
|
||||
if ($this->editingItemId === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (trim($this->editingText) === '') {
|
||||
$this->addError('editingText', __('jobs.tasks.checklist.errors.text_required'));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$renameItem->execute($this->jobId, $this->taskId, $this->editingItemId, $this->editingText);
|
||||
} catch (InvalidChecklistItemText) {
|
||||
$this->addError('editingText', __('jobs.tasks.checklist.errors.invalid_text'));
|
||||
|
||||
return;
|
||||
} catch (TaskNotFound|ChecklistItemNotFound) {
|
||||
$this->cancelRename();
|
||||
$this->notFound();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->refreshItems();
|
||||
$this->message = __('jobs.tasks.checklist.renamed', ['text' => trim($this->editingText)]);
|
||||
$this->cancelRename();
|
||||
}
|
||||
|
||||
public function cancelRename(): void
|
||||
{
|
||||
$itemId = $this->editingItemId;
|
||||
|
||||
$this->reset('editingItemId', 'editingText');
|
||||
$this->resetErrorBag();
|
||||
|
||||
if ($itemId !== null) {
|
||||
$this->js("document.getElementById('checklist-rename-{$itemId}')?.focus()");
|
||||
}
|
||||
}
|
||||
|
||||
public function move(int $itemId, string $direction, MoveChecklistItem $moveItem): void
|
||||
{
|
||||
$this->authorize('jobs.manage-tasks', $this->jobId);
|
||||
|
||||
$item = $this->item($itemId);
|
||||
$moveDirection = MoveDirection::tryFrom($direction);
|
||||
|
||||
if ($item === null || $moveDirection === null) {
|
||||
$this->notFound();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$moved = $moveItem->execute($this->jobId, $this->taskId, $itemId, $moveDirection);
|
||||
} catch (TaskNotFound|ChecklistItemNotFound) {
|
||||
$this->notFound();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->refreshItems();
|
||||
$this->message = __(match (true) {
|
||||
$moved && $moveDirection === MoveDirection::Up => 'jobs.tasks.checklist.moved_up',
|
||||
$moved => 'jobs.tasks.checklist.moved_down',
|
||||
$moveDirection === MoveDirection::Up => 'jobs.tasks.checklist.already_first',
|
||||
default => 'jobs.tasks.checklist.already_last',
|
||||
}, ['text' => $item->text]);
|
||||
|
||||
// The item moved in the DOM: keep focus on the same button.
|
||||
$this->js("document.getElementById('checklist-move-{$moveDirection->value}-{$itemId}')?.focus()");
|
||||
}
|
||||
|
||||
public function remove(int $itemId, RemoveChecklistItem $removeItem): void
|
||||
{
|
||||
$this->authorize('jobs.manage-tasks', $this->jobId);
|
||||
|
||||
$item = $this->item($itemId);
|
||||
|
||||
if ($item === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$removeItem->execute($this->jobId, $this->taskId, $itemId);
|
||||
} catch (TaskNotFound|ChecklistItemNotFound) {
|
||||
$this->notFound();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ($this->editingItemId === $itemId) {
|
||||
$this->reset('editingItemId', 'editingText');
|
||||
}
|
||||
|
||||
$this->refreshItems();
|
||||
$this->message = __('jobs.tasks.checklist.removed', ['text' => $item->text]);
|
||||
|
||||
// The removed item's buttons are gone: move focus to the field for a new item.
|
||||
$this->js("document.getElementById('checklist-new-{$this->taskId}')?.focus()");
|
||||
}
|
||||
|
||||
private function item(int $itemId): ?ChecklistItemSummary
|
||||
{
|
||||
foreach ($this->items as $item) {
|
||||
if ($item->id === $itemId) {
|
||||
return $item;
|
||||
}
|
||||
}
|
||||
|
||||
$this->notFound();
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private function notFound(): void
|
||||
{
|
||||
$this->refreshItems();
|
||||
$this->message = __('jobs.tasks.checklist.not_found');
|
||||
}
|
||||
|
||||
private function refreshItems(): void
|
||||
{
|
||||
unset($this->items);
|
||||
}
|
||||
};
|
||||
?>
|
||||
|
||||
<section aria-labelledby="checklist-{{ $taskId }}-heading" class="space-y-2">
|
||||
@php($canCheck = Gate::allows('jobs.update-task-status', $jobId))
|
||||
@php($canManage = Gate::allows('jobs.manage-tasks', $jobId))
|
||||
@php($doneCount = count(array_filter($this->items, fn ($item) => $item->done)))
|
||||
|
||||
<h4 id="checklist-{{ $taskId }}-heading" class="font-medium">
|
||||
{{ __('jobs.tasks.checklist.title') }}
|
||||
@if ($this->items !== [])
|
||||
<span class="font-normal">{{ __('jobs.tasks.checklist.progress', ['done' => $doneCount, 'total' => count($this->items)]) }}</span>
|
||||
@endif
|
||||
</h4>
|
||||
|
||||
<p role="status" aria-live="polite" class="text-sm">{{ $message }}</p>
|
||||
|
||||
@if ($this->items === [])
|
||||
<p class="text-zinc-600 dark:text-zinc-300">{{ __('jobs.tasks.checklist.empty') }}</p>
|
||||
@else
|
||||
<ul class="space-y-2" aria-label="{{ __('jobs.tasks.checklist.list_of', ['title' => $taskTitle]) }}">
|
||||
@foreach ($this->items as $item)
|
||||
<li wire:key="checklist-item-{{ $item->id }}" class="flex flex-wrap items-center gap-x-3 gap-y-2">
|
||||
@if ($editingItemId === $item->id)
|
||||
<form wire:submit="saveRename" class="flex w-full flex-wrap items-end gap-2">
|
||||
<div class="min-w-0 grow sm:max-w-md">
|
||||
<x-input id="checklist-edit-{{ $item->id }}" wire:model="editingText" :label="__('jobs.tasks.checklist.text')" maxlength="255" />
|
||||
</div>
|
||||
<x-button submit sm icon="check" :text="__('jobs.tasks.checklist.save')" />
|
||||
<x-button wire:click="cancelRename" subtle color="black" sm :text="__('jobs.tasks.checklist.cancel')" />
|
||||
</form>
|
||||
@else
|
||||
@if ($canCheck)
|
||||
<label class="inline-flex min-h-6 min-w-0 grow cursor-pointer items-start gap-2 break-words">
|
||||
<input
|
||||
type="checkbox"
|
||||
id="checklist-item-{{ $item->id }}"
|
||||
wire:change="check({{ $item->id }}, $event.target.checked)"
|
||||
@checked($item->done)
|
||||
class="mt-0.5 size-4 shrink-0 rounded border-zinc-400 text-primary-600 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-700 dark:border-zinc-500 dark:bg-zinc-800"
|
||||
/>
|
||||
<span @class(['line-through' => $item->done])>{{ $item->text }}</span>
|
||||
</label>
|
||||
@else
|
||||
<span class="min-w-0 grow break-words">
|
||||
<span @class(['line-through' => $item->done])>{{ $item->text }}</span>
|
||||
<span class="text-zinc-600 dark:text-zinc-300">({{ $item->done ? __('jobs.tasks.checklist.done') : __('jobs.tasks.checklist.not_done') }})</span>
|
||||
</span>
|
||||
@endif
|
||||
|
||||
@if ($canManage)
|
||||
<div class="flex flex-wrap items-center gap-2">
|
||||
<x-button
|
||||
id="checklist-rename-{{ $item->id }}"
|
||||
wire:click="startRename({{ $item->id }})"
|
||||
subtle
|
||||
color="black"
|
||||
sm
|
||||
icon="pencil"
|
||||
:text="__('jobs.edit')"
|
||||
:aria-label="__('jobs.tasks.checklist.rename_named', ['text' => $item->text])"
|
||||
/>
|
||||
<x-button.circle
|
||||
id="checklist-move-up-{{ $item->id }}"
|
||||
wire:click="move({{ $item->id }}, 'up')"
|
||||
subtle
|
||||
color="black"
|
||||
sm
|
||||
icon="arrow-up"
|
||||
:aria-label="__('jobs.tasks.checklist.move_up_named', ['text' => $item->text])"
|
||||
/>
|
||||
<x-button.circle
|
||||
id="checklist-move-down-{{ $item->id }}"
|
||||
wire:click="move({{ $item->id }}, 'down')"
|
||||
subtle
|
||||
color="black"
|
||||
sm
|
||||
icon="arrow-down"
|
||||
:aria-label="__('jobs.tasks.checklist.move_down_named', ['text' => $item->text])"
|
||||
/>
|
||||
<x-button.circle
|
||||
wire:click="remove({{ $item->id }})"
|
||||
subtle
|
||||
color="black"
|
||||
sm
|
||||
icon="trash"
|
||||
:aria-label="__('jobs.tasks.checklist.remove_named', ['text' => $item->text])"
|
||||
/>
|
||||
</div>
|
||||
@endif
|
||||
@endif
|
||||
</li>
|
||||
@endforeach
|
||||
</ul>
|
||||
@endif
|
||||
|
||||
@if ($canManage)
|
||||
<form wire:submit="add" class="flex flex-wrap items-end gap-2">
|
||||
<div class="min-w-0 grow sm:max-w-md">
|
||||
<x-input id="checklist-new-{{ $taskId }}" wire:model="newItem" :label="__('jobs.tasks.checklist.new_item')" maxlength="255" />
|
||||
</div>
|
||||
<x-button submit sm icon="plus" :text="__('jobs.tasks.checklist.add')" :aria-label="__('jobs.tasks.checklist.add_named', ['title' => $taskTitle])" />
|
||||
</form>
|
||||
@endif
|
||||
</section>
|
||||
Reference in New Issue
Block a user