This repository has been archived by the owner on Feb 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
modal.js
79 lines (71 loc) · 2.13 KB
/
modal.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
79
/**
* CONSTANTS
*/
const MODAL_TOGGLE = 'WIDGET_EDITOR/MODAL_TOGGLE';
const MODAL_SET_OPTIONS = 'WIDGET_EDITOR/MODAL_SET_OPTIONS';
const MODAL_LOADING = 'WIDGET_EDITOR/MODAL_LOADING';
const MODAL_EXECUTE_CLOSE_CALLBACK = 'WIDGET_EDITOR/MODAL_EXECUTE_CLOSE_CALLBACK';
// REDUCER
const initialState = {
open: false,
options: {
children: null,
childrenProps: null,
size: '',
// Callback executed if the user closes the modal (user interaction)
// (not executed when toggleModal is executed)
onCloseModal: null
},
loading: false
};
export default function (state = initialState, action) {
switch (action.type) {
case MODAL_TOGGLE:
return Object.assign({}, state, { open: action.payload });
case MODAL_SET_OPTIONS:
return Object.assign({}, state, {
options: Object.assign({}, state.options, action.payload, {
// We remove the callback if not present
onCloseModal: action.payload.onCloseModal ? action.payload.onCloseModal : null
})
});
case MODAL_LOADING:
return Object.assign({}, state, { loading: action.payload });
case MODAL_EXECUTE_CLOSE_CALLBACK:
if (state.options.onCloseModal) {
// We make the call asynchronous to avoid blocking the state
// from being computed
setTimeout(() => state.options.onCloseModal(), 0);
}
return state;
default:
return state;
}
}
/**
* ACTIONS
* - closeModal
* - toggleModal
* - modalLoading
* - setModalOptions
*/
export function closeModal() {
return dispatch => dispatch({ type: MODAL_TOGGLE });
}
export function toggleModal(open, opts = {}, userInteraction = false) {
return (dispatch) => {
if (open && opts) {
dispatch({ type: MODAL_SET_OPTIONS, payload: opts });
}
if (userInteraction) {
dispatch({ type: MODAL_EXECUTE_CLOSE_CALLBACK });
}
dispatch({ type: MODAL_TOGGLE, payload: open });
};
}
export function modalLoading(loading) {
return dispatch => dispatch({ type: MODAL_LOADING, payload: loading });
}
export function setModalOptions(opts) {
return dispatch => dispatch({ type: MODAL_SET_OPTIONS, payload: opts });
}