Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 2852 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
3596 stevensc 1
import React, { useState } from 'react';
2
import { useDispatch } from 'react-redux';
199 stevensc 3
import {
4
  EmailIcon,
5
  EmailShareButton,
6
  FacebookIcon,
7
  FacebookShareButton,
8
  RedditIcon,
9
  RedditShareButton,
10
  TelegramIcon,
11
  TelegramShareButton,
12
  TwitterIcon,
13
  TwitterShareButton,
14
  WhatsappIcon,
2157 stevensc 15
  WhatsappShareButton
3596 stevensc 16
} from 'react-share';
17
import { styled } from '@mui/material';
2776 stevensc 18
 
3596 stevensc 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';
2851 stevensc 23
 
3596 stevensc 24
import SharePopup from './mobile-share/MobileSharePopUp';
2245 stevensc 25
 
2852 stevensc 26
const ShareContainer = styled('div')(({ theme }) => ({
2851 stevensc 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,
2852 stevensc 35
  backgroundColor: colors.main,
36
  border: `1px solid ${colors.border.primary}`
3596 stevensc 37
}));
199 stevensc 38
 
39
export default function withExternalShare(Component, url) {
2157 stevensc 40
  return function ({ children, setValue, shorterUrl }) {
3596 stevensc 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();
199 stevensc 46
 
2253 stevensc 47
    const handleClick = async () => {
3596 stevensc 48
      const shorterUrl = shareUrl || (await getShareUrl());
49
      setShareUrl(shorterUrl);
2253 stevensc 50
 
2254 stevensc 51
      if (!isMobile) {
3596 stevensc 52
        setShowOptions(!showOptions);
53
        return;
2254 stevensc 54
      }
2253 stevensc 55
 
56
      if (navigator?.share) {
57
        try {
3596 stevensc 58
          await navigator.share({ url: shorterUrl });
59
          incrementCount();
2253 stevensc 60
        } catch (error) {
3596 stevensc 61
          dispatch(addNotification({ style: 'danger', msg: error.message }));
2253 stevensc 62
        }
3596 stevensc 63
        return;
2253 stevensc 64
      }
65
 
66
      if (window?.AndroidShareHandler) {
67
        try {
3596 stevensc 68
          window.AndroidShareHandler.share(shorterUrl);
2253 stevensc 69
        } catch (error) {
3596 stevensc 70
          dispatch(addNotification({ style: 'danger', msg: error.message }));
2253 stevensc 71
        }
3596 stevensc 72
        return;
2253 stevensc 73
      }
74
 
3596 stevensc 75
      setOpenPopup(true);
76
    };
2253 stevensc 77
 
2248 stevensc 78
    const getShareUrl = async () => {
79
      try {
3596 stevensc 80
        const response = await axios.get(url);
81
        const { data, success } = response.data;
82
        if (!success) throw new Error(data);
83
        return data;
2248 stevensc 84
      } catch (error) {
3596 stevensc 85
        dispatch(addNotification({ style: 'danger', msg: error.message }));
2248 stevensc 86
      }
3596 stevensc 87
    };
2248 stevensc 88
 
199 stevensc 89
    const incrementCount = async () => {
90
      await axios
2157 stevensc 91
        .post(shorterUrl)
2244 stevensc 92
        .then((response) => {
3596 stevensc 93
          const { data, success } = response.data;
94
          if (!success) throw new Error(data);
95
          setShowOptions(false);
96
          setValue(data);
199 stevensc 97
        })
3596 stevensc 98
        .catch((err) => dispatch(addNotification({ style: 'danger', msg: err.message })));
99
    };
199 stevensc 100
 
101
    return (
2245 stevensc 102
      <>
2253 stevensc 103
        <Component onClick={handleClick}>
2245 stevensc 104
          {children}
2162 stevensc 105
 
2245 stevensc 106
          {showOptions && (
2852 stevensc 107
            <ShareContainer>
2245 stevensc 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>
2852 stevensc 150
            </ShareContainer>
2245 stevensc 151
          )}
152
        </Component>
153
        <SharePopup
154
          show={openPopup}
155
          shareData={{ url: shareUrl }}
156
          onClose={() => setOpenPopup(false)}
157
        />
158
      </>
3596 stevensc 159
    );
160
  };
199 stevensc 161
}