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); } }; ?>