Proyectos de Subversion LeadersLinked - SPA

Rev

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