query('folder'); if ($folderId === null || $folderId === '') { return; } $folderId = (int) $folderId; if (Folder::query()->forUser(auth()->id())->whereKey($folderId)->exists()) { $this->folderId = $folderId; } } public function updatedSearch(): void { $this->limit = self::PER_PAGE; } public function updatedFolderId(): void { $this->folderId = $this->folderId === null || $this->folderId === '' ? null : (int) $this->folderId; $this->limit = self::PER_PAGE; } public function updatedStatus(): void { $this->limit = self::PER_PAGE; } public function openCreateForm(?int $folderId = null): void { $this->reset('editingTodoId', 'description', 'todoStatus', 'dueDate', 'notes', 'formFolderId'); $this->resetErrorBag(); if ($folderId !== null && $this->folderTree->contains(fn ($node) => $node['folder']->id === $folderId)) { $this->formFolderId = $folderId; } elseif ($this->folderId !== null) { $this->formFolderId = $this->folderId; } $this->showTodoForm = true; } public function openEditForm(int $todoId): void { $todo = $this->todoForUser($todoId); $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')->where('user_id', auth()->id())], ]); $data = [ 'folder_id' => $validated['formFolderId'], '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->todoForUser($this->editingTodoId)->update($data); } else { Todo::create($data + [ 'user_id' => auth()->id(), 'position' => Todo::nextPositionFor(auth()->user()), ]); } $this->reset('showTodoForm', 'editingTodoId', 'description', 'todoStatus', 'dueDate', 'notes', 'formFolderId'); } public function toggleComplete(int $todoId): void { $todo = $this->todoForUser($todoId); $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); } public function confirmTodoDelete(int $todoId): void { $this->todoForUser($todoId); $this->confirmingTodoDelete = $todoId; $this->modal('confirm-todo-delete')->show(); } public function deleteTodo(): void { $this->todoForUser($this->confirmingTodoDelete)->delete(); $this->confirmingTodoDelete = null; $this->modal('confirm-todo-delete')->close(); } public function resetFilters(): void { $this->reset('search', 'folderId', 'status'); $this->limit = self::PER_PAGE; } public function loadMore(): void { $this->limit = min( (int) $this->filteredTodoQuery()->count(), $this->limit + self::PER_PAGE ); } #[Computed] public function folderTree(): Collection { return Folder::treeForUser(auth()->id()); } #[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 || $this->status !== 'all'; } #[Computed] public function statusFilters(): array { return [ 'all' => 'All', ...collect(TodoStatus::cases()) ->mapWithKeys(fn (TodoStatus $status) => [$status->value => $status->label()]) ->all(), 'completed' => 'Completed', 'overdue' => 'Overdue', ]; } #[Computed] public function totalCount(): int { return (int) $this->filteredTodoQuery()->count(); } #[Computed] public function hasMore(): bool { return $this->todos->count() < $this->totalCount; } #[Computed] public function orderedTodoIds(): array { return $this->filteredTodoQuery()->priority()->pluck('id')->all(); } #[Computed] public function todos(): Collection { return $this->filteredTodoQuery() ->with('folder.parent') ->priority() ->limit($this->limit) ->get(); } public function moveTodo(int $todoId, string $direction): void { $this->todoForUser($todoId); $ids = $this->orderedTodoIds; $currentIndex = array_search($todoId, $ids, true); if ($currentIndex === false) { return; } $target = $direction === 'up' ? $currentIndex - 1 : $currentIndex + 1; if ($target < 0 || $target >= count($ids)) { return; } $this->reorderTodo($todoId, $target); } public function handleSort(int $todoId, int $position): void { $this->todoForUser($todoId); $this->reorderTodo($todoId, $position); } /** * Move a todo to the given index of the currently visible order. 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 reorderTodo(int $todoId, int $newIndex): void { $visibleIds = $this->orderedTodoIds; $currentIndex = array_search($todoId, $visibleIds, true); if ($currentIndex === false) { return; } $newIndex = max(0, min($newIndex, count($visibleIds) - 1)); if ($newIndex === $currentIndex) { return; } $visibleOrder = $visibleIds; unset($visibleOrder[$currentIndex]); array_splice($visibleOrder, $newIndex, 0, $todoId); $ids = Todo::query()->forUser(auth()->id())->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()->forUser(auth()->id())->whereKey($id)->update(['position' => $position]); } unset($this->todos, $this->orderedTodoIds); } /** * @return Builder */ private function filteredTodoQuery(): Builder { return Todo::query() ->forUser(auth()->id()) ->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) ) ->when($this->status, function (Builder $query) { return match ($this->status) { 'open' => $query->active(), 'completed' => $query->completed(), 'overdue' => $query->overdue(), 'all', '' => $query, default => $query->where('status', $this->status)->incomplete(), }; }); } 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 todoForUser(int $todoId): Todo { return Todo::query()->forUser(auth()->id())->findOrFail($todoId); } }; ?>
{{ __('Todos') }} Browse and reorder every todo.
{{ __('New Todo') }}
@foreach ($this->statusFilters as $value => $label) {{ __($label) }} @endforeach {{ __('All folders') }} @foreach ($this->folderTree as $node) {{ str_repeat('— ', $node['depth']) }}{{ $node['folder']->name }} @endforeach @if ($this->hasActiveFilters) {{ __('Clear') }} @endif
{{ __('Done') }} {{ __('Description') }} {{ __('Status') }} {{ __('Folder') }} {{ __('Due') }} {{ __('Created') }} {{ __('Completed') }} {{ __('Actions') }} @forelse ($this->todos as $todo)
$todo->completed_at !== null])>{{ $todo->description }} @if ($todo->notes) {{ $todo->notes }} @endif {{ $todo->status->label() }}
{{ $todo->folder?->parent?->name }}@if ($todo->folder?->parent) / @endif{{ $todo->folder?->name }}
@if ($todo->completed_at) {{ $todo->due_date?->format('M j, Y') ?? __('—') }} @elseif ($todo->due_date?->isPast()) {{ $todo->due_date->format('M j, Y') }} @elseif ($todo->due_date?->isToday()) {{ __('Today') }} · {{ $todo->due_date->format('M j') }} @else {{ $todo->due_date?->format('M j, Y') ?? __('No due date') }} @endif {{ $todo->created_at->format('M j, Y') }} @if ($todo->completed_at)
{{ $todo->completed_at->format('M j, Y') }}
@if ($todo->daysToComplete() === 0) {{ __('Same day') }} @else {{ __('Completed in') }} {{ $todo->daysToComplete() }} {{ Str::plural('day', $todo->daysToComplete()) }} @endif @else — @endif
@if ($todo->id !== ($this->orderedTodoIds[0] ?? null)) @endif @if ($todo->id !== ($this->orderedTodoIds[array_key_last($this->orderedTodoIds)] ?? null)) @endif
@empty {{ __('No todos match your filters.') }} @endforelse
{{ trans_choice('Showing :count of :total todos', $this->todos->count(), [ 'count' => $this->todos->count(), 'total' => $this->totalCount, ]) }}
@if ($this->hasMore)
{{ __('Loading more…') }}
@endif
{{ $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') }}