Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 11275 | Rev 12149 | 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'
3
import { Modal, Button } from 'react-bootstrap'
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) {
23
			return dispatch(action())
24
		}
7427 stevensc 25
 
10513 stevensc 26
		if (!url && onComplete) {
27
			return onComplete()
28
		}
7498 stevensc 29
 
10513 stevensc 30
		axios.post(url)
31
			.then(({ data }) => {
11272 stevensc 32
				if (!data.success) {
11275 stevensc 33
					return dispatch(addNotification({
11272 stevensc 34
						style: 'danger',
35
						msg: data.data
10513 stevensc 36
					}))
37
				}
11911 stevensc 38
 
11272 stevensc 39
				action && dispatch(action())
40
				onComplete && onComplete()
41
 
42
				closeModal()
11911 stevensc 43
 
11272 stevensc 44
				dispatch(addNotification({
45
					style: 'success',
46
					msg: message ? message : 'Se ha eliminado con exito'
47
				}))
10513 stevensc 48
			})
49
			.catch(() => dispatch(addNotification({
50
				style: 'danger',
51
				msg: 'Ha ocurrido un error'
52
			})))
53
	}
7427 stevensc 54
 
10513 stevensc 55
	return (
56
		<Modal
57
			size="md"
58
			show={isOpen}
59
			onHide={closeModal}
60
			autoFocus={false}
61
		>
62
			<form onSubmit={handleSubmit(onSubmit)}>
63
				<Modal.Body>
64
					<h2>{title}</h2>
65
				</Modal.Body>
66
				<Modal.Footer>
67
					<Button
68
						variant="success"
69
						type="submit"
70
					>
11272 stevensc 71
10513 stevensc 72
					</Button>
73
					<Button
74
						variant="danger"
75
						onClick={closeModal}
76
					>
11272 stevensc 77
						No
10513 stevensc 78
					</Button>
79
				</Modal.Footer>
80
			</form>
81
		</Modal >
82
	)
7427 stevensc 83
}
84
 
85
export default DeleteModal