validate([ 'newFolderName' => $this->folderNameRules(parentId: null), ]); Folder::create([ 'user_id' => auth()->id(), 'name' => $validated['newFolderName'], 'parent_id' => null, ]); $this->reset('newFolderName'); } public function startChildCreate(int $folderId): void { $this->folderForUser($folderId); $this->creatingChildOf = $folderId; $this->reset('newChildName'); $this->resetErrorBag('newChildName'); } public function cancelChildCreate(): void { $this->reset('creatingChildOf', 'newChildName'); } public function createChildFolder(): void { $this->folderForUser($this->creatingChildOf); $validated = $this->validate([ 'newChildName' => $this->folderNameRules(parentId: $this->creatingChildOf), ]); Folder::create([ 'user_id' => auth()->id(), 'name' => $validated['newChildName'], 'parent_id' => $this->creatingChildOf, ]); $this->reset('creatingChildOf', 'newChildName'); } 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 { $this->folderForUser($this->confirmingFolderDelete)->delete(); $this->confirmingFolderDelete = null; $this->modal('confirm-folder-delete')->close(); } 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, ]); $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 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') }}
{{ __('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 ($creatingChildOf === $node['folder']->id)
{{ __('Sub-folder of') }} {{ $node['folder']->name }}
{{ __('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') }}
{{ __('Delete folder?') }} {{ __('Deleting a folder also deletes its sub-folders and every todo inside them. This action cannot be undone.') }}
{{ __('Cancel') }} {{ __('Delete folder') }}