Proyectos de Subversion LeadersLinked - Backend

Rev

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