Proyectos de Subversion LeadersLinked - Backend

Rev

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

Rev Autor Línea Nro. Línea
7427 stevensc 1
import React from 'react'
10513 stevensc 2
import axios from 'axios'
14174 stevensc 3
import { Modal } from 'react-bootstrap'
10513 stevensc 4
import { useForm } from 'react-hook-form'
5
import { useDispatch } from 'react-redux'
6
import { addNotification } from '../redux/notification/notification.actions'
7427 stevensc 7
 
8
const DeleteModal = ({
10513 stevensc 9
	isOpen = false,
10
	closeModal = function () { },
11
	title = 'Estas seguro?',
12
	action,
13
	onComplete,
14
	url,
15
	message
7427 stevensc 16
}) => {
17
 
10513 stevensc 18
	const { handleSubmit } = useForm()
19
	const dispatch = useDispatch()
7427 stevensc 20
 
10513 stevensc 21
	const onSubmit = () => {
22
		if (!url && action) {
12996 stevensc 23
			dispatch(action())
24
			closeModal()
25
			return
10513 stevensc 26
		}
7427 stevensc 27
 
10513 stevensc 28
		if (!url && onComplete) {
12995 stevensc 29
			onComplete()
30
			closeModal()
31
			return
10513 stevensc 32
		}
7498 stevensc 33
 
10513 stevensc 34
		axios.post(url)
35
			.then(({ data }) => {
11272 stevensc 36
				if (!data.success) {
11275 stevensc 37
					return dispatch(addNotification({
11272 stevensc 38
						style: 'danger',
39
						msg: data.data
10513 stevensc 40
					}))
41
				}
12995 stevensc 42
 
11272 stevensc 43
				action && dispatch(action())
44
				onComplete && onComplete()
45
 
46
				closeModal()
12995 stevensc 47
 
11272 stevensc 48
				dispatch(addNotification({
49
					style: 'success',
50
					msg: message ? message : 'Se ha eliminado con exito'
51
				}))
10513 stevensc 52
			})
53
			.catch(() => dispatch(addNotification({
54
				style: 'danger',
55
				msg: 'Ha ocurrido un error'
56
			})))
57
	}
7427 stevensc 58
 
10513 stevensc 59
	return (
60
		<Modal
14358 kerby 61
			size="lg"
10513 stevensc 62
			show={isOpen}
63
			onHide={closeModal}
64
			autoFocus={false}
65
		>
14359 kerby 66
			{/* <form onSubmit={handleSubmit(onSubmit)}> */}
10513 stevensc 67
				<Modal.Body>
14357 kerby 68
					<h3>{title}</h3>
10513 stevensc 69
				</Modal.Body>
70
				<Modal.Footer>
14174 stevensc 71
					<button
72
						className='btn btn-primary'
14359 kerby 73
						// type="submit"
74
						onClick={onSubmit}
10513 stevensc 75
					>
11272 stevensc 76
14174 stevensc 77
					</button>
78
					<button
79
						className='btn btn-secondary'
10513 stevensc 80
						onClick={closeModal}
81
					>
11272 stevensc 82
						No
14174 stevensc 83
					</button>
10513 stevensc 84
				</Modal.Footer>
14359 kerby 85
			{/* </form> */}
10513 stevensc 86
		</Modal >
87
	)
7427 stevensc 88
}
89
 
90
export default DeleteModal