Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3539 | Rev 3545 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
3538 stevensc 1
import React, { createContext, useCallback, useState } from 'react';
2
 
3
export const AlertModalContext = createContext({
4
  show: false,
5
  message: '',
6
  onConfirm: () => {},
7
  onCancel: () => {},
8
  showAlert: () => {},
9
  closeAlert: () => {}
10
});
11
 
3539 stevensc 12
export function AlertModalProvider({ children }) {
3538 stevensc 13
  const [show, setShow] = useState(false);
14
  const [message, setMessage] = useState('');
15
  const [onConfirm, setOnConfirm] = useState(() => {});
16
  const [onCancel, setOnCancel] = useState(() => {});
17
 
18
  const showAlert = useCallback(({ message = '', onConfirm = () => {}, onCancel = () => {} }) => {
19
    setMessage(message);
3544 stevensc 20
    setOnConfirm(() => {
21
      onConfirm();
22
    });
23
    setOnCancel(() => {
24
      onCancel();
25
    });
3538 stevensc 26
    setShow(true);
27
  }, []);
28
 
29
  const closeAlert = useCallback(() => {
30
    setMessage('');
31
    setShow(false);
32
  }, []);
33
 
34
  return (
35
    <AlertModalContext.Provider
36
      value={{
37
        show,
38
        message,
39
        showAlert,
40
        closeAlert,
41
        onConfirm,
42
        onCancel: onCancel || closeAlert
43
      }}
44
    >
45
      {children}
46
    </AlertModalContext.Provider>
47
  );
48
}