Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

Rev Autor Línea Nro. Línea
6626 stevensc 1
import React, { useRef, useState, useEffect } from 'react'
6625 stevensc 2
import { useDispatch } from 'react-redux'
3
import { axios, debounce } from '../../../utils'
4
import useOutsideClick from '../../../hooks/useOutsideClick'
5
 
6
import { addNotification } from '../../../redux/notification/notification.actions'
7
 
8
import LoveItIcon from '../icons/LoveIt'
9
import InterestIcon from '../icons/Interest'
10
import RecommendIcon from '../icons/Recommned'
11
import FunIcon from '../icons/Fun'
12
import SupportIcon from '../icons/Support'
13
 
14
const reactionsOptions = [
15
  {
16
    type: 'r',
17
    icon: RecommendIcon,
18
    color: '#7405f9',
19
    label: 'Me gusta',
20
  },
21
  {
22
    type: 's',
23
    icon: SupportIcon,
24
    color: '#6495ED',
25
    label: 'Dar apoyo',
26
  },
27
  {
28
    type: 'l',
29
    icon: LoveItIcon,
30
    color: '#DF704D',
31
    label: 'Me encanta',
32
  },
33
  {
34
    type: 'i',
35
    icon: InterestIcon,
36
    color: '#F5BB5C',
37
    label: 'Me interesa',
38
  },
39
  {
40
    type: 'f',
41
    icon: FunIcon,
42
    color: '#FF7F50',
43
    label: 'Me divierte',
44
  },
45
]
46
 
47
const defaultReaction = {
48
  label: 'Reaccionar',
49
  icon: RecommendIcon,
50
  color: '#626d7a',
51
  type: 'default',
52
}
53
 
54
const ReactionsButton = ({
6626 stevensc 55
  currentReaction,
6625 stevensc 56
  onChange,
57
  withLabel,
58
  reactionTypesUrl,
59
  deleteUrl,
60
}) => {
6626 stevensc 61
  const [settedReaction, setSettedReaction] = useState(defaultReaction)
6625 stevensc 62
  const [showReactions, setShowReactions] = useState(false)
63
  const rectionBtn = useRef(null)
64
  const dispatch = useDispatch()
65
 
66
  useOutsideClick(rectionBtn, () => setShowReactions(false))
67
 
68
  const saveReaction = async (type) => {
69
    await axios.post(reactionTypesUrl[type]).then((res) => {
70
      const { success, data } = res.data
71
 
72
      if (!success) {
73
        dispatch(addNotification({ style: 'danger', msg: data }))
74
      }
75
 
76
      onChange(data.reactions)
77
      setSettedReaction(reactionsOptions[type])
78
    })
79
  }
80
 
81
  const deleteReaction = async () => {
82
    await axios.post(deleteUrl).then((res) => {
83
      const { success, data } = res.data
84
 
85
      if (!success) {
86
        dispatch(addNotification({ style: 'danger', msg: data }))
87
        return
88
      }
89
 
90
      onChange(data.reactions)
91
      setSettedReaction(defaultReaction)
92
    })
93
  }
94
 
95
  const onHover = debounce(() => setShowReactions(true), 500)
96
 
97
  const onUnhover = debounce(() => setShowReactions(false), 500)
98
 
6626 stevensc 99
  useEffect(() => {
100
    const currentOption = reactionsOptions.find(
101
      (reaction) => reaction.icon === currentReaction
102
    )
103
    if (!currentOption) {
104
      setSettedReaction(defaultReaction)
105
      return
106
    }
107
 
108
    setSettedReaction(currentOption)
109
  }, [currentReaction])
110
 
6625 stevensc 111
  return (
112
    <>
113
      <button
114
        className="feed__share-option position-relative"
115
        onMouseOver={onHover}
116
        onMouseOut={onUnhover}
117
        ref={rectionBtn}
118
        onClick={() =>
119
          settedReaction.type !== 'default'
120
            ? deleteReaction()
121
            : saveReaction(reactionsOptions[0].type)
122
        }
123
      >
124
        {withLabel && settedReaction.label}
125
        <div className={`reactions ${showReactions ? 'active' : ''}`}>
126
          {reactionsOptions.map((reaction) => {
127
            const { icon: Icon, color, type, label } = reaction
128
 
129
            return (
130
              <button
131
                key={type}
132
                onClick={(e) => {
133
                  e.stopPropagation()
134
                  saveReaction(type)
135
                }}
136
                title={label}
137
              >
138
                <Icon style={{ color }} />
139
              </button>
140
            )
141
          })}
142
        </div>
143
      </button>
144
    </>
145
  )
146
}
147
 
148
export default ReactionsButton