query('folder'); if ($folderId === null || $folderId === '') { return; } $folderId = (int) $folderId; if (Folder::query()->visibleTo(auth()->id(), $this->activeWorkspaceId())->whereKey($folderId)->exists()) { $this->folderId = $folderId; } } public function updatedSearch(): void { unset($this->columns); } public function updatedFolderId(): void { $this->folderId = $this->folderId === null || $this->folderId === '' ? null : (int) $this->folderId; unset($this->columns, $this->filterFolderScope); } public function openCreateForm(?string $status = null): void { $this->reset('editingTodoId', 'description', 'todoStatus', 'dueDate', 'notes', 'formFolderId'); $this->resetErrorBag(); if ($status !== null && TodoStatus::tryFrom($status) !== null) { $this->todoStatus = $status; } if ($this->folderId !== null) { $node = $this->folderTree->first(fn (array $node): bool => $node['folder']->id === $this->folderId); if ($node !== null && auth()->user()->can('create', [Todo::class, $node['folder']])) { $this->formFolderId = $this->folderId; } } $this->showTodoForm = true; } public function openEditForm(int $todoId): void { $todo = $this->todoForAction($todoId, 'update'); $this->editingTodoId = $todo->id; $this->description = $todo->description; $this->todoStatus = $todo->status->value; $this->dueDate = $todo->due_date?->format('Y-m-d') ?? ''; $this->notes = $todo->notes ?? ''; $this->formFolderId = $todo->folder_id; $this->resetErrorBag(); $this->showTodoForm = true; } public function saveTodo(): void { $validated = $this->validate([ 'description' => ['required', 'string', 'max:255'], 'todoStatus' => ['required', Rule::enum(TodoStatus::class)], 'dueDate' => ['nullable', 'date'], 'notes' => ['nullable', 'string', 'max:'.auth()->user()->notesLimit()], 'formFolderId' => ['required', 'integer', Rule::exists('folders', 'id')->whereIn( 'id', Folder::query()->visibleTo(auth()->id(), $this->activeWorkspaceId())->select('folders.id') )], ]); $folder = Folder::query() ->visibleTo(auth()->id(), $this->activeWorkspaceId()) ->findOrFail($validated['formFolderId']); abort_unless(auth()->user()->can('create', [Todo::class, $folder]), 403); $data = [ 'folder_id' => $folder->id, 'description' => $validated['description'], 'status' => TodoStatus::from($validated['todoStatus']), 'due_date' => filled($validated['dueDate']) ? Carbon::parse($validated['dueDate']) : null, 'notes' => filled($validated['notes']) ? $validated['notes'] : null, ]; if ($this->editingTodoId !== null) { $this->todoForAction($this->editingTodoId, 'update')->update($data); } else { Todo::create($data + [ 'user_id' => auth()->id(), 'position' => Todo::nextPositionFor($folder), ]); } $this->reset('showTodoForm', 'editingTodoId', 'description', 'todoStatus', 'dueDate', 'notes', 'formFolderId'); unset($this->columns); } public function toggleComplete(int $todoId): void { $todo = $this->todoForAction($todoId, 'markComplete'); $data = ['completed_at' => $todo->completed_at === null ? now() : null]; if ($todo->completed_at === null && in_array($todo->status, [TodoStatus::Open, TodoStatus::InProgress], true)) { $data['status'] = TodoStatus::Completed; } elseif ($todo->completed_at !== null && $todo->status === TodoStatus::Completed) { $data['status'] = TodoStatus::Open; } $todo->update($data); unset($this->columns); } public function confirmTodoDelete(int $todoId): void { $this->todoForAction($todoId, 'delete'); $this->confirmingTodoDelete = $todoId; $this->modal('confirm-todo-delete')->show(); } public function deleteTodo(): void { $this->todoForAction($this->confirmingTodoDelete ?? 0, 'delete')->delete(); $this->confirmingTodoDelete = null; $this->modal('confirm-todo-delete')->close(); unset($this->columns); } public function resetFilters(): void { $this->reset('search', 'folderId'); unset($this->columns, $this->filterFolderScope); } public function moveTodo(int $todoId, int $position, string $columnId): void { if ($columnId !== 'completed' && TodoStatus::tryFrom($columnId) === null) { return; } $todo = $this->todoForAction($todoId, 'update'); if ($columnId === 'completed') { $data = ['completed_at' => $todo->completed_at ?? now()]; if ($todo->completed_at === null && in_array($todo->status, [TodoStatus::Open, TodoStatus::InProgress], true)) { $data['status'] = TodoStatus::Completed; } $todo->update($data); } else { $todo->update([ 'status' => TodoStatus::from($columnId), 'completed_at' => null, ]); } $this->reorderColumn($todo->id, $position, $columnId); } /** * Move a todo to the given index of a board column. The reordered todos are * written back into the slots they already occupy in the user's full * priority order, so reordering under an active filter permutes the visible * todos without renumbering them from zero and colliding with the todos the * filter hides. */ private function reorderColumn(int $todoId, int $newIndex, string $columnId): void { $visibleIds = $this->columnTodoIds($columnId); $currentIndex = array_search($todoId, $visibleIds, true); if ($currentIndex === false) { return; } $newIndex = max(0, min($newIndex, count($visibleIds) - 1)); if ($newIndex === $currentIndex) { unset($this->columns); return; } $visibleOrder = $visibleIds; unset($visibleOrder[$currentIndex]); array_splice($visibleOrder, $newIndex, 0, $todoId); $ids = Todo::query()->visibleTo(auth()->id(), $this->activeWorkspaceId())->priority()->pluck('id')->all(); $slots = array_keys(array_intersect($ids, $visibleIds)); foreach ($slots as $visibleSlot => $slot) { $ids[$slot] = $visibleOrder[$visibleSlot]; } foreach ($ids as $position => $id) { Todo::query()->visibleTo(auth()->id(), $this->activeWorkspaceId())->whereKey($id)->update(['position' => $position]); } unset($this->columns); } /** * @return list */ private function columnTodoIds(string $columnId): array { $query = $this->filteredTodoQuery()->priority(); if ($columnId === 'completed') { $query->where(function (Builder $query): void { $query->whereNotNull('completed_at')->orWhere('status', TodoStatus::Completed->value); }); } else { $query->where('status', $columnId)->whereNull('completed_at'); } return $query->pluck('id')->all(); } /** * @return Builder */ private function filteredTodoQuery(): Builder { return Todo::query() ->visibleTo(auth()->id(), $this->activeWorkspaceId()) ->when( $this->search !== '', fn (Builder $query) => $query->where('description', 'like', '%'.$this->search.'%') ) ->when( $this->folderId !== null, fn (Builder $query) => $query->whereIn('folder_id', $this->filterFolderScope) ); } private function descendantIds(int $folderId): array { $ids = [$folderId]; foreach ($this->folderTree->where('folder.parent_id', $folderId)->values() as $child) { $ids = [...$ids, ...$this->descendantIds($child['folder']->id)]; } return $ids; } private function todoForAction(int $todoId, string $ability): Todo { $todo = Todo::query() ->visibleTo(auth()->id(), $this->activeWorkspaceId()) ->with('folder') ->findOrFail($todoId); abort_unless(auth()->user()->can($ability, $todo), 403); return $todo; } private function activeWorkspaceId(): int { return (int) auth()->user()->current_organization_id; } #[Computed] public function folderTree(): Collection { return Folder::treeForVisibleTo(auth()->id(), $this->activeWorkspaceId()); } #[Computed] public function filterFolderScope(): array { if ($this->folderId === null) { return []; } return $this->descendantIds($this->folderId); } #[Computed] public function hasActiveFilters(): bool { return $this->search !== '' || $this->folderId !== null; } #[Computed] public function canCreateTodos(): bool { return $this->folderTree->contains( fn (array $node): bool => auth()->user()->can('create', [Todo::class, $node['folder']]) ); } /** * @return list}> */ #[Computed] public function columns(): array { $todos = $this->filteredTodoQuery() ->with('folder.parent') ->priority() ->get(); $columns = collect([ TodoStatus::Open, TodoStatus::InProgress, TodoStatus::OnHold, TodoStatus::Blocked, TodoStatus::Cancelled, TodoStatus::Deferred, ])->map(fn (TodoStatus $status): array => [ 'id' => $status->value, 'label' => $status->label(), 'color' => $status->badgeColor(), 'todos' => $todos ->filter(fn (Todo $todo): bool => $todo->status === $status && $todo->completed_at === null) ->values(), ]); $columns->push([ 'id' => 'completed', 'label' => TodoStatus::Completed->label(), 'color' => TodoStatus::Completed->badgeColor(), 'todos' => $todos ->filter(fn (Todo $todo): bool => $todo->completed_at !== null || $todo->status === TodoStatus::Completed) ->values(), ]); return $columns->all(); } }; ?>
{{ __('Kanban') }} {{ __('Drag todos between columns to update their status.') }}
{{ __('List view') }} @if ($this->canCreateTodos) {{ __('New Todo') }} @endif
@if (! $this->canCreateTodos) {{ __('Create a folder you can add todos to before using the board.') }} {{ __('Manage folders') }} @endif
{{ __('All folders') }} @foreach ($this->folderTree as $node) {{ str_repeat('— ', $node['depth']) }}{{ $node['folder']->name }} @endforeach @if ($this->hasActiveFilters) {{ __('Clear') }} @endif
@foreach ($this->columns as $column)
{{ $column['label'] }} {{ $column['todos']->count() }}
@if ($this->canCreateTodos) @endif
@forelse ($column['todos'] as $todo) @php $canUpdateTodo = auth()->user()->can('update', $todo); $dueColor = $todo->completed_at !== null ? 'zinc' : ($todo->due_date?->isPast() ? 'red' : ($todo->due_date?->isToday() ? 'amber' : 'zinc')); @endphp
$todo->completed_at !== null, ]) >

