| 3593 |
stevensc |
1 |
import React, { useCallback, useMemo, useState } from 'react';
|
|
|
2 |
import { useDispatch } from 'react-redux';
|
|
|
3 |
import { styled, Typography } from '@mui/material';
|
|
|
4 |
|
|
|
5 |
import { axios } from '@app/utils';
|
|
|
6 |
import { REACTIONS } from '@app/constants/feed';
|
|
|
7 |
import { addNotification } from '@app/redux/notification/notification.actions';
|
|
|
8 |
import colors from '@styles/colors';
|
|
|
9 |
|
|
|
10 |
const ReactionsBox = styled('div')(({ theme }) => ({
|
|
|
11 |
position: 'absolute',
|
|
|
12 |
bottom: '100%',
|
|
|
13 |
backgroundColor: colors.main,
|
|
|
14 |
border: `1px solid ${colors.border.primary}`,
|
|
|
15 |
gap: theme.spacing(0.5),
|
|
|
16 |
left: 0,
|
|
|
17 |
display: 'flex',
|
|
|
18 |
padding: theme.spacing(0.5),
|
|
|
19 |
width: 'fit-content',
|
|
|
20 |
borderRadius: theme.shape.borderRadius,
|
|
|
21 |
transform: 'scale(0)',
|
|
|
22 |
transformOrigin: 'center',
|
|
|
23 |
transition: theme.transitions.create('transform', {
|
|
|
24 |
duration: theme.transitions.duration.short,
|
|
|
25 |
easing: theme.transitions.easing.easeInOut,
|
|
|
26 |
delay: theme.transitions.duration.short
|
|
|
27 |
}),
|
|
|
28 |
'& > button': {
|
|
|
29 |
transition: theme.transitions.create('transform', {
|
|
|
30 |
duration: theme.transitions.duration.short,
|
|
|
31 |
easing: theme.transitions.easing.easeInOut
|
|
|
32 |
}),
|
|
|
33 |
'&:hover': {
|
|
|
34 |
transform: 'scale(1.1) translateY(-4px)'
|
|
|
35 |
},
|
|
|
36 |
svg: {
|
|
|
37 |
fontSize: '32px'
|
|
|
38 |
}
|
|
|
39 |
}
|
|
|
40 |
}));
|
|
|
41 |
|
|
|
42 |
export function withReactions(Component) {
|
|
|
43 |
const ReactionsButton = ({
|
|
|
44 |
saveUrl = '',
|
|
|
45 |
deleteUrl = '',
|
|
|
46 |
currentReactionType = null,
|
|
|
47 |
onReaction = () => null
|
|
|
48 |
}) => {
|
|
|
49 |
const [isHover, setIsHover] = useState(false);
|
|
|
50 |
const dispatch = useDispatch();
|
|
|
51 |
|
|
|
52 |
const currentReaction = useMemo(
|
|
|
53 |
() => REACTIONS.find((r) => r.type === currentReactionType),
|
|
|
54 |
[currentReactionType]
|
|
|
55 |
);
|
|
|
56 |
const Icon = currentReaction ? currentReaction.icon : REACTIONS[0].icon;
|
|
|
57 |
|
|
|
58 |
const handleHover = () => setIsHover(!isHover);
|
|
|
59 |
|
|
|
60 |
const saveReaction = useCallback(
|
|
|
61 |
(type = 'r') => {
|
|
|
62 |
const formData = new FormData();
|
|
|
63 |
formData.append('reaction', type);
|
|
|
64 |
|
|
|
65 |
axios.post(saveUrl, formData).then((res) => {
|
|
|
66 |
const { success, data } = res.data;
|
|
|
67 |
|
|
|
68 |
if (!success) {
|
|
|
69 |
dispatch(addNotification({ style: 'danger', msg: data }));
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
onReaction(data, type);
|
|
|
73 |
});
|
|
|
74 |
},
|
|
|
75 |
[saveUrl, dispatch]
|
|
|
76 |
);
|
|
|
77 |
|
|
|
78 |
const deleteReaction = useCallback(() => {
|
|
|
79 |
axios.post(deleteUrl).then((res) => {
|
|
|
80 |
const { success, data } = res.data;
|
|
|
81 |
|
|
|
82 |
if (!success) {
|
|
|
83 |
dispatch(addNotification({ style: 'danger', msg: data }));
|
|
|
84 |
return;
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
onReaction({
|
|
|
88 |
reactions: data.reactions,
|
|
|
89 |
currentReactionType: ''
|
|
|
90 |
});
|
|
|
91 |
});
|
|
|
92 |
}, []);
|
|
|
93 |
|
|
|
94 |
return (
|
|
|
95 |
<Component onClick={currentReaction ? deleteReaction : handleHover}>
|
|
|
96 |
{currentReaction ? <Icon style={{ color: currentReaction.color }} /> : <Icon />}
|
|
|
97 |
|
|
|
98 |
<Typography sx={{ display: { xs: 'none', md: 'inline-flex' } }} variant='overline'>
|
|
|
99 |
{currentReaction ? currentReaction.label : 'Reaccionar'}
|
|
|
100 |
</Typography>
|
|
|
101 |
|
|
|
102 |
<ReactionsBox
|
|
|
103 |
sx={{
|
|
|
104 |
transform: isHover ? 'scale(1)' : 'scale(0)',
|
|
|
105 |
transformOrigin: 'center'
|
|
|
106 |
}}
|
|
|
107 |
>
|
|
|
108 |
{REACTIONS.map(({ type, label, icon: Icon, color }) => (
|
|
|
109 |
<button
|
|
|
110 |
key={type}
|
|
|
111 |
title={label}
|
|
|
112 |
onClickCapture={(e) => {
|
|
|
113 |
e.stopPropagation();
|
|
|
114 |
saveReaction(type);
|
|
|
115 |
}}
|
|
|
116 |
>
|
|
|
117 |
<Icon style={{ color }} />
|
|
|
118 |
</button>
|
|
|
119 |
))}
|
|
|
120 |
</ReactionsBox>
|
|
|
121 |
</Component>
|
|
|
122 |
);
|
|
|
123 |
};
|
|
|
124 |
|
|
|
125 |
return ReactionsButton;
|
|
|
126 |
}
|