validate([ 'newFolderName' => $this->folderNameRules(parentId: null), 'newFolderColor' => $this->folderColorRules(), ]); Folder::create([ 'user_id' => auth()->id(), 'name' => $validated['newFolderName'], 'color' => filled($validated['newFolderColor']) ? $validated['newFolderColor'] : null, 'parent_id' => null, ]); $this->reset('newFolderName', 'newFolderColor'); } public function startChildCreate(int $folderId): void { $this->folderForUser($folderId); $this->creatingChildOf = $folderId; $this->reset('newChildName', 'newChildColor'); $this->resetErrorBag('newChildName', 'newChildColor'); } public function cancelChildCreate(): void { $this->reset('creatingChildOf', 'newChildName', 'newChildColor'); } public function createChildFolder(): void { $this->folderForUser($this->creatingChildOf); $validated = $this->validate([ 'newChildName' => $this->folderNameRules(parentId: $this->creatingChildOf), 'newChildColor' => $this->folderColorRules(), ]); Folder::create([ 'user_id' => auth()->id(), 'name' => $validated['newChildName'], 'color' => filled($validated['newChildColor']) ? $validated['newChildColor'] : null, 'parent_id' => $this->creatingChildOf, ]); $this->reset('creatingChildOf', 'newChildName', 'newChildColor'); } public function startRename(int $folderId): void { $folder = $this->folderForUser($folderId); $this->renamingFolderId = $folder->id; $this->renameName = $folder->name; $this->resetErrorBag('renameName'); } public function cancelRename(): void { $this->reset('renamingFolderId', 'renameName'); } public function saveRename(): void { $folder = $this->folderForUser($this->renamingFolderId); $validated = $this->validate([ 'renameName' => $this->folderNameRules(parentId: $folder->parent_id, ignoreId: $folder->id), ]); $folder->update(['name' => $validated['renameName']]); $this->reset('renamingFolderId', 'renameName'); } public function confirmFolderDelete(int $folderId): void { $this->folderForUser($folderId); $this->confirmingFolderDelete = $folderId; $this->modal('confirm-folder-delete')->show(); } public function deleteFolder(): void { $folder = $this->folderForUser($this->confirmingFolderDelete); if ($this->expandedFolderId === $folder->id) { $this->expandedFolderId = null; } $folder->delete(); $this->confirmingFolderDelete = null; $this->modal('confirm-folder-delete')->close(); } public function toggleExpandFolder(int $folderId): void { $this->folderForUser($folderId); $this->expandedFolderId = $this->expandedFolderId === $folderId ? null : $folderId; } public function startColorEdit(int $folderId): void { $folder = $this->folderForUser($folderId); $this->colorEditingFolderId = $folder->id; $this->colorDraft = $folder->color ?? ''; $this->resetErrorBag('colorDraft'); $this->modal('folder-color')->show(); } public function saveFolderColor(): void { $folder = $this->folderForUser($this->colorEditingFolderId ?? 0); $validated = $this->validate([ 'colorDraft' => $this->folderColorRules(), ]); $folder->update([ 'color' => filled($validated['colorDraft']) ? $validated['colorDraft'] : null, ]); $this->reset('colorEditingFolderId', 'colorDraft'); $this->modal('folder-color')->close(); } public function moveTodoToFolder(int $todoId, int $folderId): void { $todo = Todo::query()->forUser(auth()->id())->findOrFail($todoId); $folder = Folder::query()->forUser(auth()->id())->findOrFail($folderId); $todo->update(['folder_id' => $folder->id]); } public function todosForFolder(int $folderId): Collection { return Todo::query() ->forUser(auth()->id()) ->where('folder_id', $folderId) ->active() ->priority() ->get(); } public function startTodoCreate(int $folderId): void { $this->folderForUser($folderId); $this->todoFolderId = $folderId; $this->reset('todoDescription', 'todoStatus', 'todoDueDate', 'todoNotes'); $this->resetErrorBag('todoDescription', 'todoStatus', 'todoDueDate', 'todoNotes'); $this->showTodoForm = true; } public function saveFolderTodo(): void { $folder = $this->folderForUser($this->todoFolderId ?? 0); $validated = $this->validate([ 'todoDescription' => ['required', 'string', 'max:255'], 'todoStatus' => ['required', Rule::enum(TodoStatus::class)], 'todoDueDate' => ['nullable', 'date'], 'todoNotes' => ['nullable', 'string', 'max:'.auth()->user()->notesLimit()], ]); Todo::create([ 'user_id' => auth()->id(), 'folder_id' => $folder->id, 'description' => $validated['todoDescription'], 'status' => TodoStatus::from($validated['todoStatus']), 'due_date' => filled($validated['todoDueDate']) ? Carbon::parse($validated['todoDueDate']) : null, 'notes' => filled($validated['todoNotes']) ? $validated['todoNotes'] : null, 'position' => Todo::nextPositionFor(auth()->user()), ]); $this->reset('showTodoForm', 'todoFolderId', 'todoDescription', 'todoStatus', 'todoDueDate', 'todoNotes'); } #[Computed] public function folderTree(): Collection { return Folder::treeForUser(auth()->id()); } #[Computed] public function todoFolder(): ?Folder { if ($this->todoFolderId === null) { return null; } return Folder::query()->with('parent')->forUser(auth()->id())->find($this->todoFolderId); } #[Computed] public function todoFolderPath(): string { if ($this->todoFolder === null) { return ''; } return collect([$this->todoFolder->parent?->name, $this->todoFolder->name]) ->filter() ->implode(' / '); } #[Computed] public function openCounts(): Collection { return Todo::query() ->forUser(auth()->id()) ->active() ->pluck('folder_id') ->countBy(); } /** * @return array */ private function folderColorRules(): array { return ['nullable', 'string', 'max:20', Rule::in([...Folder::COLORS, ''])]; } /** * @return array */ private function folderNameRules(?int $parentId = null, ?int $ignoreId = null): array { return [ 'required', 'string', 'max:255', Rule::unique('folders', 'name') ->ignore($ignoreId) ->where(fn ($query) => $query ->where('user_id', auth()->id()) ->where('parent_id', $parentId)), ]; } private function folderForUser(int $folderId): Folder { return Folder::query()->forUser(auth()->id())->findOrFail($folderId); } }; ?>
{{ __('Folders') }} {{ __('Organize your todos into a top-level and nested folder hierarchy.') }}
@if ($this->folderTree->isEmpty()) {{ __('No folders yet. Create your first top-level folder below.') }} @endif
{{ __('New top-level folder') }}
{{ __('Color') }}
{{ __('Add folder') }}
@forelse ($this->folderTree as $node)
@if ($renamingFolderId === $node['folder']->id)
{{ __('Save') }} {{ __('Cancel') }}
@else
{{ $node['folder']->name }}
{{ $this->openCounts->get($node['folder']->id, 0) }} {{ __('open') }}
@endif
@if ($expandedFolderId === $node['folder']->id)
{{ __('Drag a todo onto any folder row to move it.') }} @forelse ($this->todosForFolder($node['folder']->id) as $todo)
{{ $todo->description }} {{ $todo->status->label() }}
@empty {{ __('No open todos in this folder.') }} @endforelse
@endif @if ($creatingChildOf === $node['folder']->id)
{{ __('Sub-folder of') }} {{ $node['folder']->name }}
{{ __('Color') }}
{{ __('Add') }} {{ __('Cancel') }}
@endif
@empty
{{ __('No folders to display.') }}
@endforelse
@if ($this->todoFolder !== null) {{ __('Add todo') }} - {{ $this->todoFolderPath }} @else {{ __('Add todo') }} @endif {{ __('Add a task with a description. A due date is optional.') }} @if ($this->todoFolder !== null)
{{ __('Adding to') }} {{ $this->todoFolderPath }}
@endif
{{ __('Description') }} {{ __('Status') }} @foreach (TodoStatus::cases() as $status) {{ $status->label() }} @endforeach {{ __('Due date') }} {{ __('Notes') }}
{{ __('Cancel') }} {{ __('Add todo') }}
{{ __('Folder color') }} {{ __('Choose a color for this folder, or clear it to remove any color.') }}
{{ __('Color') }}
{{ __('Cancel') }} {{ __('Save color') }}
{{ __('Delete folder?') }} {{ __('Deleting a folder also deletes its sub-folders and every todo inside them. This action cannot be undone.') }}
{{ __('Cancel') }} {{ __('Delete folder') }}