Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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