Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 631 | Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
199 stevensc 1
import React, { useEffect, useState } from "react";
2
import { useDispatch } from "react-redux";
3
import {
4
  EmailIcon,
5
  EmailShareButton,
6
  FacebookIcon,
7
  FacebookShareButton,
8
  RedditIcon,
9
  RedditShareButton,
10
  TelegramIcon,
11
  TelegramShareButton,
12
  TwitterIcon,
13
  TwitterShareButton,
14
  WhatsappIcon,
15
  WhatsappShareButton,
16
} from "react-share";
17
import { addNotification } from "../../../redux/notification/notification.actions";
18
import { axios } from "../../../utils";
19
 
20
export default function withExternalShare(Component, url) {
21
  return function (props) {
22
    const dispatch = useDispatch();
23
    const [shareOptions, setShareOptions] = useState(false);
24
    const [shareUrl, setShareUrl] = useState("");
25
 
26
    const handleDisplayReactionList = () => setShareOptions(!shareOptions);
27
 
28
    const getShareUrl = async () => {
29
      await axios
30
        .get(url)
31
        .then(({ data }) => {
32
          if (!data.success) {
33
            dispatch(addNotification({ style: "danger", msg: data.data }));
34
            setShareOptions(false);
35
            return;
36
          }
37
 
38
          setShareUrl(data.data);
39
        })
40
        .catch((err) => console.log(err));
41
    };
42
 
43
    const incrementCount = async () => {
44
      await axios
45
        .post(props.shareUrl)
46
        .then(({ data }) => {
47
          if (!data.success) {
48
            dispatch(addNotification({ style: "danger", msg: data.data }));
49
            return;
50
          }
51
 
52
          setShareOptions(false);
53
          props.setValue(data.data);
54
        })
55
        .catch((err) => console.log(err));
56
    };
57
 
58
    useEffect(() => {
59
      if (shareOptions && !shareUrl) getShareUrl();
60
    }, [shareOptions]);
61
 
62
    return (
63
      <Component onClick={handleDisplayReactionList} {...props}>
64
        {shareOptions && (
65
          <div className="external__share">
66
            <FacebookShareButton
67
              url={shareUrl}
68
              onShareWindowClose={() => incrementCount()}
69
            >
70
              <FacebookIcon size={32} round />
71
            </FacebookShareButton>
72
            <TwitterShareButton
73
              url={shareUrl}
74
              onShareWindowClose={() => incrementCount()}
75
            >
76
              <TwitterIcon size={32} round />
77
            </TwitterShareButton>
78
            <TelegramShareButton
79
              url={shareUrl}
80
              onShareWindowClose={() => incrementCount()}
81
            >
82
              <TelegramIcon size={32} round />
83
            </TelegramShareButton>
84
            <WhatsappShareButton
85
              url={shareUrl}
86
              onShareWindowClose={() => incrementCount()}
87
            >
88
              <WhatsappIcon size={32} round />
89
            </WhatsappShareButton>
90
            <RedditShareButton
91
              url={shareUrl}
92
              onShareWindowClose={() => incrementCount()}
93
            >
94
              <RedditIcon size={32} round />
95
            </RedditShareButton>
96
            <EmailShareButton
97
              url={shareUrl}
98
              onShareWindowClose={() => incrementCount()}
99
            >
100
              <EmailIcon size={32} round />
101
            </EmailShareButton>
102
          </div>
103
        )}
104
      </Component>
105
    );
106
  };
107
}