logo

pleroma-fe

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

polls.js (1719B)


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