Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev Autor Línea Nro. Línea
3719 stevensc 1
import React, { 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 { styled } from '@mui/material';
18
 
19
import { axios } from '@app/utils';
20
import { useMobile } from '@hooks';
21
import { addNotification } from '@app/redux/notification/notification.actions';
22
import colors from '@styles/config/colors';
23
 
24
import SharePopup from './mobile-share/MobileSharePopUp';
25
 
26
const ShareContainer = styled('div')(({ theme }) => ({
27
  display: 'flex',
28
  position: 'absolute',
29
  alignItems: 'center',
30
  padding: theme.spacing(0.5),
31
  bottom: `calc(100% + ${theme.spacing(0.5)})`,
32
  gap: theme.spacing(0.5),
33
  right: 0,
34
  borderRadius: theme.shape.borderRadius,
35
  backgroundColor: colors.main,
36
  border: `1px solid ${colors.border.primary}`
37
}));
38
 
39
export default function withExternalShare(Component, url) {
40
  return function ComponentWithExternalShare({ children, setValue, shorterUrl }) {
41
    const [showOptions, setShowOptions] = useState(false);
42
    const [openPopup, setOpenPopup] = useState(false);
43
    const [shareUrl, setShareUrl] = useState('');
44
    const isMobile = useMobile();
45
    const dispatch = useDispatch();
46
 
47
    const handleClick = async () => {
48
      const shorterUrl = shareUrl || (await getShareUrl());
49
      setShareUrl(shorterUrl);
50
 
51
      if (!isMobile) {
52
        setShowOptions(!showOptions);
53
        return;
54
      }
55
 
56
      if (navigator?.share) {
57
        try {
58
          await navigator.share({ url: shorterUrl });
59
          incrementCount();
60
        } catch (error) {
61
          dispatch(addNotification({ style: 'danger', msg: error.message }));
62
        }
63
        return;
64
      }
65
 
66
      if (window?.AndroidShareHandler) {
67
        try {
68
          window.AndroidShareHandler.share(shorterUrl);
69
        } catch (error) {
70
          dispatch(addNotification({ style: 'danger', msg: error.message }));
71
        }
72
        return;
73
      }
74
 
75
      setOpenPopup(true);
76
    };
77
 
78
    const getShareUrl = async () => {
79
      try {
80
        const response = await axios.get(url);
81
        const { data, success } = response.data;
82
        if (!success) throw new Error(data);
83
        return data;
84
      } catch (error) {
85
        dispatch(addNotification({ style: 'danger', msg: error.message }));
86
      }
87
    };
88
 
89
    const incrementCount = async () => {
90
      await axios
91
        .post(shorterUrl)
92
        .then((response) => {
93
          const { data, success } = response.data;
94
          if (!success) throw new Error(data);
95
          setShowOptions(false);
96
          setValue(data);
97
        })
98
        .catch((err) => dispatch(addNotification({ style: 'danger', msg: err.message })));
99
    };
100
 
101
    return (
102
      <>
103
        <Component onClick={handleClick}>
104
          {children}
105
 
106
          {showOptions && (
107
            <ShareContainer>
108
              <FacebookShareButton
109
                url={shareUrl}
110
                onShareWindowClose={incrementCount}
111
                disabled={!shareUrl}
112
              >
113
                <FacebookIcon size={32} round />
114
              </FacebookShareButton>
115
              <TwitterShareButton
116
                url={shareUrl}
117
                onShareWindowClose={incrementCount}
118
                disabled={!shareUrl}
119
              >
120
                <TwitterIcon size={32} round />
121
              </TwitterShareButton>
122
              <TelegramShareButton
123
                url={shareUrl}
124
                onShareWindowClose={incrementCount}
125
                disabled={!shareUrl}
126
              >
127
                <TelegramIcon size={32} round />
128
              </TelegramShareButton>
129
              <WhatsappShareButton
130
                url={shareUrl}
131
                onShareWindowClose={incrementCount}
132
                disabled={!shareUrl}
133
              >
134
                <WhatsappIcon size={32} round />
135
              </WhatsappShareButton>
136
              <RedditShareButton
137
                url={shareUrl}
138
                onShareWindowClose={incrementCount}
139
                disabled={!shareUrl}
140
              >
141
                <RedditIcon size={32} round />
142
              </RedditShareButton>
143
              <EmailShareButton
144
                url={shareUrl}
145
                onShareWindowClose={incrementCount}
146
                disabled={!shareUrl}
147
              >
148
                <EmailIcon size={32} round />
149
              </EmailShareButton>
150
            </ShareContainer>
151
          )}
152
        </Component>
153
        <SharePopup
154
          show={openPopup}
155
          shareData={{ url: shareUrl }}
156
          onClose={() => setOpenPopup(false)}
157
        />
158
      </>
159
    );
160
  };
161
}