Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev 623 Rev 813
Línea 1... Línea 1...
1
import React, { useEffect, useRef, useState } from "react";
1
import React, { useEffect, useRef, useState } from 'react'
2
import { axios } from "../../utils";
2
import { axios } from '../../utils'
3
import { useForm } from "react-hook-form";
3
import { useForm } from 'react-hook-form'
4
import { connect } from "react-redux";
4
import { connect } from 'react-redux'
Línea 5... Línea 5...
5
 
5
 
6
import { updateFeed } from "../../redux/feed/feed.actions";
6
import { updateFeed } from '../../redux/feed/feed.actions'
Línea 7... Línea 7...
7
import { addNotification } from "../../redux/notification/notification.actions";
7
import { addNotification } from '../../redux/notification/notification.actions'
8
 
8
 
Línea 9... Línea 9...
9
import LockClockIcon from "@mui/icons-material/LockClock";
9
import LockClockIcon from '@mui/icons-material/LockClock'
10
import PublicIcon from "@mui/icons-material/Public";
10
import PublicIcon from '@mui/icons-material/Public'
Línea 11... Línea 11...
11
 
11
 
12
import styles from "./survey.module.scss";
12
import styles from './survey.module.scss'
13
import styled, { css } from "styled-components";
13
import styled, { css } from 'styled-components'
14
 
14
 
Línea 30... Línea 30...
30
  label {
30
  label {
31
    color: var(--font-color);
31
    color: var(--font-color);
32
    font-weight: 500;
32
    font-weight: 500;
33
  }
33
  }
34
  &::before {
34
  &::before {
35
    content: "";
35
    content: '';
36
    position: absolute;
36
    position: absolute;
37
    left: 0;
37
    left: 0;
38
    top: 0;
38
    top: 0;
39
    height: 100%;
39
    height: 100%;
40
    width: ${(props) => (props.porcentage ? `${props.porcentage}%` : "0%")};
40
    width: ${(props) => (props.porcentage ? `${props.porcentage}%` : '0%')};
41
    background-color: #0002;
41
    background-color: #0002;
42
    z-index: 4;
42
    z-index: 4;
43
  }
43
  }
