Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3736 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
3736 stevensc 1
import React, { useState } from 'react';
2
import { connect } from 'react-redux';
3
import { Link } from 'react-router-dom';
4
 
5
import { axios } from '@app/utils';
6
import { asyncLogout } from '@store/auth/auth.actions';
7
import { addNotification } from '@app/redux/notification/notification.actions';
8
 
9
import Form from '@app/components/UI/form/Form';
10
import Widget from '@app/components/UI/Widget';
11
import Button from '@app/components/UI/buttons/Buttons';
12
import Spinner from '@app/components/UI/Spinner';
13
import Input from '@app/components/UI/inputs/Input';
14
 
15
function CloseAccount({ addNotification }) {
16
  const [showInputCode, setShowInputCode] = useState(false);
17
  const [loading, setLoading] = useState(false);
18
  const [code, setCode] = useState('');
19
 
20
  const handleAlert = ({ status = true, message = 'Email enviado exitosamente' }) => {
21
    addNotification({
22
      style: status ? 'success' : 'danger',
23
      msg: message
24
    });
25
  };
26
 
27
  const handleGetCode = async () => {
28
    try {
29
      setShowInputCode(true);
30
      setLoading(true);
31
      const response = await axios.get('/account-settings/delete-account');
32
      const { data, success } = response.data;
33
      handleAlert(success, data.message);
34
    } catch (error) {
3741 stevensc 35
      handleAlert(false, error.message);
3736 stevensc 36
    } finally {
37
      setLoading(false);
38
    }
39
  };
40
 
41
  const resendCode = async (e) => {
42
    e.preventDefault();
43
    try {
44
      setLoading(true);
45
      const res = await axios.get('/account-settings/delete-account');
46
      handleAlert(res.data.success, res.data.data.message);
47
      console.log('>>: resend code > ', res);
48
    } catch (error) {
49
      handleAlert(false, 'Disculpe, ha ocurrido un error');
50
      console.log('>>: error > ', error);
51
    } finally {
52
      setLoading(false);
53
    }
54
  };
55
 
56
  const handleSubmit = async (e) => {
57
    try {
58
      e.preventDefault();
59
      const data = new FormData();
60
      data.append('code', code);
61
      setLoading(true);
62
      const res = await axios.post('/account-settings/delete-account', data);
63
      handleAlert(res.data.success, res.data.data.message);
64
      if (res.data.success) {
65
        asyncLogout();
66
      }
67
    } catch (error) {
68
      handleAlert(false, 'Disculpe, ha ocurrido un error');
69
      console.log('>>: error > ', error);
70
    } finally {
71
      setLoading(false);
72
    }
73
  };
74
 
75
  return (
76
    <Widget>
77
      <Widget.Body>
78
        {showInputCode ? (
79
          <Form onSubmit={handleSubmit}>
80
            <Input
81
              id='verify'
82
              name='verify'
83
              label='Ingrese el codigo enviado a su correo electrónico'
84
              onChange={(e) => setCode(e.target.value)}
85
              value={code}
86
            />
87
 
88
            <Link to='/reset-code' onClick={resendCode}>
89
              ¿No ha recibido su correo?, solicite un codigo nuevo
90
            </Link>
91
 
92
            <Button color='primary' type='submit'>
93
              Enviar
94
            </Button>
95
          </Form>
96
        ) : (
97
          <>
98
            <h2>¿Esta seguro de eliminar su cuenta?</h2>
99
 
100
            <Button
101
              color='primary'
102
              onClick={() => handleGetCode()}
103
              disabled={showInputCode}
104
              styles={{
105
                marginBottom: '.5rem'
106
              }}
107
            >
108
              Si, estoy seguro
109
            </Button>
110
          </>
111
        )}
112
 
113
        {loading && <Spinner />}
114
      </Widget.Body>
115
    </Widget>
116
  );
117
}
118
 
119
const mapDispatchToProps = {
120
  addNotification: (notification) => addNotification(notification)
121
};
122
 
123
export default connect(null, mapDispatchToProps)(CloseAccount);