Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 2852 | Ir a la última revisión | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 2852 Rev 3596
Línea 1... Línea 1...
1
import React, { useState } from 'react'
1
import React, { useState } from 'react';
2
import { useDispatch } from 'react-redux'
2
import { useDispatch } from 'react-redux';
3
import {
3
import {
4
  EmailIcon,
4
  EmailIcon,
5
  EmailShareButton,
5
  EmailShareButton,
6
  FacebookIcon,
6
  FacebookIcon,
7
  FacebookShareButton,
7
  FacebookShareButton,
Línea 11... Línea 11...
11
  TelegramShareButton,
11
  TelegramShareButton,
12
  TwitterIcon,
12
  TwitterIcon,
13
  TwitterShareButton,
13
  TwitterShareButton,
14
  WhatsappIcon,
14
  WhatsappIcon,
15
  WhatsappShareButton
15
  WhatsappShareButton
16
} from 'react-share'
16
} from 'react-share';
17
import { styled } from '@mui/material'
17
import { styled } from '@mui/material';
Línea 18... Línea 18...
18
 
18
 
19
import { axios } from '@app/utils'
19
import { axios } from '@app/utils';
20
import { useMobile } from '@hooks'
20
import { useMobile } from '@hooks';
21
import { addNotification } from '@app/redux/notification/notification.actions'
21
import { addNotification } from '@app/redux/notification/notification.actions';
Línea 22... Línea 22...
22
import colors from '@styles/colors'
22
import colors from '@styles/config/colors';
Línea 23... Línea 23...
23
 
23
 
24
import SharePopup from './mobile-share/MobileSharePopUp'
24
import SharePopup from './mobile-share/MobileSharePopUp';
25
 
25
 
26
const ShareContainer = styled('div')(({ theme }) => ({
26
const ShareContainer = styled('div')(({ theme }) => ({
Línea 32... Línea 32...
32
  gap: theme.spacing(0.5),
32
  gap: theme.spacing(0.5),
33
  right: 0,
33
  right: 0,
34
  borderRadius: theme.shape.borderRadius,
34
  borderRadius: theme.shape.borderRadius,
35
  backgroundColor: colors.main,
35
  backgroundColor: colors.main,
36
  border: `1px solid ${colors.border.primary}`
36
  border: `1px solid ${colors.border.primary}`
37
}))
37
}));
Línea 38... Línea 38...
38
 
38
 
39
export default function withExternalShare(Component, url) {
39
export default function withExternalShare(Component, url) {
40
  return function ({ children, setValue, shorterUrl }) {
40
  return function ({ children, setValue, shorterUrl }) {
41
    const [showOptions, setShowOptions] = useState(false)
41
    const [showOptions, setShowOptions] = useState(false);
42
    const [openPopup, setOpenPopup] = useState(false)
42
    const [openPopup, setOpenPopup] = useState(false);
43
    const [shareUrl, setShareUrl] = useState('')
43
    const [shareUrl, setShareUrl] = useState('');
44
    const isMobile = useMobile()
44
    const isMobile = useMobile();
Línea 45... Línea 45...
45
    const dispatch = useDispatch()
45
    const dispatch = useDispatch();
46
 
46
 
47
    const handleClick = async () => {
47
    const handleClick = async () => {
Línea 48... Línea 48...
48
      const shorterUrl = shareUrl || (await getShareUrl())
48
      const shorterUrl = shareUrl || (await getShareUrl());
49
      setShareUrl(shorterUrl)
49
      setShareUrl(shorterUrl);
50
 
50
 
51
      if (!isMobile) {
51
      if (!isMobile) {
Línea 52... Línea 52...
52
        setShowOptions(!showOptions)
52
        setShowOptions(!showOptions);
53
        return
53
        return;
54
      }
54
      }
55
 
55
 
56
      if (navigator?.share) {
56
      if (navigator?.share) {
57
        try {
57
        try {
58
          await navigator.share({ url: shorterUrl })
58
          await navigator.share({ url: shorterUrl });
59
          incrementCount()
59
          incrementCount();
60
        } catch (error) {
60
        } catch (error) {
Línea 61... Línea 61...
61
          dispatch(addNotification({ style: 'danger', msg: error.message }))
61
          dispatch(addNotification({ style: 'danger', msg: error.message }));
62
        }
62
        }
63
        return
63
        return;
64
      }
64
      }
65
 
65
 
66
      if (window?.AndroidShareHandler) {
66
      if (window?.AndroidShareHandler) {
67
        try {
67
        try {
68
          window.AndroidShareHandler.share(shorterUrl)
68
          window.AndroidShareHandler.share(shorterUrl);
Línea 69... Línea 69...
69
        } catch (error) {
69
        } catch (error) {
70
          dispatch(addNotification({ style: 'danger', msg: error.message }))
70
          dispatch(addNotification({ style: 'danger', msg: error.message }));
Línea 71... Línea 71...
71
        }
71
        }
72
        return
72
        return;
73
      }
73
      }
74
 
74
 
75
      setOpenPopup(true)
75
      setOpenPopup(true);
76
    }
76
    };
77
 
77
 
78
    const getShareUrl = async () => {
78
    const getShareUrl = async () => {
79
      try {
79
      try {
80
        const response = await axios.get(url)
80
        const response = await axios.get(url);
Línea 81... Línea 81...
81
        const { data, success } = response.data
81
        const { data, success } = response.data;
82
        if (!success) throw new Error(data)
82
        if (!success) throw new Error(data);
83
        return data
83
        return data;
84
      } catch (error) {
84
      } catch (error) {
85
        dispatch(addNotification({ style: 'danger', msg: error.message }))
85
        dispatch(addNotification({ style: 'danger', msg: error.message }));
86
      }
86
      }
87
    }
87
    };
88
 
88
 
89
    const incrementCount = async () => {
89
    const incrementCount = async () => {
90
      await axios
-
 
91
        .post(shorterUrl)
90
      await axios
92
        .then((response) => {
-
 
93
          const { data, success } = response.data
91
        .post(shorterUrl)
Línea 94... Línea 92...
94
          if (!success) throw new Error(data)
92
        .then((response) => {
95
          setShowOptions(false)
93
          const { data, success } = response.data;
96
          setValue(data)
94
          if (!success) throw new Error(data);
97
        })
95
          setShowOptions(false);
Línea 156... Línea 154...
156
          show={openPopup}
154
          show={openPopup}
157
          shareData={{ url: shareUrl }}
155
          shareData={{ url: shareUrl }}
158
          onClose={() => setOpenPopup(false)}
156
          onClose={() => setOpenPopup(false)}
159
        />
157
        />
160
      </>
158
      </>
161
    )
159
    );
162
  }
160
  };
163
}
161
}