Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 7795 | Rev 11272 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |

import React from 'react'
import axios from 'axios';
import { Modal, Button } from 'react-bootstrap';
import { useForm } from "react-hook-form";
import { useDispatch } from 'react-redux';
import { addNotification } from '../redux/notification/notification.actions';

const DeleteModal = ({
    isOpen = false,
    closeModal = function () { },
    title = 'Estas seguro?',
    action,
    onComplete,
    url,
    message
}) => {

    const { handleSubmit } = useForm();
    const dispatch = useDispatch();

    const onSubmit = () => {
        if (!url && action) {
            return dispatch(action())
        }

        if (!url && onComplete) {
            return onComplete()
        }

        axios.post(url)
            .then(({ data }) => {
                if (data.success) {
                    action && dispatch(action())
                    onComplete && onComplete()

                    closeModal()

                    dispatch(addNotification({
                        style: "success",
                        msg: message ? message : 'Se ha eliminado con exito'
                    }))
                }
            })
            .catch((err) => dispatch(addNotification({
                style: "danger",
                msg: "Ha ocurrido un error"
            })))
    };

    return (
        <Modal
            size="md"
            show={isOpen}
            onHide={closeModal}
            autoFocus={false}
        >
            <form onSubmit={handleSubmit(onSubmit)}>
                <Modal.Body>
                    <h2>{title}</h2>
                </Modal.Body>
                <Modal.Footer>
                    <Button
                        variant="success"
                        type="submit"
                    >
                    </Button>
                    <Button
                        variant="danger"
                        onClick={closeModal}
                    >
                        No
                    </Button>
                </Modal.Footer>
            </form>
        </Modal >
    )
}

export default DeleteModal