Skip to content

Commit

Permalink
Add status ready button
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonmestevao committed Apr 21, 2022
1 parent f32e98a commit 359b6a2
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 46 deletions.
13 changes: 13 additions & 0 deletions lib/karaokium/polling.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ defmodule Karaokium.Polling do
%Vote{}
|> Vote.changeset(attrs)
|> Repo.insert()
|> broadcast(:created)
end

@doc """
Expand Down Expand Up @@ -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
10 changes: 10 additions & 0 deletions lib/karaokium_web/live/admin/karaoke_live/show.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"

Expand Down
4 changes: 4 additions & 0 deletions lib/karaokium_web/live/admin/karaoke_live/show.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
<td><strong>PIN</strong></td>
<td><%= @karaoke.code %></td>
</tr>
<tr>
<td><strong>Status:</strong></td>
<td><%= @karaoke.status %></td>
</tr>
<tr>
<td><strong>Start date</strong></td>
<td><%= @karaoke.start_date %></td>
Expand Down
4 changes: 1 addition & 3 deletions lib/karaokium_web/live/karaoke_live/show.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
88 changes: 45 additions & 43 deletions lib/karaokium_web/live/karaoke_live/show.html.heex
Original file line number Diff line number Diff line change
@@ -1,46 +1,48 @@
<%= if @karaoke.status == :started do %>
<%= unless is_nil(@karaoke.performing_id) do %>
<section style="margin-top: -50px;">
<aside style="padding: 0;">
<img alt="HTML only" src={hd(@karaoke.performing.song.album.images).url} height="285" />
<div style="padding: 0 0.5rem;">
<h3><%= @karaoke.performing.song.name %></h3>
<p>
<strong><%= @karaoke.performing.song.album.name %></strong>
</p>
<p>
<em><%= Enum.map(@karaoke.performing.song.artists, & &1.name) |> Enum.join(", ") %></em>
</p>
<center>
<table style="padding: 0; margin-bottom: 10px;">
<tbody>
<tr>
<td><strong>Team</strong></td>
<td><%= @karaoke.performing.team.name || "No name" %></td>
</tr>
<tr>
<td><strong>Score</strong></td>
<td><%= score(@karaoke.performing) %></td>
</tr>
</tbody>
</table>
</center>
</div>
</aside>
</section>
<section style=""></section>
<div id="karaoke">
<%= if @karaoke.status == :started do %>
<%= unless is_nil(@karaoke.performing_id) do %>
<section style="margin-top: -50px;">
<aside style="padding: 0;">
<img alt="HTML only" src={hd(@karaoke.performing.song.album.images).url} height="285" />
<div style="padding: 0 0.5rem;">
<h3><%= @karaoke.performing.song.name %></h3>
<p>
<strong><%= @karaoke.performing.song.album.name %></strong>
</p>
<p>
<em><%= Enum.map(@karaoke.performing.song.artists, & &1.name) |> Enum.join(", ") %></em>
</p>
<center>
<table style="padding: 0; margin-bottom: 10px;">
<tbody>
<tr>
<td><strong>Team</strong></td>
<td><%= @karaoke.performing.team.name || "No name" %></td>
</tr>
<tr>
<td><strong>Score</strong></td>
<td><%= score(@karaoke.performing) %></td>
</tr>
</tbody>
</table>
</center>
</div>
</aside>
</section>
<section style=""></section>

<section>
<%= if @karaoke.performing.voting? and not has_voted?(@current_user, @karaoke.performing) do %>
<header>
<p>Vote now!</p>
</header>
<%= for pontuation <- 1..10 do %>
<button phx-click="vote" phx-value-pontuation={pontuation} style="margin: 10px;"><%= pontuation %></button>
<section>
<%= if @karaoke.performing.voting? and not has_voted?(@current_user, @karaoke.performing) do %>
<header>
<p>Vote now!</p>
</header>
<%= for pontuation <- 1..10 do %>
<button phx-click="vote" phx-value-pontuation={pontuation} style="margin: 10px;"><%= pontuation %></button>
<% end %>
<% end %>
<% end %>
</section>
</section>
<% end %>
<% else %>
<.status karaoke={@karaoke} />
<% end %>
<% else %>
<.status karaoke={@karaoke} />
<% end %>
</div>

0 comments on commit 359b6a2

Please sign in to comment.