$todo->completed_at !== null, ])> {{ $todo->description }}

@if ($todo->notes) {{ $todo->notes }} @endif
@if ($canUpdateTodo)
{{ __('Edit') }} {{ __('Delete') }}
@endif
@can('markComplete', $todo)
@endcan
{{ $todo->folder?->parent?->name }}@if ($todo->folder?->parent) / @endif{{ $todo->folder?->name }}
@if ($column['id'] === 'completed' && $todo->status !== TodoStatus::Completed) {{ $todo->status->label() }} @endif @if ($todo->due_date) {{ $todo->due_date->isToday() ? __('Today') : $todo->due_date->format('M j') }} @endif
@empty
{{ __('No todos') }}
@endforelse
@endforeach
{{ $editingTodoId === null ? __('New todo') : __('Edit todo') }} {{ __('Add a task with a description and folder. A due date is optional.') }}
{{ __('Description') }} {{ __('Status') }} @foreach (TodoStatus::cases() as $status) {{ $status->label() }} @endforeach {{ __('Due date') }} {{ __('Notes') }} {{ __('Folder') }} {{ __('Select a folder…') }} @foreach ($this->folderTree as $node) {{ str_repeat('— ', $node['depth']) }}{{ $node['folder']->name }} @endforeach
{{ __('Cancel') }} {{ __('Save todo') }}
{{ __('Delete todo?') }} {{ __('This todo will be permanently deleted. This action cannot be undone.') }}
{{ __('Cancel') }} {{ __('Delete') }}