From 70aeb019a525786de0fb4c58e2a458453253ff43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20B=C3=B6hme?= Date: Mon, 7 Apr 2025 11:22:44 +0200 Subject: [PATCH] feat: allow completing tasks and deleting completions --- lib/putzplan/tasks/completed_task.ex | 6 +-- .../live/task_live/form_component.ex | 3 +- lib/putzplan_web/live/task_live/index.ex | 38 +++++++++++-------- lib/putzplan_web/live/task_live/show.ex | 31 +++++++-------- 4 files changed, 41 insertions(+), 37 deletions(-) diff --git a/lib/putzplan/tasks/completed_task.ex b/lib/putzplan/tasks/completed_task.ex index 44ac3a6..c94e59b 100644 --- a/lib/putzplan/tasks/completed_task.ex +++ b/lib/putzplan/tasks/completed_task.ex @@ -2,7 +2,7 @@ defmodule Putzplan.Tasks.CompletedTask do use Ash.Resource, otp_app: :putzplan, domain: Putzplan.Tasks, data_layer: AshSqlite.DataLayer actions do - defaults [:read] + defaults [:destroy] read :read_with_relations do primary? true @@ -13,8 +13,8 @@ defmodule Putzplan.Tasks.CompletedTask do create :create do primary? true - argument :user, :map, allow_nil?: false - argument :task, :map, allow_nil?: false + argument :user, :uuid, allow_nil?: false + argument :task, :uuid, allow_nil?: false change set_attribute(:completion, &Date.utc_today/0) change manage_relationship(:user, :users, type: :append) diff --git a/lib/putzplan_web/live/task_live/form_component.ex b/lib/putzplan_web/live/task_live/form_component.ex index 346553a..7fd3d2f 100644 --- a/lib/putzplan_web/live/task_live/form_component.ex +++ b/lib/putzplan_web/live/task_live/form_component.ex @@ -17,7 +17,6 @@ defmodule PutzplanWeb.TaskLive.FormComponent do phx-change="validate" phx-submit="save" > - <.input field={@form[:description]} type="text" label="Description" /><.input field={@form[:repetition_days]} type="number" label="Repetition days" /> @@ -46,7 +45,7 @@ defmodule PutzplanWeb.TaskLive.FormComponent do def handle_event("save", %{"task" => task_params}, socket) do case AshPhoenix.Form.submit(socket.assigns.form, params: task_params) do {:ok, task} -> - notify_parent({:saved, task}) + notify_parent({:saved, task |> Ash.load!(:due)}) socket = socket diff --git a/lib/putzplan_web/live/task_live/index.ex b/lib/putzplan_web/live/task_live/index.ex index 964368a..efafbbb 100644 --- a/lib/putzplan_web/live/task_live/index.ex +++ b/lib/putzplan_web/live/task_live/index.ex @@ -18,20 +18,21 @@ defmodule PutzplanWeb.TaskLive.Index do rows={@streams.tasks} row_click={fn {_id, task} -> JS.navigate(~p"/tasks/#{task}") end} > - + <:col :let={{_id, task}} label="Description"><%= task.description %> - + <:col :let={{_id, task}} label="Due"><%= task.due %> - + <:action :let={{_id, task}}>
<.link navigate={~p"/tasks/#{task}"}>Show
- + <.link patch={~p"/tasks/#{task}/edit"}>Edit - + <.link phx-click={JS.push("complete", value: %{id: task.id})}>Complete + - + <:action :let={{id, task}}> <.link phx-click={JS.push("delete", value: %{id: task.id}) |> hide("##{id}")} @@ -40,24 +41,19 @@ defmodule PutzplanWeb.TaskLive.Index do Delete - + - - <.modal :if={@live_action in [:new, :edit]} id="task-modal" show on_cancel={JS.patch(~p"/tasks")}> <.live_component module={PutzplanWeb.TaskLive.FormComponent} id={(@task && @task.id) || :new} title={@page_title} - - current_user={@current_user} - + current_user={@current_user} action={@live_action} task={@task} - patch={~p"/tasks"} + patch={~p"/"} /> - """ end @@ -65,7 +61,10 @@ defmodule PutzplanWeb.TaskLive.Index do def mount(_params, _session, socket) do {:ok, socket - |> stream(:tasks, Ash.read!(Putzplan.Tasks.Task, load: [:due], actor: socket.assigns[:current_user])) + |> stream( + :tasks, + Ash.read!(Putzplan.Tasks.Task, load: [:due], actor: socket.assigns[:current_user]) + ) |> assign_new(:current_user, fn -> nil end)} end @@ -104,4 +103,13 @@ defmodule PutzplanWeb.TaskLive.Index do {:noreply, stream_delete(socket, :tasks, task)} end + + @impl true + def handle_event("complete", %{"id" => id}, socket) do + Putzplan.Tasks.CompletedTask + |> Ash.Changeset.for_create(:create, %{task: id, user: socket.assigns.current_user.id}) + |> Ash.create!(actor: socket.assigns.current_user) + + {:noreply, stream_insert(socket, :tasks, Ash.get!(Putzplan.Tasks.Task, id, load: [:due]))} + end end diff --git a/lib/putzplan_web/live/task_live/show.ex b/lib/putzplan_web/live/task_live/show.ex index 98c4d7a..0f04a92 100644 --- a/lib/putzplan_web/live/task_live/show.ex +++ b/lib/putzplan_web/live/task_live/show.ex @@ -21,25 +21,14 @@ defmodule PutzplanWeb.TaskLive.Show do > <: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"/tasks"}>Back to tasks - - - <.modal :if={@live_action == :edit} id="task-modal" show on_cancel={JS.patch(~p"/tasks/#{@task}")}> - <.live_component - module={PutzplanWeb.TaskLive.FormComponent} - id={@task.id} - title={@page_title} - action={@live_action} - - current_user={@current_user} - - task={@task} - patch={~p"/tasks/#{@task}"} - /> - - + <.back navigate={~p"/"}>Back to tasks """ end @@ -66,4 +55,12 @@ defmodule PutzplanWeb.TaskLive.Show do 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