feat: allow listing completed tasks for task

This commit is contained in:
Moritz Böhme 2025-04-07 08:06:11 +02:00
parent 20027b42f0
commit a5504e6e6e
6 changed files with 37 additions and 26 deletions

View file

@ -75,8 +75,9 @@ defmodule Putzplan.Accounts.User do
authorize_if always() authorize_if always()
end end
policy always() do # TODO: figure out something better?
forbid_if always() policy action_type(:read) do
authorize_if always()
end end
end end

View file

@ -4,6 +4,12 @@ defmodule Putzplan.Tasks.CompletedTask do
actions do actions do
defaults [:read] defaults [:read]
read :read_with_relations do
primary? true
prepare build(load: [:users, :tasks])
end
create :create do create :create do
primary? true primary? true

View file

@ -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")}> <.modal :if={@live_action == :new} id="completed_task-modal" show on_cancel={JS.patch(~p"/completed_tasks")}>
<.live_component <.live_component
module={PutzplanWeb.CompletedTaskLive.FormComponent} module={PutzplanWeb.CompletedTaskLive.FormComponent}
current_user={@current_user}
current_user={@current_user}
id={:new} id={:new}
title={@page_title} title={@page_title}
action={@live_action} action={@live_action}

View file

@ -7,13 +7,13 @@ defmodule PutzplanWeb.CompletedTaskLive.Show do
<.header> <.header>
Completed task <%= @completed_task.id %> Completed task <%= @completed_task.id %>
<:subtitle>This is a completed_task record from your database.</:subtitle> <:subtitle>This is a completed_task record from your database.</:subtitle>
</.header> </.header>
<.list> <.list>
<:item title="Id"><%= @completed_task.id %></:item> <:item title="Id"><%= @completed_task.id %></:item>
</.list> </.list>
<.back navigate={~p"/completed_tasks"}>Back to completed_tasks</.back> <.back navigate={~p"/completed_tasks"}>Back to completed_tasks</.back>
@ -29,12 +29,15 @@ defmodule PutzplanWeb.CompletedTaskLive.Show do
@impl true @impl true
def handle_params(%{"id" => id}, _, socket) do def handle_params(%{"id" => id}, _, socket) do
actor = socket.assigns.current_user
completed_task = Ash.get!(Putzplan.Tasks.CompletedTask, id, actor: actor)
{:noreply, {:noreply,
socket socket
|> assign(:page_title, page_title(socket.assigns.live_action)) |> assign(:page_title, page_title(socket.assigns.live_action))
|> assign( |> assign(
:completed_task, :completed_task,
Ash.get!(Putzplan.Tasks.CompletedTask, id, actor: socket.assigns.current_user) completed_task
)} )}
end end

View file

@ -19,11 +19,9 @@ defmodule PutzplanWeb.TaskLive.Index do
row_click={fn {_id, task} -> JS.navigate(~p"/tasks/#{task}") end} row_click={fn {_id, task} -> JS.navigate(~p"/tasks/#{task}") end}
> >
<:col :let={{_id, task}} label="Id"><%= task.id %></:col>
<:col :let={{_id, task}} label="Description"><%= task.description %></:col> <:col :let={{_id, task}} label="Description"><%= task.description %></:col>
<:col :let={{_id, task}} label="Repetition days"><%= task.repetition_days %></:col> <:col :let={{_id, task}} label="Due"><%= task.due %></:col>
<:action :let={{_id, task}}> <:action :let={{_id, task}}>
<div class="sr-only"> <div class="sr-only">
@ -67,7 +65,7 @@ defmodule PutzplanWeb.TaskLive.Index do
def mount(_params, _session, socket) do def mount(_params, _session, socket) do
{:ok, {:ok,
socket 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)} |> assign_new(:current_user, fn -> nil end)}
end end

View file

@ -1,13 +1,12 @@
defmodule PutzplanWeb.TaskLive.Show do defmodule PutzplanWeb.TaskLive.Show do
require Ash.Query
use PutzplanWeb, :live_view use PutzplanWeb, :live_view
@impl true @impl true
def render(assigns) do def render(assigns) do
~H""" ~H"""
<.header> <.header>
Task <%= @task.id %> Task <%= @task.description %>
<:subtitle>This is a task record from your database.</:subtitle>
<:actions> <:actions>
<.link patch={~p"/tasks/#{@task}/show/edit"} phx-click={JS.push_focus()}> <.link patch={~p"/tasks/#{@task}/show/edit"} phx-click={JS.push_focus()}>
<.button>Edit task</.button> <.button>Edit task</.button>
@ -16,15 +15,13 @@ defmodule PutzplanWeb.TaskLive.Show do
</.header> </.header>
<.list> <.table
id="completed_tasks"
<:item title="Id"><%= @task.id %></:item> rows={@streams.completed_tasks}
>
<:item title="Description"><%= @task.description %></:item> <:col :let={{_id, completed_task}} label="Completed by"><%= completed_task.users.name %></:col>
<:col :let={{_id, completed_task}} label="Date"><%= completed_task.completion %></:col>
<:item title="Repetition days"><%= @task.repetition_days %></:item> </.table>
</.list>
<.back navigate={~p"/tasks"}>Back to tasks</.back> <.back navigate={~p"/tasks"}>Back to tasks</.back>
@ -53,10 +50,18 @@ defmodule PutzplanWeb.TaskLive.Show do
@impl true @impl true
def handle_params(%{"id" => id}, _, socket) do def handle_params(%{"id" => id}, _, socket) do
task = Ash.get!(Putzplan.Tasks.Task, id, actor: socket.assigns.current_user)
{:noreply, {:noreply,
socket socket
|> assign(:page_title, page_title(socket.assigns.live_action)) |> 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 end
defp page_title(:show), do: "Show Task" defp page_title(:show), do: "Show Task"