feat: allow completing tasks and deleting completions
This commit is contained in:
parent
3c4f868f12
commit
70aeb019a5
4 changed files with 41 additions and 37 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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>
|
||||
|
||||
|
||||
<:col :let={{_id, task}} label="Due"><%= task.due %></:col>
|
||||
|
||||
|
||||
<:action :let={{_id, task}}>
|
||||
<div class="sr-only">
|
||||
<.link navigate={~p"/tasks/#{task}"}>Show</.link>
|
||||
</div>
|
||||
|
||||
|
||||
<.link patch={~p"/tasks/#{task}/edit"}>Edit</.link>
|
||||
|
||||
<.link phx-click={JS.push("complete", value: %{id: task.id})}>Complete</.link>
|
||||
|
||||
</:action>
|
||||
|
||||
|
||||
<: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
|
||||
</.link>
|
||||
</:action>
|
||||
|
||||
|
||||
</.table>
|
||||
|
||||
|
||||
<.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"/"}
|
||||
/>
|
||||
</.modal>
|
||||
|
||||
"""
|
||||
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
|
||||
|
|
|
|||
|
|
@ -21,25 +21,14 @@ defmodule PutzplanWeb.TaskLive.Show do
|
|||
>
|
||||
<:col :let={{_id, completed_task}} label="Completed by"><%= completed_task.users.name %></:col>
|
||||
<:col :let={{_id, completed_task}} label="Date"><%= completed_task.completion %></:col>
|
||||
<:action :let={{id, completed_task}}>
|
||||
<.link phx-click={JS.push("delete", value: %{id: completed_task.id}) |> hide("##{id}")}>
|
||||
Delete
|
||||
</.link>
|
||||
</:action>
|
||||
</.table>
|
||||
|
||||
<.back navigate={~p"/tasks"}>Back to tasks</.back>
|
||||
|
||||
|
||||
<.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}"}
|
||||
/>
|
||||
</.modal>
|
||||
|
||||
<.back navigate={~p"/"}>Back to tasks</.back>
|
||||
"""
|
||||
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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue