feat: sort tasks by due date

This commit is contained in:
Moritz Böhme 2025-05-18 18:11:10 +02:00
parent d4d8a9a201
commit 65f0284ce8
No known key found for this signature in database
GPG key ID: 970C6E89EB0547A9

View file

@ -13,10 +13,10 @@ defmodule PutzplanWeb.TaskLive.Index do
</:actions> </:actions>
</.header> </.header>
<div id="tasks" class="grid grid-cols-1 gap-4 sm:grid-cols-1 lg:grid-cols-2" phx-update="stream"> <div id="tasks" class="grid grid-cols-1 gap-4 sm:grid-cols-1 lg:grid-cols-2">
<%= for {id, task} <- @streams.tasks do %> <%= for task <- @tasks do %>
<div <div
id={id} id={task.id}
class="flex flex-col p-4 bg-white rounded-lg shadow-md hover:shadow-lg transition-shadow duration-200 cursor-pointer" class="flex flex-col p-4 bg-white rounded-lg shadow-md hover:shadow-lg transition-shadow duration-200 cursor-pointer"
phx-click={JS.navigate(~p"/tasks/#{task}")} phx-click={JS.navigate(~p"/tasks/#{task}")}
> >
@ -74,7 +74,7 @@ defmodule PutzplanWeb.TaskLive.Index do
</.link> </.link>
<.link <.link
phx-click={JS.push("delete", value: %{id: task.id}) |> hide(id)} phx-click={JS.push("delete", value: %{id: task.id}) |> hide(task.id)}
data-confirm="Are you sure?" data-confirm="Are you sure?"
class="text-red-600 hover:text-red-800 p-1 rounded-full hover:bg-red-100" class="text-red-600 hover:text-red-800 p-1 rounded-full hover:bg-red-100"
phx-click-stop-propagation="true" phx-click-stop-propagation="true"
@ -118,15 +118,20 @@ defmodule PutzplanWeb.TaskLive.Index do
def mount(_params, _session, socket) do def mount(_params, _session, socket) do
if connected?(socket), do: Putzplan.PubSub.subscribe_tasks() if connected?(socket), do: Putzplan.PubSub.subscribe_tasks()
tasks =
Ash.read!(Putzplan.Tasks.Task, load: [:due], actor: socket.assigns[:current_user])
|> sort_tasks_by_due_date()
{:ok, {:ok,
socket socket
|> stream( |> assign(:tasks, tasks)
:tasks,
Ash.read!(Putzplan.Tasks.Task, load: [:due], actor: socket.assigns[:current_user])
)
|> assign_new(:current_user, fn -> nil end)} |> assign_new(:current_user, fn -> nil end)}
end end
defp sort_tasks_by_due_date(tasks) do
Enum.sort_by(tasks, & &1.due, Date)
end
@impl true @impl true
def handle_params(params, _url, socket) do def handle_params(params, _url, socket) do
{:noreply, apply_action(socket, socket.assigns.live_action, params)} {:noreply, apply_action(socket, socket.assigns.live_action, params)}
@ -134,18 +139,32 @@ defmodule PutzplanWeb.TaskLive.Index do
@impl true @impl true
def handle_info({:delete, task}, socket) do def handle_info({:delete, task}, socket) do
{:noreply, stream_delete(socket, :tasks, task)} updated_tasks = Enum.reject(socket.assigns.tasks, fn t -> t.id == task.id end)
{:noreply, assign(socket, :tasks, updated_tasks)}
end end
@impl true @impl true
def handle_info({:upsert, task}, socket) do def handle_info({:upsert, task}, socket) do
{:noreply, stream_insert(socket, :tasks, task)} updated_tasks =
socket.assigns.tasks
|> Enum.reject(fn t -> t.id == task.id end)
|> Enum.concat([task])
|> sort_tasks_by_due_date()
{:noreply, assign(socket, :tasks, updated_tasks)}
end end
@impl true @impl true
def handle_info({:update, task_id}, socket) do def handle_info({:update, task_id}, socket) do
task = Ash.get!(Putzplan.Tasks.Task, task_id, load: [:due]) task = Ash.get!(Putzplan.Tasks.Task, task_id, load: [:due])
{:noreply, stream_insert(socket, :tasks, task)}
updated_tasks =
socket.assigns.tasks
|> Enum.reject(fn t -> t.id == task.id end)
|> Enum.concat([task])
|> sort_tasks_by_due_date()
{:noreply, assign(socket, :tasks, updated_tasks)}
end end
defp apply_action(socket, :edit, %{"id" => id}) do defp apply_action(socket, :edit, %{"id" => id}) do