Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 16646 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
16646 stevensc 1
import React from "react";
2
import axios from "axios";
3
import { Modal } from "react-bootstrap";
4
import { useDispatch } from "react-redux";
5
import { addNotification } from "../redux/notification/notification.actions";
7427 stevensc 6
 
7
const DeleteModal = ({
16646 stevensc 8
  isOpen = false,
9
  closeModal = function () {},
10
  title = "Estas seguro?",
11
  action,
12
  onComplete,
13
  url,
14
  message,
7427 stevensc 15
}) => {
16646 stevensc 16
  const dispatch = useDispatch();
7427 stevensc 17
 
16646 stevensc 18
  const onSubmit = () => {
19
    if (!url && action) {
20
      dispatch(action());
21
      closeModal();
22
      return;
23
    }
7427 stevensc 24
 
16646 stevensc 25
    if (!url && onComplete) {
26
      onComplete();
27
      closeModal();
28
      return;
29
    }
7427 stevensc 30
 
16646 stevensc 31
    axios
32
      .post(url)
33
      .then(({ data }) => {
34
        if (!data.success) {
35
          typeof data.data === "string"
36
            ? dispatch(addNotification({ style: "danger", msg: data.data }))
37
            : Object.entries(data.data).map(([key, value]) =>
38
                value.map((err) =>
39
                  dispatch(
40
                    addNotification({ style: "danger", msg: `${key}: ${err}` })
41
                  )
42
                )
43
              );
44
          return;
45
        }
7498 stevensc 46
 
16646 stevensc 47
        action && dispatch(action());
48
        onComplete && onComplete();
12995 stevensc 49
 
16646 stevensc 50
        closeModal();
11272 stevensc 51
 
16646 stevensc 52
        dispatch(
53
          addNotification({
54
            style: "success",
55
            msg: message ? message : "Se ha eliminado con exito",
56
          })
57
        );
58
      })
59
      .catch(() =>
60
        dispatch(
61
          addNotification({
62
            style: "danger",
63
            msg: "Ha ocurrido un error",
64
          })
65
        )
66
      );
67
  };
12995 stevensc 68
 
16646 stevensc 69
  return (
70
    <Modal size="sm" show={isOpen} onHide={closeModal} autoFocus={false}>
71
      <Modal.Body>
72
        <h3>{title}</h3>
73
      </Modal.Body>
74
      <Modal.Footer>
75
        <button className="btn btn-primary" onClick={onSubmit}>
16647 stevensc 76
          Aceptar
16646 stevensc 77
        </button>
78
        <button className="btn btn-secondary" onClick={closeModal}>
16647 stevensc 79
          Cancelar
16646 stevensc 80
        </button>
81
      </Modal.Footer>
82
    </Modal>
83
  );
84
};
7427 stevensc 85
 
16646 stevensc 86
export default DeleteModal;