Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev Autor Línea Nro. Línea
3019 stevensc 1
import React, { useMemo } from 'react'
813 stevensc 2
import { useForm } from 'react-hook-form'
3017 stevensc 3
import { styled, Typography } from '@mui/material'
2970 stevensc 4
import { Public, LockClock } from '@mui/icons-material'
5 stevensc 5
 
2969 stevensc 6
import { axios } from '@utils'
2970 stevensc 7
import { updateFeed } from '@store/feed/feed.actions'
2969 stevensc 8
import { addNotification } from '@store/notification/notification.actions'
5 stevensc 9
 
3017 stevensc 10
import Widget from '@components/UI/Widget'
3018 stevensc 11
import CheckboxInput from '@components/UI/inputs/Checkbox'
2970 stevensc 12
 
3018 stevensc 13
const AnswerContainer = styled('div')`
5 stevensc 14
  display: flex;
15
  align-items: center;
16
  gap: 0.5rem;
17
  padding: 0.5rem 1rem;
18
  border: 2px solid var(--border-primary);
19
  border-radius: 50px;
20
  cursor: pointer;
21
  transition: all 200ms ease;
22
  position: relative;
23
  overflow: hidden;
24
  margin-bottom: 0.5rem;
25
  &:hover {
26
    border-color: var(--font-color);
27
    text-shadow: 0 0 1px var(--font-color);
28
  }
813 stevensc 29
`
5 stevensc 30
 
3017 stevensc 31
const SurveyForm = ({
32
  active = false,
33
  question = '¿Cómo consideras el ambiente laboral?',
34
  answers = [],
35
  votes = [],
36
  time = 0,
37
  voteUrl = '/feed/vote/d454717c-ba6f-485c-b94c-4fbb5f5bed94',
38
  resultType = 'pu'
39
}) => {
3019 stevensc 40
  const totalVotes = useMemo(
41
    () =>
42
      votes.reduce((acc, cur) => {
43
        if (!cur) return acc
44
        return acc + cur
45
      }, 0),
46
    [votes]
47
  )
48
 
3018 stevensc 49
  const { control, handleSubmit } = useForm()
5 stevensc 50
 
3014 stevensc 51
  const sendVote = handleSubmit(({ vote }) => {
52
    const formData = new FormData()
53
    formData.append('vote', vote)
54
 
55
    axios
56
      .post(voteUrl, formData)
57
      .then(({ data: response }) => {
58
        const { success, data } = response
59
 
60
        if (!success) {
61
          const errorMessage =
62
            typeof data === 'string'
63
              ? data
64
              : 'Error interno, por favor intente mas tarde.'
65
          throw new Error(errorMessage)
66
        }
67
 
68
        updateFeed({ feed: data, uuid: data.feed_uuid })
69
        addNotification({ style: 'success', msg: 'Voto emitido con exito' })
70
      })
71
      .catch((err) => {
72
        addNotification({ style: 'danger', msg: err.message })
73
      })
74
  })
75
 
3019 stevensc 76
  function getTimeDiff(segundos) {
77
    const currentDate = new Date()
78
    const futureDate = new Date(currentDate.getTime() + segundos * 1000)
79
    const diff = futureDate - currentDate
3014 stevensc 80
 
3019 stevensc 81
    const days = Math.floor(diff / (1000 * 60 * 60 * 24))
82
    const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60))
83
    const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60))
84
 
85
    return `${addZero(days)}d ${addZero(hours)}h ${addZero(minutes)}m`
86
  }
87
 
88
  function addZero(unit) {
89
    return String(unit).padStart(2, '0')
90
  }
91
 
92
  function getPorcentage(n, total) {
93
    return (n / total) * 100
94
  }
95
 
3017 stevensc 96
  return (
97
    <Widget>
98
      <Widget.Body>
99
        <Typography variant='h3'>{question}</Typography>
3014 stevensc 100
 
3017 stevensc 101
        {resultType === 'pu' ? (
102
          <Typography
103
            variant='overline'
104
            title='El número de votos es visible para todos los usuarios'
3019 stevensc 105
            sx={{ paddingBottom: (theme) => theme.spacing(0.5) }}
3017 stevensc 106
          >
107
            <Public /> Público
108
          </Typography>
109
        ) : (
110
          <Typography
111
            variant='overline'
112
            title='Los resultados de la votación son privados'
3019 stevensc 113
            sx={{ paddingBottom: (theme) => theme.spacing(0.5) }}
3017 stevensc 114
          >
115
            <LockClock /> Privado
116
          </Typography>
117
        )}
3014 stevensc 118
 
3020 stevensc 119
        <form onChange={sendVote}>
3017 stevensc 120
          {answers.map((answer, index) => {
121
            if (answer === null) return null
3014 stevensc 122
 
3017 stevensc 123
            return (
3019 stevensc 124
              <AnswerContainer
125
                key={answer}
126
                sx={{
127
                  '::before': {
128
                    content: '',
129
                    position: 'absolute',
130
                    left: 0,
131
                    top: 0,
132
                    height: '100%',
133
                    backgroundColor: '#0002',
134
                    zIndex: 4,
135
                    width: totalVotes
136
                      ? `${getPorcentage(votes[index], totalVotes)}%`
137
                      : 0
138
                  }
139
                }}
140
              >
3018 stevensc 141
                <CheckboxInput
3017 stevensc 142
                  name='vote'
143
                  value={index + 1}
3018 stevensc 144
                  label={answer}
145
                  control={control}
3019 stevensc 146
                  styles={{
147
                    display: 'flex',
148
                    alignItems: 'center',
149
                    gap: 0.5
150
                  }}
3017 stevensc 151
                />
3018 stevensc 152
 
3020 stevensc 153
                {totalVotes ? (
3019 stevensc 154
                  <Typography variant='overline'>
155
                    {getPorcentage(votes[index], totalVotes)}%
156
                  </Typography>
3020 stevensc 157
                ) : null}
3018 stevensc 158
              </AnswerContainer>
3017 stevensc 159
            )
160
          })}
3018 stevensc 161
 
162
          <Typography variant='overline'>Tiempo restante: </Typography>
163
 
164
          {!active && (
165
            <Typography variant='overline'>
166
              El formulario ya ha finalizado
167
            </Typography>
168
          )}
3017 stevensc 169
        </form>
170
      </Widget.Body>
171
    </Widget>
813 stevensc 172
  )
173
}
5 stevensc 174
 
2969 stevensc 175
export default SurveyForm