<commit_before>var React = require('react')
var django = require('django')
var Alert = require('../../../static/Alert')
var update = require('immutability-helper')

var api = require('../../../static/api')
var config = require('../../../static/config')

class Question extends React.Component {
  constructor (props) {
    super(props)

    const question = this.props.question
    this.state = {
      question: question,
      selectedChoices: question.userChoices,
      showResult: !(question.userChoices.length === 0) || question.isReadOnly,
      alert: null
    }
  }

  toggleShowResult () {
    this.setState({
      selectedChoices: this.state.question.userChoices,
      showResult: !this.state.showResult
    })
  }

  handleSubmit (event) {
    event.preventDefault()

    if (this.state.question.isReadOnly) {
      return false
    }

    let newChoices = this.state.selectedChoices

    let submitData = {
      choices: newChoices,
      urlReplaces: {questionId: this.state.question.id}
    }

    api.poll.vote(submitData)
      .done((data) => {
        this.setState({
          showResult: true,
          selectedChoices: data.question.userChoices,
          question: data.question,
          alert: {
            type: 'success',
            message: django.gettext('Vote counted')
          }
        })
      })
      .fail((xhr, status, err) => {
        this.setState({
          showResult: false,
          selectedChoices: newChoices,
          alert: {
            type: 'danger',
            message: django.gettext('Vote has not been counted due to a server error.')
          }
        })
      })
  }

  removeAlert () {
    this.setState({
      alert: null
    })
  }

  handleOnChange (event) {
    const choiceId = parseInt(event.target.value)
    this.setState({
      selectedChoices: [choiceId]
    })
  }

  handleOnMultiChange (event) {
    const choiceId = parseInt(event.target.value)
    const index = this.state.selectedChoices.indexOf(choiceId)

    var diff = {}
    if (index === -1) {
      diff = {'$push': [choiceId]}
    } else {
      diff = {'$splice': [[index, 1]]}
    }

    this.setState({
      selectedChoices: update(this.state.selectedChoices, diff)
    })
  }

  getVoteButton () {
    if (this.state.question.isReadOnly) {
      return null
    }

    if (this.state.question.authenticated) {
      const disabled = this.state.selectedChoices === this.state.question.userChoices || this.state.selectedChoices.length === 0
      return (
        <button
          type="submit"
          className="btn btn--primary"
          disabled={disabled}>
          { django.gettext('Vote') }
        </button>
      )
    } else {
      return (
        <a href={config.getLoginUrl()} className="btn btn--primary">
          { django.gettext('Please login to vote') }
        </a>
      )
    }
  }

  doBarTransition (node, style) {
    if (node && node.style) {
      window.requestAnimationFrame(() => Object.assign(node.style, style))
    }
  }

