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()
end
policy always() do
forbid_if always()
# TODO: figure out something better?
policy action_type(:read) do
authorize_if always()
end
end

View file

@ -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

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")}>
<.live_component
module={PutzplanWeb.CompletedTaskLive.FormComponent}
current_user={@current_user}
current_user={@current_user}
id={:new}
title={@page_title}
action={@live_action}

View file

@ -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.</:subtitle>
</.header>
<.list>
<:item title="Id"><%= @completed_task.id %></:item>
</.list>
<.back navigate={~p"/completed_tasks"}>Back to completed_tasks</.back>
@ -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

View file

@ -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>
<: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}}>
<div class="sr-only">
@ -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

View file

@ -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.</:subtitle>
Task <%= @task.description %>
<:actions>
<.link patch={~p"/tasks/#{@task}/show/edit"} phx-click={JS.push_focus()}>
<.button>Edit task</.button>
@ -16,15 +15,13 @@ defmodule PutzplanWeb.TaskLive.Show do
</.header>
<.list>
<:item title="Id"><%= @task.id %></:item>
<:item title="Description"><%= @task.description %></:item>
<:item title="Repetition days"><%= @task.repetition_days %></:item>
</.list>
<.table
id="completed_tasks"
rows={@streams.completed_tasks}
>
<:col :let={{_id, completed_task}} label="Completed by"><%= completed_task.users.name %></:col>
<:col :let={{_id, completed_task}} label="Date"><%= completed_task.completion %></:col>
</.table>
<.back navigate={~p"/tasks"}>Back to tasks</.back>
@ -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"