Proyectos de Subversion LeadersLinked - SPA

Rev

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

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