  render () {
    const total = this.state.question.totalVoteCount
    const max = Math.max.apply(null, this.state.question.choices.map(c => c.count))

    let showTotalOrVoteButton
    let toggleShowResultButton
    let toggleShowResultButtonText

    if (this.state.showResult) {
      showTotalOrVoteButton =
        `${total} ${django.ngettext('vote', 'votes', total)}`
      toggleShowResultButtonText = django.gettext('To poll')

      if (this.state.selectedChoices.length !== 0) {
        toggleShowResultButtonText = django.gettext('Change vote')
      }
    } else {
      showTotalOrVoteButton = this.getVoteButton()
      toggleShowResultButtonText = django.gettext('Show preliminary results')
    }

    if (!this.state.question.isReadOnly) {
      toggleShowResultButton = (
        <button type="button" className="btn btn--link" onClick={this.toggleShowResult.bind(this)}>
          {toggleShowResultButtonText}
        </button>
      )
    }

    return (
      <form onSubmit={this.handleSubmit.bind(this)} className="poll">
        <h2>{ this.state.question.label }</h2>

        <div className="poll__rows">
          {
            this.state.question.choices.map((choice, i) => {
              let checked = this.state.selectedChoices.indexOf(choice.id) !== -1
              let chosen = this.state.question.userChoices.indexOf(choice.id) !== -1
              let percent = total === 0 ? 0 : Math.round(choice.count / total * 100)
              let highlight = choice.count === max && max > 0

              if (this.state.showResult) {
                return (<commit_msg>adding multiple answer text to answer page<commit_after>var React = require('react')
var django = require('django')
var Alert = require('../../../static/Alert')
var update = require('immutability-helper')

var api = require('../../../static/api')
var config = require('../../../static/config')

class Question extends React.Component {
  constructor (props) {
    super(props)

    const question = this.props.question
    this.state = {
      question: question,
      selectedChoices: question.userChoices,
      showResult: !(question.userChoices.length === 0) || question.isReadOnly,
      alert: null
    }
  }

  toggleShowResult () {
    this.setState({
      selectedChoices: this.state.question.userChoices,
      showResult: !this.state.showResult
    })
  }

  handleSubmit (event) {
    event.preventDefault()

    if (this.state.question.isReadOnly) {
      return false
    }

    let newChoices = this.state.selectedChoices

    let submitData = {
      choices: newChoices,
      urlReplaces: {questionId: this.state.question.id}
    }

    api.poll.vote(submitData)
      .done((data) => {
        this.setState({
          showResult: true,
          selectedChoices: data.question.userChoices,
          question: data.question,
          alert: {
            type: 'success',
            message: django.gettext('Vote counted')
          }
        })
      })
      .fail((xhr, status, err) => {
        this.setState({
          showResult: false,
          selectedChoices: newChoices,
          alert: {
            type: 'danger',
            message: django.gettext('Vote has not been counted due to a server error.')
          }
        })
      })
  }

  removeAlert () {
    this.setState({
      alert: null
    })
  }

  handleOnChange (event) {
    const choiceId = parseInt(event.target.value)
    this.setState({
      selectedChoices: [choiceId]
    })
  }

  handleOnMultiChange (event) {
    const choiceId = parseInt(event.target.value)
    const index = this.state.selectedChoices.indexOf(choiceId)

    var diff = {}
    if (index === -1) {
      diff = {'$push': [choiceId]}
    } else {
      diff = {'$splice': [[index, 1]]}
    }

    this.setState({
      selectedChoices: update(this.state.selectedChoices, diff)
    })
  }

  getVoteButton () {
    if (this.state.question.isReadOnly) {
      return null
    }

    if (this.state.question.authenticated) {
      const disabled = this.state.selectedChoices === this.state.question.userChoices || this.state.selectedChoices.length === 0
      return (
        <button
          type="submit"
          className="btn btn--primary"
          disabled={disabled}>
          { django.gettext('Vote') }
        </button>
      )
    } else {
      return (
        <a href={config.getLoginUrl()} className="btn btn--primary">
          { django.gettext('Please login to vote') }
        </a>
      )
    }
  }

  doBarTransition (node, style) {
    if (node && node.style) {
      window.requestAnimationFrame(() => Object.assign(node.style, style))
    }
  }

  render () {
    const total = this.state.question.totalVoteCount
    const max = Math.max.apply(null, this.state.question.choices.map(c => c.count))

    let showTotalOrVoteButton
    let toggleShowResultButton
    let toggleShowResultButtonText
    let multipleChoiceText

    if (this.state.question.multiple_choice) {
      multipleChoiceText = django.gettext('Multiple answers are possible for this question')
    }

    if (this.state.showResult) {
      showTotalOrVoteButton =
        `${total} ${django.ngettext('vote', 'votes', total)}`
      toggleShowResultButtonText = django.gettext('To poll')

      if (this.state.selectedChoices.length !== 0) {
        toggleShowResultButtonText = django.gettext('Change vote')
      }
    } else {
      showTotalOrVoteButton = this.getVoteButton()
      toggleShowResultButtonText = django.gettext('Show preliminary results')
    }

    if (!this.state.question.isReadOnly) {
      toggleShowResultButton = (
        <button type="button" className="btn btn--link" onClick={this.toggleShowResult.bind(this)}>
          {toggleShowResultButtonText}
        </button>
      )
    }

    return (
      <form onSubmit={this.handleSubmit.bind(this)} className="poll">
        <h2>{ this.state.question.label }</h2>
        {multipleChoiceText}
        <div className="poll__rows">
          {
            this.state.question.choices.map((choice, i) => {
              let checked = this.state.selectedChoices.indexOf(choice.id) !== -1
              let chosen = this.state.question.userChoices.indexOf(choice.id) !== -1
              let percent = total === 0 ? 0 : Math.round(choice.count / total * 100)
              let highlight = choice.count === max && max > 0

              if (this.state.showResult) {
                return (
{'fraction_non_alphanumeric': 0.13269088213491476, 'fraction_numerical': 0.003176956475696283, 'mean_word_length': 2.2917392819797837, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4111608', 'n_tokens_mistral': 2855, 'n_tokens_neox': 2765, 'n_words': 541}