defmodule PutzplanWeb.TaskLive.Show do require Ash.Query use PutzplanWeb, :live_view @impl true def render(assigns) do ~H""" <.header> Task {@task.description} <:actions> <.link patch={~p"/tasks/#{@task}/show/edit"} phx-click={JS.push_focus()}> <.button>Edit task <.table id="completed_tasks" rows={@streams.completed_tasks}> <:col :let={{_id, completed_task}} label="Completed by">{completed_task.users.name} <:col :let={{_id, completed_task}} label="Date">{completed_task.completion} <:action :let={{id, completed_task}}> <.link phx-click={JS.push("delete", value: %{id: completed_task.id}) |> hide("##{id}")}> Delete <.back navigate={~p"/"}>Back to tasks """ end @impl true def mount(_params, _session, socket) do {:ok, socket} end @impl true def handle_params(%{"id" => id}, _, socket) do task = Ash.get!(Putzplan.Tasks.Task, id, actor: socket.assigns.current_user) {:noreply, socket |> assign(:page_title, page_title(socket.assigns.live_action)) |> assign(:task, task) |> stream( :completed_tasks, Putzplan.Tasks.CompletedTask |> Ash.Query.filter(task_id == ^task.id) |> Ash.read!(actor: socket.assigns[:current_user]) )} end defp page_title(:show), do: "Show Task" defp page_title(:edit), do: "Edit Task" @impl true def handle_event("delete", %{"id" => id}, socket) do completed_task = Ash.get!(Putzplan.Tasks.CompletedTask, id, actor: socket.assigns.current_user) Ash.destroy!(completed_task, actor: socket.assigns.current_user) {:noreply, stream_delete(socket, :completed_tasks, completed_task)} end end