refactor: add pub_sub module
This commit is contained in:
parent
8de0f1e39f
commit
e25fc5ed64
3 changed files with 55 additions and 25 deletions
43
lib/putzplan/pub_sub.ex
Normal file
43
lib/putzplan/pub_sub.ex
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
defmodule Putzplan.PubSub do
|
||||||
|
@tasks_topic "tasks"
|
||||||
|
|
||||||
|
def subscribe_completed(id) do
|
||||||
|
Phoenix.PubSub.subscribe(__MODULE__, get_topic(id))
|
||||||
|
end
|
||||||
|
|
||||||
|
def subscribe_tasks do
|
||||||
|
Phoenix.PubSub.subscribe(__MODULE__, @tasks_topic)
|
||||||
|
end
|
||||||
|
|
||||||
|
def upsert_task(task) do
|
||||||
|
Phoenix.PubSub.broadcast!(__MODULE__, @tasks_topic, {:upsert, task})
|
||||||
|
end
|
||||||
|
|
||||||
|
def delete_task(task) do
|
||||||
|
Phoenix.PubSub.broadcast!(__MODULE__, @tasks_topic, {:delete, task})
|
||||||
|
end
|
||||||
|
|
||||||
|
def update_task_by_id(id) do
|
||||||
|
Phoenix.PubSub.broadcast!(__MODULE__, @tasks_topic, {:update, id})
|
||||||
|
end
|
||||||
|
|
||||||
|
def upsert_completed_task(completed_task) do
|
||||||
|
Phoenix.PubSub.broadcast!(
|
||||||
|
__MODULE__,
|
||||||
|
get_topic(completed_task.tasks.id),
|
||||||
|
{:upsert, completed_task}
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
def delete_completed_task(completed_task) do
|
||||||
|
Phoenix.PubSub.broadcast!(
|
||||||
|
__MODULE__,
|
||||||
|
get_topic(completed_task.tasks.id),
|
||||||
|
{:delete, completed_task}
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp get_topic(id) do
|
||||||
|
"completed:" <> id
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
@ -1,13 +1,6 @@
|
||||||
defmodule PutzplanWeb.TaskLive.Index do
|
defmodule PutzplanWeb.TaskLive.Index do
|
||||||
use PutzplanWeb, :live_view
|
use PutzplanWeb, :live_view
|
||||||
|
|
||||||
@pubsub_name Putzplan.PubSub
|
|
||||||
@pubsub_topic "tasks"
|
|
||||||
|
|
||||||
defp get_topic(id) do
|
|
||||||
"completed:" <> id
|
|
||||||
end
|
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def render(assigns) do
|
def render(assigns) do
|
||||||
~H"""
|
~H"""
|
||||||
|
|
@ -23,7 +16,7 @@ defmodule PutzplanWeb.TaskLive.Index do
|
||||||
<div id="tasks" class="grid grid-cols-1 gap-4 sm:grid-cols-1 lg:grid-cols-2" phx-update="stream">
|
<div id="tasks" class="grid grid-cols-1 gap-4 sm:grid-cols-1 lg:grid-cols-2" phx-update="stream">
|
||||||
<%= for {id, task} <- @streams.tasks do %>
|
<%= for {id, task} <- @streams.tasks do %>
|
||||||
<div
|
<div
|
||||||
id={"task-#{id}"}
|
id={id}
|
||||||
class="flex flex-col p-4 bg-white rounded-lg shadow-md hover:shadow-lg transition-shadow duration-200 cursor-pointer"
|
class="flex flex-col p-4 bg-white rounded-lg shadow-md hover:shadow-lg transition-shadow duration-200 cursor-pointer"
|
||||||
phx-click={JS.navigate(~p"/tasks/#{task}")}
|
phx-click={JS.navigate(~p"/tasks/#{task}")}
|
||||||
>
|
>
|
||||||
|
|
@ -81,7 +74,7 @@ defmodule PutzplanWeb.TaskLive.Index do
|
||||||
</.link>
|
</.link>
|
||||||
|
|
||||||
<.link
|
<.link
|
||||||
phx-click={JS.push("delete", value: %{id: task.id}) |> hide("#task-#{id}")}
|
phx-click={JS.push("delete", value: %{id: task.id}) |> hide(id)}
|
||||||
data-confirm="Are you sure?"
|
data-confirm="Are you sure?"
|
||||||
class="text-red-600 hover:text-red-800 p-1 rounded-full hover:bg-red-100"
|
class="text-red-600 hover:text-red-800 p-1 rounded-full hover:bg-red-100"
|
||||||
phx-click-stop-propagation="true"
|
phx-click-stop-propagation="true"
|
||||||
|
|
@ -123,7 +116,7 @@ defmodule PutzplanWeb.TaskLive.Index do
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def mount(_params, _session, socket) do
|
def mount(_params, _session, socket) do
|
||||||
if connected?(socket), do: Phoenix.PubSub.subscribe(@pubsub_name, @pubsub_topic)
|
if connected?(socket), do: Putzplan.PubSub.subscribe_tasks()
|
||||||
|
|
||||||
{:ok,
|
{:ok,
|
||||||
socket
|
socket
|
||||||
|
|
@ -180,9 +173,9 @@ defmodule PutzplanWeb.TaskLive.Index do
|
||||||
def handle_event("delete", %{"id" => id}, socket) do
|
def handle_event("delete", %{"id" => id}, socket) do
|
||||||
task = Ash.get!(Putzplan.Tasks.Task, id, actor: socket.assigns.current_user)
|
task = Ash.get!(Putzplan.Tasks.Task, id, actor: socket.assigns.current_user)
|
||||||
Ash.destroy!(task, actor: socket.assigns.current_user)
|
Ash.destroy!(task, actor: socket.assigns.current_user)
|
||||||
Phoenix.PubSub.broadcast(@pubsub_name, @pubsub_topic, {:delete, task})
|
Putzplan.PubSub.delete_task(task)
|
||||||
|
|
||||||
{:noreply, task}
|
{:noreply, socket}
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
|
|
@ -193,8 +186,8 @@ defmodule PutzplanWeb.TaskLive.Index do
|
||||||
|> Ash.create!(actor: socket.assigns.current_user)
|
|> Ash.create!(actor: socket.assigns.current_user)
|
||||||
|
|
||||||
task = Ash.get!(Putzplan.Tasks.Task, id, load: [:due])
|
task = Ash.get!(Putzplan.Tasks.Task, id, load: [:due])
|
||||||
Phoenix.PubSub.broadcast(@pubsub_name, @pubsub_topic, {:upsert, task})
|
Putzplan.PubSub.upsert_task(task)
|
||||||
Phoenix.PubSub.broadcast(@pubsub_name, get_topic(id), {:upsert, completed_task})
|
Putzplan.PubSub.upsert_completed_task(completed_task)
|
||||||
|
|
||||||
{:noreply, socket}
|
{:noreply, socket}
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -2,17 +2,11 @@ defmodule PutzplanWeb.TaskLive.Show do
|
||||||
require Ash.Query
|
require Ash.Query
|
||||||
use PutzplanWeb, :live_view
|
use PutzplanWeb, :live_view
|
||||||
|
|
||||||
@pubsub_name Putzplan.PubSub
|
|
||||||
|
|
||||||
defp get_topic(id) do
|
|
||||||
"completed:" <> id
|
|
||||||
end
|
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def render(assigns) do
|
def render(assigns) do
|
||||||
~H"""
|
~H"""
|
||||||
<.header>
|
<.header>
|
||||||
Task {@task.description}
|
{@task.description}
|
||||||
<: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>
|
||||||
|
|
@ -87,7 +81,7 @@ defmodule PutzplanWeb.TaskLive.Show do
|
||||||
current_user={@current_user}
|
current_user={@current_user}
|
||||||
action={@live_action}
|
action={@live_action}
|
||||||
task={@task}
|
task={@task}
|
||||||
patch={~p"/"}
|
patch={~p"/tasks/#{@task}"}
|
||||||
/>
|
/>
|
||||||
</.modal>
|
</.modal>
|
||||||
|
|
||||||
|
|
@ -97,7 +91,7 @@ defmodule PutzplanWeb.TaskLive.Show do
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def mount(%{"id" => id}, _session, socket) do
|
def mount(%{"id" => id}, _session, socket) do
|
||||||
if connected?(socket), do: Phoenix.PubSub.subscribe(@pubsub_name, get_topic(id))
|
if connected?(socket), do: Putzplan.PubSub.subscribe_completed(id)
|
||||||
{:ok, socket}
|
{:ok, socket}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -127,8 +121,8 @@ defmodule PutzplanWeb.TaskLive.Show do
|
||||||
|
|
||||||
Ash.destroy!(completed_task, actor: socket.assigns.current_user)
|
Ash.destroy!(completed_task, actor: socket.assigns.current_user)
|
||||||
|
|
||||||
Phoenix.PubSub.broadcast!(@pubsub_name, get_topic(id), {:delete, completed_task})
|
Putzplan.PubSub.delete_completed_task(completed_task)
|
||||||
Phoenix.PubSub.broadcast!(@pubsub_name, "tasks", {:update, completed_task.tasks.id})
|
Putzplan.PubSub.update_task_by_id(completed_task.tasks.id)
|
||||||
|
|
||||||
{:noreply, socket}
|
{:noreply, socket}
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue