Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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