Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3021 | Rev 3076 | 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'
3021 stevensc 7
import { getTimeDiff } from '@utils/dates'
2970 stevensc 8
import { updateFeed } from '@store/feed/feed.actions'
2969 stevensc 9
import { addNotification } from '@store/notification/notification.actions'
5 stevensc 10
 
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
  )
3021 stevensc 49
  const timeRemaining = useMemo(() => getTimeDiff(time), [time])
3019 stevensc 50
 
3018 stevensc 51
  const { control, handleSubmit } = useForm()
5 stevensc 52
 
3014 stevensc 53
  const sendVote = handleSubmit(({ vote }) => {
54
    const formData = new FormData()
55
    formData.append('vote', vote)
56
 
57
    axios
58
      .post(voteUrl, formData)
59
      .then(({ data: response }) => {
60
        const { success, data } = response
61
 
62
        if (!success) {
63
          const errorMessage =
64
            typeof data === 'string'
65
              ? data
66
              : 'Error interno, por favor intente mas tarde.'
67
          throw new Error(errorMessage)
68
        }
69
 
70
        updateFeed({ feed: data, uuid: data.feed_uuid })
71
        addNotification({ style: 'success', msg: 'Voto emitido con exito' })
72
      })
73
      .catch((err) => {
74
        addNotification({ style: 'danger', msg: err.message })
75
      })
76
  })
77
 
3019 stevensc 78
  function getPorcentage(n, total) {
79
    return (n / total) * 100
80
  }
81
 
3017 stevensc 82
  return (
83
    <Widget>
84
      <Widget.Body>
85
        <Typography variant='h3'>{question}</Typography>
3014 stevensc 86
 
3017 stevensc 87
        {resultType === 'pu' ? (
88
          <Typography
89
            variant='overline'
90
            title='El número de votos es visible para todos los usuarios'
3019 stevensc 91
            sx={{ paddingBottom: (theme) => theme.spacing(0.5) }}
3017 stevensc 92
          >
3021 stevensc 93
            <Public sx={{ fontSize: '1.3rem' }} /> Público
3017 stevensc 94
          </Typography>
95
        ) : (
96
          <Typography
97
            variant='overline'
98
            title='Los resultados de la votación son privados'
3019 stevensc 99
            sx={{ paddingBottom: (theme) => theme.spacing(0.5) }}
3017 stevensc 100
          >
3021 stevensc 101
            <LockClock sx={{ fontSize: '1.3rem' }} /> Privado
3017 stevensc 102
          </Typography>
103
        )}
3014 stevensc 104
 
3020 stevensc 105
        <form onChange={sendVote}>
3017 stevensc 106
          {answers.map((answer, index) => {
107
            if (answer === null) return null
3014 stevensc 108
 
3017 stevensc 109
            return (
3019 stevensc 110
              <AnswerContainer
111
                key={answer}
112
                sx={{
113
                  '::before': {
114
                    content: '',
115
                    position: 'absolute',
116
                    left: 0,
117
                    top: 0,
118
                    height: '100%',
119
                    backgroundColor: '#0002',
120
                    zIndex: 4,
121
                    width: totalVotes
122
                      ? `${getPorcentage(votes[index], totalVotes)}%`
123
                      : 0
124
                  }
125
                }}
126
              >
3018 stevensc 127
                <CheckboxInput
3017 stevensc 128
                  name='vote'
129
                  value={index + 1}
3018 stevensc 130
                  label={answer}
131
                  control={control}
3023 stevensc 132
                  disabled={!active}
3021 stevensc 133
                  labelStyles={{
3019 stevensc 134
                    display: 'flex',
135
                    alignItems: 'center',
3021 stevensc 136
                    gap: 0.5,
137
                    margin: 0
3019 stevensc 138
                  }}
3021 stevensc 139
                  checkBoxStyles={{ padding: 0 }}
3017 stevensc 140
                />
3018 stevensc 141
 
3020 stevensc 142
                {totalVotes ? (
3019 stevensc 143
                  <Typography variant='overline'>
144
                    {getPorcentage(votes[index], totalVotes)}%
145
                  </Typography>
3020 stevensc 146
                ) : null}
3018 stevensc 147
              </AnswerContainer>
3017 stevensc 148
            )
149
          })}
3018 stevensc 150
 
3021 stevensc 151
          <Typography variant='overline'>
152
            Tiempo restante: {timeRemaining}
153
          </Typography>
3018 stevensc 154
 
155
          {!active && (
156
            <Typography variant='overline'>
157
              El formulario ya ha finalizado
158
            </Typography>
159
          )}
3017 stevensc 160
        </form>
161
      </Widget.Body>
162
    </Widget>
813 stevensc 163
  )
164
}
5 stevensc 165
 
2969 stevensc 166
export default SurveyForm