This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
namespace App\Concerns;
|
||||
|
||||
use App\Models\Folder;
|
||||
use App\Models\Todo;
|
||||
use App\TodoStatus;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
/**
|
||||
* Adds sortable column state and query ordering for todo tables.
|
||||
*/
|
||||
trait SortsTodos
|
||||
{
|
||||
public ?string $sortBy = null;
|
||||
|
||||
public string $sortDirection = 'asc';
|
||||
|
||||
/**
|
||||
* Toggle the sort column and direction. Clicking the active column flips
|
||||
* its direction, and clicking it once more restores the manual order.
|
||||
*/
|
||||
public function sort(string $column): void
|
||||
{
|
||||
if (! in_array($column, $this->sortableTodoColumns(), true)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($this->sortBy !== $column) {
|
||||
$this->sortBy = $column;
|
||||
$this->sortDirection = 'asc';
|
||||
} elseif ($this->sortDirection === 'asc') {
|
||||
$this->sortDirection = 'desc';
|
||||
} else {
|
||||
$this->sortBy = null;
|
||||
$this->sortDirection = 'asc';
|
||||
}
|
||||
|
||||
$this->sortChanged();
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether a column sort is currently applied.
|
||||
*/
|
||||
protected function hasActiveSort(): bool
|
||||
{
|
||||
return $this->sortBy !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<string>
|
||||
*/
|
||||
protected function sortableTodoColumns(): array
|
||||
{
|
||||
return ['description', 'status', 'folder', 'due_date', 'created_at', 'completed_at'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Builder<Todo> $query
|
||||
* @return Builder<Todo>
|
||||
*/
|
||||
protected function applyTodoSort(Builder $query): Builder
|
||||
{
|
||||
if ($this->sortBy === null) {
|
||||
return $query->priority();
|
||||
}
|
||||
|
||||
$direction = $this->sortDirection === 'desc' ? 'desc' : 'asc';
|
||||
|
||||
return (match ($this->sortBy) {
|
||||
'description' => $query->orderBy('description', $direction),
|
||||
'status' => $this->orderByStatus($query, $direction),
|
||||
'folder' => $query->orderBy(
|
||||
Folder::query()->select('name')->whereColumn('folders.id', 'todos.folder_id'),
|
||||
$direction
|
||||
),
|
||||
'due_date' => $query
|
||||
->orderByRaw('due_date IS NULL')
|
||||
->orderBy('due_date', $direction),
|
||||
'created_at' => $query->orderBy('created_at', $direction),
|
||||
'completed_at' => $query
|
||||
->orderByRaw('completed_at IS NULL')
|
||||
->orderBy('completed_at', $direction),
|
||||
default => $query->priority(),
|
||||
})->orderBy('id');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Builder<Todo> $query
|
||||
* @return Builder<Todo>
|
||||
*/
|
||||
private function orderByStatus(Builder $query, string $direction): Builder
|
||||
{
|
||||
$statuses = TodoStatus::cases();
|
||||
|
||||
$cases = collect($statuses)
|
||||
->map(fn (TodoStatus $status, int $index) => 'WHEN ? THEN '.$index)
|
||||
->implode(' ');
|
||||
|
||||
return $query->orderByRaw(
|
||||
"CASE todos.status {$cases} ELSE 999 END {$direction}",
|
||||
collect($statuses)->map(fn (TodoStatus $status) => $status->value)->all()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook for components to reset their pagination when the sort changes.
|
||||
*/
|
||||
protected function sortChanged(): void {}
|
||||
}
|
||||
Reference in New Issue
Block a user