Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

Rev Autor Línea Nro. Línea
7077 stevensc 1
import React, { useEffect, 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 = ({
7077 stevensc 16
  currentReaction,
6625 stevensc 17
  onChange,
18
  withLabel,
19
  reactionTypesUrl,
20
  deleteUrl,
21
}) => {
7077 stevensc 22
  const [reactionIndex, setReactionIndex] = useState(0)
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
 
70
      onChange(data.reactions)
71
    })
72
  }
73
 
7073 stevensc 74
  const deleteReaction = () => {
75
    axios.post(deleteUrl).then((res) => {
6625 stevensc 76
      const { success, data } = res.data
77
 
78
      if (!success) {
79
        dispatch(addNotification({ style: 'danger', msg: data }))
80
        return
81
      }
82
 
83
      onChange(data.reactions)
7076 stevensc 84
      setReactionIndex(0)
6625 stevensc 85
    })
86
  }
87
 
88
  const onHover = debounce(() => setShowReactions(true), 500)
89
 
90
  const onUnhover = debounce(() => setShowReactions(false), 500)
91
 
7077 stevensc 92
  useEffect(() => {
93
    const currentIndex = reactionsOptions.findIndex(
94
      ({ type }) => type === currentReaction
95
    )
96
    setReactionIndex(currentIndex)
97
  }, [currentReaction])
98
 
6625 stevensc 99
  return (
7077 stevensc 100
    <button
101
      className="btn position-relative"
102
      onMouseOver={onHover}
103
      onMouseOut={onUnhover}
104
      ref={rectionBtn}
105
      onClick={() => {
106
        reactionIndex
107
          ? deleteReaction()
108
          : saveReaction(reactionsOptions[0].type)
109
      }}
110
    >
111
      {reactionsOptions[reactionIndex].icon}
112
      {withLabel && reactionsOptions[reactionIndex].label}
7076 stevensc 113
 
7077 stevensc 114
      <div className={`reactions ${showReactions ? 'active' : ''}`}>
7078 stevensc 115
        {reactionsOptions.map(({ icon, type, label }, index) => (
7077 stevensc 116
          <button
117
            key={type}
118
            onClick={(e) => {
119
              e.stopPropagation()
120
              saveReaction(type)
7078 stevensc 121
              setReactionIndex(index)
7077 stevensc 122
            }}
123
            title={label}
124
          >
125
            {icon}
126
          </button>
127
        ))}
128
      </div>
129
    </button>
6625 stevensc 130
  )
131
}
132
 
133
export default ReactionsButton