Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

Rev Autor Línea Nro. Línea
6399 stevensc 1
import React, { useState } 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
 
6410 stevensc 6
import { updateFeed } from '../../../redux/feed/feed.actions'
6392 stevensc 7
import { addNotification } from '../../../redux/notification/notification.actions'
6390 stevensc 8
 
6410 stevensc 9
import LockClockIcon from '@mui/icons-material/LockClock'
10
import PublicIcon from '@mui/icons-material/Public'
11
 
6393 stevensc 12
import styles from './survey.module.scss'
13
 
14
const SurveyForm = ({
15
  question,
16
  answers = [],
17
  active,
18
  time,
6401 stevensc 19
  resultType,
6393 stevensc 20
  voteUrl,
6401 stevensc 21
  addNotification, // Redux action
22
  updateFeed, // Redux action
6393 stevensc 23
}) => {
6395 stevensc 24
  const [isActive, setIsActive] = useState(Boolean(active))
6393 stevensc 25
  const { register, handleSubmit } = useForm()
6390 stevensc 26
 
6395 stevensc 27
  const sendVote = handleSubmit(({ vote }) => {
28
    setIsActive(false)
6392 stevensc 29
    const formData = new FormData()
30
 
6395 stevensc 31
    formData.append('vote', vote)
32
 
6392 stevensc 33
    axios
34
      .post(voteUrl, formData)
35
      .then(({ data: response }) => {
36
        const { success, data } = response
37
        if (!success) {
38
          addNotification({ style: 'danger', msg: `Error: ${data}` })
6395 stevensc 39
          setIsActive(true)
6392 stevensc 40
        }
41
 
6407 stevensc 42
        updateFeed({ feed: data, uuid: data.feed_uuid })
6392 stevensc 43
      })
44
      .catch((err) => {
45
        addNotification({ style: 'danger', msg: `Error: ${err}` })
6395 stevensc 46
        setIsActive(true)
6392 stevensc 47
        throw new Error(err)
48
      })
6393 stevensc 49
  })
6392 stevensc 50
 
6390 stevensc 51
  return (
6392 stevensc 52
    <form onChange={sendVote} className={styles.survey_form}>
6390 stevensc 53
      <h3>{question}</h3>
6410 stevensc 54
      {resultType === 'pu' && (
55
        <span>
56
          <PublicIcon /> El autor puede ver tu voto
57
        </span>
58
      )}
59
      {resultType === 'pr' && (
60
        <span>
61
          <LockClockIcon /> Tu voto es privado
62
        </span>
63
      )}
6390 stevensc 64
      {answers.map(
65
        (option, index) =>
66
          option && (
6401 stevensc 67
            <div
6402 stevensc 68
              className={
69
                isActive
70
                  ? styles.survey_input
6404 stevensc 71
                  : styles.survey_input + ' ' + styles.disabled
6402 stevensc 72
              }
6401 stevensc 73
              key={index}
74
            >
6390 stevensc 75
              <input
76
                type="radio"
6392 stevensc 77
                name="vote"
78
                id={`vote-${index + 1}`}
6390 stevensc 79
                disabled={!isActive}
6392 stevensc 80
                ref={register({ required: true })}
81
                value={index + 1}
6390 stevensc 82
              />
6392 stevensc 83
              <label htmlFor={`vote-${index + 1}`}>{option}</label>
6390 stevensc 84
            </div>
85
          )
86
      )}
87
    </form>
88
  )
89
}
90
 
6393 stevensc 91
const mapDispatchToProps = {
92
  addNotification: (notification) => addNotification(notification),
6401 stevensc 93
  updateFeed: (payload) => updateFeed(payload),
6393 stevensc 94
}
95
 
96
export default connect(null, mapDispatchToProps)(SurveyForm)