Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 2775 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
3719 stevensc 1
import React, { useState, useEffect } from 'react';
2
import { useDispatch } from 'react-redux';
3
 
4
import { axios, debounce } from '../../../utils';
5
import { useOutsideClick } from '@hooks';
6
 
7
import { REACTIONS } from '@app/constants/feed';
8
import { addNotification } from '../../../redux/notification/notification.actions';
9
 
10
const ReactionsButton = ({
11
  currentReaction,
12
  onChange = () => null,
13
  withLabel,
14
  saveUrl,
15
  deleteUrl,
16
  ...rest
17
}) => {
18
  const [reaction, setReaction] = useState(null);
19
  const [isHover, setIsHover] = useState(false);
20
  const dispatch = useDispatch();
21
 
22
  const [ref] = useOutsideClick(() => setIsHover(false));
23
 
24
  const saveReaction = (type) => {
25
    const formData = new FormData();
26
    formData.append('reaction', type);
27
 
28
    axios.post(saveUrl, formData).then((res) => {
29
      const { success, data } = res.data;
30
 
31
      if (!success) {
32
        dispatch(addNotification({ style: 'danger', msg: data }));
33
        return;
34
      }
35
 
36
      onChange(data);
37
    });
38
  };
39
 
40
  const deleteReaction = () => {
41
    axios.post(deleteUrl).then((res) => {
42
      const { success, data } = res.data;
43
 
44
      if (!success) {
45
        dispatch(addNotification({ style: 'danger', msg: data }));
46
        return;
47
      }
48
 
49
      setReaction(null);
50
      onChange(data);
51
    });
52
  };
53
 
54
  const onHover = debounce(() => setIsHover(true), 500);
55
 
56
  const onUnhover = debounce(() => setIsHover(false), 500);
57
 
58
  useEffect(() => {
59
    const currentOption = REACTIONS.find(({ type }) => type === currentReaction);
60
    currentOption ? setReaction(currentOption) : setReaction(null);
61
  }, [currentReaction]);
62
 
63
  return (
64
    <>
65
      <button
66
        {...rest}
67
        onMouseOver={onHover}
68
        onMouseOut={onUnhover}
69
        ref={ref}
70
        onClick={() =>
71
          reaction.type !== 'default' ? deleteReaction() : saveReaction(REACTIONS[0].type)
72
        }
73
      >
74
        {reaction.icon}
75
        {withLabel && reaction.label}
76
        <div className={`reactions ${isHover ? 'active' : ''}`}>
77
          {REACTIONS.map(({ icon: Icon, type, label, color }) => (
78
            <button
79
              key={type}
80
              onClick={(e) => {
81
                e.stopPropagation();
82
                saveReaction(type);
83
              }}
84
              title={label}
85
            >
86
              <Icon style={{ color }} />
87
            </button>
88
          ))}
89
        </div>
90
      </button>
91
    </>
92
  );
93
};
94
 
95
export default ReactionsButton;