Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions backend/api/v1/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ module.exports = cache => {
router.use('/files', require('./files')(cache))
router.use('/chat', require('./chat')(cache))
router.use('/podcasts', require('./podcasts')(cache))
router.use('/poll', require('./poll')(cache))

// lets map to the start of url request
router.use('/tse', require('./tse')(cache))
Expand Down
59 changes: 59 additions & 0 deletions backend/api/v1/poll.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import express from 'express'
import axios from 'axios'

const env = process.env.NODE_ENV === 'dev' ? 'dev' : 'prod'
const base_api = process.env.BASE_API

const getPoll = async (poll_id, user_id) => {
let url = base_api + `/poll?poll_id=${poll_id}&user_id=${user_id}`

try {
const pollInfo = await axios.get(url)
return pollInfo.data
} catch (err) {
console.log('Error while getting Poll Info.', err.name)
throw err
}
}

const votePoll = async data => {
const url = `${base_api}/poll/vote`

try {
const voteResult = await axios.post(url, data)
return voteResult.data
} catch (err) {
console.log('Error while updating vote.', err.name)
throw err
}
}

module.exports = cache => {
const router = express.Router()

router.get('/', async (req, res) => {
const poll_id = +req.query.poll_id
const user_id = +req.query.user_id

try {
const pollInfo = await getPoll(poll_id, user_id)

res.status(200).send(pollInfo);
} catch (err) {
res.status(500).send({ success: false, error: err })
}
})

router.post('/vote', async (req, res) => {
const data = req.body

try {
const voteResult = await votePoll(data)
res.status(200).send(voteResult)
} catch (err) {
res.status(500).send({success: false, error: err})
}
})

return router
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
"prettier": "1.12.1",
"prop-types": "^15.6.1",
"qs": "^6.5.1",
"rc-progress": "^2.2.5",
"rc-slider": "^5.3.5",
"rc-time-picker": "^3.2.1",
"react": "^15.6.2",
Expand Down
148 changes: 148 additions & 0 deletions shared/Home/Content/Poll/PollQuestion/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import React, { Component } from 'react'
import { connect } from 'react-redux'
import {
PollContainer,
PollTitle,
Question,
ChoiceList,
PollChoice,
PollLabel,
ViewVoteContainer,
ViewResultLink,
VoteContainer,
VoteButton,
VoteTick
} from "./style"
import Loading from '../../../../Common/Components/Loading'

class PollQuestion extends Component {
constructor(props) {
super(props)
this.state = { choice_id: 1 }
}

loadPoll = (poll_id, user_id) => {
const { dispatch } = this.props

dispatch({
type: 'GET_POLL',
payload: {
poll_id,
user_id,
dispatch
}
})
}

onChangeChoice = e => {
this.setState({
choice_id: parseInt(e.currentTarget.value)
})
}

onVote = () => {
const { poll_id, dispatch } = this.props
const user_id = -1
const question_id = -1
const { choice_id } = this.state
const data = {
poll_id: poll_id,
user_id: user_id,
question_id: question_id,
choice_id: choice_id
}

dispatch({
type: 'POST_POLL',
payload: {
data,
dispatch
}
})

this.onViewResult()
}

onViewResult = () => {
const { dispatch } = this.props

dispatch({
type: 'VIEW_POLL_RESULT',
})
}

componentWillMount() {
const poll_id = -1
const user_id = -1

this.loadPoll(poll_id, user_id)
}

renderPollChoice = results => {
const { choice_id } = this.state
return results.map((vote, i) => {
return (
<PollChoice key={i}>
<input
type='radio'
id={ `pollChoice${i + 1}` }
name='pollChoice'
value={i + 1}
checked={choice_id === i + 1}
onChange={this.onChangeChoice}/>
<label htmlFor={ `pollChoice${i + 1}` }>
<PollLabel>{vote.response}</PollLabel>
</label>
</PollChoice>
)
})
}

render() {
const { question, results, isLoading, loaded } = this.props

return (
<PollContainer>
<PollTitle>
Poll Question
</PollTitle>
<Question>
{question}
</Question>
{isLoading && <Loading />}
{loaded &&
<div>
<ChoiceList>
{this.renderPollChoice(results)}
</ChoiceList>
<ViewVoteContainer>
<div>
<ViewResultLink
onClick={this.onViewResult}
>
View result
</ViewResultLink>
</div>
<VoteContainer>
<span>Vote</span>
<VoteButton
onClick={this.onVote}
>
<VoteTick>&#10004;</VoteTick>
</VoteButton>
</VoteContainer>
</ViewVoteContainer>
</div>
}
</PollContainer>
)
}
}

export default connect(state => ({
isLoading: state.poll.get('loading'),
loaded: state.poll.get('loaded'),
poll_id: state.poll.get('poll_id'),
question: state.poll.get('question'),
results: state.poll.get('results').toJS()
}))(PollQuestion)
146 changes: 146 additions & 0 deletions shared/Home/Content/Poll/PollQuestion/style.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import styled from 'styled-components'

export const PollContainer = styled.div`
box-sizing: border-box;
background-color: #E4DCC6;
border: solid 5px white;
box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.5);
margin-top: 30px;
padding: 0 30px;
font-family: Helvetica Neue,Helvetica,Arial,sans-serif;
`

export const PollTitle = styled.div`
font-size: 26px;
color: #38383A;
margin-top: 40px;
`

export const Question = styled.div`
margin-top: 7px;
font-size: 20px;
color: #575959;
font-weight: normal;
`

export const ChoiceList = styled.div`
margin-top: 10px;
`

export const PollChoice = styled.div`
font-size: 20px;
height: 49px;
padding: 9px 0 0 15px;
border-bottom: 1px solid #CFC8B5;
& > input[type='radio'] {
display: none;
}
& > label {
position: relative;
}
& > input[type='radio']:checked + label ${PollLabel}::after {
background-color: #F0D943;
}
`

export const PollLabel = styled.span`
margin-left: 5px;
font-size: 20px;
color: #575959;
font-weight: normal;
&:hover {
cursor: pointer;
}
&:before {
content: '';
position: absolute;
top: 0;
bottom: 0;
margin: auto;
}
&:after {
content: '';
position: absolute;
top: 0;
bottom: 0;
margin: auto;
left: -15px;
width: 16px;
height: 16px;
border: solid 3px white;
border-radius: 8px;
box-shadow: 1px 1px 1px 1px #CFC8B5;
background-color: white;
transition: ease .25s, background-color .25s;
}
`

export const ViewVoteContainer = styled.div`
display: flex;
justify-content: space-between;
align-items: center;
margin-top: 20px;
margin-bottom: 17px;
`

export const ViewResultLink = styled.span`
font-size: 14px;
font-weight: normal;
color: #2E1453;
position: relative;

:hover {
cursor: pointer;
}

:before,
:after {
border-right: 2px solid;
content: '';
display: block;
height: 8px;
margin-top: -6px;
position: absolute;
transform: rotate(135deg);
right: -10px;
top: 50%;
width: 0;
}

:after {
margin-top: -1px;
transform: rotate(45deg);
}
`

export const VoteContainer = styled.div`
display: flex;
align-items: center;
`

export const VoteButton = styled.div`
width: 70px;
height: 70px;
border-radius: 35px;
background-color: #2E1453;
display: flex;
justify-content: center;
align-items: center;
margin-left: 11px;

:hover {
cursor: pointer;
}

& > span {
font-size: 16px;
font-weight: normal;
color: #333333;
}
`

export const VoteTick = styled.div`
color: #F0D943;
font-size: 18px;
font-weight: normal;
`
Loading