Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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