Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev Autor Línea Nro. Línea
3719 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) {
35
      console.log('>>: error > ', error);
36
      handleAlert(false, 'Disculpe, ha ocurrido un error');
37
    } finally {
38
      setLoading(false);
39
    }
40
  };
41
 
42
  const resendCode = async (e) => {
43
    e.preventDefault();
44
    try {
45
      setLoading(true);
46
      const res = await axios.get('/account-settings/delete-account');
47
      handleAlert(res.data.success, res.data.data.message);
48
      console.log('>>: resend code > ', res);
49
    } catch (error) {
50
      handleAlert(false, 'Disculpe, ha ocurrido un error');
51
      console.log('>>: error > ', error);
52
    } finally {
53
      setLoading(false);
54
    }
55
  };
56
 
57
  const handleSubmit = async (e) => {
58
    try {
59
      e.preventDefault();
60
      const data = new FormData();
61
      data.append('code', code);
62
      setLoading(true);
63
      const res = await axios.post('/account-settings/delete-account', data);
64
      handleAlert(res.data.success, res.data.data.message);
65
      if (res.data.success) {
66
        asyncLogout();
67
      }
68
    } catch (error) {
69
      handleAlert(false, 'Disculpe, ha ocurrido un error');
70
      console.log('>>: error > ', error);
71
    } finally {
72
      setLoading(false);
73
    }
74
  };
75
 
76
  return (
77
    <Widget>
78
      <Widget.Body>
79
        {showInputCode ? (
80
          <Form onSubmit={handleSubmit}>
81
            <Input
82
              id='verify'
83
              name='verify'
84
              label='Ingrese el codigo enviado a su correo electrónico'
85
              onChange={(e) => setCode(e.target.value)}
86
              value={code}
87
            />
88
 
89
            <Link to='/reset-code' onClick={resendCode}>
90
              ¿No ha recibido su correo?, solicite un codigo nuevo
91
            </Link>
92
 
93
            <Button color='primary' type='submit'>
94
              Enviar
95
            </Button>
96
          </Form>
97
        ) : (
98
          <>
99
            <h2>¿Esta seguro de eliminar su cuenta?</h2>
100
 
101
            <Button
102
              color='primary'
103
              onClick={() => handleGetCode()}
104
              disabled={showInputCode}
105
              styles={{
106
                marginBottom: '.5rem'
107
              }}
108
            >
109
              Si, estoy seguro
110
            </Button>
111
          </>
112
        )}
113
 
114
        {loading && <Spinner />}
115
      </Widget.Body>
116
    </Widget>
117
  );
118
}
119
 
120
const mapDispatchToProps = {
121
  addNotification: (notification) => addNotification(notification)
122
};
123
 
124
export default connect(null, mapDispatchToProps)(CloseAccount);