Skip to content

Commit e070986

Browse files
Merge pull request #11 from elixirschool/master_refactoring
Master refactoring
2 parents c2fc463 + a51d4af commit e070986

File tree

16 files changed

+55
-61
lines changed

16 files changed

+55
-61
lines changed

assets/js/socket.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,18 @@ let driving = false;
1010
// set up your syncDiff function using updateUsers as a callback
1111

1212
const startButton = document.querySelector('.start-button')
13-
startButton.addEventListener('click', e => {
13+
startButton.addEventListener('click', event => {
1414
driving = true;
1515
// send 'start_pointing' message to the channel here
1616
})
1717

18-
const nextCardButtons = document.getElementsByClassName('next-card')
19-
for (let i = 0;i < nextCardButtons.length; i++) {
20-
nextCardButtons[i].addEventListener('click', e => {
18+
document
19+
.querySelectorAll('.next-card')
20+
.forEach(elem => {
21+
elem.addEventListener('click', event => {
2122
// send 'finalized_points' message to the channel here
23+
})
2224
})
23-
}
2425

2526
document
2627
.querySelector('.calculate-points')
@@ -34,7 +35,7 @@ document
3435
// 'winner'
3536
// 'tie'
3637

37-
function showCard(state) {
38+
const showCard = state => {
3839
document
3940
.querySelector('.start-button')
4041
.style.display = "none"
@@ -58,7 +59,7 @@ function showCard(state) {
5859
.innerHTML = state.card.description
5960
}
6061

61-
function showWinner(state) {
62+
const showWinner = state => {
6263
document
6364
.querySelector('.winner')
6465
.style.display = "block"
@@ -76,7 +77,7 @@ function showWinner(state) {
7677
.disabled = !driving
7778
}
7879

79-
function showTie(state) {
80+
const showTie = state => {
8081
document
8182
.querySelector('.tie')
8283
.style.display = "block"

assets/js/users.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,26 @@ const usersElem = document.querySelector('.users')
55
const updateUsers = presence => {
66
usersElem.innerHTML = ''
77

8-
// let user = list presences with the help of a listBy function
9-
for(let i = 0; i < users.length; i++) {
10-
addUser(users[i])
11-
}
8+
// let users = list presences with the help of a listBy function
9+
users.forEach(user => addUser(user))
1210

1311
// implement a feature that
1412
// 1. checks if all fo the users in the present list have voted, i.e. have points values that are not nil
1513
// 2. displays the user's vote next to their name if so
1614
}
1715

18-
const listBy = (username, { metas: [{ points }, ..._rest]}) => {
16+
const listBy = (username, {metas: [{points}, ..._rest]}) => {
1917
// build out the listBy function so that it returns a list of users
2018
// where each user looks like this:
2119
// {username: username, points: points}
2220
}
2321

24-
const showPoints = usersElem => ({userId, points}) => {
22+
const showPoints = ({userId, points}) => {
2523
const userElem = document.querySelector(`.${userId}.user-estimate`)
2624
userElem.innerHTML = points
2725
}
2826

29-
function addUser(user) {
27+
const addUser = user => {
3028
const userElem = document.createElement('dt')
3129
userElem.appendChild(document.createTextNode(user.username))
3230
userElem.setAttribute('class', 'col-8')

assets/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
"watch": "webpack --mode development --watch"
77
},
88
"dependencies": {
9-
"ramda": "^0.26.1",
109
"phoenix": "file:../deps/phoenix",
11-
"phoenix_html": "file:../deps/phoenix_html"
10+
"phoenix_html": "file:../deps/phoenix_html",
11+
"ramda": "^0.26.1"
1212
},
1313
"devDependencies": {
1414
"@babel/core": "^7.0.0",

config/cards.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@ config :pointing_party,
3434
description: """
3535
Update our existing authentication flow to use the newly created Auth module.
3636
"""
37-
},
37+
}
3838
]

lib/pointing_party/account.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ defmodule PointingParty.Account do
1111

1212
def create(attrs) do
1313
changeset = changeset(%Account{}, attrs)
14+
1415
if changeset.valid? do
1516
account = apply_changes(changeset)
1617
{:ok, account}
@@ -19,7 +20,6 @@ defmodule PointingParty.Account do
1920
end
2021
end
2122

22-
@doc false
2323
def changeset(account, attrs \\ %{}) do
2424
account
2525
|> cast(attrs, [:username])

lib/pointing_party/card.ex

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ defmodule PointingParty.Card do
1313
timestamps()
1414
end
1515

16-
def points_range, do: @pointing_scale
17-
18-
@doc false
1916
def changeset(card, attrs) do
2017
card
2118
|> cast(attrs, [:title, :description, :points])
@@ -28,4 +25,6 @@ defmodule PointingParty.Card do
2825
|> Application.get_env(:cards)
2926
|> Enum.map(&struct(__MODULE__, &1))
3027
end
28+
29+
def points_range, do: @pointing_scale
3130
end

lib/pointing_party/vote_calculator.ex

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@ defmodule PointingParty.VoteCalculator do
1919
end
2020

2121
defp get_points(score_card, users) do
22-
votes =
23-
users
24-
|> Enum.map(fn {_username, %{metas: [%{points: points}]}} ->
25-
points
26-
end)
22+
votes = Enum.map(users, fn {_username, %{metas: [%{points: points}]}} -> points end)
2723

2824
update_score_card(score_card, :votes, votes)
2925
end

lib/pointing_party_web/channels/room_channel.ex

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
defmodule PointingPartyWeb.RoomChannel do
22
use PointingPartyWeb, :channel
3+
34
alias PointingParty.Card
5+
46
def join("room:lobby", _payload, socket) do
57
send(self(), :after_join)
68

@@ -9,29 +11,34 @@ defmodule PointingPartyWeb.RoomChannel do
911

1012
def handle_info(:after_join, socket) do
1113
# handle Presence listing and tracking here
14+
1215
{:noreply, socket}
1316
end
1417

1518
def handle_in("user_estimated", %{"points" => points}, socket) do
1619
# update votes for user presence
1720
# calculate votes if everyone voted with the help of the VoteCalculator
1821
# broadcast the 'winner'/'tie' event with a payload of %{points: points}
22+
1923
{:noreply, socket}
2024
end
2125

2226
def handle_in("finalized_points", %{"points" => points}, socket) do
2327
# update state by setting the current card to the next card
2428
# broadcast the "new_card" message with a payload of %{card: new_current_card}
29+
2530
{:reply, :ok, socket}
2631
end
2732

2833
def handle_in("start_pointing", _params, socket) do
2934
updated_socket = initialize_state(socket)
3035
# broadcast the "new_card" message with a payload of %{card: current_card}
36+
3137
{:reply, :ok, updated_socket}
3238
end
3339

3440
defp initialize_state(%{assigns: %{cards: _cards}} = socket), do: socket
41+
3542
defp initialize_state(socket) do
3643
[first | cards] = Card.cards()
3744

lib/pointing_party_web/controllers/session_controller.ex

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule PointingPartyWeb.SessionController do
22
use PointingPartyWeb, :controller
3-
alias PointingParty.Account.Auth
4-
alias PointingParty.Account
3+
4+
alias PointingParty.{Account, Account.Auth}
55

66
def new(conn, _params) do
77
changeset = Account.changeset(%Account{})
@@ -15,13 +15,16 @@ defmodule PointingPartyWeb.SessionController do
1515
|> put_session(:username, username)
1616
|> redirect(to: "/cards")
1717
|> halt()
18+
1819
{:error, changeset} ->
1920
render(conn, "new.html", changeset: changeset)
2021
end
2122
end
2223

2324
def delete(conn, _params) do
24-
clear_session(conn)
25-
|> redirect(to: "/login") |> halt()
25+
conn
26+
|> clear_session()
27+
|> redirect(to: "/login")
28+
|> halt()
2629
end
2730
end

lib/pointing_party_web/plugs/auth.ex

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@ defmodule PointingPartyWeb.Plugs.Auth do
66

77
def call(conn, _default) do
88
case authenticate(conn) do
9-
nil -> conn |> redirect(to: "/login") |> halt()
10-
username -> assign(conn, :username, username)
9+
nil ->
10+
conn
11+
|> redirect(to: "/login")
12+
|> halt()
13+
14+
username ->
15+
assign(conn, :username, username)
1116
end
1217
end
1318

0 commit comments

Comments
 (0)