Rev 2242 | Rev 2244 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
import React, { useEffect, 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 { addNotification } from 'store/notification/notification.actions'import { axios } from '../../../utils'import styled from 'styled-components'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 [shareOptions, setShareOptions] = useState(false)const [shareUrl, setShareUrl] = useState('')const dispatch = useDispatch()const toggleShareOptions = () => {setShareOptions(!shareOptions)}const getShorterUrl = () =>new Promise(() => {axios.get(url).then(({ data }) => {if (!data.success) {setShareOptions(false)throw new Error(data.data)}setShareUrl(data.data)}).catch((err) =>dispatch(addNotification({ style: 'danger', msg: err.message })))})const incrementCount = async () => {await axios.post(shorterUrl).then(({ data }) => {if (!data.success) {dispatch(addNotification({ style: 'danger', msg: data.data }))return}setShareOptions(false)setValue(data.data)}).catch((err) => console.log(err))}return (<Component onClick={toggleShareOptions}>{children}{shareOptions && (<StyledShareContainer><FacebookShareButtonurl={shareUrl}onShareWindowClose={() => incrementCount()}beforeOnClick={getShorterUrl}><FacebookIcon size={32} round /></FacebookShareButton><TwitterShareButtonurl={shareUrl}onShareWindowClose={() => incrementCount()}><TwitterIcon size={32} round /></TwitterShareButton><TelegramShareButtonurl={shareUrl}onShareWindowClose={() => incrementCount()}><TelegramIcon size={32} round /></TelegramShareButton><WhatsappShareButtonurl={shareUrl}onShareWindowClose={() => incrementCount()}><WhatsappIcon size={32} round /></WhatsappShareButton><RedditShareButtonurl={shareUrl}onShareWindowClose={() => incrementCount()}><RedditIcon size={32} round /></RedditShareButton><EmailShareButtonurl={shareUrl}onShareWindowClose={() => incrementCount()}><EmailIcon size={32} round /></EmailShareButton></StyledShareContainer>)}</Component>)}}