Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3156 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 3156 Rev 3719
Línea 1... Línea 1...
1
import React, { useEffect, useState } from 'react'
1
import React, { useEffect, useState } from 'react';
2
import { useDispatch } from 'react-redux'
2
import { useDispatch } from 'react-redux';
3
 
3
 
4
import { axios } from '@app/utils'
4
import { axios } from '@app/utils';
5
import { addNotification } from '@app/redux/notification/notification.actions'
5
import { addNotification } from '@app/redux/notification/notification.actions';
6
 
6
 
7
import Modal from '@app/components/UI/modal/Modal'
7
import Modal from '@app/components/UI/modal/Modal';
8
import Input from '@app/components/UI/inputs/Input'
8
import Input from '@app/components/UI/inputs/Input';
9
import Button from '@app/components/UI/buttons/Buttons'
9
import Button from '@app/components/UI/buttons/Buttons';
10
 
10
 
11
const SharePopup = ({ shareData, onClose, onError, show }) => {
11
const SharePopup = ({ shareData, onClose, onError, show }) => {
12
  const [shareUrl, setShareUrl] = useState('')
12
  const [shareUrl, setShareUrl] = useState('');
13
  const [state, setState] = useState('pending')
13
  const [state, setState] = useState('pending');
14
  const dispatch = useDispatch()
14
  const dispatch = useDispatch();
15
 
15
 
16
  const getShareUrl = (url = '') => {
16
  const getShareUrl = (url = '') => {
17
    axios
17
    axios
18
      .get(url)
18
      .get(url)
19
      .then(({ data }) => {
19
      .then(({ data }) => {
20
        if (!data.success) {
20
        if (!data.success) {
21
          throw new Error(data.data)
21
          throw new Error(data.data);
22
        }
22
        }
23
 
23
 
24
        setShareUrl(data.data)
24
        setShareUrl(data.data);
25
      })
25
      })
26
      .catch((error) => {
26
      .catch((error) => {
27
        onError(error)
27
        onError(error);
28
        dispatch(addNotification({ style: 'danger', msg: error.message }))
28
        dispatch(addNotification({ style: 'danger', msg: error.message }));
29
      })
29
      });
30
  }
30
  };
31
 
31
 
32
  const copyClicked = async () => {
32
  const copyClicked = async () => {
33
    try {
33
    try {
34
      await navigator.clipboard.writeText(shareUrl || '')
34
      await navigator.clipboard.writeText(shareUrl || '');
35
      setState('success')
35
      setState('success');
36
    } catch (err) {
36
    } catch (err) {
37
      onError && onError(err)
37
      onError && onError(err);
38
      setState('error')
38
      setState('error');
39
    }
39
    }
40
  }
40
  };
41
 
41
 
42
  const getButtonText = (state) => {
42
  const getButtonText = (state) => {
43
    switch (state) {
43
    switch (state) {
44
      case 'success':
44
      case 'success':
45
        return 'URL Copiada al portapapeles'
45
        return 'URL Copiada al portapapeles';
46
      case 'pending':
46
      case 'pending':
47
      default:
47
      default:
48
        return 'Copiar URL'
48
        return 'Copiar URL';
49
    }
49
    }
50
  }
50
  };
51
 
51
 
52
  useEffect(() => {
52
  useEffect(() => {
53
    if (show) {
53
    if (show) {
54
      getShareUrl(shareData?.url)
54
      getShareUrl(shareData?.url);
55
    }
55
    }
56
  }, [shareData, show])
56
  }, [shareData, show]);
57
 
57
 
58
  return (
58
  return (
59
    <Modal show={show} onClose={onClose}>
59
    <Modal show={show} onClose={onClose}>
60
      <Input
60
      <Input
61
        value={shareUrl}
61
        value={shareUrl}
62
        readOnly
62
        readOnly
63
        error={
63
        error={
64
          state === 'error'
64
          state === 'error'
65
            ? 'No se pudo copiar al portapapeles, por favor copia la url manualmente para compartir.'
65
            ? 'No se pudo copiar al portapapeles, por favor copia la url manualmente para compartir.'
66
            : null
66
            : null
67
        }
67
        }
68
      />
68
      />
69
 
69
 
70
      <Button color='primary' onClick={copyClicked}>
70
      <Button color='primary' onClick={copyClicked}>
71
        {getButtonText(state)}
71
        {getButtonText(state)}
72
      </Button>
72
      </Button>
73
    </Modal>
73
    </Modal>
74
  )
74
  );
75
}
75
};
76
 
76
 
77
export default SharePopup
77
export default SharePopup;