Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev Autor Línea Nro. Línea
2268 stevensc 1
import React, { useState } from 'react'
691 stevensc 2
import { useDispatch } from 'react-redux'
2281 stevensc 3
import { Box, styled } from '@mui/material'
5 stevensc 4
 
2268 stevensc 5
import { axios } from '@app/utils'
6
import { addNotification } from '@app/redux/notification/notification.actions'
2780 stevensc 7
import { useFetch } from '@hooks'
5 stevensc 8
 
2268 stevensc 9
import Widget from '@app/components/UI/Widget'
10
import Spinner from '@app/components/UI/Spinner'
11
 
2276 stevensc 12
const EmojisList = styled('div')(({ theme }) => ({
13
  display: 'flex',
14
  gap: theme.spacing(0.5),
15
  justifyContent: 'center',
16
  padding: theme.spacing(0.5, 0)
17
}))
41 stevensc 18
 
2268 stevensc 19
const EmojiItem = styled('button')`
20
  width: 32px;
21
  height: 32px;
22
  border-radius: 50%;
2274 stevensc 23
  transition: all 300ms;
2268 stevensc 24
  & > img {
25
    height: 100%;
26
    object-fit: cover;
2271 stevensc 27
    width: 100%;
2268 stevensc 28
  }
2272 stevensc 29
  &:hover {
2271 stevensc 30
    transform: translateY(-5px);
2268 stevensc 31
  }
691 stevensc 32
`
5 stevensc 33
 
691 stevensc 34
const DailyPulse = ({ dailyPulseUrl = '' }) => {
2268 stevensc 35
  const { data, refetch } = useFetch(dailyPulseUrl)
36
  const {
37
    emojis_how_are_you_feel: emojisHowAreYouFeel = [],
38
    emojis_climate_on_your_organization: emojisClimateOrganization
39
  } = data || {}
40
  const completed =
41
    emojisHowAreYouFeel.length === 1 && emojisClimateOrganization.length === 1
691 stevensc 42
 
2281 stevensc 43
  if (completed) {
44
    return null
45
  }
46
 
2268 stevensc 47
  return (
2281 stevensc 48
    <Widget>
2276 stevensc 49
      <Widget.Header title='Pulso Diario' />
5 stevensc 50
 
2271 stevensc 51
      <Widget.Body
52
        styles={{ textAlign: 'center', '& h4': { fontWeight: 400 } }}
53
      >
2268 stevensc 54
        <Emojis
55
          title='¿Como te sientes hoy?'
56
          options={emojisHowAreYouFeel}
57
          onComplete={refetch}
58
        />
59
        <Emojis
60
          title='¿Como esta el clima en la organización?'
61
          options={emojisClimateOrganization}
62
          onComplete={refetch}
63
        />
64
      </Widget.Body>
65
    </Widget>
691 stevensc 66
  )
67
}
5 stevensc 68
 
2268 stevensc 69
const Emojis = ({ options = [], title = '', onComplete = () => null }) => {
695 stevensc 70
  const [isLoading, setIsLoading] = useState(false)
691 stevensc 71
  const dispatch = useDispatch()
5 stevensc 72
 
73
  const handleEmojiSave = (url) => {
691 stevensc 74
    if (!url) return
75
    setIsLoading(true)
5 stevensc 76
 
691 stevensc 77
    axios
78
      .post(url)
79
      .then(({ data: responseData }) => {
80
        const { data, success } = responseData
5 stevensc 81
 
691 stevensc 82
        if (!success) {
83
          const errorMessage =
84
            typeof data === 'string' ? data : 'Ha ocurrido un error'
85
          throw new Error(errorMessage)
86
        }
5 stevensc 87
 
691 stevensc 88
        onComplete()
89
      })
90
      .catch((error) => {
91
        dispatch(addNotification({ style: 'danger', msg: error.message }))
92
      })
93
      .finally(() => setIsLoading(false))
94
  }
95
 
5 stevensc 96
  return (
41 stevensc 97
    <>
5 stevensc 98
      <h4>{title}</h4>
2268 stevensc 99
      <EmojisList>
5 stevensc 100
        {options.map(({ link_save, id, image }, index) => (
2268 stevensc 101
          <EmojiItem onClick={() => handleEmojiSave(link_save)} key={id}>
102
            <img
103
              className='fadedown'
104
              src={image}
105
              style={{ animationDelay: `${index + 10}00ms` }}
106
            />
107
          </EmojiItem>
5 stevensc 108
        ))}
2268 stevensc 109
      </EmojisList>
2281 stevensc 110
      {isLoading && (
111
        <Box
112
          sx={{
113
            position: 'absolute !important',
114
            top: '50%',
115
            left: '50%',
116
            margin: 0,
117
            transform: 'translate(-50%, -50%)',
118
            zIndex: 100
119
          }}
120
        >
121
          <Spinner />
122
        </Box>
123
      )}
41 stevensc 124
    </>
691 stevensc 125
  )
126
}
5 stevensc 127
 
691 stevensc 128
export default DailyPulse