2020-11-15 18:48:32 +00:00
|
|
|
import api from '../api'
|
|
|
|
import { importFetchedPoll } from './importer'
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
export const POLL_VOTE_REQUEST = 'POLL_VOTE_REQUEST'
|
|
|
|
export const POLL_VOTE_SUCCESS = 'POLL_VOTE_SUCCESS'
|
|
|
|
export const POLL_VOTE_FAIL = 'POLL_VOTE_FAIL'
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
export const POLL_FETCH_REQUEST = 'POLL_FETCH_REQUEST'
|
|
|
|
export const POLL_FETCH_SUCCESS = 'POLL_FETCH_SUCCESS'
|
|
|
|
export const POLL_FETCH_FAIL = 'POLL_FETCH_FAIL'
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2019-07-02 08:10:25 +01:00
|
|
|
export const vote = (pollId, choices) => (dispatch, getState) => {
|
2020-11-15 18:48:32 +00:00
|
|
|
dispatch(voteRequest())
|
2019-07-02 08:10:25 +01:00
|
|
|
|
|
|
|
api(getState).post(`/api/v1/polls/${pollId}/votes`, { choices })
|
|
|
|
.then(({ data }) => {
|
2020-11-15 18:48:32 +00:00
|
|
|
dispatch(importFetchedPoll(data))
|
|
|
|
dispatch(voteSuccess(data))
|
2019-07-02 08:10:25 +01:00
|
|
|
})
|
2020-11-15 18:48:32 +00:00
|
|
|
.catch(err => dispatch(voteFail(err)))
|
|
|
|
}
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
const voteRequest = () => ({
|
2019-07-02 08:10:25 +01:00
|
|
|
type: POLL_VOTE_REQUEST,
|
2020-11-15 18:48:32 +00:00
|
|
|
})
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
const voteSuccess = (poll) => ({
|
2019-07-02 08:10:25 +01:00
|
|
|
type: POLL_VOTE_SUCCESS,
|
2020-12-08 04:39:13 +00:00
|
|
|
showToast: true,
|
2019-07-02 08:10:25 +01:00
|
|
|
poll,
|
2020-11-15 18:48:32 +00:00
|
|
|
})
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
const voteFail = (error) => ({
|
2019-07-02 08:10:25 +01:00
|
|
|
type: POLL_VOTE_FAIL,
|
2020-12-08 04:39:13 +00:00
|
|
|
showToast: true,
|
2019-07-02 08:10:25 +01:00
|
|
|
error,
|
2020-11-15 18:48:32 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
export const fetchPoll = pollId => (dispatch, getState) => {
|
|
|
|
dispatch(fetchPollRequest())
|
|
|
|
|
|
|
|
api(getState).get(`/api/v1/polls/${pollId}`)
|
|
|
|
.then(({ data }) => {
|
|
|
|
dispatch(importFetchedPoll(data))
|
|
|
|
dispatch(fetchPollSuccess(data))
|
|
|
|
})
|
|
|
|
.catch(err => dispatch(fetchPollFail(err)))
|
|
|
|
}
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
const fetchPollRequest = () => ({
|
2019-07-02 08:10:25 +01:00
|
|
|
type: POLL_FETCH_REQUEST,
|
2020-11-15 18:48:32 +00:00
|
|
|
})
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
const fetchPollSuccess = (poll) => ({
|
2019-07-02 08:10:25 +01:00
|
|
|
type: POLL_FETCH_SUCCESS,
|
|
|
|
poll,
|
2020-11-15 18:48:32 +00:00
|
|
|
})
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
const fetchPollFail = (error) => ({
|
2019-07-02 08:10:25 +01:00
|
|
|
type: POLL_FETCH_FAIL,
|
|
|
|
error,
|
2020-11-15 18:48:32 +00:00
|
|
|
})
|