+ <%= for task <- @tasks do %>
@@ -74,7 +74,7 @@ defmodule PutzplanWeb.TaskLive.Index do
<.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?"
class="text-red-600 hover:text-red-800 p-1 rounded-full hover:bg-red-100"
phx-click-stop-propagation="true"
@@ -118,15 +118,20 @@ defmodule PutzplanWeb.TaskLive.Index do
def mount(_params, _session, socket) do
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,
socket
- |> stream(
- :tasks,
- Ash.read!(Putzplan.Tasks.Task, load: [:due], actor: socket.assigns[:current_user])
- )
+ |> assign(:tasks, tasks)
|> assign_new(:current_user, fn -> nil end)}
end
+ defp sort_tasks_by_due_date(tasks) do
+ Enum.sort_by(tasks, & &1.due, Date)
+ end
+
@impl true
def handle_params(params, _url, socket) do
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
@@ -134,18 +139,32 @@ defmodule PutzplanWeb.TaskLive.Index do
@impl true
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
@impl true
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
@impl true
def handle_info({:update, task_id}, socket) do
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
defp apply_action(socket, :edit, %{"id" => id}) do