Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 7072 | Rev 7074 | 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
 
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 defaultReaction = {
16
  label: 'Reaccionar',
6631 stevensc 17
  icon: ReactionIcon,
6625 stevensc 18
  type: 'default',
19
}
20
 
21
const ReactionsButton = ({
6626 stevensc 22
  currentReaction,
6625 stevensc 23
  onChange,
24
  withLabel,
25
  reactionTypesUrl,
26
  deleteUrl,
27
}) => {
6626 stevensc 28
  const [settedReaction, setSettedReaction] = useState(defaultReaction)
6625 stevensc 29
  const [showReactions, setShowReactions] = useState(false)
30
  const rectionBtn = useRef(null)
31
  const dispatch = useDispatch()
7073 stevensc 32
  useOutsideClick(rectionBtn, () => setShowReactions(false))
6625 stevensc 33
 
7073 stevensc 34
  const { icon: Icon } = settedReaction
35
 
7070 stevensc 36
  const reactionsOptions = [
37
    {
38
      type: 'r',
39
      icon: RecommendIcon,
40
      label: 'Me gusta',
41
    },
42
    {
43
      type: 's',
44
      icon: SupportIcon,
45
      label: 'Dar apoyo',
46
    },
47
    {
48
      type: 'l',
49
      icon: LoveItIcon,
50
      label: 'Me encanta',
51
    },
52
    {
53
      type: 'i',
54
      icon: InterestIcon,
55
      label: 'Me interesa',
56
    },
57
    {
58
      type: 'f',
59
      icon: FunIcon,
60
      label: 'Me divierte',
61
    },
62
  ]
63
 
7073 stevensc 64
  const saveReaction = (type) => {
65
    axios.post(reactionTypesUrl[type]).then((res) => {
6625 stevensc 66
      const { success, data } = res.data
67
 
68
      if (!success) {
69
        dispatch(addNotification({ style: 'danger', msg: data }))
70
      }
71
 
7071 stevensc 72
      const typeIndex = reactionsOptions.findIndex(
73
        (option) => option.type === type
74
      )
7069 stevensc 75
 
6625 stevensc 76
      onChange(data.reactions)
7071 stevensc 77
      setSettedReaction(reactionsOptions[typeIndex])
6625 stevensc 78
    })
79
  }
80
 
7073 stevensc 81
  const deleteReaction = () => {
82
    axios.post(deleteUrl).then((res) => {
6625 stevensc 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
    )
7073 stevensc 103
 
6626 stevensc 104
    if (!currentOption) {
105
      setSettedReaction(defaultReaction)
106
      return
107
    }
108
 
109
    setSettedReaction(currentOption)
110
  }, [currentReaction])
111
 
6625 stevensc 112
  return (
113
    <>
114
      <button
6630 stevensc 115
        className="btn position-relative"
6625 stevensc 116
        onMouseOver={onHover}
117
        onMouseOut={onUnhover}
118
        ref={rectionBtn}
119
        onClick={() =>
120
          settedReaction.type !== 'default'
121
            ? deleteReaction()
122
            : saveReaction(reactionsOptions[0].type)
123
        }
124
      >
6631 stevensc 125
        <Icon />
6625 stevensc 126
        {withLabel && settedReaction.label}
127
        <div className={`reactions ${showReactions ? 'active' : ''}`}>
128
          {reactionsOptions.map((reaction) => {
129
            const { icon: Icon, color, type, label } = reaction
130
 
131
            return (
132
              <button
133
                key={type}
134
                onClick={(e) => {
135
                  e.stopPropagation()
136
                  saveReaction(type)
137
                }}
138
                title={label}
139
              >
140
                <Icon style={{ color }} />
141
              </button>
142
            )
143
          })}
144
        </div>
145
      </button>
146
    </>
147
  )
148
}
149
 
150
export default ReactionsButton