Skip to content

Commit

Permalink
Merge pull request #11 from elixirschool/master_refactoring
Browse files Browse the repository at this point in the history
Master refactoring
  • Loading branch information
SophieDeBenedetto authored Aug 18, 2019
2 parents c2fc463 + a51d4af commit e070986
Show file tree
Hide file tree
Showing 16 changed files with 55 additions and 61 deletions.
17 changes: 9 additions & 8 deletions assets/js/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@ let driving = false;
// set up your syncDiff function using updateUsers as a callback

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

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

document
.querySelector('.calculate-points')
Expand All @@ -34,7 +35,7 @@ document
// 'winner'
// 'tie'

function showCard(state) {
const showCard = state => {
document
.querySelector('.start-button')
.style.display = "none"
Expand All @@ -58,7 +59,7 @@ function showCard(state) {
.innerHTML = state.card.description
}

function showWinner(state) {
const showWinner = state => {
document
.querySelector('.winner')
.style.display = "block"
Expand All @@ -76,7 +77,7 @@ function showWinner(state) {
.disabled = !driving
}

function showTie(state) {
const showTie = state => {
document
.querySelector('.tie')
.style.display = "block"
Expand Down
12 changes: 5 additions & 7 deletions assets/js/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,26 @@ const usersElem = document.querySelector('.users')
const updateUsers = presence => {
usersElem.innerHTML = ''

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

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

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

const showPoints = usersElem => ({userId, points}) => {
const showPoints = ({userId, points}) => {
const userElem = document.querySelector(`.${userId}.user-estimate`)
userElem.innerHTML = points
}

function addUser(user) {
const addUser = user => {
const userElem = document.createElement('dt')
userElem.appendChild(document.createTextNode(user.username))
userElem.setAttribute('class', 'col-8')
Expand Down
4 changes: 2 additions & 2 deletions assets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
"watch": "webpack --mode development --watch"
},
"dependencies": {
"ramda": "^0.26.1",
"phoenix": "file:../deps/phoenix",
"phoenix_html": "file:../deps/phoenix_html"
"phoenix_html": "file:../deps/phoenix_html",
"ramda": "^0.26.1"
},
"devDependencies": {
"@babel/core": "^7.0.0",
Expand Down
2 changes: 1 addition & 1 deletion config/cards.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ config :pointing_party,
description: """
Update our existing authentication flow to use the newly created Auth module.
"""
},
}
]
2 changes: 1 addition & 1 deletion lib/pointing_party/account.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ defmodule PointingParty.Account do

def create(attrs) do
changeset = changeset(%Account{}, attrs)

if changeset.valid? do
account = apply_changes(changeset)
{:ok, account}
Expand All @@ -19,7 +20,6 @@ defmodule PointingParty.Account do
end
end

@doc false
def changeset(account, attrs \\ %{}) do
account
|> cast(attrs, [:username])
Expand Down
5 changes: 2 additions & 3 deletions lib/pointing_party/card.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ defmodule PointingParty.Card do
timestamps()
end

def points_range, do: @pointing_scale

@doc false
def changeset(card, attrs) do
card
|> cast(attrs, [:title, :description, :points])
Expand All @@ -28,4 +25,6 @@ defmodule PointingParty.Card do
|> Application.get_env(:cards)
|> Enum.map(&struct(__MODULE__, &1))
end

def points_range, do: @pointing_scale
end
6 changes: 1 addition & 5 deletions lib/pointing_party/vote_calculator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ defmodule PointingParty.VoteCalculator do
end

defp get_points(score_card, users) do
votes =
users
|> Enum.map(fn {_username, %{metas: [%{points: points}]}} ->
points
end)
votes = Enum.map(users, fn {_username, %{metas: [%{points: points}]}} -> points end)

update_score_card(score_card, :votes, votes)
end
Expand Down
7 changes: 7 additions & 0 deletions lib/pointing_party_web/channels/room_channel.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
defmodule PointingPartyWeb.RoomChannel do
use PointingPartyWeb, :channel

alias PointingParty.Card

def join("room:lobby", _payload, socket) do
send(self(), :after_join)

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

def handle_info(:after_join, socket) do
# handle Presence listing and tracking here

{:noreply, socket}
end

def handle_in("user_estimated", %{"points" => points}, socket) do
# update votes for user presence
# calculate votes if everyone voted with the help of the VoteCalculator
# broadcast the 'winner'/'tie' event with a payload of %{points: points}

{:noreply, socket}
end

def handle_in("finalized_points", %{"points" => points}, socket) do
# update state by setting the current card to the next card
# broadcast the "new_card" message with a payload of %{card: new_current_card}

{:reply, :ok, socket}
end

def handle_in("start_pointing", _params, socket) do
updated_socket = initialize_state(socket)
# broadcast the "new_card" message with a payload of %{card: current_card}

{:reply, :ok, updated_socket}
end

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

defp initialize_state(socket) do
[first | cards] = Card.cards()

Expand Down
11 changes: 7 additions & 4 deletions lib/pointing_party_web/controllers/session_controller.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule PointingPartyWeb.SessionController do
use PointingPartyWeb, :controller
alias PointingParty.Account.Auth
alias PointingParty.Account

alias PointingParty.{Account, Account.Auth}

def new(conn, _params) do
changeset = Account.changeset(%Account{})
Expand All @@ -15,13 +15,16 @@ defmodule PointingPartyWeb.SessionController do
|> put_session(:username, username)
|> redirect(to: "/cards")
|> halt()

{:error, changeset} ->
render(conn, "new.html", changeset: changeset)
end
end

def delete(conn, _params) do
clear_session(conn)
|> redirect(to: "/login") |> halt()
conn
|> clear_session()
|> redirect(to: "/login")
|> halt()
end
end
9 changes: 7 additions & 2 deletions lib/pointing_party_web/plugs/auth.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ defmodule PointingPartyWeb.Plugs.Auth do

def call(conn, _default) do
case authenticate(conn) do
nil -> conn |> redirect(to: "/login") |> halt()
username -> assign(conn, :username, username)
nil ->
conn
|> redirect(to: "/login")
|> halt()

username ->
assign(conn, :username, username)
end
end

Expand Down
10 changes: 5 additions & 5 deletions lib/pointing_party_web/templates/card/index.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
<div class="col-2">
<label for="story-points">Story Points</label>
<select class="form-control story-points" id="story-points">
<option>0</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>5</option>
</select>
Expand All @@ -24,12 +24,12 @@
<a href="#" class="btn btn-primary calculate-points">Vote!</a>
<div class="winner" style="display: none;">
<p class="final-points"></p>
<button disabled=true class="btn btn-primary next-card" value="">Next Card</a>
<button disabled=true class="btn btn-primary next-card" value="">Next Card</button>
</div>
<div class="tie" style="display: none;">
<p class="card-text"> TIE! </p>
<button disabled=true class="btn btn-primary next-card" value=""></a>
<button disabled=true class="btn btn-primary next-card" value=""></a>
<p class="card-text">TIE!</p>
<button disabled=true class="btn btn-primary next-card" value=""></button>
<button disabled=true class="btn btn-primary next-card" value=""></button>
</div>
</div>
</div>
Expand Down
11 changes: 5 additions & 6 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,15 @@ defmodule PointingParty.MixProject do
# Type `mix help deps` for examples and options.
defp deps do
[
{:ecto, "~> 3.1"},
{:gettext, "~> 0.11"},
{:jason, "~> 1.0"},
{:phoenix, "~> 1.4.9"},
{:phoenix_pubsub, "~> 1.1"},
{:phoenix_ecto, "~> 4.0"},
{:ecto, "~> 3.1"},
{:phoenix_html, "~> 2.11"},
{:phoenix_live_reload, "~> 1.2", only: :dev},
{:gettext, "~> 0.11"},
{:jason, "~> 1.0"},
{:plug_cowboy, "~> 2.0"},
{:ex_machina, "~> 2.3", only: :test}
{:phoenix_pubsub, "~> 1.1"},
{:plug_cowboy, "~> 2.0"}
]
end
end
Loading

0 comments on commit e070986

Please sign in to comment.