Proyectos de Subversion LeadersLinked - SPA

Rev

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