Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev 633 Rev 1978
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
 
-
 
63
  const saveReaction = (type) => {
60
    }
64
    const formData = new FormData();
61
  ]
Línea 65... Línea 62...
65
    formData.append("reaction", type);
62
 
66
 
63
  const saveReaction = (type) => {
67
    axios.post(saveUrl, formData).then((res) => {
64
    axios.post(saveUrl, { type }).then((res) => {
68
      const { success, data } = res.data;
65
      const { success, data } = res.data
Línea 69... Línea 66...
69
 
66
 
70
      if (!success) {
67
      if (!success) {
Línea 71... Línea 68...
71
        dispatch(addNotification({ style: "danger", msg: data }));
68
        dispatch(addNotification({ style: 'danger', msg: data }))
72
        return;
69
        return
73
      }
70
      }
Línea 74... Línea 71...
74
 
71
 
75
      const newReaction = reactions.find((reaction) => reaction.type === type);
72
      const newReaction = reactions.find((reaction) => reaction.type === type)
76
      setReaction(newReaction);
73
      setReaction(newReaction)
Línea 77... Línea 74...
77
 
74
 
78
      onChange(data);
75
      onChange(data)
79
    });
76
    })
80
  };
77
  }
Línea 81... Línea 78...
81
 
78
 
82
  const deleteReaction = () => {
79
  const deleteReaction = () => {
83
    axios.post(deleteUrl).then((res) => {
80
    axios.post(deleteUrl).then((res) => {
84
      const { success, data } = res.data;
81
      const { success, data } = res.data
Línea 85... Línea 82...
85
 
82
 
Línea 86... Línea 83...
86
      if (!success) {
83
      if (!success) {
Línea 87... Línea 84...
87
        dispatch(addNotification({ style: "danger", msg: data }));
84
        dispatch(addNotification({ style: 'danger', msg: data }))
88
        return;
-
 
89
      }
85
        return
90
 
-
 
Línea 91... Línea 86...
91
      onChange(data);
86
      }
92
      setReaction(initialReaction);
87
 
93
    });
88
      onChange(data)
94
  };
89
      setReaction(initialReaction)
Línea 95... Línea 90...
95
 
90
    })
96
  const onHover = debounce(() => setIsHover(true), 500);
91
  }
Línea 97... Línea 92...
97
 
92
 
98
  const onUnhover = debounce(() => setIsHover(false), 500);
93
  const onHover = debounce(() => setIsHover(true), 500)
99
 
94
 
100
  useEffect(() => {
95
  const onUnhover = debounce(() => setIsHover(false), 500)
101
    const currentOption = reactions.find(
96
 
102
      ({ type }) => type === currentReaction
97
  useEffect(() => {
103
    );
98
    const currentOption = reactions.find(({ type }) => type === currentReaction)
104
 
99
 
105
    if (!currentOption) {
100
    if (!currentOption) {
106
      setReaction(initialReaction);
101
      setReaction(initialReaction)
107
      return;
102
      return
108
    }
103
    }
109
 
104
 
110
    setReaction(currentOption);
105
    setReaction(currentOption)
111
  }, [currentReaction]);
106
  }, [currentReaction])
112
 
107
 
113
  return (
108
  return (
114
    <>
109
    <>
115
      <button
110
      <button
116
        {...rest}
111
        {...rest}
117
        onMouseOver={onHover}
112
        onMouseOver={onHover}
118
        onMouseOut={onUnhover}
113
        onMouseOut={onUnhover}
119
        ref={rectionBtn}
114
        ref={rectionBtn}
120
        onClick={() =>
115
        onClick={() =>
121
          reaction.type !== "default"
116
          reaction.type !== 'default'
122
            ? deleteReaction()
117
            ? deleteReaction()
123
            : saveReaction(reactions[0].type)
118
            : saveReaction(reactions[0].type)
124
        }
119
        }
125
      >
120
      >
126
        {reaction.icon}
121
        {reaction.icon}
127
        {withLabel && reaction.label}
122
        {withLabel && reaction.label}
128
        <div className={`reactions ${isHover ? "active" : ""}`}>
123
        <div className={`reactions ${isHover ? 'active' : ''}`}>
129
          {reactions.map(({ icon, type, label }) => (
124
          {reactions.map(({ icon, type, label }) => (
Línea 130... Línea 125...
130
            <button
125
            <button