Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3330 | Rev 3332 | 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'
10
import EmojiSelector from '@components/common/emoji-selector'
11
import LoadingWrapper from '@components/common/loading-wrapper'
2268 stevensc 12
 
3330 stevensc 13
const DailyPulse = ({ dailyPulseUrl = '' }) => {
14
  const [isSubmitting, setIsSubmitting] = useState(false)
15
  const dispatch = useDispatch()
41 stevensc 16
 
3330 stevensc 17
  const { data, isLoading, mutate } = useFetch(dailyPulseUrl, {
3331 stevensc 18
    emojis_how_are_you_feel: [],
19
    emojis_climate_on_your_organization: []
3330 stevensc 20
  })
5 stevensc 21
 
3330 stevensc 22
  const saveEmoji = async (url = '') => {
23
    try {
24
      setIsSubmitting(true)
25
      const response = await axios.post(url)
26
      const { data, success } = response.data
691 stevensc 27
 
3330 stevensc 28
      if (!success) {
29
        const errMsg = typeof data === 'string' ? data : 'Ha ocurrido un error'
30
        throw new Error(errMsg)
31
      }
32
 
33
      const isClimate = url.includes('climate')
34
 
35
      if (isClimate) {
36
        const selectedEmojis = data.emojis_climate_on_your_organization.filter(
37
          (emoji) => emoji.link_save === url
38
        )
39
        mutate({
40
          ...data,
41
          emojis_climate_on_your_organization: selectedEmojis
42
        })
43
      } else {
44
        const selectedEmojis = data.emojis_how_are_you_feel.filter(
45
          (emoji) => emoji.link_save === url
46
        )
47
        mutate({
48
          ...data,
49
          emojis_how_are_you_feel: selectedEmojis
50
        })
51
      }
52
    } catch (error) {
53
      dispatch(addNotification({ style: 'danger', msg: error.message }))
54
    } finally {
55
      setIsSubmitting(false)
56
    }
2281 stevensc 57
  }
58
 
2268 stevensc 59
  return (
2281 stevensc 60
    <Widget>
2276 stevensc 61
      <Widget.Header title='Pulso Diario' />
5 stevensc 62
 
3330 stevensc 63
      <Widget.Body>
64
        <LoadingWrapper
65
          loading={isLoading || isSubmitting}
66
          displayChildren={isSubmitting}
67
        >
3331 stevensc 68
          <Typography variant='h4' textAlign='center'>
69
            ¿Como te sientes hoy?
70
          </Typography>
3330 stevensc 71
          <EmojiSelector
72
            options={data.emojis_how_are_you_feel}
73
            onSelect={saveEmoji}
74
          />
75
 
3331 stevensc 76
          <Typography variant='h4' textAlign='center'>
3330 stevensc 77
            ¿Como esta el clima en la organización?
78
          </Typography>
79
          <EmojiSelector
80
            options={data.emojis_climate_on_your_organization}
81
            onSelect={saveEmoji}
82
          />
83
        </LoadingWrapper>
2268 stevensc 84
      </Widget.Body>
85
    </Widget>
691 stevensc 86
  )
87
}
5 stevensc 88
 
691 stevensc 89
export default DailyPulse