Files
mytodos/resources/views/pages/⚡folders.blade.php
T
2026-09-21 00:33:10 -07:00

643 lines
25 KiB
PHP

<?php
use App\Models\Folder;
use App\Models\Todo;
use App\TodoStatus;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Validation\Rule;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Locked;
use Livewire\Attributes\Title;
use Livewire\Component;
new #[Title('Folders')] class extends Component
{
public string $newFolderName = '';
public string $newFolderColor = '';
#[Locked]
public ?int $creatingChildOf = null;
public string $newChildName = '';
public string $newChildColor = '';
#[Locked]
public ?int $renamingFolderId = null;
public string $renameName = '';
#[Locked]
public ?int $confirmingFolderDelete = null;
#[Locked]
public ?int $expandedFolderId = null;
#[Locked]
public ?int $colorEditingFolderId = null;
public string $colorDraft = '';
public bool $showTodoForm = false;
#[Locked]
public ?int $todoFolderId = null;
public string $todoDescription = '';
public string $todoStatus = 'open';
public ?string $todoDueDate = null;
public string $todoNotes = '';
public function createTopLevelFolder(): void
{
$validated = $this->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<int, mixed>
*/
private function folderColorRules(): array
{
return ['nullable', 'string', 'max:20', Rule::in([...Folder::COLORS, ''])];
}
/**
* @return array<int, mixed>
*/
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);
}
}; ?>
<div class="flex h-full w-full flex-1 flex-col gap-6">
<div class="flex flex-wrap items-center justify-between gap-4">
<div>
<flux:heading size="xl">{{ __('Folders') }}</flux:heading>
<flux:text class="mt-1">{{ __('Organize your todos into a top-level and nested folder hierarchy.') }}</flux:text>
</div>
</div>
@if ($this->folderTree->isEmpty())
<flux:callout variant="warning" icon="folder">
{{ __('No folders yet. Create your first top-level folder below.') }}
</flux:callout>
@endif
<flux:card>
<form wire:submit="createTopLevelFolder" class="flex flex-wrap items-end gap-3">
<div class="min-w-64 flex-1">
<flux:label>{{ __('New top-level folder') }}</flux:label>
<flux:input
wire:model="newFolderName"
placeholder="{{ __('e.g. Work, Business, Personal') }}"
data-test="new-folder-input"
/>
<flux:error name="newFolderName" />
</div>
<div>
<flux:label>{{ __('Color') }}</flux:label>
<x-color-picker model="newFolderColor" />
<flux:error name="newFolderColor" />
</div>
<flux:button variant="primary" type="submit" icon="plus" data-test="add-folder-button">
{{ __('Add folder') }}
</flux:button>
</form>
</flux:card>
<div class="flex flex-col divide-y divide-zinc-200 rounded-xl border border-zinc-200 bg-white dark:divide-zinc-700 dark:border-zinc-700 dark:bg-zinc-900" data-test="folder-list">
@forelse ($this->folderTree as $node)
<div class="p-4" :style="'margin-left: {{ $node['depth'] * 1.5 }}rem'">
<div
x-data="{ drop: false }"
class="flex flex-wrap items-center gap-3 transition-colors"
:class="drop && 'rounded-lg bg-blue-50 ring-2 ring-inset ring-blue-400 dark:bg-blue-500/10'"
@dragover.prevent="drop = true"
@dragleave="!$el.contains($event.relatedTarget) && (drop = false)"
@drop.prevent="drop = false; if (window.__mtTodoDrag && window.__mtTodoDrag.folderId !== {{ $node['folder']->id }}) $wire.moveTodoToFolder(window.__mtTodoDrag.todoId, {{ $node['folder']->id }}); window.__mtTodoDrag = null"
data-test="folder-row-{{ $node['folder']->id }}"
>
@if ($renamingFolderId === $node['folder']->id)
<form wire:submit="saveRename" class="flex flex-1 flex-wrap items-center gap-3">
<flux:input
wire:model="renameName"
size="sm"
class="min-w-48 flex-1"
autofocus
data-test="rename-input"
/>
<div class="flex gap-2">
<flux:button size="sm" type="submit" variant="primary" icon="check" data-test="save-rename-button">
{{ __('Save') }}
</flux:button>
<flux:button size="sm" variant="ghost" wire:click="cancelRename">
{{ __('Cancel') }}
</flux:button>
</div>
<flux:error name="renameName" />
</form>
@else
<div class="flex min-w-0 items-center gap-2">
<x-folder-dot :color="$node['folder']->color" data-test="folder-dot-{{ $node['folder']->id }}" />
<flux:heading size="sm" class="truncate" data-test="folder-name-{{ $node['folder']->id }}">
{{ $node['folder']->name }}
</flux:heading>
</div>
<flux:badge
size="sm"
color="blue"
class="cursor-pointer select-none"
wire:click="toggleExpandFolder({{ $node['folder']->id }})"
data-test="expand-folder-{{ $node['folder']->id }}"
>
<flux:icon.chevron-down
variant="micro"
:class="'me-0.5' . ($expandedFolderId === $node['folder']->id ? ' rotate-180 transition-transform' : ' transition-transform')"
/>
{{ $this->openCounts->get($node['folder']->id, 0) }} {{ __('open') }}
</flux:badge>
<flux:spacer />
<div class="flex items-center gap-1">
<flux:tooltip :content="__('Choose color')">
<flux:button
size="sm"
variant="ghost"
icon="swatch"
wire:click="startColorEdit({{ $node['folder']->id }})"
data-test="color-folder-{{ $node['folder']->id }}"
/>
</flux:tooltip>
<flux:tooltip :content="__('View todos')">
<flux:button
size="sm"
variant="ghost"
icon="magnifying-glass"
:href="route('dashboard', ['folder' => $node['folder']->id])"
wire:navigate
data-test="view-todos-{{ $node['folder']->id }}"
/>
</flux:tooltip>
<flux:tooltip :content="__('Add todo')">
<flux:button
size="sm"
variant="ghost"
icon="document-plus"
wire:click="startTodoCreate({{ $node['folder']->id }})"
data-test="add-todo-{{ $node['folder']->id }}"
/>
</flux:tooltip>
<flux:tooltip :content="__('Add sub-folder')">
<flux:button
size="sm"
variant="ghost"
icon="folder-plus"
wire:click="startChildCreate({{ $node['folder']->id }})"
data-test="add-child-{{ $node['folder']->id }}"
/>
</flux:tooltip>
<flux:tooltip :content="__('Rename')">
<flux:button
size="sm"
variant="ghost"
icon="pencil-square"
wire:click="startRename({{ $node['folder']->id }})"
data-test="rename-folder-{{ $node['folder']->id }}"
/>
</flux:tooltip>
<flux:tooltip :content="__('Delete')">
<flux:button
size="sm"
variant="ghost"
icon="trash"
wire:click="confirmFolderDelete({{ $node['folder']->id }})"
data-test="delete-folder-{{ $node['folder']->id }}"
/>
</flux:tooltip>
</div>
@endif
</div>
@if ($expandedFolderId === $node['folder']->id)
<div class="mt-3 flex flex-col gap-2 rounded-lg border border-zinc-200 bg-zinc-50 p-3 dark:border-zinc-700 dark:bg-zinc-800/50" data-test="folder-todos-{{ $node['folder']->id }}">
<flux:text size="sm" class="text-zinc-500">{{ __('Drag a todo onto any folder row to move it.') }}</flux:text>
@forelse ($this->todosForFolder($node['folder']->id) as $todo)
<div
x-data="{ dragging: false }"
draggable="true"
class="flex cursor-grab select-none flex-wrap items-center gap-3"
:class="dragging && 'opacity-40'"
@dragstart="dragging = true; window.__mtTodoDrag = { todoId: {{ $todo->id }}, folderId: {{ $node['folder']->id }} }"
@dragend="dragging = false; window.__mtTodoDrag = null"
data-test="folder-todo-row-{{ $todo->id }}"
>
<flux:icon.chevrons-up-down variant="micro" class="text-zinc-400 dark:text-zinc-500" />
<flux:heading size="sm" class="min-w-0 flex-1 truncate" data-test="folder-todo-description-{{ $todo->id }}">
{{ $todo->description }}
</flux:heading>
<flux:badge size="sm" :color="$todo->status->badgeColor()">
{{ $todo->status->label() }}
</flux:badge>
</div>
@empty
<flux:text class="text-sm text-zinc-500">{{ __('No open todos in this folder.') }}</flux:text>
@endforelse
</div>
@endif
@if ($creatingChildOf === $node['folder']->id)
<form wire:submit="createChildFolder" class="mt-3 flex flex-wrap items-end gap-3">
<div class="min-w-64 flex-1">
<flux:label size="sm">{{ __('Sub-folder of') }} {{ $node['folder']->name }}</flux:label>
<flux:input
wire:model="newChildName"
size="sm"
placeholder="{{ __('Sub-folder name') }}"
data-test="new-child-input"
/>
<flux:error name="newChildName" />
</div>
<div>
<flux:label size="sm">{{ __('Color') }}</flux:label>
<x-color-picker model="newChildColor" />
<flux:error name="newChildColor" />
</div>
<div class="flex gap-2">
<flux:button size="sm" variant="primary" type="submit" icon="plus" data-test="add-child-button">
{{ __('Add') }}
</flux:button>
<flux:button size="sm" variant="ghost" wire:click="cancelChildCreate">
{{ __('Cancel') }}
</flux:button>
</div>
</form>
@endif
</div>
@empty
<div class="p-8">
<flux:text align="center" class="text-sm text-zinc-500">{{ __('No folders to display.') }}</flux:text>
</div>
@endforelse
</div>
<flux:modal wire:model.self="showTodoForm" class="md:w-xl">
<form wire:submit="saveFolderTodo" class="space-y-6">
<div>
@if ($this->todoFolder !== null)
<flux:heading size="lg" class="truncate">
{{ __('Add todo') }} - <span class="text-zinc-500 dark:text-zinc-400" data-test="add-todo-title-folder">{{ $this->todoFolderPath }}</span>
</flux:heading>
@else
<flux:heading size="lg">{{ __('Add todo') }}</flux:heading>
@endif
<flux:text class="mt-2">{{ __('Add a task with a description. A due date is optional.') }}</flux:text>
@if ($this->todoFolder !== null)
<div class="mt-3 flex items-center gap-1.5" data-test="add-todo-folder-name">
<flux:icon.folder variant="micro" class="text-zinc-500 dark:text-zinc-400" />
<flux:text size="sm" class="text-zinc-600 dark:text-zinc-300">
{{ __('Adding to') }}
<strong class="text-zinc-900 dark:text-zinc-100">
{{ $this->todoFolderPath }}
</strong>
</flux:text>
</div>
@endif
</div>
<flux:field>
<flux:label>{{ __('Description') }}</flux:label>
<flux:input
wire:model="todoDescription"
type="text"
autofocus
placeholder="{{ __('What needs to be done?') }}"
data-test="todo-description-input"
/>
<flux:error name="todoDescription" />
</flux:field>
<flux:field>
<flux:label>{{ __('Status') }}</flux:label>
<flux:select wire:model="todoStatus" data-test="todo-status-select">
@foreach (TodoStatus::cases() as $status)
<flux:select.option :value="$status->value">{{ $status->label() }}</flux:select.option>
@endforeach
</flux:select>
<flux:error name="todoStatus" />
</flux:field>
<flux:field>
<flux:label>{{ __('Due date') }}</flux:label>
<flux:input wire:model="todoDueDate" type="date" data-test="todo-due-date-input" />
<flux:error name="todoDueDate" />
</flux:field>
<flux:field>
<flux:label>{{ __('Notes') }}</flux:label>
<flux:textarea
wire:model="todoNotes"
rows="3"
placeholder="{{ __('Add any extra details…') }}"
data-test="todo-notes-input"
/>
<flux:error name="todoNotes" />
</flux:field>
<div class="flex justify-end gap-3">
<flux:modal.close>
<flux:button variant="ghost">{{ __('Cancel') }}</flux:button>
</flux:modal.close>
<flux:button type="submit" variant="primary">{{ __('Add todo') }}</flux:button>
</div>
</form>
</flux:modal>
<flux:modal name="folder-color" class="md:w-lg">
<form wire:submit="saveFolderColor" class="space-y-6">
<div>
<flux:heading size="lg">{{ __('Folder color') }}</flux:heading>
<flux:text class="mt-2">{{ __('Choose a color for this folder, or clear it to remove any color.') }}</flux:text>
</div>
<flux:field>
<flux:label>{{ __('Color') }}</flux:label>
<x-color-picker model="colorDraft" />
<flux:error name="colorDraft" />
</flux:field>
<div class="flex justify-end gap-3">
<flux:modal.close>
<flux:button variant="ghost">{{ __('Cancel') }}</flux:button>
</flux:modal.close>
<flux:button type="submit" variant="primary" data-test="save-folder-color">
{{ __('Save color') }}
</flux:button>
</div>
</form>
</flux:modal>
<flux:modal name="confirm-folder-delete" class="md:w-lg">
<div class="space-y-6">
<div>
<flux:heading size="lg">{{ __('Delete folder?') }}</flux:heading>
<flux:text class="mt-2">
{{ __('Deleting a folder also deletes its sub-folders and every todo inside them. This action cannot be undone.') }}
</flux:text>
</div>
<div class="flex justify-end gap-3">
<flux:modal.close>
<flux:button variant="ghost">{{ __('Cancel') }}</flux:button>
</flux:modal.close>
<flux:button variant="danger" wire:click="deleteFolder" data-test="confirm-delete-folder">
{{ __('Delete folder') }}
</flux:button>
</div>
</div>
</flux:modal>
</div>