Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev 5 Rev 623
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 22... Línea 22...
22
  cursor: pointer;
22
  cursor: pointer;
23
  transition: all 200ms ease;
23
  transition: all 200ms ease;
24
  position: relative;
24
  position: relative;
25
  overflow: hidden;
25
  overflow: hidden;
26
  margin-bottom: 0.5rem;
26
  margin-bottom: 0.5rem;
27
 
-
 
28
  input {
27
  input {
29
    margin: 0 !important;
28
    margin: 0 !important;
30
  }
29
  }
31
 
-
 
32
  label {
30
  label {
33
    color: var(--font-color);
31
    color: var(--font-color);
34
    font-weight: 500;
32
    font-weight: 500;
35
  }
33
  }
36
 
-
 
37
  &::before {
34
  &::before {
38
    content: '';
35
    content: "";
39
    position: absolute;
36
    position: absolute;
40
    left: 0;
37
    left: 0;
41
    top: 0;
38
    top: 0;
42
    height: 100%;
39
    height: 100%;
43
    width: ${(props) => (props.porcentage ? `${props.porcentage}%` : '0%')};
40
    width: ${(props) => (props.porcentage ? `${props.porcentage}%` : "0%")};
44
    background-color: #0002;
41
    background-color: #0002;
45
    z-index: 4;
42
    z-index: 4;
46
  }
43
  }
47
 
-
 
48
  &:hover {
44
  &:hover {
49
    border-color: var(--font-color);
45
    border-color: var(--font-color);
50
    text-shadow: 0 0 1px var(--font-color);
46
    text-shadow: 0 0 1px var(--font-color);
51
  }
47
  }
52
 
-
 
53
  ${(props) =>
48
  ${(props) =>
54
    props.disabled &&
49
    props.disabled &&
55
    css`
50
    css`
56
      background-color: #9992;
51
      background-color: #9992;
57
      cursor: auto;
52
      cursor: auto;
Línea 63... Línea 58...
63
      &:hover {
58
      &:hover {
64
        border-color: var(--border-primary);
59
        border-color: var(--border-primary);
65
        text-shadow: none;
60
        text-shadow: none;
66
      }
61
      }
67
    `}
62
    `}
68
`
63
`;
Línea 69... Línea 64...
69
 
64
 
70
const VoteTag = styled.span`
65
const VoteTag = styled.span`
71
  position: absolute;
66
  position: absolute;
72
  bottom: 1rem;
67
  bottom: 1rem;
73
  right: 1rem;
68
  right: 1rem;
74
  color: var(--font-color) !important;
69
  color: var(--font-color) !important;
75
  font-weight: 600;
70
  font-weight: 600;
Línea 76... Línea 71...
76
`
71
`;
77
 
72
 
78
const SurveyForm = ({
73
const SurveyForm = ({
79
  question,
74
  question,
Línea 84... Línea 79...
84
  resultType,
79
  resultType,
85
  voteUrl,
80
  voteUrl,
86
  addNotification, // Redux action
81
  addNotification, // Redux action
87
  updateFeed, // Redux action
82
  updateFeed, // Redux action
88
}) => {
83
}) => {
89
  const [remainingTime, setRemainingTime] = useState('00:00:00')
84
  const [remainingTime, setRemainingTime] = useState("00:00:00");
90
  const [isActive, setIsActive] = useState()
85
  const [isActive, setIsActive] = useState();
91
  const [totalVotes, setTotalVotes] = useState(0)
86
  const [totalVotes, setTotalVotes] = useState(0);
92
  const timeRef = useRef(time)
87
  const timeRef = useRef(time);
93
  const { register, handleSubmit } = useForm()
88
  const { register, handleSubmit } = useForm();
Línea 94... Línea 89...
94
 
89
 
95
  const sendVote = handleSubmit(({ vote }) => {
90
  const sendVote = handleSubmit(({ vote }) => {
96
    setIsActive(false)
91
    setIsActive(false);
Línea 97... Línea 92...
97
    const formData = new FormData()
92
    const formData = new FormData();
Línea 98... Línea 93...
98
 
93
 
99
    formData.append('vote', vote)
94
    formData.append("vote", vote);
100
 
95
 
101
    axios
96
    axios
102
      .post(voteUrl, formData)
97
      .post(voteUrl, formData)
103
      .then(({ data: response }) => {
98
      .then(({ data: response }) => {
104
        const { success, data } = response
99
        const { success, data } = response;
105
        if (!success) {
100
        if (!success) {
Línea 106... Línea 101...
106
          addNotification({ style: 'danger', msg: `Error: ${data}` })
101
          addNotification({ style: "danger", msg: `Error: ${data}` });
107
          setIsActive(true)
102
          setIsActive(true);
108
        }
103
        }
109
 
104
 
110
        updateFeed({ feed: data, uuid: data.feed_uuid })
105
        updateFeed({ feed: data, uuid: data.feed_uuid });
111
        addNotification({ style: 'success', msg: 'Voto emitido con exito' })
106
        addNotification({ style: "success", msg: "Voto emitido con exito" });
112
      })
107
      })
113
      .catch((err) => {
108
      .catch((err) => {
114
        addNotification({ style: 'danger', msg: `Error: ${err}` })
109
        addNotification({ style: "danger", msg: `Error: ${err}` });
Línea 115... Línea 110...
115
        setIsActive(true)
110
        setIsActive(true);
116
        throw new Error(err)
111
        throw new Error(err);
117
      })
112
      });
Línea 118... Línea 113...
118
  })
113
  });
119
 
114
 
Línea 120... Línea 115...
120
  function getTimeDiff(segundos) {
115
  function getTimeDiff(segundos) {
121
    // Obtener la fecha y hora actual
116
    // Obtener la fecha y hora actual
Línea 122... Línea 117...
122
    const currentDate = new Date()
117
    const currentDate = new Date();
123
 
118
 
124
    // Calcular la fecha y hora futura sumando los segundos proporcionados
119
    // Calcular la fecha y hora futura sumando los segundos proporcionados
125
    const futureDate = new Date(currentDate.getTime() + segundos * 1000)
120
    const futureDate = new Date(currentDate.getTime() + segundos * 1000);
Línea 126... Línea 121...
126
 
121
 
127
    // Calcular la diferencia entre la fecha futura y la fecha actual
122
    // Calcular la diferencia entre la fecha futura y la fecha actual
128
    const diff = futureDate - currentDate
123
    const diff = futureDate - currentDate;
Línea 129... Línea 124...
129
 
124
 
130
    // Calcular los componentes de la diferencia de tiempo
125
    // Calcular los componentes de la diferencia de tiempo
131
    const days = Math.floor(diff / (1000 * 60 * 60 * 24))
126
    const days = Math.floor(diff / (1000 * 60 * 60 * 24));
Línea 132... Línea 127...
132
    const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60))
127
    const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
133
    const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60))
128
    const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
134
 
129
 
Línea 135... Línea 130...
135
    // Devolver el resultado
130
    // Devolver el resultado
136
    return `${addZero(days)}d ${addZero(hours)}h ${addZero(minutes)}m`
131
    return `${addZero(days)}d ${addZero(hours)}h ${addZero(minutes)}m`;
Línea 137... Línea 132...
137
  }
