Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev Autor Línea Nro. Línea
2157 stevensc 1
import React, { useEffect, useState } from 'react'
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'
17
import { addNotification } from 'store/notification/notification.actions'
18
import { axios } from '../../../utils'
19
import styled from 'styled-components'
199 stevensc 20
 
641 stevensc 21
const StyledShareContainer = styled.div`
22
  display: flex;
23
  position: absolute;
24
  align-items: center;
25
  padding: 5px 1rem;
26
  bottom: calc(100% + 0.5rem);
27
  gap: 0.5rem;
28
  right: 0;
29
  width: 16.5rem;
30
  border-radius: 10px;
31
  background-color: #fff;
32
  box-shadow: 0px 4px 4px -2px rgb(0 0 0 / 12%),
33
    0px -4px 4px -2px rgb(0 0 0 / 12%);
2157 stevensc 34
`
641 stevensc 35
 
199 stevensc 36
export default function withExternalShare(Component, url) {
2157 stevensc 37
  return function ({ children, setValue, shorterUrl }) {
38
    const [shareOptions, setShareOptions] = useState(false)
39
    const [shareUrl, setShareUrl] = useState('')
40
    const dispatch = useDispatch()
199 stevensc 41
 
631 stevensc 42
    const toggleShareOptions = () => {
2157 stevensc 43
      setShareOptions(!shareOptions)
44
    }
199 stevensc 45
 
2242 stevensc 46
    const getShorterUrl = new Promise(() => {
47
      axios
199 stevensc 48
        .get(url)
49
        .then(({ data }) => {
50
          if (!data.success) {
2157 stevensc 51
            setShareOptions(false)
2242 stevensc 52
            throw new Error(data.data)
199 stevensc 53
          }
54
 
2157 stevensc 55
          setShareUrl(data.data)
199 stevensc 56
        })
2242 stevensc 57
        .catch((err) =>
58
          dispatch(addNotification({ style: 'danger', msg: err.message }))
59
        )
60
    })
199 stevensc 61
 
62
    const incrementCount = async () => {
63
      await axios
2157 stevensc 64
        .post(shorterUrl)
199 stevensc 65
        .then(({ data }) => {
66
          if (!data.success) {
2157 stevensc 67
            dispatch(addNotification({ style: 'danger', msg: data.data }))
68
            return
199 stevensc 69
          }
70
 
2157 stevensc 71
          setShareOptions(false)
72
          setValue(data.data)
199 stevensc 73
        })
2157 stevensc 74
        .catch((err) => console.log(err))
75
    }
199 stevensc 76
 
77
    return (
2157 stevensc 78
      <Component onClick={toggleShareOptions}>
79
        {children}
2162 stevensc 80
 
199 stevensc 81
        {shareOptions && (
641 stevensc 82
          <StyledShareContainer>
199 stevensc 83
            <FacebookShareButton
84
              url={shareUrl}
85
              onShareWindowClose={() => incrementCount()}
2242 stevensc 86
              beforeOnClick={getShorterUrl}
199 stevensc 87
            >
88
              <FacebookIcon size={32} round />
89
            </FacebookShareButton>
90
            <TwitterShareButton
91
              url={shareUrl}
92
              onShareWindowClose={() => incrementCount()}
93
            >
94
              <TwitterIcon size={32} round />
95
            </TwitterShareButton>
96
            <TelegramShareButton
97
              url={shareUrl}
98
              onShareWindowClose={() => incrementCount()}
99
            >
100
              <TelegramIcon size={32} round />
101
            </TelegramShareButton>
102
            <WhatsappShareButton
103
              url={shareUrl}
104
              onShareWindowClose={() => incrementCount()}
105
            >
106
              <WhatsappIcon size={32} round />
107
            </WhatsappShareButton>
108
            <RedditShareButton
109
              url={shareUrl}
110
              onShareWindowClose={() => incrementCount()}
111
            >
112
              <RedditIcon size={32} round />
113
            </RedditShareButton>
114
            <EmailShareButton
115
              url={shareUrl}
116
              onShareWindowClose={() => incrementCount()}
117
            >
118
              <EmailIcon size={32} round />
119
            </EmailShareButton>
641 stevensc 120
          </StyledShareContainer>
199 stevensc 121
        )}
122
      </Component>
2157 stevensc 123
    )
124
  }
199 stevensc 125
}