Skip to content

Commit

Permalink
make migration, schema for Vote table and start changing vote submitted
Browse files Browse the repository at this point in the history
functionality
  • Loading branch information
qlaire committed Aug 16, 2017
1 parent 606b2f1 commit a3cbfd4
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 10 deletions.
18 changes: 18 additions & 0 deletions priv/repo/migrations/20170815184450_add_vote_table.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
defmodule RemoteRetro.Repo.Migrations.AddVoteTable do
use Ecto.Migration

def change do
create table(:votes) do
add :user_id, references(:users, [
column: :id,
on_delete: :nothing
])
add :idea_id, references(:ideas, [
column: :id,
on_delete: :nothing
])

timestamps()
end
end
end
15 changes: 10 additions & 5 deletions web/channels/retro_channel.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule RemoteRetro.RetroChannel do
use RemoteRetro.Web, :channel

alias RemoteRetro.{Presence, PresenceUtils, Idea, Emails, Mailer, Retro}
alias RemoteRetro.{Presence, PresenceUtils, Idea, Emails, Mailer, Retro, Vote}

def join("retro:" <> retro_id, _, socket) do
socket = assign(socket, :retro_id, retro_id)
Expand Down Expand Up @@ -76,11 +76,16 @@ defmodule RemoteRetro.RetroChannel do
{:noreply, socket}
end

def handle_in("submit_vote", %{"id" => id}, socket) do
query = from i in Idea, where: i.id == ^id
{_row_count, [updated_idea]} = Repo.update_all(query, [inc: [vote_count: 1]], returning: true)
def handle_in("submit_vote", %{"ideaId" => idea_id, "userId" => user_id}, socket) do
vote =
%Vote{
idea_id: idea_id,
user_id: user_id
}
|> Vote.changeset
|> Repo.insert!

broadcast! socket, "vote_submitted", updated_idea
broadcast! socket, "vote_submitted", vote
{:noreply, socket}
end

Expand Down
1 change: 1 addition & 0 deletions web/models/idea.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ defmodule RemoteRetro.Idea do

belongs_to :retro, RemoteRetro.Retro, type: Ecto.UUID
belongs_to :user, RemoteRetro.User
has_many :vote, RemoteRetro.Vote

timestamps(type: :utc_datetime)
end
Expand Down
1 change: 1 addition & 0 deletions web/models/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ defmodule RemoteRetro.User do
field :last_login, Ecto.DateTime

has_many :participations, RemoteRetro.Participation
has_many :votes, RemoteRetro.Vote

timestamps(type: :utc_datetime)
end
Expand Down
19 changes: 19 additions & 0 deletions web/models/vote.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
defmodule RemoteRetro.Vote do
use RemoteRetro.Web, :model

@derive {Poison.Encoder, except: [:__meta__]}
schema "votes" do
belongs_to :user, RemoteRetro.User
belongs_to :idea, RemoteRetro.Idea

timestamps(type: :utc_datetime)
end

@required_fields [:user_id, :idea_id]

def changeset(struct, params \\ %{}) do
struct
|> cast(params, @required_fields)
|> validate_required(@required_fields)
end
end
6 changes: 5 additions & 1 deletion web/static/js/components/idea_controls.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ const IdeaControls = props => {
function renderIcons() {
if (stage !== "idea-generation" && category !== "action-item") {
return (
<VoteCounter retroChannel={retroChannel} idea={idea} />
<VoteCounter
retroChannel={retroChannel}
idea={idea}
currentUser={currentUser}
/>
)
}
if (currentUser.is_facilitator) {
Expand Down
5 changes: 3 additions & 2 deletions web/static/js/components/vote_counter.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class VoteCounter extends React.Component {
}

handleClick() {
const { idea, retroChannel } = this.props
retroChannel.push("submit_vote", { id: idea.id })
const { idea, retroChannel, currentUser } = this.props
retroChannel.push("submit_vote", { ideaId: idea.id, userId: currentUser.id })
}

render() {
Expand All @@ -32,6 +32,7 @@ class VoteCounter extends React.Component {
VoteCounter.propTypes = {
retroChannel: AppPropTypes.retroChannel.isRequired,
idea: AppPropTypes.idea.isRequired,
currentUser: AppPropTypes.user.isRequired,
}

export default VoteCounter
2 changes: 2 additions & 0 deletions web/static/js/reducers/ideas.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// const updateIdea = state

const ideas = (state = [], action) => {
switch (action.type) {
case "SET_INITIAL_STATE":
Expand Down
5 changes: 3 additions & 2 deletions web/static/js/services/retro_channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ const applyListenerCallbacks = (retroChannel, store, actions) => {
actions.deleteIdea(deletedIdea.id)
})

retroChannel.on("vote_submitted", votedOnIdea => {
actions.updateIdea(votedOnIdea.id, { vote_count: votedOnIdea.vote_count })
retroChannel.on("vote_submitted", ideaId => {
// actions.updateIdea(votedOnIdea.id, { vote_count: votedOnIdea.vote_count })
console.log("in vote submitted", ideaId)
})

retroChannel.on("idea_highlighted", highlightedIdea => {
Expand Down

0 comments on commit a3cbfd4

Please sign in to comment.