Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3378 | Rev 3416 | 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'
3330 stevensc 3
import { Typography } from '@mui/material'
5 stevensc 4
 
3330 stevensc 5
import { axios } from '@utils'
2780 stevensc 6
import { useFetch } from '@hooks'
3330 stevensc 7
import { addNotification } from '@store/notification/notification.actions'
5 stevensc 8
 
3330 stevensc 9
import Widget from '@components/UI/Widget'
3374 stevensc 10
 
3330 stevensc 11
import LoadingWrapper from '@components/common/loading-wrapper'
3374 stevensc 12
import EmojiGroup from '@components/common/emoji-selector'
2268 stevensc 13
 
3330 stevensc 14
const DailyPulse = ({ dailyPulseUrl = '' }) => {
15
  const [isSubmitting, setIsSubmitting] = useState(false)
16
  const dispatch = useDispatch()
41 stevensc 17
 
3332 stevensc 18
  const { data, isLoading, refetch } = useFetch(dailyPulseUrl, {
3331 stevensc 19
    emojis_how_are_you_feel: [],
20
    emojis_climate_on_your_organization: []
3330 stevensc 21
  })
5 stevensc 22
 
3374 stevensc 23
  const saveEmoji = async (link_save = '') => {
3330 stevensc 24
    try {
25
      setIsSubmitting(true)
3332 stevensc 26
      const response = await axios.post(link_save)
3330 stevensc 27
      const { data, success } = response.data
691 stevensc 28
 
3330 stevensc 29
      if (!success) {
3378 stevensc 30
        const errMsg = typeof data === 'string' ? data : 'Error al guardar'
3330 stevensc 31
        throw new Error(errMsg)
32
      }
33
 
3332 stevensc 34
      refetch()
3330 stevensc 35
    } catch (error) {
36
      dispatch(addNotification({ style: 'danger', msg: error.message }))
37
    } finally {
38
      setIsSubmitting(false)
39
    }
2281 stevensc 40
  }
41
 
3378 stevensc 42
  if (
3379 stevensc 43
    data.emojis_how_are_you_feel.length <= 1 &&
44
    data.emojis_climate_on_your_organization.length <= 1
3378 stevensc 45
  ) {
46
    return null
47
  }
48
 
2268 stevensc 49
  return (
2281 stevensc 50
    <Widget>
2276 stevensc 51
      <Widget.Header title='Pulso Diario' />
5 stevensc 52
 
3330 stevensc 53
      <Widget.Body>
54
        <LoadingWrapper
55
          loading={isLoading || isSubmitting}
56
          displayChildren={isSubmitting}
57
        >
3331 stevensc 58
          <Typography variant='h4' textAlign='center'>
59
            ¿Como te sientes hoy?
60
          </Typography>
3330 stevensc 61
 
3374 stevensc 62
          <EmojiGroup>
63
            {data.emojis_how_are_you_feel?.map(
64
              ({ id, image, link_save }, index) => (
65
                <EmojiGroup.Item
66
                  key={id}
67
                  image={image}
68
                  index={index}
69
                  onClick={() => saveEmoji(link_save)}
70
                />
71
              )
72
            )}
73
          </EmojiGroup>
74
 
3331 stevensc 75
          <Typography variant='h4' textAlign='center'>
3330 stevensc 76
            ¿Como esta el clima en la organización?
77
          </Typography>
3374 stevensc 78
 
79
          <EmojiGroup>
80
            {data.emojis_climate_on_your_organization?.map(
81
              ({ id, image, link_save }, index) => (
82
                <EmojiGroup.Item
83
                  key={id}
84
                  image={image}
85
                  index={index}
86
                  onClick={() => saveEmoji(link_save)}
87
                />
88
              )
89
            )}
90
          </EmojiGroup>
3330 stevensc 91
        </LoadingWrapper>
2268 stevensc 92
      </Widget.Body>
93
    </Widget>
691 stevensc 94
  )
95
}
5 stevensc 96
 
691 stevensc 97
export default DailyPulse