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:
2026-11-24 16:38:08 +02:00
co-authored by Claude Opus 5.5
parent e521df647c
commit dfe4941f02
28 changed files with 1376 additions and 3 deletions
@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Application\Contracts;
use App\Modules\Jobs\Application\DTOs\ChecklistItemSummary;
interface ChecklistQueries
{
/**
* The task's checklist in its order; empty when the task belongs to
* another job.
*
* @return list<ChecklistItemSummary>
*/
public function checklistOf(int $jobId, int $taskId): array;
}
@@ -0,0 +1,14 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Application\DTOs;
final readonly class ChecklistItemSummary
{
public function __construct(
public int $id,
public string $text,
public bool $done,
) {}
}
@@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Application\Services;
use App\Modules\Jobs\Domain\Contracts\ChecklistItemRepository;
use App\Modules\Jobs\Domain\Contracts\TaskRepository;
use App\Modules\Jobs\Domain\Entities\ChecklistItem;
use App\Modules\Jobs\Domain\Exceptions\ChecklistItemNotFound;
use App\Modules\Jobs\Domain\Exceptions\TaskNotFound;
/**
* Finds a checklist item only through its task and the task's job, so a
* page of one job never changes another job's items.
*/
final readonly class ChecklistItemFinder
{
public function __construct(
private TaskRepository $tasks,
private ChecklistItemRepository $items,
) {}
/**
* @throws TaskNotFound
* @throws ChecklistItemNotFound
*/
public function find(int $jobId, int $taskId, int $itemId): ChecklistItem
{
$this->ensureTask($jobId, $taskId);
return $this->items->findForTask($taskId, $itemId) ?? throw new ChecklistItemNotFound($taskId, $itemId);
}
/**
* @throws TaskNotFound
*/
public function ensureTask(int $jobId, int $taskId): void
{
if ($this->tasks->findForJob($jobId, $taskId) === null) {
throw new TaskNotFound($jobId, $taskId);
}
}
}
@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Application\UseCases;
use App\Modules\Jobs\Application\Services\ChecklistItemFinder;
use App\Modules\Jobs\Domain\Contracts\ChecklistItemRepository;
use App\Modules\Jobs\Domain\Entities\ChecklistItem;
use App\Modules\Jobs\Domain\Exceptions\InvalidChecklistItemText;
use App\Modules\Jobs\Domain\Exceptions\TaskNotFound;
use App\Modules\Jobs\Domain\ValueObjects\ChecklistItemText;
use App\Modules\Shared\Application\Contracts\TransactionManager;
/**
* Adds an item to the end of a task's checklist.
*/
final class AddChecklistItem
{
public function __construct(
private ChecklistItemFinder $finder,
private ChecklistItemRepository $items,
private TransactionManager $transactions,
) {}
/**
* @return int The new item's id.
*
* @throws TaskNotFound
* @throws InvalidChecklistItemText
*/
public function execute(int $jobId, int $taskId, string $text): int
{
$text = new ChecklistItemText($text);
return $this->transactions->run(function () use ($jobId, $taskId, $text): int {
$this->finder->ensureTask($jobId, $taskId);
return (int) $this->items->save(ChecklistItem::create($taskId, $text, $this->items->nextSortOrder($taskId)))->id();
});
}
}
@@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Application\UseCases;
use App\Modules\Jobs\Application\Services\ChecklistItemFinder;
use App\Modules\Jobs\Domain\Contracts\ChecklistItemRepository;
use App\Modules\Jobs\Domain\Exceptions\ChecklistItemNotFound;
use App\Modules\Jobs\Domain\Exceptions\TaskNotFound;
use App\Modules\Shared\Application\Contracts\TransactionManager;
use App\Modules\Users\Application\Contracts\Clock;
/**
* Marks a checklist item done (recording who and when) or not done.
*/
final class CheckChecklistItem
{
public function __construct(
private ChecklistItemFinder $finder,
private ChecklistItemRepository $items,
private TransactionManager $transactions,
private Clock $clock,
) {}
/**
* @throws TaskNotFound
* @throws ChecklistItemNotFound
*/
public function execute(int $jobId, int $taskId, int $itemId, bool $done, int $userId): void
{
$this->transactions->run(function () use ($jobId, $taskId, $itemId, $done, $userId): void {
$item = $this->finder->find($jobId, $taskId, $itemId);
if ($done) {
$item->check($userId, $this->clock->now());
} else {
$item->uncheck();
}
$this->items->save($item);
});
}
}
@@ -0,0 +1,48 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Application\UseCases;
use App\Modules\Jobs\Application\Services\ChecklistItemFinder;
use App\Modules\Jobs\Domain\Contracts\ChecklistItemRepository;
use App\Modules\Jobs\Domain\Enums\MoveDirection;
use App\Modules\Jobs\Domain\Exceptions\ChecklistItemNotFound;
use App\Modules\Jobs\Domain\Exceptions\TaskNotFound;
use App\Modules\Shared\Application\Contracts\TransactionManager;
/**
* Moves a checklist item one place up or down.
*/
final class MoveChecklistItem
{
public function __construct(
private ChecklistItemFinder $finder,
private ChecklistItemRepository $items,
private TransactionManager $transactions,
) {}
/**
* @return bool False when the item already was first (up) or last (down).
*
* @throws TaskNotFound
* @throws ChecklistItemNotFound
*/
public function execute(int $jobId, int $taskId, int $itemId, MoveDirection $direction): bool
{
return $this->transactions->run(function () use ($jobId, $taskId, $itemId, $direction): bool {
$item = $this->finder->find($jobId, $taskId, $itemId);
$neighbour = $this->items->neighbour($item, $direction);
if ($neighbour === null) {
return false;
}
$item->swapOrderWith($neighbour);
$this->items->save($item);
$this->items->save($neighbour);
return true;
});
}
}
@@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Application\UseCases;
use App\Modules\Jobs\Application\Services\ChecklistItemFinder;
use App\Modules\Jobs\Domain\Contracts\ChecklistItemRepository;
use App\Modules\Jobs\Domain\Exceptions\ChecklistItemNotFound;
use App\Modules\Jobs\Domain\Exceptions\TaskNotFound;
use App\Modules\Shared\Application\Contracts\TransactionManager;
final class RemoveChecklistItem
{
public function __construct(
private ChecklistItemFinder $finder,
private ChecklistItemRepository $items,
private TransactionManager $transactions,
) {}
/**
* @throws TaskNotFound
* @throws ChecklistItemNotFound
*/
public function execute(int $jobId, int $taskId, int $itemId): void
{
$this->transactions->run(function () use ($jobId, $taskId, $itemId): void {
$this->items->delete($this->finder->find($jobId, $taskId, $itemId));
});
}
}
@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Application\UseCases;
use App\Modules\Jobs\Application\Services\ChecklistItemFinder;
use App\Modules\Jobs\Domain\Contracts\ChecklistItemRepository;
use App\Modules\Jobs\Domain\Exceptions\ChecklistItemNotFound;
use App\Modules\Jobs\Domain\Exceptions\InvalidChecklistItemText;
use App\Modules\Jobs\Domain\Exceptions\TaskNotFound;
use App\Modules\Jobs\Domain\ValueObjects\ChecklistItemText;
use App\Modules\Shared\Application\Contracts\TransactionManager;
final class RenameChecklistItem
{
public function __construct(
private ChecklistItemFinder $finder,
private ChecklistItemRepository $items,
private TransactionManager $transactions,
) {}
/**
* @throws TaskNotFound
* @throws ChecklistItemNotFound
* @throws InvalidChecklistItemText
*/
public function execute(int $jobId, int $taskId, int $itemId, string $text): void
{
$text = new ChecklistItemText($text);
$this->transactions->run(function () use ($jobId, $taskId, $itemId, $text): void {
$item = $this->finder->find($jobId, $taskId, $itemId);
$item->rename($text);
$this->items->save($item);
});
}
}
@@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Domain\Contracts;
use App\Modules\Jobs\Domain\Entities\ChecklistItem;
use App\Modules\Jobs\Domain\Enums\MoveDirection;
interface ChecklistItemRepository
{
/**
* Null when the item does not exist or belongs to another task.
*/
public function findForTask(int $taskId, int $itemId): ?ChecklistItem;
/**
* The item next to the given one in the task's order, or null at the end.
*/
public function neighbour(ChecklistItem $item, MoveDirection $direction): ?ChecklistItem;
public function nextSortOrder(int $taskId): int;
public function save(ChecklistItem $item): ChecklistItem;
public function delete(ChecklistItem $item): void;
}
@@ -0,0 +1,97 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Domain\Entities;
use App\Modules\Jobs\Domain\ValueObjects\ChecklistItemText;
use DateTimeImmutable;
/**
* One item of a task's checklist (todo), checked done by someone or not.
*/
final class ChecklistItem
{
private function __construct(
private ?int $id,
private int $taskId,
private ChecklistItemText $text,
private int $sortOrder,
private ?DateTimeImmutable $doneAt,
private ?int $doneBy,
) {}
public static function create(int $taskId, ChecklistItemText $text, int $sortOrder): self
{
return new self(null, $taskId, $text, $sortOrder, null, null);
}
public static function reconstitute(int $id, int $taskId, ChecklistItemText $text, int $sortOrder, ?DateTimeImmutable $doneAt, ?int $doneBy): self
{
return new self($id, $taskId, $text, $sortOrder, $doneAt, $doneBy);
}
public function rename(ChecklistItemText $text): void
{
$this->text = $text;
}
/**
* Checking an item that already is done keeps who did it and when.
*/
public function check(int $userId, DateTimeImmutable $at): void
{
if ($this->doneAt !== null) {
return;
}
$this->doneAt = $at;
$this->doneBy = $userId;
}
public function uncheck(): void
{
$this->doneAt = null;
$this->doneBy = null;
}
public function swapOrderWith(self $other): void
{
[$this->sortOrder, $other->sortOrder] = [$other->sortOrder, $this->sortOrder];
}
public function id(): ?int
{
return $this->id;
}
public function taskId(): int
{
return $this->taskId;
}
public function text(): ChecklistItemText
{
return $this->text;
}
public function sortOrder(): int
{
return $this->sortOrder;
}
public function isDone(): bool
{
return $this->doneAt !== null;
}
public function doneAt(): ?DateTimeImmutable
{
return $this->doneAt;
}
public function doneBy(): ?int
{
return $this->doneBy;
}
}
@@ -0,0 +1,11 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Domain\Enums;
enum MoveDirection: string
{
case Up = 'up';
case Down = 'down';
}
@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
/**
* The checklist item does not exist or belongs to another task or job.
*/
final class ChecklistItemNotFound extends DomainException
{
public function __construct(public readonly int $taskId, public readonly int $itemId)
{
parent::__construct("Checklist item {$itemId} of task {$taskId} was not found.");
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class InvalidChecklistItemText extends DomainException
{
public function __construct()
{
parent::__construct('A checklist item must be 1–255 characters.');
}
}
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Domain\ValueObjects;
use App\Modules\Jobs\Domain\Exceptions\InvalidChecklistItemText;
final readonly class ChecklistItemText
{
public const int MAX_LENGTH = 255;
public string $value;
/**
* @throws InvalidChecklistItemText
*/
public function __construct(string $value)
{
$value = trim(preg_replace('/\s+/u', ' ', $value) ?? $value);
if ($value === '' || mb_strlen($value) > self::MAX_LENGTH) {
throw new InvalidChecklistItemText;
}
$this->value = $value;
}
}
@@ -0,0 +1,37 @@
<?php
namespace App\Modules\Jobs\Infrastructure\Persistence\Eloquent;
use Carbon\CarbonImmutable;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\Table;
use Illuminate\Database\Eloquent\Model;
/**
* Persistence model of a task's checklist item, used only inside the Jobs
* Infrastructure layer.
*
* @property int $id
* @property int $task_id
* @property string $text
* @property int $sort_order
* @property CarbonImmutable|null $done_at
* @property int|null $done_by
*/
#[Table('task_checklist_items')]
#[Fillable(['task_id', 'text', 'sort_order', 'done_at', 'done_by'])]
class ChecklistItemModel extends Model
{
/**
* @return array<string, string>
*/
protected function casts(): array
{
return [
'task_id' => 'integer',
'sort_order' => 'integer',
'done_at' => 'immutable_datetime',
'done_by' => 'integer',
];
}
}
@@ -0,0 +1,73 @@
<?php
namespace App\Modules\Jobs\Infrastructure\Persistence\Repositories;
use App\Modules\Jobs\Domain\Contracts\ChecklistItemRepository;
use App\Modules\Jobs\Domain\Entities\ChecklistItem;
use App\Modules\Jobs\Domain\Enums\MoveDirection;
use App\Modules\Jobs\Domain\ValueObjects\ChecklistItemText;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\ChecklistItemModel;
use DateTimeImmutable;
class EloquentChecklistItemRepository implements ChecklistItemRepository
{
public function findForTask(int $taskId, int $itemId): ?ChecklistItem
{
$model = ChecklistItemModel::query()->where('task_id', $taskId)->find($itemId);
return $model === null ? null : $this->toEntity($model);
}
public function neighbour(ChecklistItem $item, MoveDirection $direction): ?ChecklistItem
{
$up = $direction === MoveDirection::Up;
$model = ChecklistItemModel::query()
->where('task_id', $item->taskId())
->whereKeyNot($item->id())
->where(fn ($query) => $query
->where('sort_order', $up ? '<' : '>', $item->sortOrder())
->orWhere(fn ($query) => $query->where('sort_order', $item->sortOrder())->where('id', $up ? '<' : '>', $item->id())))
->orderBy('sort_order', $up ? 'desc' : 'asc')
->orderBy('id', $up ? 'desc' : 'asc')
->first();
return $model === null ? null : $this->toEntity($model);
}
public function nextSortOrder(int $taskId): int
{
return (int) ChecklistItemModel::query()->where('task_id', $taskId)->max('sort_order') + 1;
}
public function save(ChecklistItem $item): ChecklistItem
{
$model = $item->id() === null ? new ChecklistItemModel : ChecklistItemModel::query()->findOrFail($item->id());
$model->fill([
'task_id' => $item->taskId(),
'text' => $item->text()->value,
'sort_order' => $item->sortOrder(),
'done_at' => $item->doneAt()?->format('Y-m-d H:i:s'),
'done_by' => $item->doneBy(),
])->save();
return $this->toEntity($model->refresh());
}
public function delete(ChecklistItem $item): void
{
ChecklistItemModel::query()->whereKey($item->id())->delete();
}
private function toEntity(ChecklistItemModel $model): ChecklistItem
{
return ChecklistItem::reconstitute(
$model->id,
$model->task_id,
new ChecklistItemText($model->text),
$model->sort_order,
$model->done_at === null ? null : DateTimeImmutable::createFromInterface($model->done_at),
$model->done_by,
);
}
}
@@ -0,0 +1,25 @@
<?php
namespace App\Modules\Jobs\Infrastructure\Persistence\Repositories;
use App\Modules\Jobs\Application\Contracts\ChecklistQueries;
use App\Modules\Jobs\Application\DTOs\ChecklistItemSummary;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\ChecklistItemModel;
class EloquentChecklistQueries implements ChecklistQueries
{
public function checklistOf(int $jobId, int $taskId): array
{
return ChecklistItemModel::query()
->where('task_id', $taskId)
->whereExists(fn ($query) => $query->selectRaw('1')->from('tasks')
->whereColumn('tasks.id', 'task_checklist_items.task_id')
->where('tasks.customer_job_id', $jobId))
->orderBy('sort_order')
->orderBy('id')
->get()
->map(fn (ChecklistItemModel $model): ChecklistItemSummary => new ChecklistItemSummary($model->id, $model->text, $model->done_at !== null))
->values()
->all();
}
}
@@ -4,15 +4,19 @@ namespace App\Modules\Jobs\Infrastructure\Providers;
use App\Models\User;
use App\Modules\Customers\Application\Contracts\CustomerRowUsage;
use App\Modules\Jobs\Application\Contracts\ChecklistQueries;
use App\Modules\Jobs\Application\Contracts\CustomerJobQueries;
use App\Modules\Jobs\Application\Contracts\TaskQueries;
use App\Modules\Jobs\Application\Contracts\TaskUsage;
use App\Modules\Jobs\Domain\Contracts\ChecklistItemRepository;
use App\Modules\Jobs\Domain\Contracts\CustomerJobRepository;
use App\Modules\Jobs\Domain\Contracts\JobNumberSequence;
use App\Modules\Jobs\Domain\Contracts\TaskRepository;
use App\Modules\Jobs\Infrastructure\CustomerRowUsageFromJobs;
use App\Modules\Jobs\Infrastructure\NothingUsesTasks;
use App\Modules\Jobs\Infrastructure\Persistence\DatabaseJobNumberSequence;
use App\Modules\Jobs\Infrastructure\Persistence\Repositories\EloquentChecklistItemRepository;
use App\Modules\Jobs\Infrastructure\Persistence\Repositories\EloquentChecklistQueries;
use App\Modules\Jobs\Infrastructure\Persistence\Repositories\EloquentCustomerJobQueries;
use App\Modules\Jobs\Infrastructure\Persistence\Repositories\EloquentCustomerJobRepository;
use App\Modules\Jobs\Infrastructure\Persistence\Repositories\EloquentTaskQueries;
@@ -38,6 +42,8 @@ class JobsServiceProvider extends ServiceProvider
CustomerRowUsage::class => CustomerRowUsageFromJobs::class,
TaskRepository::class => EloquentTaskRepository::class,
TaskQueries::class => EloquentTaskQueries::class,
ChecklistItemRepository::class => EloquentChecklistItemRepository::class,
ChecklistQueries::class => EloquentChecklistQueries::class,
// Replaced by the TimeTracking module, which knows which tasks have time entries.
TaskUsage::class => NothingUsesTasks::class,
];
@@ -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>