Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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