-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAppStates.js
More file actions
54 lines (45 loc) · 1.18 KB
/
AppStates.js
File metadata and controls
54 lines (45 loc) · 1.18 KB
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
import {useReducer} from "react";
export const ACTIONS = {
'SHOW_DIALOG': 'show_dialog',
'HIDE_DIALOG': 'hide_dialog',
'APPEND_LOG': 'append_log'
};
const initState = {
dialogVisiblity: false,
dialolgData:{
title:'',
message: '',
button1Action:()=>{},
button2Action:()=>{},
button1Title:'Ok',
button2Title:'Cancel',
},
log :'Click an action to test it'
};
export function useAppState() {
function reducer(state, action) {
switch (action.type) {
case ACTIONS.SHOW_DIALOG:
return {
...state,
dialogVisiblity: true,
dialolgData: {...action.payload}
};
case ACTIONS.HIDE_DIALOG:
return {
...state,
dialogVisiblity: false,
log: action.log
};
case ACTIONS.APPEND_LOG:
return {
...state,
log: action.log
}
default:
return state;
}
}
const [state, updateAppState] = useReducer(reducer, initState);
return [state, updateAppState];
}