feat: add task liveview
This commit is contained in:
parent
efa5489225
commit
26b33060ee
4 changed files with 271 additions and 25 deletions
109
lib/putzplan_web/live/task_live/index.ex
Normal file
109
lib/putzplan_web/live/task_live/index.ex
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
defmodule PutzplanWeb.TaskLive.Index do
|
||||
use PutzplanWeb, :live_view
|
||||
|
||||
@impl true
|
||||
def render(assigns) do
|
||||
~H"""
|
||||
<.header>
|
||||
Listing Tasks
|
||||
<:actions>
|
||||
<.link patch={~p"/tasks/new"}>
|
||||
<.button>New Task</.button>
|
||||
</.link>
|
||||
</:actions>
|
||||
</.header>
|
||||
|
||||
<.table
|
||||
id="tasks"
|
||||
rows={@streams.tasks}
|
||||
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>
|
||||
|
||||
<:action :let={{_id, task}}>
|
||||
<div class="sr-only">
|
||||
<.link navigate={~p"/tasks/#{task}"}>Show</.link>
|
||||
</div>
|
||||
|
||||
<.link patch={~p"/tasks/#{task}/edit"}>Edit</.link>
|
||||
|
||||
</:action>
|
||||
|
||||
<:action :let={{id, task}}>
|
||||
<.link
|
||||
phx-click={JS.push("delete", value: %{id: task.id}) |> hide("##{id}")}
|
||||
data-confirm="Are you sure?"
|
||||
>
|
||||
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}
|
||||
|
||||
action={@live_action}
|
||||
task={@task}
|
||||
patch={~p"/tasks"}
|
||||
/>
|
||||
</.modal>
|
||||
|
||||
"""
|
||||
end
|
||||
|
||||
@impl true
|
||||
def mount(_params, _session, socket) do
|
||||
{:ok,
|
||||
socket
|
||||
|> stream(:tasks, Ash.read!(Putzplan.Tasks.Task, actor: socket.assigns[:current_user]))
|
||||
|> assign_new(:current_user, fn -> nil end)}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_params(params, _url, socket) do
|
||||
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
|
||||
end
|
||||
|
||||
defp apply_action(socket, :edit, %{"id" => id}) do
|
||||
socket
|
||||
|> assign(:page_title, "Edit Task")
|
||||
|> assign(:task, Ash.get!(Putzplan.Tasks.Task, id, actor: socket.assigns.current_user))
|
||||
end
|
||||
|
||||
defp apply_action(socket, :new, _params) do
|
||||
socket
|
||||
|> assign(:page_title, "New Task")
|
||||
|> assign(:task, nil)
|
||||
end
|
||||
|
||||
defp apply_action(socket, :index, _params) do
|
||||
socket
|
||||
|> assign(:page_title, "Listing Tasks")
|
||||
|> assign(:task, nil)
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_info({PutzplanWeb.TaskLive.FormComponent, {:saved, task}}, socket) do
|
||||
{:noreply, stream_insert(socket, :tasks, task)}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("delete", %{"id" => id}, socket) do
|
||||
task = Ash.get!(Putzplan.Tasks.Task, id, actor: socket.assigns.current_user)
|
||||
Ash.destroy!(task, actor: socket.assigns.current_user)
|
||||
|
||||
{:noreply, stream_delete(socket, :tasks, task)}
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue