Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 1978 | Rev 2134 | Ir a la última revisión | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1978 Rev 1979
Línea 1... Línea 1...
1
import React, { useRef, useState, useEffect } from 'react'
1
import React, { useRef, useState, useEffect } from "react";
2
import { useDispatch } from 'react-redux'
2
import { useDispatch } from "react-redux";
3
import { axios, debounce } from '../../../utils'
3
import { axios, debounce } from "../../../utils";
4
import useOutsideClick from '../../../hooks/useOutsideClick'
4
import useOutsideClick from "../../../hooks/useOutsideClick";
5
 
5
 
6
import { addNotification } from '../../../redux/notification/notification.actions'
6
import { addNotification } from "../../../redux/notification/notification.actions";
7
 
7
 
8
import ReactionIcon from '@mui/icons-material/Recommend'
8
import ReactionIcon from "@mui/icons-material/Recommend";
9
import FunIcon from '../icons/Fun'
9
import FunIcon from "../icons/Fun";
10
import LoveItIcon from '../icons/LoveIt'
10
import LoveItIcon from "../icons/LoveIt";
11
import SupportIcon from '../icons/Support'
11
import SupportIcon from "../icons/Support";
12
import InterestIcon from '../icons/Interest'
12
import InterestIcon from "../icons/Interest";
13
import RecommendIcon from '../icons/Recommned'
13
import RecommendIcon from "../icons/Recommned";
Línea 14... Línea 14...
14
 
14
 
15
const initialReaction = {
15
const initialReaction = {
16
  label: 'Reaccionar',
16
  label: "Reaccionar",
17
  icon: <ReactionIcon />,
17
  icon: <ReactionIcon />,
18
  type: 'default'
18
  type: "default",
Línea 19... Línea 19...
19
}
19
};
20
 
20
 
