Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 7076 | Rev 7078 | 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
 
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
 
7077 stevensc 97
  useEffect(() => {
98
    const currentIndex = reactionsOptions.findIndex(
99
      ({ type }) => type === currentReaction
100
    )
101
    setReactionIndex(currentIndex)
102
  }, [currentReaction])
103
 
6625 stevensc 104
  return (
7077 stevensc 105
    <button
106
      className="btn position-relative"
107
      onMouseOver={onHover}
108
      onMouseOut={onUnhover}
109
      ref={rectionBtn}
110
      onClick={() => {
111
        reactionIndex
112
          ? deleteReaction()
113
          : saveReaction(reactionsOptions[0].type)
114
      }}
115
    >
116
      {reactionsOptions[reactionIndex].icon}
117
      {withLabel && reactionsOptions[reactionIndex].label}
7076 stevensc 118
 
7077 stevensc 119
      <div className={`reactions ${showReactions ? 'active' : ''}`}>
120
        {reactionsOptions.map(({ icon, type, label }) => (
121
          <button
122
            key={type}
123
            onClick={(e) => {
124
              e.stopPropagation()
125
              saveReaction(type)
126
            }}
127
            title={label}
128
          >
129
            {icon}
130
          </button>
131
        ))}
132
      </div>
133
    </button>
6625 stevensc 134
  )
135
}
136
 
137
export default ReactionsButton