Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 199 | Rev 641 | Ir a la última revisión | | Comparar con el anterior | 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 [shareOptions, setShareOptions] = useState(false);
23
    const [shareUrl, setShareUrl] = useState("");
631 stevensc 24
    const dispatch = useDispatch();
199 stevensc 25
 
631 stevensc 26
    const toggleShareOptions = () => {
27
      setShareOptions(!shareOptions);
28
    };
199 stevensc 29
 
30
    const getShareUrl = async () => {
31
      await axios
32
        .get(url)
33
        .then(({ data }) => {
34
          if (!data.success) {
35
            dispatch(addNotification({ style: "danger", msg: data.data }));
36
            setShareOptions(false);
37
            return;
38
          }
39
 
40
          setShareUrl(data.data);
41
        })
42
        .catch((err) => console.log(err));
43
    };
44
 
45
    const incrementCount = async () => {
46
      await axios
47
        .post(props.shareUrl)
48
        .then(({ data }) => {
49
          if (!data.success) {
50
            dispatch(addNotification({ style: "danger", msg: data.data }));
51
            return;
52
          }
53
 
54
          setShareOptions(false);
55
          props.setValue(data.data);
56
        })
57
        .catch((err) => console.log(err));
58
    };
59
 
60
    useEffect(() => {
61
      if (shareOptions && !shareUrl) getShareUrl();
62
    }, [shareOptions]);
63
 
64
    return (
631 stevensc 65
      <Component onClick={toggleShareOptions} {...props}>
199 stevensc 66
        {shareOptions && (
67
          <div className="external__share">
68
            <FacebookShareButton
69
              url={shareUrl}
70
              onShareWindowClose={() => incrementCount()}
71
            >
72
              <FacebookIcon size={32} round />
73
            </FacebookShareButton>
74
            <TwitterShareButton
75
              url={shareUrl}
76
              onShareWindowClose={() => incrementCount()}
77
            >
78
              <TwitterIcon size={32} round />
79
            </TwitterShareButton>
80
            <TelegramShareButton
81
              url={shareUrl}
82
              onShareWindowClose={() => incrementCount()}
83
            >
84
              <TelegramIcon size={32} round />
85
            </TelegramShareButton>
86
            <WhatsappShareButton
87
              url={shareUrl}
88
              onShareWindowClose={() => incrementCount()}
89
            >
90
              <WhatsappIcon size={32} round />
91
            </WhatsappShareButton>
92
            <RedditShareButton
93
              url={shareUrl}
94
              onShareWindowClose={() => incrementCount()}
95
            >
96
              <RedditIcon size={32} round />
97
            </RedditShareButton>
98
            <EmailShareButton
99
              url={shareUrl}
100
              onShareWindowClose={() => incrementCount()}
101
            >
102
              <EmailIcon size={32} round />
103
            </EmailShareButton>
104
          </div>
105
        )}
106
      </Component>
107
    );
108
  };
109
}