44
  &:hover {
44
  &:hover {
45
    border-color: var(--font-color);
45
    border-color: var(--font-color);
Línea 58... Línea 58...
58
      &:hover {
58
      &:hover {
59
        border-color: var(--border-primary);
59
        border-color: var(--border-primary);
60
        text-shadow: none;
60
        text-shadow: none;
61
      }
61
      }
62
    `}
62
    `}
63
`;
63
`
Línea 64... Línea 64...
64
 
64
 
65
const VoteTag = styled.span`
65
const VoteTag = styled.span`
66
  position: absolute;
66
  position: absolute;
67
  bottom: 1rem;
67
  bottom: 1rem;
68
  right: 1rem;
68
  right: 1rem;
69
  color: var(--font-color) !important;
69
  color: var(--font-color) !important;
70
  font-weight: 600;
70
  font-weight: 600;
Línea 71... Línea 71...
71
`;
71
`
72
 
72
 
73
const SurveyForm = ({
73
const SurveyForm = ({
74
  question,
74
  question,
75
  answers = [],
75
  answers = [],
76
  votes,
76
  votes,
77
  active = false,
77
  active = false,
78
  time,
78
  time,
79
  resultType,
79
  resultType,
80
  voteUrl,
80
  voteUrl,
81
  addNotification, // Redux action
81
  addNotification, // Redux action
82
  updateFeed, // Redux action
82
  updateFeed // Redux action
83
}) => {
83
}) => {
84
  const [remainingTime, setRemainingTime] = useState("00:00:00");
84
  const [remainingTime, setRemainingTime] = useState('00:00:00')
85
  const [isActive, setIsActive] = useState();
85
  const [isActive, setIsActive] = useState(true)
86
  const [totalVotes, setTotalVotes] = useState(0);
86
  const [totalVotes, setTotalVotes] = useState(0)
Línea 87... Línea 87...
87
  const timeRef = useRef(time);
87
  const timeRef = useRef(time)
88
  const { register, handleSubmit } = useForm();
88
  const { register, handleSubmit } = useForm()
89
 
-
 
Línea -... Línea 89...
-
 
89
 
90
  const sendVote = handleSubmit(({ vote }) => {
90
  const sendVote = handleSubmit(({ vote }) => {
Línea 91... Línea 91...
91
    setIsActive(false);
91
    setIsActive(false)
92
    const formData = new FormData();
92
 
93
 
93
    const formData = new FormData()
94
    formData.append("vote", vote);
94
    formData.append('vote', vote)
-
 
95
 
95
 
96
    axios
96
    axios
97
      .post(voteUrl, formData)
97
      .post(voteUrl, formData)
98
      .then(({ data: response }) => {
98
      .then(({ data: response }) => {
99
        const { success, data } = response
Línea 99... Línea 100...
99
        const { success, data } = response;
100
 
100
        if (!success) {
101
        if (!success) {
101
          addNotification({ style: "danger", msg: `Error: ${data}` });
102
          addNotification({ style: 'danger', msg: `Error: ${data}` })
102
          setIsActive(true);
103
          setIsActive(true)
103
        }
104
        }
104
 
105
 
105
        updateFeed({ feed: data, uuid: data.feed_uuid });
106
        updateFeed({ feed: data, uuid: data.feed_uuid })
106
        addNotification({ style: "success", msg: "Voto emitido con exito" });
107
        addNotification({ style: 'success', msg: 'Voto emitido con exito' })
107
      })
108
      })
Línea 108... Línea 109...
108
      .catch((err) => {
109
      .catch((err) => {
109
        addNotification({ style: "danger", msg: `Error: ${err}` });
110
        addNotification({ style: 'danger', msg: `Error: ${err}` })
110
        setIsActive(true);
111
        setIsActive(true)
Línea 111... Línea 112...
111
        throw new Error(err);
112
        throw new Error(err)
112
      });
113
      })
Línea 113... Línea 114...
113
  });
114
  })
114
 
115
 
Línea 115... Línea 116...
115
  function getTimeDiff(segundos) {
116
  function getTimeDiff(segundos) {
116
    // Obtener la fecha y hora actual
117
    // Obtener la fecha y hora actual
117
    const currentDate = new Date();
118
    const currentDate = new Date()
118
 
119
 
Línea 119... Línea 120...
119
    // Calcular la fecha y hora futura sumando los segundos proporcionados
120
    // Calcular la fecha y hora futura sumando los segundos proporcionados
120
    const futureDate = new Date(currentDate.getTime() + segundos * 1000);
121
    const futureDate = new Date(currentDate.getTime() + segundos * 1000)
121
 
122
 
Línea 122... Línea 123...
122
    // Calcular la diferencia entre la fecha futura y la fecha actual
123
    // Calcular la diferencia entre la fecha futura y la fecha actual
123
    const diff = futureDate - currentDate;
124
    const diff = futureDate - currentDate
124
 
125
 
Línea 125... Línea 126...
125
    // Calcular los componentes de la diferencia de tiempo
126
    // Calcular los componentes de la diferencia de tiempo
126
    const days = Math.floor(diff / (1000 * 60 * 60 * 24));
127
    const days = Math.floor(diff / (1000 * 60 * 60 * 24))
127
    const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
128
    const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60))
Línea 128... Línea 129...
128
    const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
129
    const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60))
129
 
130
 
Línea 130... Línea 131...
130
    // Devolver el resultado
131
    // Devolver el resultado
Línea 131... Línea 132...
131
    return `${addZero(days)}d ${addZero(hours)}h ${addZero(minutes)}m`;
132
    return `${addZero(days)}d ${addZero(hours)}h ${addZero(minutes)}m`
132
  }
133
  }
133
 
134
 
134
  function addZero(unit) {
135
  function addZero(unit) {
135
    return String(unit).padStart(2, "0");
136
    return String(unit).padStart(2, '0')
136
  }
137
  }
Línea 137... Línea 138...
137
 
138
 
138
  function getPorcentage(n, total) {
139
  function getPorcentage(n, total) {
139
    return (n / total) * 100;
140
    return (n / total) * 100
140
  }
141
  }
141
 
142
 
