From a5504e6e6e4f0fa8c1934903c78588e1216e32d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20B=C3=B6hme?= Date: Mon, 7 Apr 2025 08:06:11 +0200 Subject: [PATCH] feat: allow listing completed tasks for task --- lib/putzplan/accounts/user.ex | 5 +-- lib/putzplan/tasks/completed_task.ex | 6 ++++ .../live/completed_task_live/index.ex | 4 +-- .../live/completed_task_live/show.ex | 11 ++++--- lib/putzplan_web/live/task_live/index.ex | 6 ++-- lib/putzplan_web/live/task_live/show.ex | 31 +++++++++++-------- 6 files changed, 37 insertions(+), 26 deletions(-) diff --git a/lib/putzplan/accounts/user.ex b/lib/putzplan/accounts/user.ex index 89afbce..0a57183 100644 --- a/lib/putzplan/accounts/user.ex +++ b/lib/putzplan/accounts/user.ex @@ -75,8 +75,9 @@ defmodule Putzplan.Accounts.User do authorize_if always() end - policy always() do - forbid_if always() + # TODO: figure out something better? + policy action_type(:read) do + authorize_if always() end end diff --git a/lib/putzplan/tasks/completed_task.ex b/lib/putzplan/tasks/completed_task.ex index 2bb8c5b..44ac3a6 100644 --- a/lib/putzplan/tasks/completed_task.ex +++ b/lib/putzplan/tasks/completed_task.ex @@ -4,6 +4,12 @@ defmodule Putzplan.Tasks.CompletedTask do actions do defaults [:read] + read :read_with_relations do + primary? true + + prepare build(load: [:users, :tasks]) + end + create :create do primary? true diff --git a/lib/putzplan_web/live/completed_task_live/index.ex b/lib/putzplan_web/live/completed_task_live/index.ex index 4d734d9..5197afa 100644 --- a/lib/putzplan_web/live/completed_task_live/index.ex +++ b/lib/putzplan_web/live/completed_task_live/index.ex @@ -34,9 +34,7 @@ defmodule PutzplanWeb.CompletedTaskLive.Index do <.modal :if={@live_action == :new} id="completed_task-modal" show on_cancel={JS.patch(~p"/completed_tasks")}> <.live_component module={PutzplanWeb.CompletedTaskLive.FormComponent} - - current_user={@current_user} - + current_user={@current_user} id={:new} title={@page_title} action={@live_action} diff --git a/lib/putzplan_web/live/completed_task_live/show.ex b/lib/putzplan_web/live/completed_task_live/show.ex index 3961577..f196e58 100644 --- a/lib/putzplan_web/live/completed_task_live/show.ex +++ b/lib/putzplan_web/live/completed_task_live/show.ex @@ -7,13 +7,13 @@ defmodule PutzplanWeb.CompletedTaskLive.Show do <.header> Completed task <%= @completed_task.id %> <:subtitle>This is a completed_task record from your database. - + <.list> - + <:item title="Id"><%= @completed_task.id %> - + <.back navigate={~p"/completed_tasks"}>Back to completed_tasks @@ -29,12 +29,15 @@ defmodule PutzplanWeb.CompletedTaskLive.Show do @impl true def handle_params(%{"id" => id}, _, socket) do + actor = socket.assigns.current_user + completed_task = Ash.get!(Putzplan.Tasks.CompletedTask, id, actor: actor) + {:noreply, socket |> assign(:page_title, page_title(socket.assigns.live_action)) |> assign( :completed_task, - Ash.get!(Putzplan.Tasks.CompletedTask, id, actor: socket.assigns.current_user) + completed_task )} end diff --git a/lib/putzplan_web/live/task_live/index.ex b/lib/putzplan_web/live/task_live/index.ex index 7cdb65e..65f816b 100644 --- a/lib/putzplan_web/live/task_live/index.ex +++ b/lib/putzplan_web/live/task_live/index.ex @@ -19,11 +19,9 @@ defmodule PutzplanWeb.TaskLive.Index do row_click={fn {_id, task} -> JS.navigate(~p"/tasks/#{task}") end} > - <:col :let={{_id, task}} label="Id"><%= task.id %> - <:col :let={{_id, task}} label="Description"><%= task.description %> - <:col :let={{_id, task}} label="Repetition days"><%= task.repetition_days %> + <:col :let={{_id, task}} label="Due"><%= task.due %> <:action :let={{_id, task}}>
@@ -67,7 +65,7 @@ defmodule PutzplanWeb.TaskLive.Index do def mount(_params, _session, socket) do {:ok, socket - |> stream(:tasks, Ash.read!(Putzplan.Tasks.Task, actor: socket.assigns[:current_user])) + |> stream(:tasks, Ash.read!(Putzplan.Tasks.Task, actor: socket.assigns[:current_user]) |> Ash.load!(:due)) |> assign_new(:current_user, fn -> nil end)} end diff --git a/lib/putzplan_web/live/task_live/show.ex b/lib/putzplan_web/live/task_live/show.ex index 809aed2..98c4d7a 100644 --- a/lib/putzplan_web/live/task_live/show.ex +++ b/lib/putzplan_web/live/task_live/show.ex @@ -1,13 +1,12 @@ defmodule PutzplanWeb.TaskLive.Show do + require Ash.Query use PutzplanWeb, :live_view @impl true def render(assigns) do ~H""" <.header> - Task <%= @task.id %> - <:subtitle>This is a task record from your database. - + Task <%= @task.description %> <:actions> <.link patch={~p"/tasks/#{@task}/show/edit"} phx-click={JS.push_focus()}> <.button>Edit task @@ -16,15 +15,13 @@ defmodule PutzplanWeb.TaskLive.Show do - <.list> - - <:item title="Id"><%= @task.id %> - - <:item title="Description"><%= @task.description %> - - <:item title="Repetition days"><%= @task.repetition_days %> - - + <.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 %> + <.back navigate={~p"/tasks"}>Back to tasks @@ -53,10 +50,18 @@ defmodule PutzplanWeb.TaskLive.Show do @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, Ash.get!(Putzplan.Tasks.Task, id, actor: socket.assigns.current_user))} + |> 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"