Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 7071 | Rev 7073 | 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()
32
 
7070 stevensc 33
  const reactionsOptions = [
34
    {
35
      type: 'r',
36
      icon: RecommendIcon,
37
      label: 'Me gusta',
38
    },
39
    {
40
      type: 's',
41
      icon: SupportIcon,
42
      label: 'Dar apoyo',
43
    },
44
    {
45
      type: 'l',
46
      icon: LoveItIcon,
47
      label: 'Me encanta',
48
    },
49
    {
50
      type: 'i',
51
      icon: InterestIcon,
52
      label: 'Me interesa',
53
    },
54
    {
55
      type: 'f',
56
      icon: FunIcon,
57
      label: 'Me divierte',
58
    },
59
  ]
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
 
7071 stevensc 73
      const typeIndex = reactionsOptions.findIndex(
74
        (option) => option.type === type
75
      )
7069 stevensc 76
 
7072 stevensc 77
      console.log(typeIndex)
78
      console.log(reactionsOptions)
79
      console.log(reactionsOptions[typeIndex])
80
 
6625 stevensc 81
      onChange(data.reactions)
7071 stevensc 82
      setSettedReaction(reactionsOptions[typeIndex])
6625 stevensc 83
    })
84
  }
85
 
86
  const deleteReaction = async () => {
87
    await axios.post(deleteUrl).then((res) => {
88
      const { success, data } = res.data
89
 
90
      if (!success) {
91
        dispatch(addNotification({ style: 'danger', msg: data }))
92
        return
93
      }
94
 
95
      onChange(data.reactions)
96
      setSettedReaction(defaultReaction)
97
    })
98
  }
99
 
100
  const onHover = debounce(() => setShowReactions(true), 500)
101
 
102
  const onUnhover = debounce(() => setShowReactions(false), 500)
103
 
6626 stevensc 104
  useEffect(() => {
105
    const currentOption = reactionsOptions.find(
106
      (reaction) => reaction.icon === currentReaction
107
    )
108
    if (!currentOption) {
109
      setSettedReaction(defaultReaction)
110
      return
111
    }
112
 
113
    setSettedReaction(currentOption)
114
  }, [currentReaction])
115
 
6625 stevensc 116
  return (
117
    <>
118
      <button
6630 stevensc 119
        className="btn position-relative"
6625 stevensc 120
        onMouseOver={onHover}
121
        onMouseOut={onUnhover}
122
        ref={rectionBtn}
123
        onClick={() =>
124
          settedReaction.type !== 'default'
125
            ? deleteReaction()
126
            : saveReaction(reactionsOptions[0].type)
127
        }
128
      >
6631 stevensc 129
        <Icon />
6625 stevensc 130
        {withLabel && settedReaction.label}
131
        <div className={`reactions ${showReactions ? 'active' : ''}`}>
132
          {reactionsOptions.map((reaction) => {
133
            const { icon: Icon, color, type, label } = reaction
134
 
135
            return (
136
              <button
137
                key={type}
138
                onClick={(e) => {
139
                  e.stopPropagation()
140
                  saveReaction(type)
141
                }}
142
                title={label}
143
              >
144
                <Icon style={{ color }} />
145
              </button>
146
            )
147
          })}
148
        </div>
149
      </button>
150
    </>
151
  )
152
}
153
 
154
export default ReactionsButton