Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 2243 | Rev 2245 | 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 [showOptions, setShowOptions] = useState(false)
    const [shareUrl, setShareUrl] = useState('')
    const dispatch = useDispatch()

    const toggleShareOptions = () => setShowOptions(!showOptions)

    const incrementCount = async () => {
      await axios
        .post(shorterUrl)
        .then((response) => {
          const { data, success } = response.data
          if (!success) throw new Error(data)
          setShowOptions(false)
          setValue(data)
        })
        .catch((err) =>
          dispatch(addNotification({ style: 'danger', msg: err.message }))
        )
    }

    useEffect(() => {
      if (!showOptions || shareUrl) return

      const getShareUrl = async () => {
        try {
          const response = await axios.get(url)
          const { data, success } = response.data
          if (!success) throw new Error(data)
          setShareUrl(data)
        } catch (error) {
          dispatch(addNotification({ style: 'danger', msg: error.message }))
        }
      }

      getShareUrl()
    }, [showOptions, shareUrl])

    return (
      <Component onClick={toggleShareOptions}>
        {children}

        {showOptions && (
          <StyledShareContainer>
            <FacebookShareButton
              url={shareUrl}
              onShareWindowClose={incrementCount}
              disabled={!shareUrl}
            >
              <FacebookIcon size={32} round />
            </FacebookShareButton>
            <TwitterShareButton
              url={shareUrl}
              onShareWindowClose={incrementCount}
              disabled={!shareUrl}
            >
              <TwitterIcon size={32} round />
            </TwitterShareButton>
            <TelegramShareButton
              url={shareUrl}
              onShareWindowClose={incrementCount}
              disabled={!shareUrl}
            >
              <TelegramIcon size={32} round />
            </TelegramShareButton>
            <WhatsappShareButton
              url={shareUrl}
              onShareWindowClose={incrementCount}
              disabled={!shareUrl}
            >
              <WhatsappIcon size={32} round />
            </WhatsappShareButton>
            <RedditShareButton
              url={shareUrl}
              onShareWindowClose={incrementCount}
              disabled={!shareUrl}
            >
              <RedditIcon size={32} round />
            </RedditShareButton>
            <EmailShareButton
              url={shareUrl}
              onShareWindowClose={incrementCount}
              disabled={!shareUrl}
            >
              <EmailIcon size={32} round />
            </EmailShareButton>
          </StyledShareContainer>
        )}
      </Component>
    )
  }
}