Skip to content

Commit e148d47

Browse files
provide channel helper function to save points and switch to next card
1 parent 7288f5a commit e148d47

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

lib/pointing_party_web/channels/room_channel.ex

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ defmodule PointingPartyWeb.RoomChannel do
3131
end
3232

3333
def handle_in("next_card", %{"points" => points}, socket) do
34-
# update the current card's `points` value to `points` from the message payload
35-
# update state by setting the current card to the next card
34+
save_vote_next_card(points, socket)
3635
# broadcast the "new_card" message with a payload of %{card: new_current_card}
3736

3837
{:reply, :ok, socket}
@@ -47,4 +46,24 @@ defmodule PointingPartyWeb.RoomChannel do
4746
|> assign(:unvoted, cards)
4847
|> assign(:current, first)
4948
end
49+
50+
defp save_vote_next_card(points, socket) do
51+
# save the points on the card
52+
latest_card =
53+
socket.assigns
54+
|> Map.get(:current)
55+
|> Map.put(:points, points)
56+
57+
# fetch the next card from the list of cards
58+
{next, remaining} =
59+
socket.assigns
60+
|> Map.get(:unvoted)
61+
|> List.pop_at(0)
62+
63+
# update socket state by moving the current card into `voted` and the next card into `current_card`
64+
socket
65+
|> assign(:unvoted, remaining)
66+
|> assign(:current, next)
67+
|> assign(:voted, [latest_card | socket.assigns[:voted]])
68+
end
5069
end

0 commit comments

Comments
 (0)