Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 6390 | Rev 6393 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
6390 stevensc 1
import React, { useState, useEffect } from 'react'
2
import styles from './survey.module.scss'
6392 stevensc 3
import { useForm } from 'react-hook-form'
4
import { axios } from '../../../utils'
5
import { addNotification } from '../../../redux/notification/notification.actions'
6390 stevensc 6
 
6392 stevensc 7
const SurveyForm = ({ question, answers = [], active, time, voteUrl }) => {
6390 stevensc 8
  const [isActive, setIsActive] = useState(true)
6392 stevensc 9
  const { register } = useForm()
6390 stevensc 10
 
6392 stevensc 11
  const sendVote = (data) => {
12
    setIsActive(!isActive)
13
    const formData = new FormData()
14
    formData.append('vote', data.vote)
15
 
16
    axios
17
      .post(voteUrl, formData)
18
      .then(({ data: response }) => {
19
        const { success, data } = response
20
        if (!success) {
21
          addNotification({ style: 'danger', msg: `Error: ${data}` })
22
          setIsActive(!isActive)
23
          return
24
        }
25
 
26
        addNotification({ style: 'success', msg: data })
27
      })
28
      .catch((err) => {
29
        addNotification({ style: 'danger', msg: `Error: ${err}` })
30
        throw new Error(err)
31
      })
32
  }
33
 
6390 stevensc 34
  useEffect(() => {
35
    setIsActive(Boolean(active))
36
  }, [active])
37
 
38
  return (
6392 stevensc 39
    <form onChange={sendVote} className={styles.survey_form}>
6390 stevensc 40
      <h3>{question}</h3>
41
      {answers.map(
42
        (option, index) =>
43
          option && (
44
            <div className={styles.survey_input} key={index}>
45
              <input
46
                type="radio"
6392 stevensc 47
                name="vote"
48
                id={`vote-${index + 1}`}
6390 stevensc 49
                disabled={!isActive}
6392 stevensc 50
                ref={register({ required: true })}
51
                value={index + 1}
6390 stevensc 52
              />
6392 stevensc 53
              <label htmlFor={`vote-${index + 1}`}>{option}</label>
6390 stevensc 54
            </div>
55
          )
56
      )}
57
    </form>
58
  )
59
}
60
 
61
export default SurveyForm