Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
6623 stevensc 1
/* eslint-disable react/display-name */
2
import React, { useEffect, useRef, useState } from 'react'
3
import RecommendIcon from './reactions/Recommned'
4
import FavoriteIcon from './reactions/LoveIt'
5
import VolunteerActivismIcon from './reactions/Support'
6
import EmojiEmotionsIcon from './reactions/Fun'
7
import TungstenIcon from './reactions/UserIdea'
8
 
9
import { debounce } from '../../../utils'
10
import useOutsideClick from '../../../hooks/useOutsideClick'
11
 
12
const withReactions = (
13
  Component,
14
  { onSelect, onDelete, myReaction, withTitle = false }
15
) => {
16
  return () => {
17
    const [settedReaction, setSettedReaction] = useState(null)
18
    const [showReactions, setShowReactions] = useState(false)
19
    const rectionBtn = useRef(null)
20
    const outsideClick = useOutsideClick(rectionBtn)
21
 
22
    const reactionsOptions = [
23
      {
24
        type: 'r',
25
        icon: RecommendIcon,
26
        color: '#7405f9',
27
        label: 'Me gusta',
28
      },
29
      {
30
        type: 's',
31
        icon: VolunteerActivismIcon,
32
        color: '#6495ED',
33
        label: 'Dar apoyo',
34
      },
35
      {
36
        type: 'l',
37
        icon: FavoriteIcon,
38
        color: '#DF704D',
39
        label: 'Me encanta',
40
      },
41
      {
42
        type: 'i',
43
        icon: TungstenIcon,
44
        color: '#F5BB5C',
45
        label: 'Me interesa',
46
      },
47
      {
48
        type: 'f',
49
        icon: EmojiEmotionsIcon,
50
        color: '#FF7F50',
51
        label: 'Me divierte',
52
      },
53
    ]
54
 
55
    const onHover = debounce(() => setShowReactions(true), 500)
56
 
57
    const onUnhover = debounce(() => setShowReactions(false), 500)
58
 
59
    useEffect(() => {
60
      if (outsideClick) setShowReactions(false)
61
    }, [outsideClick])
62
 
63
    useEffect(() => {
64
      const currentReaction = reactionsOptions.find(
65
        (reaction) => reaction.type === myReaction
66
      )
67
      setSettedReaction(currentReaction)
68
    }, [])
69
 
70
    return (
71
      <Component
72
        onMouseOver={onHover}
73
        onMouseOut={onUnhover}
74
        ref={rectionBtn}
75
        Icon={settedReaction ? settedReaction.icon : RecommendIcon}
76
        color={settedReaction ? settedReaction.color : '#626d7a'}
77
        title={settedReaction ? settedReaction.label : 'Reaccionar'}
78
        onClick={() => {
79
          settedReaction ? onDelete() : onSelect(reactionsOptions[0].type)
80
        }}
81
        withTitle={withTitle}
82
      >
83
        <div className={`reactions ${showReactions ? 'active' : ''}`}>
84
          {reactionsOptions.map((reaction) => {
85
            const { icon: Icon, color, type, label } = reaction
86
            return (
87
              <button
88
                key={type}
89
                onClick={(e) => {
90
                  e.stopPropagation()
91
                  onSelect(type)
92
                }}
93
                title={label}
94
              >
95
                <Icon style={{ color }} />
96
              </button>
97
            )
98
          })}
99
        </div>
100
      </Component>
101
    )
102
  }
103
}
104
 
105
export default withReactions