Rev 2254 | Rev 2851 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
import React, { useState } from 'react'import { useDispatch } from 'react-redux'import {EmailIcon,EmailShareButton,FacebookIcon,FacebookShareButton,RedditIcon,RedditShareButton,TelegramIcon,TelegramShareButton,TwitterIcon,TwitterShareButton,WhatsappIcon,WhatsappShareButton} from 'react-share'import styled from 'styled-components'import { addNotification } from 'store/notification/notification.actions'import { axios } from '../../../utils'import SharePopup from './mobile-share/MobileSharePopUp'import { useMobile } from '@hooks'const StyledShareContainer = styled.div`display: flex;position: absolute;align-items: center;padding: 5px 1rem;bottom: calc(100% + 0.5rem);gap: 0.5rem;right: 0;width: 16.5rem;border-radius: 10px;background-color: #fff;box-shadow: 0px 4px 4px -2px rgb(0 0 0 / 12%),0px -4px 4px -2px rgb(0 0 0 / 12%);`export default function withExternalShare(Component, url) {return function ({ children, setValue, shorterUrl }) {const [showOptions, setShowOptions] = useState(false)const [openPopup, setOpenPopup] = useState(false)const [shareUrl, setShareUrl] = useState('')const isMobile = useMobile()const dispatch = useDispatch()const handleClick = async () => {const shorterUrl = shareUrl || (await getShareUrl())setShareUrl(shorterUrl)if (!isMobile) {setShowOptions(!showOptions)return}if (navigator?.share) {try {await navigator.share({ url: shorterUrl })incrementCount()} catch (error) {dispatch(addNotification({ style: 'danger', msg: error.message }))}return}if (window?.AndroidShareHandler) {try {window.AndroidShareHandler.share(shorterUrl)} catch (error) {dispatch(addNotification({ style: 'danger', msg: error.message }))}return}setOpenPopup(true)}const getShareUrl = async () => {try {const response = await axios.get(url)const { data, success } = response.dataif (!success) throw new Error(data)return data} catch (error) {dispatch(addNotification({ style: 'danger', msg: error.message }))}}const incrementCount = async () => {await axios.post(shorterUrl).then((response) => {const { data, success } = response.dataif (!success) throw new Error(data)setShowOptions(false)setValue(data)}).catch((err) =>dispatch(addNotification({ style: 'danger', msg: err.message })))}return (<><Component onClick={handleClick}>{children}{showOptions && (<StyledShareContainer><FacebookShareButtonurl={shareUrl}onShareWindowClose={incrementCount}disabled={!shareUrl}><FacebookIcon size={32} round /></FacebookShareButton><TwitterShareButtonurl={shareUrl}onShareWindowClose={incrementCount}disabled={!shareUrl}><TwitterIcon size={32} round /></TwitterShareButton><TelegramShareButtonurl={shareUrl}onShareWindowClose={incrementCount}disabled={!shareUrl}><TelegramIcon size={32} round /></TelegramShareButton><WhatsappShareButtonurl={shareUrl}onShareWindowClose={incrementCount}disabled={!shareUrl}><WhatsappIcon size={32} round /></WhatsappShareButton><RedditShareButtonurl={shareUrl}onShareWindowClose={incrementCount}disabled={!shareUrl}><RedditIcon size={32} round /></RedditShareButton><EmailShareButtonurl={shareUrl}onShareWindowClose={incrementCount}disabled={!shareUrl}><EmailIcon size={32} round /></EmailShareButton></StyledShareContainer>)}</Component><SharePopupshow={openPopup}shareData={{ url: shareUrl }}onClose={() => setOpenPopup(false)}/></>)}}