diff --git a/lib/karaokium/polling.ex b/lib/karaokium/polling.ex index 4169b77..cc685f9 100644 --- a/lib/karaokium/polling.ex +++ b/lib/karaokium/polling.ex @@ -53,6 +53,7 @@ defmodule Karaokium.Polling do %Vote{} |> Vote.changeset(attrs) |> Repo.insert() + |> broadcast(:created) end @doc """ @@ -101,4 +102,16 @@ defmodule Karaokium.Polling do def change_vote(%Vote{} = vote, attrs \\ %{}) do Vote.changeset(vote, attrs) end + + def subscribe(topic) when topic in ["votes"] do + Phoenix.PubSub.subscribe(Karaokium.PubSub, topic) + end + + defp broadcast({:error, _reason} = error, _event), do: error + + defp broadcast({:ok, %Vote{} = vote}, event) + when event in [:created] do + Phoenix.PubSub.broadcast!(Karaokium.PubSub, "votes", {event, vote}) + {:ok, vote} + end end diff --git a/lib/karaokium_web/live/admin/karaoke_live/show.ex b/lib/karaokium_web/live/admin/karaoke_live/show.ex index e55227b..9fffeb1 100644 --- a/lib/karaokium_web/live/admin/karaoke_live/show.ex +++ b/lib/karaokium_web/live/admin/karaoke_live/show.ex @@ -3,9 +3,14 @@ defmodule KaraokiumWeb.Admin.KaraokeLive.Show do alias Karaokium.Events alias Karaokium.Performances + alias Karaokium.Polling @impl true def mount(_params, _session, socket) do + if connected?(socket) do + Polling.subscribe("votes") + end + {:ok, socket} end @@ -50,6 +55,11 @@ defmodule KaraokiumWeb.Admin.KaraokeLive.Show do {:noreply, reload(socket)} end + @impl true + def handle_info({:update, _changes}, socket) do + {:noreply, reload(socket)} + end + defp page_title(:show), do: "Show Karaoke" defp page_title(:edit), do: "Edit Karaoke" diff --git a/lib/karaokium_web/live/admin/karaoke_live/show.html.heex b/lib/karaokium_web/live/admin/karaoke_live/show.html.heex index c4f2c36..37677d2 100644 --- a/lib/karaokium_web/live/admin/karaoke_live/show.html.heex +++ b/lib/karaokium_web/live/admin/karaoke_live/show.html.heex @@ -33,6 +33,10 @@ PIN <%= @karaoke.code %> + + Status: + <%= @karaoke.status %> + Start date <%= @karaoke.start_date %> diff --git a/lib/karaokium_web/live/karaoke_live/show.ex b/lib/karaokium_web/live/karaoke_live/show.ex index 19ac339..85d550d 100644 --- a/lib/karaokium_web/live/karaoke_live/show.ex +++ b/lib/karaokium_web/live/karaoke_live/show.ex @@ -40,9 +40,7 @@ defmodule KaraokiumWeb.KaraokeLive.Show do defp reload(socket) do id = socket.assigns.id - karaoke = - Events.get_karaoke!(id, performing: [:team, :votes, song: [:album, :artists]]) - |> Map.put(:status, :started) + karaoke = Events.get_karaoke!(id, performing: [:team, :votes, song: [:album, :artists]]) socket |> assign(:page_title, karaoke.name) diff --git a/lib/karaokium_web/live/karaoke_live/show.html.heex b/lib/karaokium_web/live/karaoke_live/show.html.heex index b1d35cd..af55ccf 100644 --- a/lib/karaokium_web/live/karaoke_live/show.html.heex +++ b/lib/karaokium_web/live/karaoke_live/show.html.heex @@ -1,46 +1,48 @@ -<%= if @karaoke.status == :started do %> - <%= unless is_nil(@karaoke.performing_id) do %> -
- -
-
+
+ <%= if @karaoke.status == :started do %> + <%= unless is_nil(@karaoke.performing_id) do %> +
+ +
+
-
- <%= if @karaoke.performing.voting? and not has_voted?(@current_user, @karaoke.performing) do %> -
-

Vote now!

-
- <%= for pontuation <- 1..10 do %> - +
+ <%= if @karaoke.performing.voting? and not has_voted?(@current_user, @karaoke.performing) do %> +
+

Vote now!

+
+ <%= for pontuation <- 1..10 do %> + + <% end %> <% end %> - <% end %> -
+
+ <% end %> + <% else %> + <.status karaoke={@karaoke} /> <% end %> -<% else %> - <.status karaoke={@karaoke} /> -<% end %> +