Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

Rev Autor Línea Nro. Línea
6436 stevensc 1
import React, { useEffect, useRef, 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'
6440 stevensc 13
import styled, { css } from 'styled-components'
6393 stevensc 14
 
6440 stevensc 15
const RadioButton = styled.div`
16
  display: flex;
17
  align-items: center;
18
  gap: 0.5rem;
19
  padding: 0.5rem 1rem;
20
  margin-bottom: 0.5rem;
6441 stevensc 21
  border: 2px solid var(--border-primary);
6440 stevensc 22
  border-radius: 50px;
23
  cursor: pointer;
24
  transition: all 200ms ease;
25
  position: relative;
6442 stevensc 26
  overflow: hidden;
6440 stevensc 27
 
28
  input {
29
    margin: 0 !important;
30
  }
31
 
32
  label {
6441 stevensc 33
    color: var(--font-color);
6440 stevensc 34
    font-weight: 500;
35
  }
36
 
37
  &::before {
6447 stevensc 38
    content: '';
6440 stevensc 39
    position: absolute;
40
    left: 0;
41
    top: 0;
42
    height: 100%;
6447 stevensc 43
    width: ${(props) => (props.porcentage ? `${props.porcentage}%` : '0%')};
6440 stevensc 44
    background-color: #0006;
45
    z-index: 4;
46
  }
47
 
48
  &:hover {
6441 stevensc 49
    border-color: var(--font-color);
50
    text-shadow: 0 0 1px var(--font-color);
6440 stevensc 51
  }
52
 
53
  ${(props) =>
6446 stevensc 54
    props.disabled &&
6440 stevensc 55
    css`
6446 stevensc 56
      background-color: #9992;
6440 stevensc 57
      cursor: auto;
58
 
59
      label {
60
        color: gray;
61
      }
62
 
63
      &:hover {
6441 stevensc 64
        border-color: var(--border-primary);
6440 stevensc 65
        text-shadow: none;
66
      }
67
    `}
68
`
69
 
6393 stevensc 70
const SurveyForm = ({
71
  question,
72
  answers = [],
6439 stevensc 73
  votes = [],
6393 stevensc 74
  active,
75
  time,
6401 stevensc 76
  resultType,
6393 stevensc 77
  voteUrl,
6401 stevensc 78
  addNotification, // Redux action
79
  updateFeed, // Redux action
6393 stevensc 80
}) => {
6438 stevensc 81
  const [remainingTime, setRemainingTime] = useState('00:00:00')
6395 stevensc 82
  const [isActive, setIsActive] = useState(Boolean(active))
6436 stevensc 83
  const timeRef = useRef(time)
6393 stevensc 84
  const { register, handleSubmit } = useForm()
6390 stevensc 85
 
6395 stevensc 86
  const sendVote = handleSubmit(({ vote }) => {
87
    setIsActive(false)
6392 stevensc 88
    const formData = new FormData()
89
 
6395 stevensc 90
    formData.append('vote', vote)
91
 
6392 stevensc 92
    axios
93
      .post(voteUrl, formData)
94
      .then(({ data: response }) => {
95
        const { success, data } = response
96
        if (!success) {
97
          addNotification({ style: 'danger', msg: `Error: ${data}` })
6395 stevensc 98
          setIsActive(true)
6392 stevensc 99
        }
100
 
6407 stevensc 101
        updateFeed({ feed: data, uuid: data.feed_uuid })
6392 stevensc 102
      })
103
      .catch((err) => {
104
        addNotification({ style: 'danger', msg: `Error: ${err}` })
6395 stevensc 105
        setIsActive(true)
6392 stevensc 106
        throw new Error(err)
107
      })
6393 stevensc 108
  })
6392 stevensc 109
 
6415 stevensc 110
  function getTimeDiff(segundos) {
111
    // Obtener la fecha y hora actual
112
    const currentDate = new Date()
113
 
114
    // Calcular la fecha y hora futura sumando los segundos proporcionados
6416 stevensc 115
    const futureDate = new Date(currentDate.getTime() + segundos * 1000)
6415 stevensc 116
 
117
    // Calcular la diferencia entre la fecha futura y la fecha actual
118
    const diff = futureDate - currentDate
119
 
120
    // Calcular los componentes de la diferencia de tiempo
6419 stevensc 121
    const days = Math.floor(diff / (1000 * 60 * 60 * 24))
122
    const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60))
123
    const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60))
6415 stevensc 124
 
125
    // Devolver el resultado
6438 stevensc 126
    return `${addZero(days)}:${addZero(hours)}:${addZero(minutes)}`
6415 stevensc 127
  }
128
 
6438 stevensc 129
  function addZero(unit) {
130
    return String(unit).padStart(2, '0')
131
  }
132
 
6439 stevensc 133
  function getPorcentage(n, total) {
134
    return (n * total) / 100
135
  }
136
 
6415 stevensc 137
  useEffect(() => {
6421 stevensc 138
    setRemainingTime(getTimeDiff(time))
139
 
6433 stevensc 140
    const interval = setInterval(() => {
6436 stevensc 141
      timeRef.current = timeRef.current - 60
142
      setRemainingTime(() => getTimeDiff(timeRef.current))
6437 stevensc 143
    }, 60000)
6417 stevensc 144
 
6433 stevensc 145
    return () => {
146
      clearInterval(interval)
147
    }
148
  }, [])
149
 
6390 stevensc 150
  return (
6392 stevensc 151
    <form onChange={sendVote} className={styles.survey_form}>
6390 stevensc 152
      <h3>{question}</h3>
6410 stevensc 153
      {resultType === 'pu' && (
154
        <span>
6442 stevensc 155
          <PublicIcon /> Los resultados estaran disponibles al finalizar la
156
          encuesta.
6410 stevensc 157
        </span>
158
      )}
159
      {resultType === 'pr' && (
160
        <span>
6442 stevensc 161
          <LockClockIcon /> Los resultados de la votación son privados.
6410 stevensc 162
        </span>
163
      )}
6390 stevensc 164
      {answers.map(
165
        (option, index) =>
166
          option && (
6448 stevensc 167
            <RadioButton disabled={!isActive} porcentage={50} key={index}>
6390 stevensc 168
              <input
169
                type="radio"
6392 stevensc 170
                name="vote"
171
                id={`vote-${index + 1}`}
6390 stevensc 172
                disabled={!isActive}
6392 stevensc 173
                ref={register({ required: true })}
174
                value={index + 1}
6390 stevensc 175
              />
6392 stevensc 176
              <label htmlFor={`vote-${index + 1}`}>{option}</label>
6440 stevensc 177
            </RadioButton>
6390 stevensc 178
          )
179
      )}
6438 stevensc 180
      <span>Tiempo restante: {remainingTime}</span>
6390 stevensc 181
    </form>
182
  )
183
}
184
 
6393 stevensc 185
const mapDispatchToProps = {
186
  addNotification: (notification) => addNotification(notification),
6401 stevensc 187
  updateFeed: (payload) => updateFeed(payload),
6393 stevensc 188
}
189
 
190
export default connect(null, mapDispatchToProps)(SurveyForm)