Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

Rev Autor Línea Nro. Línea
7076 stevensc 1
import React, { useRef, useState } 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
 
6631 stevensc 8
import ReactionIcon from '@mui/icons-material/Recommend'
9
import FunIcon from '../icons/Fun'
6625 stevensc 10
import LoveItIcon from '../icons/LoveIt'
6631 stevensc 11
import SupportIcon from '../icons/Support'
6625 stevensc 12
import InterestIcon from '../icons/Interest'
13
import RecommendIcon from '../icons/Recommned'
14
 
15
const ReactionsButton = ({
7076 stevensc 16
  currentReaction = 0,
6625 stevensc 17
  onChange,
18
  withLabel,
19
  reactionTypesUrl,
20
  deleteUrl,
21
}) => {
7076 stevensc 22
  const [reactionIndex, setReactionIndex] = useState(currentReaction)
6625 stevensc 23
  const [showReactions, setShowReactions] = useState(false)
24
  const rectionBtn = useRef(null)
25
  const dispatch = useDispatch()
7073 stevensc 26
  useOutsideClick(rectionBtn, () => setShowReactions(false))
6625 stevensc 27
 
7070 stevensc 28
  const reactionsOptions = [
29
    {
7076 stevensc 30
      label: 'Reaccionar',
31
      icon: <ReactionIcon />,
32
      type: 'default',
33
    },
34
    {
7070 stevensc 35
      type: 'r',
7075 stevensc 36
      icon: <RecommendIcon />,
7070 stevensc 37
      label: 'Me gusta',
38
    },
39
    {
40
      type: 's',
7075 stevensc 41
      icon: <SupportIcon />,
7070 stevensc 42
      label: 'Dar apoyo',
43
    },
44
    {
45
      type: 'l',
7075 stevensc 46
      icon: <LoveItIcon />,
7070 stevensc 47
      label: 'Me encanta',
48
    },
49
    {
50
      type: 'i',
7075 stevensc 51
      icon: <InterestIcon />,
7070 stevensc 52
      label: 'Me interesa',
53
    },
54
    {
55
      type: 'f',
7075 stevensc 56
      icon: <FunIcon />,
7070 stevensc 57
      label: 'Me divierte',
58
    },
59
  ]
60
 
7073 stevensc 61
  const saveReaction = (type) => {
7074 stevensc 62
    axios.post(reactionTypesUrl[type]).then((response) => {
63
      const { success, data } = response.data
6625 stevensc 64
 
65
      if (!success) {
66
        dispatch(addNotification({ style: 'danger', msg: data }))
7074 stevensc 67
        return
6625 stevensc 68
      }
69
 
7071 stevensc 70
      const typeIndex = reactionsOptions.findIndex(
71
        (option) => option.type === type
72
      )
7075 stevensc 73
 
6625 stevensc 74
      onChange(data.reactions)
7076 stevensc 75
      setReactionIndex(typeIndex)
6625 stevensc 76
    })
77
  }
78
 
7073 stevensc 79
  const deleteReaction = () => {
80
    axios.post(deleteUrl).then((res) => {
6625 stevensc 81
      const { success, data } = res.data
82
 
83
      if (!success) {
84
        dispatch(addNotification({ style: 'danger', msg: data }))
85
        return
86
      }
87
 
88
      onChange(data.reactions)
7076 stevensc 89
      setReactionIndex(0)
6625 stevensc 90
    })
91
  }
92
 
93
  const onHover = debounce(() => setShowReactions(true), 500)
94
 
95
  const onUnhover = debounce(() => setShowReactions(false), 500)
96
 
97
  return (
98
    <>
99
      <button
6630 stevensc 100
        className="btn position-relative"
6625 stevensc 101
        onMouseOver={onHover}
102
        onMouseOut={onUnhover}
103
        ref={rectionBtn}
7076 stevensc 104
        onClick={() => {
105
          reactionIndex
106
            ? deleteReaction()
107
            : saveReaction(reactionsOptions[0].type)
108
        }}
6625 stevensc 109
      >
7076 stevensc 110
        {reactionsOptions[reactionIndex].icon}
111
        {withLabel && reactionsOptions[reactionIndex].label}
112
 
6625 stevensc 113
        <div className={`reactions ${showReactions ? 'active' : ''}`}>
7075 stevensc 114
          {reactionsOptions.map(({ icon, type, label }) => (
115
            <button
116
              key={type}
117
              onClick={(e) => {
118
                e.stopPropagation()
119
                saveReaction(type)
120
              }}
121
              title={label}
122
            >
123
              {icon}
124
            </button>
125
          ))}
6625 stevensc 126
        </div>
127
      </button>
128
    </>
129
  )
130
}
131
 
132
export default ReactionsButton