Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

Rev Autor Línea Nro. Línea
6515 stevensc 1
import React, { useEffect, useState } from 'react'
2
import { axios } from '../../../utils'
3
import { useDispatch } from 'react-redux'
4
 
5
import { addNotification } from '../../../redux/notification/notification.actions'
6
 
6548 stevensc 7
const DailyPulse = ({ dailyPulseUrl }) => {
6515 stevensc 8
  const [emojisHowAreYouFeel, setEmojisHowAreYouFeel] = useState([])
9
  const [emojisClimateOnYourOrganization, setEmojisClimateOnYourOrganization] =
10
    useState([])
11
 
6548 stevensc 12
  const getData = async (url) => {
6515 stevensc 13
    try {
14
      const { data: response } = await axios.get(url)
15
      if (response.success) {
16
        setEmojisHowAreYouFeel(response.data.emojis_how_are_you_feel)
17
        setEmojisClimateOnYourOrganization(
18
          response.data.emojis_climate_on_your_organization
19
        )
20
      }
21
    } catch (error) {
22
      console.log(error)
23
    }
24
  }
25
 
26
  useEffect(() => {
6548 stevensc 27
    getData(dailyPulseUrl)
6515 stevensc 28
  }, [])
29
 
30
  return (
31
    <div className="daily_pulse-widget">
32
      <h3>Pulso Diario</h3>
33
      <DailyPulse.List
34
        options={emojisHowAreYouFeel}
35
        title="¿Como te sientes hoy?"
36
        onComplete={getData}
37
      />
38
      <DailyPulse.List
39
        options={emojisClimateOnYourOrganization}
40
        title="¿Como esta el clima en la organización?"
41
        onComplete={getData}
42
      />
43
    </div>
44
  )
45
}
46
 
47
const PulseList = ({ options = [], title = '', onComplete = () => null }) => {
48
  const dispatch = useDispatch()
49
 
50
  const handleEmojiSave = (url) => {
51
    if (!url) {
52
      return false
53
    }
54
 
55
    axios.post(url).then(({ data }) => {
56
      if (!data.success) {
57
        return dispatch(
58
          addNotification({
59
            style: 'danger',
60
            msg:
61
              typeof data.data === 'string'
62
                ? data.data
63
                : 'Ha ocurrido un error',
64
          })
65
        )
66
      }
67
 
68
      return onComplete()
69
    })
70
  }
71
 
72
  return (
73
    <div className="daily_pulse-quest">
74
      <h4>{title}</h4>
75
      <ul>
76
        {options.map(({ link_save, id, image }, index) => (
77
          <li key={id}>
78
            <a
79
              href={link_save}
80
              onClick={(e) => {
81
                e.preventDefault()
82
                handleEmojiSave(link_save)
83
              }}
84
            >
85
              <img
86
                className="fadedown"
87
                src={image}
88
                style={{ animationDelay: `${index + 10}00ms` }}
89
              />
90
            </a>
91
          </li>
92
        ))}
93
      </ul>
94
    </div>
95
  )
96
}
97
 
98
DailyPulse.List = PulseList
99
 
100
export default DailyPulse