Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 2776 | Rev 2852 | 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
 
2851 stevensc 26
const StyledShareContainer = 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
}))
199 stevensc 37
 
38
export default function withExternalShare(Component, url) {
2157 stevensc 39
  return function ({ children, setValue, shorterUrl }) {
2244 stevensc 40
    const [showOptions, setShowOptions] = useState(false)
2245 stevensc 41
    const [openPopup, setOpenPopup] = useState(false)
2157 stevensc 42
    const [shareUrl, setShareUrl] = useState('')
2245 stevensc 43
    const isMobile = useMobile()
2157 stevensc 44
    const dispatch = useDispatch()
199 stevensc 45
 
2253 stevensc 46
    const handleClick = async () => {
47
      const shorterUrl = shareUrl || (await getShareUrl())
48
      setShareUrl(shorterUrl)
49
 
2254 stevensc 50
      if (!isMobile) {
51
        setShowOptions(!showOptions)
52
        return
53
      }
2253 stevensc 54
 
55
      if (navigator?.share) {
56
        try {
57
          await navigator.share({ url: shorterUrl })
58
          incrementCount()
59
        } catch (error) {
60
          dispatch(addNotification({ style: 'danger', msg: error.message }))
61
        }
62
        return
63
      }
64
 
65
      if (window?.AndroidShareHandler) {
66
        try {
67
          window.AndroidShareHandler.share(shorterUrl)
68
        } catch (error) {
69
          dispatch(addNotification({ style: 'danger', msg: error.message }))
70
        }
71
        return
72
      }
73
 
74
      setOpenPopup(true)
75
    }
76
 
2248 stevensc 77
    const getShareUrl = async () => {
78
      try {
79
        const response = await axios.get(url)
80
        const { data, success } = response.data
81
        if (!success) throw new Error(data)
82
        return data
83
      } catch (error) {
84
        dispatch(addNotification({ style: 'danger', msg: error.message }))
85
      }
86
    }
87
 
199 stevensc 88
    const incrementCount = async () => {
89
      await axios
2157 stevensc 90
        .post(shorterUrl)
2244 stevensc 91
        .then((response) => {
92
          const { data, success } = response.data
93
          if (!success) throw new Error(data)
94
          setShowOptions(false)
95
          setValue(data)
199 stevensc 96
        })
2244 stevensc 97
        .catch((err) =>
98
          dispatch(addNotification({ style: 'danger', msg: err.message }))
99
        )
2157 stevensc 100
    }
199 stevensc 101
 
102
    return (
2245 stevensc 103
      <>
2253 stevensc 104
        <Component onClick={handleClick}>
2245 stevensc 105
          {children}
2162 stevensc 106
 
2245 stevensc 107
          {showOptions && (
108
            <StyledShareContainer>
109
              <FacebookShareButton
110
                url={shareUrl}
111
                onShareWindowClose={incrementCount}
112
                disabled={!shareUrl}
113
              >
114
                <FacebookIcon size={32} round />
115
              </FacebookShareButton>
116
              <TwitterShareButton
117
                url={shareUrl}
118
                onShareWindowClose={incrementCount}
119
                disabled={!shareUrl}
120
              >
121
                <TwitterIcon size={32} round />
122
              </TwitterShareButton>
123
              <TelegramShareButton
124
                url={shareUrl}
125
                onShareWindowClose={incrementCount}
126
                disabled={!shareUrl}
127
              >
128
                <TelegramIcon size={32} round />
129
              </TelegramShareButton>
130
              <WhatsappShareButton
131
                url={shareUrl}
132
                onShareWindowClose={incrementCount}
133
                disabled={!shareUrl}
134
              >
135
                <WhatsappIcon size={32} round />
136
              </WhatsappShareButton>
137
              <RedditShareButton
138
                url={shareUrl}
139
                onShareWindowClose={incrementCount}
140
                disabled={!shareUrl}
141
              >
142
                <RedditIcon size={32} round />
143
              </RedditShareButton>
144
              <EmailShareButton
145
                url={shareUrl}
146
                onShareWindowClose={incrementCount}
147
                disabled={!shareUrl}
148
              >
149
                <EmailIcon size={32} round />
150
              </EmailShareButton>
151
            </StyledShareContainer>
152
          )}
153
        </Component>
154
        <SharePopup
155
          show={openPopup}
156
          shareData={{ url: shareUrl }}
157
          onClose={() => setOpenPopup(false)}
158
        />
159
      </>
2157 stevensc 160
    )
161
  }
199 stevensc 162
}