Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 7074 | Rev 7076 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |

import React, { useRef, useState, useEffect } from 'react'
import { useDispatch } from 'react-redux'
import { axios, debounce } from '../../../utils'
import useOutsideClick from '../../../hooks/useOutsideClick'

import { addNotification } from '../../../redux/notification/notification.actions'

import ReactionIcon from '@mui/icons-material/Recommend'
import FunIcon from '../icons/Fun'
import LoveItIcon from '../icons/LoveIt'
import SupportIcon from '../icons/Support'
import InterestIcon from '../icons/Interest'
import RecommendIcon from '../icons/Recommned'

const defaultReaction = {
  label: 'Reaccionar',
  icon: <ReactionIcon />,
  type: 'default',
}

const ReactionsButton = ({
  currentReaction,
  onChange,
  withLabel,
  reactionTypesUrl,
  deleteUrl,
}) => {
  const [settedReaction, setSettedReaction] = useState(defaultReaction)
  const [showReactions, setShowReactions] = useState(false)
  const rectionBtn = useRef(null)
  const dispatch = useDispatch()
  useOutsideClick(rectionBtn, () => setShowReactions(false))

  const reactionsOptions = [
    {
      type: 'r',
      icon: <RecommendIcon />,
      label: 'Me gusta',
    },
    {
      type: 's',
      icon: <SupportIcon />,
      label: 'Dar apoyo',
    },
    {
      type: 'l',
      icon: <LoveItIcon />,
      label: 'Me encanta',
    },
    {
      type: 'i',
      icon: <InterestIcon />,
      label: 'Me interesa',
    },
    {
      type: 'f',
      icon: <FunIcon />,
      label: 'Me divierte',
    },
  ]

  const saveReaction = (type) => {
    axios.post(reactionTypesUrl[type]).then((response) => {
      const { success, data } = response.data

      if (!success) {
        dispatch(addNotification({ style: 'danger', msg: data }))
        return
      }

      const typeIndex = reactionsOptions.findIndex(
        (option) => option.type === type
      )
      const newReaction = reactionsOptions[typeIndex]
      console.log(newReaction)

      onChange(data.reactions)
      setSettedReaction(newReaction)
    })
  }

  const deleteReaction = () => {
    axios.post(deleteUrl).then((res) => {
      const { success, data } = res.data

      if (!success) {
        dispatch(addNotification({ style: 'danger', msg: data }))
        return
      }

      onChange(data.reactions)
      setSettedReaction(defaultReaction)
    })
  }

  const onHover = debounce(() => setShowReactions(true), 500)

  const onUnhover = debounce(() => setShowReactions(false), 500)

  useEffect(() => {
    const currentOption = reactionsOptions.find(
      (reaction) => reaction.icon === currentReaction
    )

    if (!currentOption) {
      setSettedReaction(defaultReaction)
      return
    }

    setSettedReaction(currentOption)
  }, [currentReaction])

  return (
    <>
      <button
        className="btn position-relative"
        onMouseOver={onHover}
        onMouseOut={onUnhover}
        ref={rectionBtn}
        onClick={() =>
          settedReaction.type === 'default'
            ? saveReaction(reactionsOptions[0].type)
            : deleteReaction()
        }
      >
        {settedReaction.icon}
        {withLabel && settedReaction.label}
        <div className={`reactions ${showReactions ? 'active' : ''}`}>
          {reactionsOptions.map(({ icon, type, label }) => (
            <button
              key={type}
              onClick={(e) => {
                e.stopPropagation()
                saveReaction(type)
              }}
              title={label}
            >
              {icon}
            </button>
          ))}
        </div>
      </button>
    </>
  )
}

export default ReactionsButton