Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev Autor Línea Nro. Línea
2253 stevensc 1
import React, { useState } from 'react'
2157 stevensc 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
16
} from 'react-share'
2851 stevensc 17
import { styled } from '@mui/material'
2776 stevensc 18
 
2851 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/colors'
23
 
2245 stevensc 24
import SharePopup from './mobile-share/MobileSharePopUp'
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}`
2851 stevensc 37
}))
199 stevensc 38
 
39
export default function withExternalShare(Component, url) {
2157 stevensc 40
  return function ({ children, setValue, shorterUrl }) {
2244 stevensc 41
    const [showOptions, setShowOptions] = useState(false)
2245 stevensc 42
    const [openPopup, setOpenPopup] = useState(false)
2157 stevensc 43
    const [shareUrl, setShareUrl] = useState('')
2245 stevensc 44
    const isMobile = useMobile()
2157 stevensc 45
    const dispatch = useDispatch()
199 stevensc 46
 
2253 stevensc 47
    const handleClick = async () => {
48
      const shorterUrl = shareUrl || (await getShareUrl())
49
      setShareUrl(shorterUrl)
50
 
2254 stevensc 51
      if (!isMobile) {
52
        setShowOptions(!showOptions)
53
        return
54
      }
2253 stevensc 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
 
2248 stevensc 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
 
199 stevensc 89
    const incrementCount = async () => {
90
      await axios
2157 stevensc 91
        .post(shorterUrl)
2244 stevensc 92
        .then((response) => {
93
          const { data, success } = response.data
94
          if (!success) throw new Error(data)
95
          setShowOptions(false)
96
          setValue(data)
199 stevensc 97
        })
2244 stevensc 98
        .catch((err) =>
99
          dispatch(addNotification({ style: 'danger', msg: err.message }))
100
        )
2157 stevensc 101
    }
199 stevensc 102
 
103
    return (
2245 stevensc 104
      <>
2253 stevensc 105
        <Component onClick={handleClick}>
2245 stevensc 106
          {children}
2162 stevensc 107
 
2245 stevensc 108
          {showOptions && (
2852 stevensc 109
            <ShareContainer>
2245 stevensc 110
              <FacebookShareButton
111
                url={shareUrl}
112
                onShareWindowClose={incrementCount}
113
                disabled={!shareUrl}
114
              >
115
                <FacebookIcon size={32} round />
116
              </FacebookShareButton>
117
              <TwitterShareButton
118
                url={shareUrl}
119
                onShareWindowClose={incrementCount}
120
                disabled={!shareUrl}
121
              >
122
                <TwitterIcon size={32} round />
123
              </TwitterShareButton>
124
              <TelegramShareButton
125
                url={shareUrl}
126
                onShareWindowClose={incrementCount}
127
                disabled={!shareUrl}
128
              >
129
                <TelegramIcon size={32} round />
130
              </TelegramShareButton>
131
              <WhatsappShareButton
132
                url={shareUrl}
133
                onShareWindowClose={incrementCount}
134
                disabled={!shareUrl}
135
              >
136
                <WhatsappIcon size={32} round />
137
              </WhatsappShareButton>
138
              <RedditShareButton
139
                url={shareUrl}
140
                onShareWindowClose={incrementCount}
141
                disabled={!shareUrl}
142
              >
143
                <RedditIcon size={32} round />
144
              </RedditShareButton>
145
              <EmailShareButton
146
                url={shareUrl}
147
                onShareWindowClose={incrementCount}
148
                disabled={!shareUrl}
149
              >
150
                <EmailIcon size={32} round />
151
              </EmailShareButton>
2852 stevensc 152
            </ShareContainer>
2245 stevensc 153
          )}
154
        </Component>
155
        <SharePopup
156
          show={openPopup}
157
          shareData={{ url: shareUrl }}
158
          onClose={() => setOpenPopup(false)}
159
        />
160
      </>
2157 stevensc 161
    )
162
  }
199 stevensc 163
}