132
  }
Línea 138... Línea 133...
138
 
133
 
139
  function addZero(unit) {
134
  function addZero(unit) {
140
    return String(unit).padStart(2, '0')
135
    return String(unit).padStart(2, "0");
141
  }
136
  }
142
 
137
 
143
  function getPorcentage(n, total) {
138
  function getPorcentage(n, total) {
Línea 144... Línea 139...
144
    return (n / total) * 100
139
    return (n / total) * 100;
145
  }
140
  }
146
 
141
 
147
  useEffect(() => {
142
  useEffect(() => {
148
    setRemainingTime(getTimeDiff(time))
143
    setRemainingTime(getTimeDiff(time));
Línea 149... Línea 144...
149
 
144
 
150
    if (!time) return
145
    if (!time) return;
151
 
146
 
Línea 152... Línea 147...
152
    const interval = setInterval(() => {
147
    const interval = setInterval(() => {
153
      if (!timeRef.current) {
148
      if (!timeRef.current) {
154
        setRemainingTime(() => getTimeDiff(0))
149
        setRemainingTime(() => getTimeDiff(0));
155
        setIsActive(false)
150
        setIsActive(false);
Línea 156... Línea 151...
156
        return
151
        return;
157
      }
152
      }
158
 
153
 
159
      if (!timeRef.current <= 60) {
154
      if (!timeRef.current <= 60) {
160
        timeRef.current -= 1
155
        timeRef.current -= 1;
Línea 161... Línea 156...
161
        setRemainingTime(() => getTimeDiff(timeRef.current))
156
        setRemainingTime(() => getTimeDiff(timeRef.current));
162
        return
157
        return;
163
      }
158
      }
Línea 164... Línea 159...
164
 
159
 
165
      timeRef.current -= 60
160
      timeRef.current -= 60;
166
      setRemainingTime(() => getTimeDiff(timeRef.current))
161
      setRemainingTime(() => getTimeDiff(timeRef.current));
167
    }, 60000)
162
    }, 60000);
168
 
163
 
169
    return () => {
164
    return () => {
170
      clearInterval(interval)
165
      clearInterval(interval);
171
    }
166
    };
172
  }, [])
167
  }, []);
173
 
168
 
174
  useEffect(() => {
169
  useEffect(() => {
175
    if (!votes) return
170
    if (!votes) return;
176
    const total = votes.reduce((acum, current) => acum + Number(current), 0)
171
    const total = votes.reduce((acum, current) => acum + Number(current), 0);
177
    setTotalVotes(total)
172
    setTotalVotes(total);
178
  }, [votes])
173
  }, [votes]);
179
 
174
 
180
  useEffect(() => {
175
  useEffect(() => {
Línea 228... Línea 223...
228
          )
223
          )
229
      )}
224
      )}
230
      <span>Tiempo restante: {remainingTime}</span>
225
      <span>Tiempo restante: {remainingTime}</span>
231
      {!isActive && <VoteTag>Tu voto ya fue emitido</VoteTag>}
226
      {!isActive && <VoteTag>Tu voto ya fue emitido</VoteTag>}
232
    </form>
227
    </form>
233
  )
228
  );
234
}
229
};
Línea 235... Línea 230...
235
 
230
 
236
const mapDispatchToProps = {
231
const mapDispatchToProps = {
237
  addNotification: (notification) => addNotification(notification),
232
  addNotification: (notification) => addNotification(notification),
238
  updateFeed: (payload) => updateFeed(payload),
233
  updateFeed: (payload) => updateFeed(payload),
Línea 239... Línea 234...
239
}
234
};