Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 7795 | Rev 10513 | 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'
2
import axios from 'axios';
3
import { Modal, Button } from 'react-bootstrap';
4
import { useForm } from "react-hook-form";
7476 stevensc 5
import { useDispatch } from 'react-redux';
7790 stevensc 6
import { addNotification } from '../redux/notification/notification.actions';
7427 stevensc 7
 
8
const DeleteModal = ({
9
    isOpen = false,
10
    closeModal = function () { },
11
    title = 'Estas seguro?',
12
    action,
7498 stevensc 13
    onComplete,
7790 stevensc 14
    url,
15
    message
7427 stevensc 16
}) => {
17
 
18
    const { handleSubmit } = useForm();
7476 stevensc 19
    const dispatch = useDispatch();
7427 stevensc 20
 
21
    const onSubmit = () => {
7498 stevensc 22
        if (!url && action) {
7476 stevensc 23
            return dispatch(action())
7427 stevensc 24
        }
25
 
7498 stevensc 26
        if (!url && onComplete) {
27
            return onComplete()
28
        }
29
 
7427 stevensc 30
        axios.post(url)
7792 stevensc 31
            .then(({ data }) => {
7427 stevensc 32
                if (data.success) {
7792 stevensc 33
                    action && dispatch(action())
34
                    onComplete && onComplete()
7427 stevensc 35
 
7792 stevensc 36
                    closeModal()
37
 
38
                    dispatch(addNotification({
39
                        style: "success",
7795 stevensc 40
                        msg: message ? message : 'Se ha eliminado con exito'
7792 stevensc 41
                    }))
7427 stevensc 42
                }
43
            })
7792 stevensc 44
            .catch((err) => dispatch(addNotification({
45
                style: "danger",
46
                msg: "Ha ocurrido un error"
47
            })))
7427 stevensc 48
    };
49
 
50
    return (
51
        <Modal
7806 stevensc 52
            size="md"
7427 stevensc 53
            show={isOpen}
54
            onHide={closeModal}
55
            autoFocus={false}
56
        >
57
            <form onSubmit={handleSubmit(onSubmit)}>
58
                <Modal.Body>
59
                    <h2>{title}</h2>
60
                </Modal.Body>
61
                <Modal.Footer>
62
                    <Button
63
                        variant="success"
64
                        type="submit"
65
                    >
66
67
                    </Button>
68
                    <Button
69
                        variant="danger"
70
                        onClick={closeModal}
71
                    >
72
                        No
73
                    </Button>
74
                </Modal.Footer>
75
            </form>
76
        </Modal >
77
    )
78
}
79
 
80
export default DeleteModal