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->resetPage(); } public function updatedFolderId(): void { $this->folderId = $this->folderId === null || $this->folderId === '' ? null : (int) $this->folderId; $this->resetPage(); } public function updatedStatus(): void { $this->resetPage(); } 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()]); } $this->reset('showTodoForm', 'editingTodoId', 'description', 'todoStatus', 'dueDate', 'notes', 'formFolderId'); } public function toggleComplete(int $todoId): void { $todo = $this->todoForUser($todoId); $todo->update([ 'completed_at' => $todo->completed_at === null ? now() : null, ]); } 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->resetPage(); } #[Computed] public function todoCounts(): array { $query = Todo::query()->forUser(auth()->id()); return [ 'open' => (clone $query)->active()->count(), 'completed' => (clone $query)->completed()->count(), 'overdue' => (clone $query)->overdue()->count(), 'dueToday' => (clone $query)->dueToday()->count(), ]; } #[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 folderCounts(): Collection { $tree = $this->folderTree; $openByFolder = Todo::query() ->forUser(auth()->id()) ->active() ->pluck('folder_id') ->countBy(); $overdueByFolder = Todo::query() ->forUser(auth()->id()) ->overdue() ->pluck('folder_id') ->countBy(); return $tree ->where('depth', 0) ->values() ->map(fn (array $node) => [ 'folder' => $node['folder'], 'open' => collect($openByFolder)->only($this->descendantIds($node['folder']->id))->sum(), 'overdue' => collect($overdueByFolder)->only($this->descendantIds($node['folder']->id))->sum(), ]); } #[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 todos(): LengthAwarePaginator { return Todo::query() ->forUser(auth()->id()) ->with('folder.parent') ->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(), }; }) ->orderByRaw('completed_at IS NOT NULL') ->orderByRaw('due_date IS NULL') ->orderBy('due_date') ->orderByDesc('id') ->paginate(10); } 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); } }; ?>
{{ __('Dashboard') }} Track your todos across folders.
{{ __('New Todo') }}
@if ($this->folderTree->isEmpty()) {{ __('Create a folder to start adding todos.') }} {{ __('Manage folders') }} @endif
{{ __('Open') }} {{ $this->todoCounts['open'] }} {{ __('Overdue') }} {{ $this->todoCounts['overdue'] }} {{ __('Due today') }} {{ $this->todoCounts['dueToday'] }} {{ __('Completed') }} {{ $this->todoCounts['completed'] }}
@if ($this->folderCounts->isNotEmpty())
@foreach ($this->folderCounts as $stat)
{{ $stat['folder']->name }} @if ($folderId === $stat['folder']->id) {{ __('Filtered') }} @endif
{{ $stat['open'] }} {{ __('open') }} @if ($stat['overdue'] > 0) {{ $stat['overdue'] }} {{ __('overdue') }} @endif
@endforeach
@endif
@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') }} {{ __('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') }}
@empty {{ __('No todos match your filters.') }} @endforelse
{{ $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') }}