Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 6390 | Rev 6393 | Ir a la última revisión | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 6390 Rev 6392
Línea 1... Línea 1...
1
import React, { useState, useEffect } from 'react'
1
import React, { useState, useEffect } from 'react'
2
import styles from './survey.module.scss'
2
import styles from './survey.module.scss'
-
 
3
import { useForm } from 'react-hook-form'
-
 
4
import { axios } from '../../../utils'
-
 
5
import { addNotification } from '../../../redux/notification/notification.actions'
Línea 3... Línea 6...
3
 
6
 
4
const SurveyForm = ({ question, answers = [], active, time }) => {
7
const SurveyForm = ({ question, answers = [], active, time, voteUrl }) => {
-
 
8
  const [isActive, setIsActive] = useState(true)
-
 
9
  const { register } = useForm()
-
 
10
 
-
 
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
      })
Línea 5... Línea 32...
5
  const [isActive, setIsActive] = useState(true)
32
  }
6
 
33
 
7
  useEffect(() => {
34
  useEffect(() => {
Línea 8... Línea 35...
8
    setIsActive(Boolean(active))
35
    setIsActive(Boolean(active))
9
  }, [active])
36
  }, [active])
10
 
37
 
11
  return (
38
  return (
12
    <form action="" className={styles.survey_form}>
39
    <form onChange={sendVote} className={styles.survey_form}>
13
      <h3>{question}</h3>
40
      <h3>{question}</h3>
14
      {answers.map(
41
      {answers.map(
15
        (option, index) =>
42
        (option, index) =>
16
          option && (
43
          option && (
17
            <div className={styles.survey_input} key={index}>
44
            <div className={styles.survey_input} key={index}>
18
              <input
45
              <input
19
                type="radio"
46
                type="radio"
-
 
47
                name="vote"
-
 
48
                id={`vote-${index + 1}`}
20
                name="option"
49
                disabled={!isActive}
21
                id={`option-${index + 1}`}
50
                ref={register({ required: true })}
22
                disabled={!isActive}
51
                value={index + 1}
23
              />
52
              />
24
              <label htmlFor={`option-${index + 1}`}>{option}</label>
53
              <label htmlFor={`vote-${index + 1}`}>{option}</label>
25
            </div>
54
            </div>
26
          )
55
          )