Línea 142... Línea 143...
142
  useEffect(() => {
143
  useEffect(() => {
143
    setRemainingTime(getTimeDiff(time));
144
    setRemainingTime(getTimeDiff(time))
144
 
145
 
Línea 145... Línea 146...
145
    if (!time) return;
146
    if (!time) return
146
 
147
 
147
    const interval = setInterval(() => {
148
    const interval = setInterval(() => {
148
      if (!timeRef.current) {
149
      if (!timeRef.current) {
Línea 149... Línea 150...
149
        setRemainingTime(() => getTimeDiff(0));
150
        setRemainingTime(() => getTimeDiff(0))
150
        setIsActive(false);
151
        setIsActive(false)
151
        return;
152
        return
152
      }
153
      }
153
 
154
 
Línea 154... Línea 155...
154
      if (!timeRef.current <= 60) {
155
      if (!timeRef.current <= 60) {
155
        timeRef.current -= 1;
156
        timeRef.current -= 1
156
        setRemainingTime(() => getTimeDiff(timeRef.current));
157
        setRemainingTime(() => getTimeDiff(timeRef.current))
Línea 157... Línea 158...
157
        return;
158
        return
158
      }
159
      }
159
 
160
 
160
      timeRef.current -= 60;
161
      timeRef.current -= 60
161
      setRemainingTime(() => getTimeDiff(timeRef.current));
162
      setRemainingTime(() => getTimeDiff(timeRef.current))
162
    }, 60000);
163
    }, 60000)
163
 
164
 
164
    return () => {
165
    return () => {
165
      clearInterval(interval);
166
      clearInterval(interval)
166
    };
167
    }
167
  }, []);
168
  }, [])
168
 
169
 
169
  useEffect(() => {
170
  useEffect(() => {
170
    if (!votes) return;
171
    if (!votes) return
171
    const total = votes.reduce((acum, current) => acum + Number(current), 0);
172
    const total = votes.reduce((acum, current) => acum + Number(current), 0)
172
    setTotalVotes(total);
173
    setTotalVotes(total)
173
  }, [votes]);
174
  }, [votes])
174
 
175
 
175
  useEffect(() => {
176
  useEffect(() => {
176
    active ?? setIsActive(Boolean(active));
177
    setIsActive(Boolean(active))
Línea 204... Línea 205...
204
                !!totalVotes && getPorcentage(votes[index], totalVotes)
205
                !!totalVotes && getPorcentage(votes[index], totalVotes)
205
              }
206
              }
206
              key={index}
207
              key={index}
207
            >
208
            >
208
              <input
209
              <input
209
                type="radio"
210
                type='radio'
210
                name="vote"
211
                name='vote'
211
                id={`vote-${index + 1}`}
212
                id={`vote-${index + 1}`}
212
                disabled={!isActive}
213
                disabled={!isActive}
213
                ref={register({ required: true })}
214
                ref={register({ required: true })}
214
                value={index + 1}
215
                value={index + 1}
215
              />
216
              />
216
              <label htmlFor={`vote-${index + 1}`}>{option}</label>
217
              <label htmlFor={`vote-${index + 1}`}>{option}</label>
217
              {!!totalVotes && (
218
              {!!totalVotes && (
218
                <span className="mb-0">
219
                <span className='mb-0'>
219
                  {getPorcentage(votes[index], totalVotes)}%
220
                  {getPorcentage(votes[index], totalVotes)}%
220
                </span>
221
                </span>
221
              )}
222
              )}
222
            </RadioButton>
223
            </RadioButton>
223
          )
224
          )
224
      )}
225
      )}
225
      <span>Tiempo restante: {remainingTime}</span>
226
      <span>Tiempo restante: {remainingTime}</span>
226
      {!isActive && <VoteTag>Tu voto ya fue emitido</VoteTag>}
227
      {!isActive && <VoteTag>Tu voto ya fue emitido</VoteTag>}
227
    </form>
228
    </form>
228
  );
229
  )
229
};
230
}
Línea 230... Línea 231...
230
 
231
 
231
const mapDispatchToProps = {
232
const mapDispatchToProps = {
232
  addNotification: (notification) => addNotification(notification),
233
  addNotification: (notification) => addNotification(notification),
233
  updateFeed: (payload) => updateFeed(payload),
234
  updateFeed: (payload) => updateFeed(payload)
Línea 234... Línea 235...
234
};
235
}