Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 15208 | Rev 16646 | 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 { useDispatch } from 'react-redux'
5
import { addNotification } from '../redux/notification/notification.actions'
7427 stevensc 6
 
7
const DeleteModal = ({
10513 stevensc 8
	isOpen = false,
9
	closeModal = function () { },
10
	title = 'Estas seguro?',
11
	action,
12
	onComplete,
13
	url,
14
	message
7427 stevensc 15
}) => {
16
 
10513 stevensc 17
	const dispatch = useDispatch()
7427 stevensc 18
 
10513 stevensc 19
	const onSubmit = () => {
20
		if (!url && action) {
12996 stevensc 21
			dispatch(action())
22
			closeModal()
23
			return
10513 stevensc 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) {
15412 stevensc 35
					typeof data.data === 'string'
36
						? dispatch(addNotification({ style: 'danger', msg: data.data }))
37
						: Object
38
							.entries(data.data)
39
							.map(([key, value]) => value.map(err => dispatch(addNotification({ style: 'danger', msg: `${key}: ${err}` }))))
40
					return
10513 stevensc 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
15208 stevensc 61
			size="md"
10513 stevensc 62
			show={isOpen}
63
			onHide={closeModal}
64
			autoFocus={false}
65
		>
15208 stevensc 66
			<Modal.Body>
67
				<h3>{title}</h3>
68
			</Modal.Body>
69
			<Modal.Footer>
70
				<button
71
					className='btn btn-primary'
72
					onClick={onSubmit}>
73
74
				</button>
75
				<button
76
					className='btn btn-secondary'
77
					onClick={closeModal}>
78
					No
79
				</button>
80
			</Modal.Footer>
10513 stevensc 81
		</Modal >
82
	)
7427 stevensc 83
}
84
 
85
export default DeleteModal