Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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