Skip to content

Commit 8289f53

Browse files
authored
Merge pull request #6 from elixirschool/mts/track_points
Track user estimates
2 parents 6eca04c + e9fcb05 commit 8289f53

File tree

8 files changed

+4040
-30
lines changed

8 files changed

+4040
-30
lines changed

assets/js/socket.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
1-
import {Socket, Presence} from 'phoenix'
1+
import { Socket, Presence } from 'phoenix'
2+
import updateUsers from './users'
23

34
const socket = new Socket('/socket', {params: {username: window.pointingParty.username}})
45
socket.connect()
56

67
const channel = socket.channel('room:lobby', {})
78
const presence = new Presence(channel)
89

9-
presence.onSync(() => {
10-
const users = document.querySelector('.users')
11-
users.innerHTML = ''
10+
presence.onSync(() => updateUsers(presence))
1211

13-
presence.list((id, _) => {
14-
const user = document.createElement('li')
15-
user.setAttribute('class', id)
16-
user.appendChild(document.createTextNode(id))
17-
users.appendChild(user)
18-
})
19-
})
12+
if (window.pointingParty.username) {
13+
channel.join ()
14+
.receive('ok', resp => { console.log('Joined successfully', resp) })
15+
.receive('error', resp => { console.log('Unable to join', resp) })
16+
}
2017

21-
channel.join()
22-
.receive('ok', resp => { console.log('Joined successfully', resp) })
23-
.receive('error', resp => { console.log('Unable to join', resp) })
18+
const calculateButton = document.querySelector('.calculate-points')
19+
calculateButton.addEventListener('click', event => {
20+
const storyPoints = document.querySelector('.story-points')
21+
channel.push('user_estimated', { points: storyPoints.value })
22+
})
2423

2524
export default socket

assets/js/users.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { forEach, isNil, map, none } from 'ramda'
2+
3+
const updateUsers = presence => {
4+
const usersElem = document.querySelector('.users')
5+
usersElem.innerHTML = ''
6+
7+
const users = presence.list(userData)
8+
forEach(addUser(usersElem))(users)
9+
10+
if (allHaveEstimated(users)) {
11+
forEach(showPoints(usersElem))(users)
12+
}
13+
}
14+
15+
const userData = (userId, { metas: [{ points }, ..._rest]}) => ({ userId, points })
16+
17+
const showPoints = usersElem => ({userId, points}) => {
18+
const userElem = document.querySelector(`.${userId}.user-estimate`)
19+
userElem.innerHTML = points
20+
}
21+
22+
const addUser = usersElem => ({userId, points}) => {
23+
const userElem = document.createElement('dt')
24+
userElem.appendChild(document.createTextNode(userId))
25+
userElem.setAttribute('class', 'col-8')
26+
27+
const estimateElem = document.createElement('dd')
28+
estimateElem.setAttribute('class', `${userId} user-estimate col-4`)
29+
30+
usersElem.appendChild(userElem)
31+
usersElem.appendChild(estimateElem)
32+
}
33+
34+
const allHaveEstimated = users => {
35+
const pointsCollection = map(({ points }) => points)(users)
36+
37+
return none(isNil)(pointsCollection)
38+
}
39+
40+
export default updateUsers

assets/package-lock.json

Lines changed: 8 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"watch": "webpack --mode development --watch"
77
},
88
"dependencies": {
9+
"ramda": "^0.26.1",
910
"phoenix": "file:../deps/phoenix",
1011
"phoenix_html": "file:../deps/phoenix_html"
1112
},

lib/pointing_party_web/channels/room_channel.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,10 @@ defmodule PointingPartyWeb.RoomChannel do
1515

1616
{:noreply, socket}
1717
end
18+
19+
def handle_in("user_estimated", %{"points" => points}, socket) do
20+
Presence.update(socket, socket.assigns.username, &(Map.put(&1, :points, points)))
21+
22+
{:noreply, socket}
23+
end
1824
end
Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<div class="row">
2-
<div class="col-md-10">
2+
<div class="col-10">
33
<div class="card text-left">
44
<div class="card-header">
55
<h2><%= @card.title %></h2>
@@ -9,20 +9,16 @@
99
<div class="form-group text-left">
1010
<div class="form-row align-items-center">
1111
<div class="col-2">
12-
<label for="storyPoints">Story Points</label>
13-
<select class="form-control" id="storyPoints">
12+
<label for="story-points">Story Points</label>
13+
<select class="form-control story-points" id="story-points">
1414
<%= Enum.map(@points, fn point -> %>
15-
<%= if @card.points == point do %>
16-
<option selected="selected'"><%= point %></option>
17-
<% else %>
18-
<option><%= point %></option>
19-
<% end %>
15+
<option><%= point %></option>
2016
<% end) %>
2117
</select>
2218
</div>
2319
</div>
2420
</div>
25-
<a href="#" class="btn btn-primary">Calculate Points</a>
21+
<a href="#" class="btn btn-primary calculate-points">Calculate Points</a>
2622
</div>
2723

2824
<div class="card-footer text-muted">
@@ -31,9 +27,9 @@
3127
</div>
3228
</div>
3329

34-
<div class="col-md-2">
30+
<div class="col-2">
3531
<h2>Users</h2>
36-
<ul class="users">
37-
</ul>
32+
<dl class="row users">
33+
</dl>
3834
</div>
3935
</div>

package-lock.json

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)