21
const ReactionsButton = ({
21
const ReactionsButton = ({
22
  currentReaction,
22
  currentReaction,
23
  onChange = () => null,
23
  onChange = () => null,
24
  withLabel,
24
  withLabel,
25
  saveUrl,
25
  saveUrl,
26
  deleteUrl,
26
  deleteUrl,
27
  ...rest
27
  ...rest
28
}) => {
28
}) => {
29
  const [reaction, setReaction] = useState(initialReaction)
29
  const [reaction, setReaction] = useState(initialReaction);
30
  const [isHover, setIsHover] = useState(false)
30
  const [isHover, setIsHover] = useState(false);
31
  const rectionBtn = useRef(null)
31
  const rectionBtn = useRef(null);
Línea 32... Línea 32...
32
  const dispatch = useDispatch()
32
  const dispatch = useDispatch();
33
  useOutsideClick(rectionBtn, () => setIsHover(false))
33
  useOutsideClick(rectionBtn, () => setIsHover(false));
34
 
34
 
35
  const reactions = [
35
  const reactions = [
36
    {
36
    {
37
      type: 'r',
37
      type: "r",
38
      icon: <RecommendIcon />,
38
      icon: <RecommendIcon />,
39
      label: 'Me gusta'
39
      label: "Me gusta",
40
    },
40
    },
41
    {
41
    {
42
      type: 's',
42
      type: "s",
43
      icon: <SupportIcon />,
43
      icon: <SupportIcon />,
44
      label: 'Dar apoyo'
44
      label: "Dar apoyo",
45
    },
45
    },
46
    {
46
    {
47
      type: 'l',
47
      type: "l",
48
      icon: <LoveItIcon />,
48
      icon: <LoveItIcon />,
49
      label: 'Me encanta'
49
      label: "Me encanta",
50
    },
50
    },
51
    {
51
    {
52
      type: 'i',
52
      type: "i",
53
      icon: <InterestIcon />,
53
      icon: <InterestIcon />,
54
      label: 'Me interesa'
54
      label: "Me interesa",
55
    },
55
    },
56
    {
56
    {
57
      type: 'f',
57
      type: "f",
58
      icon: <FunIcon />,
58
      icon: <FunIcon />,
Línea 59... Línea 59...
59
      label: 'Me divierte'
59
      label: "Me divierte",
-
 
60
    },
-
 
61
  ];
-
 
62
 
60
    }
63
  const saveReaction = (type) => {
61
  ]
64
    const formData = new FormData();
Línea 62... Línea 65...
62
 
65
    formData.append("reaction", type);
63
  const saveReaction = (type) => {
66
 
64
    axios.post(saveUrl, { type }).then((res) => {
67
    axios.post(saveUrl, formData).then((res) => {
65
      const { success, data } = res.data
68
      const { success, data } = res.data;
Línea 66... Línea 69...
66
 
69
 
67
      if (!success) {
70
      if (!success) {
Línea 68... Línea 71...
68
        dispatch(addNotification({ style: 'danger', msg: data }))
71
        dispatch(addNotification({ style: "danger", msg: data }));
69
        return
72
        return;
70
      }
73
      }
Línea 71... Línea 74...
71
 
74
 
72
      const newReaction = reactions.find((reaction) => reaction.type === type)
75
      const newReaction = reactions.find((reaction) => reaction.type === type);
73
      setReaction(newReaction)
76
      setReaction(newReaction);
Línea 74... Línea 77...
74
 
77
 
75
      onChange(data)
78
      onChange(data);
76
    })
79
    });
77
  }
80
  };
Línea 78... Línea 81...
78
 
81
 
79
  const deleteReaction = () => {
82
  const deleteReaction = () => {
80
    axios.post(deleteUrl).then((res) => {
83
    axios.post(deleteUrl).then((res) => {
81
      const { success, data } = res.data
84
      const { success, data } = res.data;
Línea 82... Línea 85...
82
 
85
 
Línea 83... Línea 86...
83
      if (!success) {
86
      if (!success) {
Línea 84... Línea 87...
84
        dispatch(addNotification({ style: 'danger', msg: data }))
87
        dispatch(addNotification({ style: "danger", msg: data }));
-
 
88
        return;
85
        return
89
      }
-
 
90
 
Línea 86... Línea 91...
86
      }
91
      onChange(data);
87
 
92
      setReaction(initialReaction);
88
      onChange(data)
93
    });
89
      setReaction(initialReaction)
94
  };
Línea 90... Línea 95...
90
    })
95
 
91
  }
96
  const onHover = debounce(() => setIsHover(true), 500);
Línea 92... Línea 97...
92
 
97
 
93
  const onHover = debounce(() => setIsHover(true), 500)
98
  const onUnhover = debounce(() => setIsHover(false), 500);
94
 
99
 
95
  const onUnhover = debounce(() => setIsHover(false), 500)
100
  useEffect(() => {
96
 
101
    const currentOption = reactions.find(
97
  useEffect(() => {
102
      ({ type }) => type === currentReaction
98
    const currentOption = reactions.find(({ type }) => type === currentReaction)
103
    );
99
 
104
 
100
    if (!currentOption) {
105
    if (!currentOption) {
101
      setReaction(initialReaction)
106
      setReaction(initialReaction);
102
      return
107
      return;
103
    }
108
    }
104
 
109
 
105
    setReaction(currentOption)
110
    setReaction(currentOption);
106
  }, [currentReaction])
111
  }, [currentReaction]);
107
 
112
 
108
  return (
113
  return (
109
    <>
114
    <>
110
      <button
115
      <button
111
        {...rest}
116
        {...rest}
112
        onMouseOver={onHover}
117
        onMouseOver={onHover}
113
        onMouseOut={onUnhover}
118
        onMouseOut={onUnhover}
114
        ref={rectionBtn}
119
        ref={rectionBtn}
115
        onClick={() =>
120
        onClick={() =>
116
          reaction.type !== 'default'
121
          reaction.type !== "default"
117
            ? deleteReaction()
122
            ? deleteReaction()
118
            : saveReaction(reactions[0].type)
123
            : saveReaction(reactions[0].type)
119
        }
124
        }
120
      >
125
      >
121
        {reaction.icon}
126
        {reaction.icon}
122
        {withLabel && reaction.label}
127
        {withLabel && reaction.label}
123
        <div className={`reactions ${isHover ? 'active' : ''}`}>
128
        <div className={`reactions ${isHover ? "active" : ""}`}>
124
          {reactions.map(({ icon, type, label }) => (
129
          {reactions.map(({ icon, type, label }) => (
Línea 125... Línea 130...
125
            <button
130
            <button