Skip to content

Commit 440c5f1

Browse files
refactor
1 parent 967667c commit 440c5f1

File tree

5 files changed

+58
-40
lines changed

5 files changed

+58
-40
lines changed

assets/js/socket.js

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,50 @@ import updateUsers from './users'
44
const socket = new Socket('/socket', {params: {username: window.pointingParty.username}})
55
socket.connect()
66

7+
const channel = socket.channel('room:lobby', {})
8+
const presence = new Presence(channel)
9+
10+
presence.onSync(() => updateUsers(presence))
711
let driving = false;
812

9-
// connect to Presence here
10-
// set up your syncDiff function using updateUsers as a callback
13+
if (window.pointingParty.username) {
14+
channel.join()
15+
.receive('ok', resp => { console.log('Joined successfully', resp) })
16+
.receive('error', resp => { console.log('Unable to join', resp) })
17+
}
1118

1219
const startButton = document.querySelector('.start-button')
1320
startButton.addEventListener('click', event => {
1421
driving = true;
15-
// send 'start_pointing' message to the channel here
22+
channel.push('start_pointing', {})
1623
})
1724

1825
document
1926
.querySelectorAll('.next-card')
2027
.forEach(elem => {
2128
elem.addEventListener('click', event => {
22-
channel.push('next_card', {points: e.target.value})
29+
channel.push('next_card', {points: event.target.value})
2330
})
2431
})
2532

2633
document
2734
.querySelector('.calculate-points')
2835
.addEventListener('click', event => {
2936
const storyPoints = document.querySelector('.story-points')
30-
// send 'user_estimated' to the channel here
37+
channel.push('user_estimated', { points: storyPoints.value })
3138
})
3239

33-
// call the relevant function defined below when you receive the following events from the channel:
34-
// 'next_card'
35-
// 'winner'
36-
// 'tie'
40+
channel.on('new_card', state => {
41+
showCard(state)
42+
})
43+
44+
channel.on('winner', state => {
45+
showWinner(state)
46+
})
47+
48+
channel.on('tie', state => {
49+
showTie(state)
50+
})
3751

3852
const showCard = state => {
3953
document

assets/js/users.js

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,40 @@
11
import { forEach, isNil, map, none } from 'ramda'
22

3-
const usersElem = document.querySelector('.users')
4-
53
const updateUsers = presence => {
4+
const usersElem = document.querySelector('.users')
65
usersElem.innerHTML = ''
76

8-
// let users = list presences with the help of a listBy function
9-
users.forEach(user => addUser(user))
7+
const users = presence.list(userData)
8+
forEach(addUser(usersElem))(users)
109

11-
// implement a feature that
12-
// 1. checks if all fo the users in the present list have voted, i.e. have points values that are not nil
13-
// 2. displays the user's vote next to their name if so
10+
if (allHaveEstimated(users)) {
11+
forEach(showPoints(usersElem))(users)
12+
}
1413
}
1514

16-
const listBy = (username, {metas: [{points}, ..._rest]}) => {
17-
// build out the listBy function so that it returns a list of users
18-
// where each user looks like this:
19-
// {username: username, points: points}
20-
}
15+
const userData = (userId, { metas: [{ points }, ..._rest]}) => ({ userId, points })
2116

22-
const showPoints = ({userId, points}) => {
17+
const showPoints = usersElem => ({userId, points}) => {
2318
const userElem = document.querySelector(`.${userId}.user-estimate`)
2419
userElem.innerHTML = points
2520
}
2621

27-
const addUser = user => {
22+
const addUser = usersElem => ({userId, points}) => {
2823
const userElem = document.createElement('dt')
29-
userElem.appendChild(document.createTextNode(user.username))
24+
userElem.appendChild(document.createTextNode(userId))
3025
userElem.setAttribute('class', 'col-8')
3126

3227
const estimateElem = document.createElement('dd')
33-
estimateElem.setAttribute('class', `${user.username} user-estimate col-4`)
28+
estimateElem.setAttribute('class', `${userId} user-estimate col-4`)
3429

3530
usersElem.appendChild(userElem)
3631
usersElem.appendChild(estimateElem)
3732
}
3833

34+
const allHaveEstimated = users => {
35+
const pointsCollection = map(({ points }) => points)(users)
36+
37+
return none(isNil)(pointsCollection)
38+
}
39+
3940
export default updateUsers

lib/pointing_party_web/channels/room_channel.ex

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
defmodule PointingPartyWeb.RoomChannel do
22
use PointingPartyWeb, :channel
33

4-
alias PointingParty.Card
4+
alias PointingParty.{Card, VoteCalculator}
5+
alias PointingPartyWeb.Presence
56

67
def join("room:lobby", _payload, socket) do
78
send(self(), :after_join)
@@ -10,8 +11,8 @@ defmodule PointingPartyWeb.RoomChannel do
1011
end
1112

1213
def handle_info(:after_join, socket) do
13-
# handle Presence listing and tracking here
14-
14+
push(socket, "presence_state", Presence.list(socket))
15+
{:ok, _} = Presence.track(socket, socket.assigns.username, %{})
1516
{:noreply, socket}
1617
end
1718

@@ -33,12 +34,18 @@ defmodule PointingPartyWeb.RoomChannel do
3334

3435
def handle_in("start_pointing", _params, socket) do
3536
updated_socket = initialize_state(socket)
36-
# broadcast the "new_card" message with a payload of %{card: current_card}
37-
37+
broadcast!(updated_socket, "new_card", %{card: current_card(updated_socket)})
3838
{:reply, :ok, updated_socket}
3939
end
4040

41-
41+
intercept ["new_card"]
42+
43+
def handle_out("new_card", payload, socket) do
44+
Presence.update(socket, socket.assigns.username, &(Map.put(&1, :points, nil)))
45+
push(socket, "new_card", payload)
46+
{:noreply, socket}
47+
end
48+
4249
defp current_card(socket) do
4350
socket.assigns
4451
|> Map.get(:current)

lib/pointing_party_web/channels/user_socket.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ defmodule PointingPartyWeb.UserSocket do
1515
#
1616
# See `Phoenix.Token` documentation for examples in
1717
# performing token verification on connect.
18-
def connect(_params, socket, _connect_info) do
19-
{:ok, socket}
20-
end
18+
def connect(%{"username" => username}, socket, _connect_info) do
19+
{:ok, assign(socket, :username, username)}
20+
end
2121

2222
# Socket id's are topics that allow you to identify all sockets for a given user:
2323
#

0 commit comments

Comments
 (0)