Rev 2246 | Rev 2248 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
import React, { useCallback, useMemo, useState } from 'react'import { useDispatch } from 'react-redux'import { Box, styled } from '@mui/material'import { axios } from '@app/utils'import { REACTIONS } from '@app/constants/feed'import { addNotification } from '@app/redux/notification/notification.actions'import useDebounce from '@app/hooks/useDebounce'const ReactionsBox = styled(Box)(({ theme }) => ({position: 'absolute',bottom: '100%',backgroundColor: 'var(--bg-color)',boxShadow: theme.shadows[1],gap: 1,left: 0,display: 'flex',padding: 1,width: 'fit-content',borderRadius: theme.shape.borderRadius,transform: 'scale(0)',transformOrigin: 'center',transition: theme.transitions.create('transform', {duration: theme.transitions.duration.short,easing: theme.transitions.easing.easeInOut,delay: theme.transitions.duration.short}),'& > button': {transition: theme.transitions.create('transform', {duration: theme.transitions.duration.short,easing: theme.transitions.easing.easeInOut}),'&:hover': {transform: 'scale(1.1) translateY(-4px)'},svg: {fontSize: '32px'}}}))export default function withReactions(Component) {const ReactionsButton = ({saveUrl = '',deleteUrl = '',currentReactionType = null,onReaction = () => null}) => {const [isHover, setIsHover] = useState(false)const debounceHover = useDebounce(isHover, 500)const dispatch = useDispatch()const currentReaction = useMemo(() => REACTIONS.find((r) => r.type === currentReactionType),[currentReactionType])const Icon = currentReaction ? currentReaction.icon : REACTIONS[0].iconconst handleHover = () => setIsHover(!isHover)const saveReaction = useCallback((type = 'r') => {const formData = new FormData()formData.append('reaction', type)axios.post(saveUrl, formData).then((res) => {const { success, data } = res.dataif (!success) {dispatch(addNotification({ style: 'danger', msg: data }))}onReaction(data, type)})},[saveUrl, dispatch])const deleteReaction = useCallback(() => {axios.post(deleteUrl).then((res) => {const { success, data } = res.dataif (!success) {dispatch(addNotification({ style: 'danger', msg: data }))return}onReaction({reactions: data.reactions,currentReactionType: ''})})}, [])return (<Component onClick={currentReaction ? deleteReaction : handleHover}>{currentReaction ? (<Icon style={{ color: currentReaction.color }} />) : (<Icon />)}{currentReaction ? currentReaction.label : 'Reaccionar'}<ReactionsBoxsx={{transform: debounceHover ? 'scale(1)' : 'scale(0)',transformOrigin: 'center'}}>{REACTIONS.map(({ type, label, icon: Icon, color }) => (<buttonkey={type}title={label}onClickCapture={(e) => {e.stopPropagation()saveReaction(type)}}><Icon style={{ color }} /></button>))}</ReactionsBox></Component>)}return ReactionsButton}