Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 6626 | Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
6625 stevensc 1
import React, { useRef, useState } from 'react'
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 = ({
55
  currentReaction = defaultReaction,
56
  onChange,
57
  withLabel,
58
  reactionTypesUrl,
59
  deleteUrl,
60
}) => {
61
  const [settedReaction, setSettedReaction] = useState(currentReaction)
62
  const [showReactions, setShowReactions] = useState(false)
63
  const rectionBtn = useRef(null)
64
  const dispatch = useDispatch()
65
 
66
  const { icon: Icon } = settedReaction
67
 
68
  useOutsideClick(rectionBtn, () => setShowReactions(false))
69
 
70
  const saveReaction = async (type) => {
71
    await axios.post(reactionTypesUrl[type]).then((res) => {
72
      const { success, data } = res.data
73
 
74
      if (!success) {
75
        dispatch(addNotification({ style: 'danger', msg: data }))
76
      }
77
 
78
      onChange(data.reactions)
79
      setSettedReaction(reactionsOptions[type])
80
    })
81
  }
82
 
83
  const deleteReaction = async () => {
84
    await axios.post(deleteUrl).then((res) => {
85
      const { success, data } = res.data
86
 
87
      if (!success) {
88
        dispatch(addNotification({ style: 'danger', msg: data }))
89
        return
90
      }
91
 
92
      onChange(data.reactions)
93
      setSettedReaction(defaultReaction)
94
    })
95
  }
96
 
97
  const onHover = debounce(() => setShowReactions(true), 500)
98
 
99
  const onUnhover = debounce(() => setShowReactions(false), 500)
100
 
101
  return (
102
    <>
103
      <button
104
        className="feed__share-option position-relative"
105
        onMouseOver={onHover}
106
        onMouseOut={onUnhover}
107
        ref={rectionBtn}
108
        onClick={() =>
109
          settedReaction.type !== 'default'
110
            ? deleteReaction()
111
            : saveReaction(reactionsOptions[0].type)
112
        }
113
      >
114
        <Icon />
115
        {withLabel && settedReaction.label}
116
        <div className={`reactions ${showReactions ? 'active' : ''}`}>
117
          {reactionsOptions.map((reaction) => {
118
            const { icon: Icon, color, type, label } = reaction
119
 
120
            return (
121
              <button
122
                key={type}
123
                onClick={(e) => {
124
                  e.stopPropagation()
125
                  saveReaction(type)
126
                }}
127
                title={label}
128
              >
129
                <Icon style={{ color }} />
130
              </button>
131
            )
132
          })}
133
        </div>
134
      </button>
135
    </>
136
  )
137
}
138
 
139
export default ReactionsButton