Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 16742 | Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
16741 stevensc 1
import React, { useEffect, useRef, useState } from 'react'
2
 
3
import PrivateIcon from '../icons/Private'
4
import PublicIcon from '../icons/Public'
5
 
6
import styles from './survey.module.scss'
7
import styled, { css } from 'styled-components'
8
 
9
const RadioButton = styled.div`
10
  display: flex;
11
  align-items: center;
12
  gap: 0.5rem;
13
  padding: 0.5rem 1rem;
14
  margin-bottom: 0.5rem;
15
  border: 2px solid var(--border-primary);
16
  border-radius: 50px;
17
  cursor: pointer;
18
  transition: all 200ms ease;
19
  position: relative;
20
  overflow: hidden;
21
 
22
  input {
23
    margin: 0 !important;
24
  }
25
 
26
  label {
27
    color: var(--font-color);
28
    font-weight: 500;
29
  }
30
 
31
  &::before {
32
    content: '';
33
    position: absolute;
34
    left: 0;
35
    top: 0;
36
    height: 100%;
37
    width: ${(props) => (props.porcentage ? `${props.porcentage}%` : '0%')};
38
    background-color: #0002;
39
    z-index: 4;
40
  }
41
 
42
  &:hover {
43
    border-color: var(--font-color);
44
    text-shadow: 0 0 1px var(--font-color);
45
  }
46
 
47
  ${(props) =>
48
    props.disabled &&
49
    css`
50
      background-color: #9992;
51
      cursor: auto;
52
 
53
      label {
54
        color: gray;
55
      }
56
 
57
      &:hover {
58
        border-color: var(--border-primary);
59
        text-shadow: none;
60
      }
61
    `}
62
`
63
 
64
const SurveyForm = ({
65
  question,
66
  answers = [],
67
  votes,
68
  active,
69
  time,
70
  resultType
71
}) => {
72
  const [remainingTime, setRemainingTime] = useState('00:00:00')
73
  const [isActive, setIsActive] = useState(Boolean(active))
74
  const timeRef = useRef(time)
75
  const voteRef = useRef(0)
76
 
77
  function getTimeDiff(segundos) {
78
    // Obtener la fecha y hora actual
79
    const currentDate = new Date()
80
 
81
    // Calcular la fecha y hora futura sumando los segundos proporcionados
82
    const futureDate = new Date(currentDate.getTime() + segundos * 1000)
83
 
84
    // Calcular la diferencia entre la fecha futura y la fecha actual
85
    const diff = futureDate - currentDate
86
 
87
    // Calcular los componentes de la diferencia de tiempo
88
    const days = Math.floor(diff / (1000 * 60 * 60 * 24))
89
    const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60))
90
    const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60))
91
 
92
    // Devolver el resultado
93
    return `${addZero(days)}días ${addZero(hours)} horas ${addZero(
94
      minutes
95
    )} minutos`
96
  }
97
 
98
  function addZero(unit) {
99
    return String(unit).padStart(2, '0')
100
  }
101
 
102
  function getPorcentage(n, total) {
103
    return (n / total) * 100
104
  }
105
 
106
  useEffect(() => {
107
    setRemainingTime(getTimeDiff(time))
108
 
109
    if (!time) return
110
 
111
    const interval = setInterval(() => {
112
      if (!timeRef.current) {
113
        setRemainingTime(() => getTimeDiff(0))
114
        setIsActive(false)
115
        return
116
      }
117
 
118
      if (!timeRef.current <= 60) {
119
        timeRef.current -= 1
120
        setRemainingTime(() => getTimeDiff(timeRef.current))
121
        return
122
      }
123
 
124
      timeRef.current -= 60
125
      setRemainingTime(() => getTimeDiff(timeRef.current))
126
    }, 60000)
127
 
128
    return () => {
129
      clearInterval(interval)
130
    }
131
  }, [])
132
 
133
  useEffect(() => {
134
    if (!votes) return
135
    votes.forEach((vote) => (voteRef.current += Number(vote)))
136
  }, [])
137
 
138
  return (
139
    <form className={styles.survey_form}>
140
      <h3>{question}</h3>
141
      {resultType === 'pu' && (
142
        <span
143
          title="Los resultados estaran disponibles al finalizar la
144
          encuesta."
145
        >
146
          <PublicIcon /> Público
147
        </span>
148
      )}
149
      {resultType === 'pr' && (
150
        <span title="Los resultados de la votación son privados.">
151
          <PrivateIcon /> Privado
152
        </span>
153
      )}
154
      {answers.map(
155
        (option, index) =>
156
          option && (
157
            <RadioButton
158
              disabled={!isActive}
159
              porcentage={
160
                !time && votes && getPorcentage(votes[index], voteRef.current)
161
              }
162
              key={index}
163
            >
164
              <input
165
                type="radio"
166
                name="vote"
167
                readOnly
168
                id={`vote-${index + 1}`}
169
                value={index + 1}
170
              />
171
              <label htmlFor={`vote-${index + 1}`}>{option}</label>
172
              {!time && votes && (
173
                <span className="mb-0">
174
                  {getPorcentage(votes[index], voteRef.current)}%
175
                </span>
176
              )}
177
            </RadioButton>
178
          )
179
      )}
180
      <span>Tiempo restante: {remainingTime}</span>
181
    </form>
182
  )
183
}
184
 
185
export default SurveyForm