poll.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import React from "react"
  2. import moment from "moment"
  3. import Results from "./results"
  4. import Voting from "./voting"
  5. export default class extends React.Component {
  6. constructor(props) {
  7. super(props)
  8. let showResults = true
  9. if (props.user.id && !props.poll.hasSelectedChoices) {
  10. showResults = false
  11. }
  12. this.state = {
  13. showResults
  14. }
  15. }
  16. showResults = () => {
  17. this.setState({
  18. showResults: true
  19. })
  20. }
  21. showVoting = () => {
  22. this.setState({
  23. showResults: false
  24. })
  25. }
  26. render() {
  27. if (!this.props.thread.poll) return null
  28. const isPollOver = getIsPollOver(this.props.poll)
  29. if (
  30. !isPollOver &&
  31. this.props.poll.acl.can_vote &&
  32. !this.state.showResults
  33. ) {
  34. return <Voting showResults={this.showResults} {...this.props} />
  35. } else {
  36. return (
  37. <Results
  38. isPollOver={isPollOver}
  39. showVoting={this.showVoting}
  40. {...this.props}
  41. />
  42. )
  43. }
  44. }
  45. }
  46. export function getIsPollOver(poll) {
  47. if (poll.length) {
  48. return moment().isAfter(poll.endsOn)
  49. }
  50. return false
  51. }