571 lines
22 KiB
PHP
571 lines
22 KiB
PHP
<?php
|
|
|
|
use App\Models\Folder;
|
|
use App\Models\Todo;
|
|
use App\TodoStatus;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
|
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;
|
|
use Livewire\WithPagination;
|
|
|
|
new #[Title('Dashboard')] class extends Component
|
|
{
|
|
use WithPagination;
|
|
|
|
#[Locked]
|
|
public ?int $editingTodoId = null;
|
|
|
|
public string $search = '';
|
|
|
|
public ?int $folderId = null;
|
|
|
|
public string $status = 'all';
|
|
|
|
public bool $showTodoForm = false;
|
|
|
|
public string $description = '';
|
|
|
|
public string $todoStatus = 'open';
|
|
|
|
public ?string $dueDate = null;
|
|
|
|
public string $notes = '';
|
|
|
|
public ?int $formFolderId = null;
|
|
|
|
#[Locked]
|
|
public ?int $confirmingTodoDelete = null;
|
|
|
|
public function mount(): void
|
|
{
|
|
$folderId = request()->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);
|
|
}
|
|
}; ?>
|
|
|
|
<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">{{ __('Dashboard') }}</flux:heading>
|
|
<flux:text class="mt-1">Track your todos across folders.</flux:text>
|
|
</div>
|
|
|
|
<flux:button
|
|
variant="primary"
|
|
wire:click="openCreateForm"
|
|
data-test="add-todo-button"
|
|
>
|
|
{{ __('New Todo') }}
|
|
</flux:button>
|
|
</div>
|
|
|
|
@if ($this->folderTree->isEmpty())
|
|
<flux:callout variant="warning" icon="folder">
|
|
<x-slot name="text">
|
|
{{ __('Create a folder to start adding todos.') }}
|
|
</x-slot>
|
|
<x-slot name="actions">
|
|
<flux:button size="sm" :href="route('folders')" wire:navigate variant="primary">
|
|
{{ __('Manage folders') }}
|
|
</flux:button>
|
|
</x-slot>
|
|
</flux:callout>
|
|
@endif
|
|
|
|
<div class="grid grid-cols-4 gap-3">
|
|
<flux:card size="sm">
|
|
<flux:heading size="sm" class="text-zinc-500">{{ __('Open') }}</flux:heading>
|
|
<flux:heading size="xl" class="mt-1" data-test="count-open">{{ $this->todoCounts['open'] }}</flux:heading>
|
|
</flux:card>
|
|
|
|
<flux:card size="sm">
|
|
<flux:heading size="sm" class="text-zinc-500">{{ __('Overdue') }}</flux:heading>
|
|
<flux:heading size="xl" class="mt-1 text-red-600 dark:text-red-400" data-test="count-overdue">{{ $this->todoCounts['overdue'] }}</flux:heading>
|
|
</flux:card>
|
|
|
|
<flux:card size="sm">
|
|
<flux:heading size="sm" class="text-zinc-500">{{ __('Due today') }}</flux:heading>
|
|
<flux:heading size="xl" class="mt-1" data-test="count-due-today">{{ $this->todoCounts['dueToday'] }}</flux:heading>
|
|
</flux:card>
|
|
|
|
<flux:card size="sm">
|
|
<flux:heading size="sm" class="text-zinc-500">{{ __('Completed') }}</flux:heading>
|
|
<flux:heading size="xl" class="mt-1" data-test="count-completed">{{ $this->todoCounts['completed'] }}</flux:heading>
|
|
</flux:card>
|
|
</div>
|
|
|
|
@if ($this->folderCounts->isNotEmpty())
|
|
<div class="grid gap-4 sm:grid-cols-2 xl:grid-cols-3">
|
|
@foreach ($this->folderCounts as $stat)
|
|
<flux:card
|
|
class="cursor-pointer transition hover:ring-2 hover:ring-zinc-300 dark:hover:ring-zinc-600"
|
|
wire:click="$set('folderId', {{ $stat['folder']->id }})"
|
|
data-test="folder-stat"
|
|
>
|
|
<div class="flex items-center justify-between gap-3">
|
|
<flux:heading size="sm" class="truncate">{{ $stat['folder']->name }}</flux:heading>
|
|
@if ($folderId === $stat['folder']->id)
|
|
<flux:badge size="sm" color="blue">{{ __('Filtered') }}</flux:badge>
|
|
@endif
|
|
</div>
|
|
<div class="mt-3 flex items-baseline gap-4">
|
|
<flux:heading size="xl">{{ $stat['open'] }}</flux:heading>
|
|
<flux:text class="text-sm">{{ __('open') }}</flux:text>
|
|
@if ($stat['overdue'] > 0)
|
|
<flux:text class="text-sm font-medium text-red-600 dark:text-red-400" data-test="folder-overdue-{{ $stat['folder']->id }}">
|
|
{{ $stat['overdue'] }} {{ __('overdue') }}
|
|
</flux:text>
|
|
@endif
|
|
</div>
|
|
</flux:card>
|
|
@endforeach
|
|
</div>
|
|
@endif
|
|
|
|
<div class="flex flex-wrap items-center gap-2">
|
|
@foreach ($this->statusFilters as $value => $label)
|
|
<flux:button
|
|
size="sm"
|
|
:variant="$status === $value ? 'primary' : 'ghost'"
|
|
wire:click="$set('status', '{{ $value }}')"
|
|
:data-test="'status-' . $value"
|
|
>
|
|
{{ __($label) }}
|
|
</flux:button>
|
|
@endforeach
|
|
|
|
<flux:spacer />
|
|
|
|
<flux:input
|
|
size="sm"
|
|
type="search"
|
|
icon="magnifying-glass"
|
|
wire:model.live.debounce.300ms="search"
|
|
placeholder="{{ __('Search todos…') }}"
|
|
class="w-full sm:w-64"
|
|
data-test="search-input"
|
|
/>
|
|
|
|
<flux:select size="sm" wire:model.live="folderId" class="w-full sm:w-56" data-test="folder-filter">
|
|
<flux:select.option value="">{{ __('All folders') }}</flux:select.option>
|
|
@foreach ($this->folderTree as $node)
|
|
<flux:select.option :value="$node['folder']->id">{{ str_repeat('— ', $node['depth']) }}{{ $node['folder']->name }}</flux:select.option>
|
|
@endforeach
|
|
</flux:select>
|
|
|
|
@if ($this->hasActiveFilters)
|
|
<flux:button size="sm" variant="danger" icon="x-mark" wire:click="resetFilters" class="shrink-0">
|
|
{{ __('Clear') }}
|
|
</flux:button>
|
|
@endif
|
|
</div>
|
|
|
|
<flux:card>
|
|
<flux:table :paginate="$this->todos">
|
|
<flux:table.columns>
|
|
<flux:table.column width="3rem">{{ __('Done') }}</flux:table.column>
|
|
<flux:table.column>{{ __('Description') }}</flux:table.column>
|
|
<flux:table.column>{{ __('Status') }}</flux:table.column>
|
|
<flux:table.column>{{ __('Folder') }}</flux:table.column>
|
|
<flux:table.column>{{ __('Due') }}</flux:table.column>
|
|
<flux:table.column>{{ __('Created') }}</flux:table.column>
|
|
<flux:table.column align="end" width="7rem">{{ __('Actions') }}</flux:table.column>
|
|
</flux:table.columns>
|
|
|
|
<flux:table.rows>
|
|
@forelse ($this->todos as $todo)
|
|
<flux:table.row :key="$todo->id" data-test="todo-row">
|
|
<flux:table.cell class="py-0">
|
|
<flux:checkbox
|
|
:checked="$todo->completed_at !== null"
|
|
:title="$todo->completed_at ? __('Mark as not done') : __('Mark as done')"
|
|
wire:click="toggleComplete({{ $todo->id }})"
|
|
data-test="toggle-complete-{{ $todo->id }}"
|
|
/>
|
|
</flux:table.cell>
|
|
|
|
<flux:table.cell :variant="$todo->completed_at ? 'strong' : 'default'" data-test="todo-description-{{ $todo->id }}">
|
|
<span @class(['line-through text-zinc-400' => $todo->completed_at !== null])>{{ $todo->description }}</span>
|
|
@if ($todo->notes)
|
|
<flux:text size="sm" class="mt-1 block max-w-md truncate text-zinc-500">{{ $todo->notes }}</flux:text>
|
|
@endif
|
|
</flux:table.cell>
|
|
|
|
<flux:table.cell>
|
|
<flux:badge size="sm" :color="$todo->status->badgeColor()" data-test="todo-status-{{ $todo->id }}">
|
|
{{ $todo->status->label() }}
|
|
</flux:badge>
|
|
</flux:table.cell>
|
|
|
|
<flux:table.cell class="text-zinc-500">
|
|
{{ $todo->folder?->parent?->name }}@if ($todo->folder?->parent) / @endif{{ $todo->folder?->name }}
|
|
</flux:table.cell>
|
|
|
|
<flux:table.cell>
|
|
@if ($todo->completed_at)
|
|
<flux:badge size="sm" color="zinc">{{ $todo->due_date?->format('M j, Y') ?? __('—') }}</flux:badge>
|
|
@elseif ($todo->due_date?->isPast())
|
|
<flux:badge size="sm" color="red">{{ $todo->due_date->format('M j, Y') }}</flux:badge>
|
|
@elseif ($todo->due_date?->isToday())
|
|
<flux:badge size="sm" color="amber">{{ __('Today') }} · {{ $todo->due_date->format('M j') }}</flux:badge>
|
|
@else
|
|
<flux:text>{{ $todo->due_date?->format('M j, Y') ?? __('No due date') }}</flux:text>
|
|
@endif
|
|
</flux:table.cell>
|
|
|
|
<flux:table.cell class="text-zinc-500" data-test="todo-created-{{ $todo->id }}">
|
|
{{ $todo->created_at->format('M j, Y') }}
|
|
</flux:table.cell>
|
|
|
|
<flux:table.cell class="py-0">
|
|
<div class="flex justify-end gap-1">
|
|
<flux:button
|
|
size="sm"
|
|
variant="ghost"
|
|
icon="pencil-square"
|
|
:title="__('Edit todo')"
|
|
wire:click="openEditForm({{ $todo->id }})"
|
|
data-test="edit-todo-{{ $todo->id }}"
|
|
/>
|
|
<flux:button
|
|
size="sm"
|
|
variant="ghost"
|
|
icon="trash"
|
|
:title="__('Delete todo')"
|
|
wire:click="confirmTodoDelete({{ $todo->id }})"
|
|
data-test="delete-todo-{{ $todo->id }}"
|
|
/>
|
|
</div>
|
|
</flux:table.cell>
|
|
</flux:table.row>
|
|
@empty
|
|
<flux:table.row>
|
|
<flux:table.cell colspan="7" align="center" class="py-10">
|
|
<flux:text>{{ __('No todos match your filters.') }}</flux:text>
|
|
</flux:table.cell>
|
|
</flux:table.row>
|
|
@endforelse
|
|
</flux:table.rows>
|
|
</flux:table>
|
|
</flux:card>
|
|
|
|
<flux:modal wire:model.self="showTodoForm" class="md:w-xl">
|
|
<form wire:submit="saveTodo" class="space-y-6">
|
|
<div>
|
|
<flux:heading size="lg">{{ $editingTodoId === null ? __('New todo') : __('Edit todo') }}</flux:heading>
|
|
<flux:text class="mt-2">{{ __('Add a task with a description and folder. A due date is optional.') }}</flux:text>
|
|
</div>
|
|
|
|
<flux:field>
|
|
<flux:label>{{ __('Description') }}</flux:label>
|
|
<flux:input
|
|
wire:model="description"
|
|
type="text"
|
|
autofocus
|
|
placeholder="{{ __('What needs to be done?') }}"
|
|
data-test="description-input"
|
|
/>
|
|
<flux:error name="description" />
|
|
</flux:field>
|
|
|
|
<flux:field>
|
|
<flux:label>{{ __('Status') }}</flux:label>
|
|
<flux:select wire:model="todoStatus" data-test="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="dueDate" type="date" data-test="due-date-input" />
|
|
<flux:error name="dueDate" />
|
|
</flux:field>
|
|
|
|
<flux:field>
|
|
<flux:label>{{ __('Notes') }}</flux:label>
|
|
<flux:textarea
|
|
wire:model="notes"
|
|
rows="3"
|
|
placeholder="{{ __('Add any extra details…') }}"
|
|
data-test="notes-input"
|
|
/>
|
|
<flux:error name="notes" />
|
|
</flux:field>
|
|
|
|
<flux:field>
|
|
<flux:label>{{ __('Folder') }}</flux:label>
|
|
<flux:select wire:model="formFolderId" data-test="folder-select">
|
|
<flux:select.option value="">{{ __('Select a folder…') }}</flux:select.option>
|
|
@foreach ($this->folderTree as $node)
|
|
<flux:select.option :value="$node['folder']->id">{{ str_repeat('— ', $node['depth']) }}{{ $node['folder']->name }}</flux:select.option>
|
|
@endforeach
|
|
</flux:select>
|
|
<flux:error name="formFolderId" />
|
|
</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">{{ __('Save todo') }}</flux:button>
|
|
</div>
|
|
</form>
|
|
</flux:modal>
|
|
|
|
<flux:modal name="confirm-todo-delete" class="md:w-lg">
|
|
<div class="space-y-6">
|
|
<div>
|
|
<flux:heading size="lg">{{ __('Delete todo?') }}</flux:heading>
|
|
<flux:text class="mt-2">{{ __('This todo will be permanently deleted. 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="deleteTodo">{{ __('Delete') }}</flux:button>
|
|
</div>
|
|
</div>
|
|
</flux:modal>
|
|
</div>
|