Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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