-
Notifications
You must be signed in to change notification settings - Fork 3
/
AlertContext.js
79 lines (69 loc) · 2.17 KB
/
AlertContext.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import React, { createContext, useState } from 'react';
import Alert from './components/alert';
import BottomSheet from './components/bottomSheet';
import FlashMessage from './components/flashMessage'
// DEFAULT ALERT PARAMS
const initialState = {
isOpen: false,
title: '',
message: '',
settings: {
type: 'Alert',
textConfirm: 'Ok',
textCancel: null,
}
};
export const alertContext = createContext(initialState);
const { Provider } = alertContext;
export default (props) => {
const [alertState, setAlertState] = useState(initialState);
const { customStyle } = props;
// SET ALERT PARAMS INSIDE STATE
alert = (title, message, settings) => {
if (alertState.isOpen) return false;
setAlertState({
isOpen: true,
title,
message,
settings: settings ? settings : initialState.settings,
});
};
// CLOSE ALERT
close = () => {
setAlertState(initialState);
if (typeof alertState.settings.onClose === "function") alertState.settings.onClose()
};
confirm = () => {
setAlertState(initialState);
if (typeof alertState.settings.onConfirm === "function") alertState.settings.onConfirm()
};
cancel = () => {
setAlertState(initialState);
if (typeof alertState.settings.onCancel === "function") alertState.settings.onCancel()
};
return (
<>
<Provider value={alert}>{props.children}</Provider>
{(() => {
switch (alertState.settings.type) {
case 'alert':
return (
<Alert {...alertState} close={close} confirm={confirm} cancel={cancel} customStyle={customStyle} />
)
case 'bottomsheet':
return (
<BottomSheet {...alertState} close={close} confirm={confirm} cancel={cancel} customStyle={customStyle} />
)
case 'flashmessage':
return (
<FlashMessage {...alertState} close={close} confirm={confirm} cancel={cancel} customStyle={customStyle} />
)
default:
return (
<Alert {...alertState} close={close} confirm={confirm} cancel={cancel} customStyle={customStyle} />
)
}
})()}
</>
);
};