Skip to content

Commit 25ae92d

Browse files
wip refactor
1 parent e148d47 commit 25ae92d

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

assets/js/socket.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ document
1919
.querySelectorAll('.next-card')
2020
.forEach(elem => {
2121
elem.addEventListener('click', event => {
22-
// send 'finalized_points' message to the channel here
22+
channel.push('next_card', {points: e.target.value})
2323
})
2424
})
2525

lib/pointing_party_web/channels/room_channel.ex

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,49 @@ defmodule PointingPartyWeb.RoomChannel do
1515
{:noreply, socket}
1616
end
1717

18+
def handle_in("user_estimated", %{"points" => points}, socket) do
19+
Presence.update(socket, socket.assigns.username, &(Map.put(&1, :points, points)))
20+
21+
if everyone_voted?(socket) do
22+
calculate_story_points(socket)
23+
end
24+
25+
{:noreply, socket}
26+
end
27+
28+
def handle_in("next_card", %{"points" => points}, socket) do
29+
updated_socket = save_vote_next_card(points, socket)
30+
broadcast!(updated_socket, "new_card", %{card: current_card(updated_socket)})
31+
{:reply, :ok, updated_socket}
32+
end
33+
1834
def handle_in("start_pointing", _params, socket) do
1935
updated_socket = initialize_state(socket)
2036
# broadcast the "new_card" message with a payload of %{card: current_card}
2137

2238
{:reply, :ok, updated_socket}
2339
end
2440

25-
def handle_in("user_estimated", %{"points" => points}, socket) do
26-
# update votes for user presence
27-
# if everyone voted, calculate story point estimate with the help of the VoteCalculator
28-
# broadcast the 'winner'/'tie' event with a payload of %{points: points}
41+
42+
defp current_card(socket) do
43+
socket.assigns
44+
|> Map.get(:current)
45+
|> Map.from_struct()
46+
|> Map.drop([:__meta__])
47+
end
2948

30-
{:noreply, socket}
49+
defp everyone_voted?(socket) do
50+
socket
51+
|> Presence.list()
52+
|> Enum.map(fn {_username, %{metas: [metas]}} -> Map.get(metas, :points) end)
53+
|> Enum.all?(&(not is_nil(&1)))
3154
end
3255

33-
def handle_in("next_card", %{"points" => points}, socket) do
34-
save_vote_next_card(points, socket)
35-
# broadcast the "new_card" message with a payload of %{card: new_current_card}
56+
defp calculate_story_points(socket) do
57+
current_users = Presence.list(socket)
3658

37-
{:reply, :ok, socket}
59+
{event, points} = VoteCalculator.calculate_votes(current_users)
60+
broadcast!(socket, event, %{points: points})
3861
end
3962

4063
defp initialize_state(%{assigns: %{cards: _cards}} = socket), do: socket

0 commit comments

Comments
 (0)