Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 2780 | Rev 3331 | 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, {
18
    emojis_how_are_you_feel: '',
19
    emojis_climate_on_your_organization: ''
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
        >
68
          <Typography variant='h4'>¿Como te sientes hoy?</Typography>
69
          <EmojiSelector
70
            options={data.emojis_how_are_you_feel}
71
            onSelect={saveEmoji}
72
          />
73
 
74
          <Typography variant='h4'>
75
            ¿Como esta el clima en la organización?
76
          </Typography>
77
          <EmojiSelector
78
            options={data.emojis_climate_on_your_organization}
79
            onSelect={saveEmoji}
80
          />
81
        </LoadingWrapper>
2268 stevensc 82
      </Widget.Body>
83
    </Widget>
691 stevensc 84
  )
85
}
5 stevensc 86
 
691 stevensc 87
export default DailyPulse