闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.5 KiB

  1. import api from '../api';
  2. import { importFetchedPoll } from './importer';
  3. export const POLL_VOTE_REQUEST = 'POLL_VOTE_REQUEST';
  4. export const POLL_VOTE_SUCCESS = 'POLL_VOTE_SUCCESS';
  5. export const POLL_VOTE_FAIL = 'POLL_VOTE_FAIL';
  6. export const POLL_FETCH_REQUEST = 'POLL_FETCH_REQUEST';
  7. export const POLL_FETCH_SUCCESS = 'POLL_FETCH_SUCCESS';
  8. export const POLL_FETCH_FAIL = 'POLL_FETCH_FAIL';
  9. export const vote = (pollId, choices) => (dispatch, getState) => {
  10. dispatch(voteRequest());
  11. api(getState).post(`/api/v1/polls/${pollId}/votes`, { choices })
  12. .then(({ data }) => {
  13. dispatch(importFetchedPoll(data));
  14. dispatch(voteSuccess(data));
  15. })
  16. .catch(err => dispatch(voteFail(err)));
  17. };
  18. export const fetchPoll = pollId => (dispatch, getState) => {
  19. dispatch(fetchPollRequest());
  20. api(getState).get(`/api/v1/polls/${pollId}`)
  21. .then(({ data }) => {
  22. dispatch(importFetchedPoll(data));
  23. dispatch(fetchPollSuccess(data));
  24. })
  25. .catch(err => dispatch(fetchPollFail(err)));
  26. };
  27. export const voteRequest = () => ({
  28. type: POLL_VOTE_REQUEST,
  29. });
  30. export const voteSuccess = poll => ({
  31. type: POLL_VOTE_SUCCESS,
  32. poll,
  33. });
  34. export const voteFail = error => ({
  35. type: POLL_VOTE_FAIL,
  36. error,
  37. });
  38. export const fetchPollRequest = () => ({
  39. type: POLL_FETCH_REQUEST,
  40. });
  41. export const fetchPollSuccess = poll => ({
  42. type: POLL_FETCH_SUCCESS,
  43. poll,
  44. });
  45. export const fetchPollFail = error => ({
  46. type: POLL_FETCH_FAIL,
  47. error,
  48. });