Proyectos de Subversion LeadersLinked - SPA

Rev

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