logo

pleroma-fe

My custom branche(s) on git.pleroma.social/pleroma/pleroma-fe git clone https://hacktivis.me/git/pleroma-fe.git

polls.js (2064B)


  1. import { merge } from 'lodash'
  2. const polls = {
  3. state: {
  4. // Contains key = id, value = number of trackers for this poll
  5. trackedPolls: {},
  6. pollsObject: {}
  7. },
  8. mutations: {
  9. mergeOrAddPoll (state, poll) {
  10. const existingPoll = state.pollsObject[poll.id]
  11. // Make expired-state change trigger re-renders properly
  12. poll.expired = Date.now() > Date.parse(poll.expires_at)
  13. if (existingPoll) {
  14. state.pollsObject[poll.id] = merge(existingPoll, poll)
  15. } else {
  16. state.pollsObject[poll.id] = poll
  17. }
  18. },
  19. trackPoll (state, pollId) {
  20. const currentValue = state.trackedPolls[pollId]
  21. if (currentValue) {
  22. state.trackedPolls[pollId] = currentValue + 1
  23. } else {
  24. state.trackedPolls[pollId] = 1
  25. }
  26. },
  27. untrackPoll (state, pollId) {
  28. const currentValue = state.trackedPolls[pollId]
  29. if (currentValue) {
  30. state.trackedPolls[pollId] = currentValue - 1
  31. } else {
  32. state.trackedPolls[pollId] = 0
  33. }
  34. }
  35. },
  36. actions: {
  37. mergeOrAddPoll ({ commit }, poll) {
  38. commit('mergeOrAddPoll', poll)
  39. },
  40. updateTrackedPoll ({ rootState, dispatch, commit }, pollId) {
  41. rootState.api.backendInteractor.fetchPoll({ pollId }).then(poll => {
  42. setTimeout(() => {
  43. if (rootState.polls.trackedPolls[pollId]) {
  44. dispatch('updateTrackedPoll', pollId)
  45. }
  46. }, 30 * 1000)
  47. commit('mergeOrAddPoll', poll)
  48. })
  49. },
  50. trackPoll ({ rootState, commit, dispatch }, pollId) {
  51. if (!rootState.polls.trackedPolls[pollId]) {
  52. setTimeout(() => dispatch('updateTrackedPoll', pollId), 30 * 1000)
  53. }
  54. commit('trackPoll', pollId)
  55. },
  56. untrackPoll ({ commit }, pollId) {
  57. commit('untrackPoll', pollId)
  58. },
  59. votePoll ({ rootState, commit }, { id, pollId, choices }) {
  60. return rootState.api.backendInteractor.vote({ pollId, choices }).then(poll => {
  61. commit('mergeOrAddPoll', poll)
  62. return poll
  63. })
  64. }
  65. }
  66. }
  67. export default polls