Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 7538 | Rev 7792 | 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)
31
            .then(async ({ data }) => {
32
                if (data.success) {
33
                    try {
7476 stevensc 34
                        action && dispatch(action())
7498 stevensc 35
                        onComplete && onComplete()
7790 stevensc 36
                        dispatch(addNotification({
37
                            style: "success",
38
                            msg: message ? message : 'Eliminado correctamente'
39
                        }))
7427 stevensc 40
 
41
                        closeModal()
42
                    }
7790 stevensc 43
                    catch (err) { dispatch(addNotification({
44
                        style: "error",
45
                        msg: "Ha ocurrido un error"
46
                    })) }
7427 stevensc 47
                }
48
            })
49
            .catch((err) => console.log(err))
50
    };
51
 
52
    return (
53
        <Modal
54
            size="sm"
55
            show={isOpen}
56
            onHide={closeModal}
57
            autoFocus={false}
58
        >
59
            <form onSubmit={handleSubmit(onSubmit)}>
60
                <Modal.Body>
61
                    <h2>{title}</h2>
62
                </Modal.Body>
63
                <Modal.Footer>
64
                    <Button
65
                        variant="success"
66
                        type="submit"
67
                    >
68
69
                    </Button>
70
                    <Button
71
                        variant="danger"
72
                        onClick={closeModal}
73
                    >
74
                        No
75
                    </Button>
76
                </Modal.Footer>
77
            </form>
78
        </Modal >
79
    )
80
}
81
 
82
export default DeleteModal