Rev 2008 | Rev 2010 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
import React, { useState, useMemo } from 'react';import Spinner from '../../../shared/loading-spinner/Spinner';import styled from 'styled-components';import axios from '../../../utils/axios';import { addNotification } from '../../../redux/notification/notification.actions';import { connect } from 'react-redux';const StyledSpinnerContainer = styled.div`position: absolute;left: 0;top: 0;width: 100%;height: 100%;background: rgba(255, 255, 255, 0.4);display: flex;justify-content: center;align-items: center;z-index: 300;`;function CloseAccount(props) {const {addNotification} = propsconst [showInputCode, setShowInputCode] = useState(false);const [loading, setLoading] = useState(false);const [code, setCode] = useState('');const handleAlert = (status, message) => {addNotification({style: status ? "success" : 'danger',msg: message || 'Email enviado exitosamente',});}const handleGetCode = async () => {try {setShowInputCode(true)setLoading(true)const res = await axios.get('/account-settings/delete-account');handleAlert(res.data.success, res.data.data.message)} catch (error) {handleAlert(false, 'Disculpe, ha ocurrido un error')console.log('>>: error > ', error)}finally{setLoading(false)}}const resendCode = async () => {try {setLoading(true)const res = await axios.get('/account-settings/delete-account');handleAlert(res.data.success, res.data.data.message)console.log('>>: resend code > ', res)} catch (error) {handleAlert(false, 'Disculpe, ha ocurrido un error')console.log('>>: error > ', error)}finally{setLoading(false)}}const logout = () => {window.location.href = '/signout';}const handleSubmit = async (e) => {try {e.preventDefault()console.log('>>: e >', e)const data = new FormData();data.append('code', code)console.log('>>: data > ', data)setLoading(true)const res = await axios.post('/account-settings/delete-account', data);handleAlert(res.data.success, res.data.data.message)// if(res.data.success){// logout()// }} catch (error) {handleAlert(false, 'Disculpe, ha ocurrido un error')console.log('>>: error > ', error)}finally{setLoading(false)}}const CloseAccountContent = () => {if(showInputCode){return <form onSubmit={handleSubmit}><div className="form-group"><label htmlFor="exampleInputEmail1">Ingrese el codigo enviado a su correo electrónico</label><inputtype="text"className="form-control"onChange={e => setCode(e.target.value)}value={code}/><button className="btn btn-link btn-sm" onClick={() => resendCode()}>¿No ha recibido su correo?, solicite un codigo nuevo</button></div><button type="submit" className="btn btn-primary">Enviar</button></form>}else{return <><h1 className="text-center">¿Esta seguro de eliminar su cuenta?</h1><div className="row"><div className='col-md col-sm-12 text-right'><button className="btn btn-primary" onClick={() => handleGetCode()} disabled={showInputCode}>Si, estoy seguro</button></div><div className='col-md col-sm-12 text-left'><button className="btn btn-primary" disabled={showInputCode}>No estoy seguro</button></div></div></>}}return (<div className="acc-setting h-100 d-flex justify-content-center align-items-center" style={{ position: "relative" }}><div className="container"><div className="">{CloseAccountContent()}</div></div>{loading && (<StyledSpinnerContainer><Spinner /></StyledSpinnerContainer>)}</div>)}const mapDispatchToProps = {addNotification: (notification) => addNotification(notification),};export default connect(null, mapDispatchToProps)(CloseAccount);