-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.js
65 lines (56 loc) · 1.48 KB
/
store.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
import { createContext, useContext, useReducer } from "react";
export const initialState = {
user: {},
sessionId: undefined,
dealer: {
uid: "",
name: "",
},
wordInPlay: "",
players: {},
};
// interface State {
// user: {
// uid?: string;
// name?: string;
// };
// sessionId?: string;
// }
// interface Action {
// type: string;
// user?: State["user"];
// sessionId?: string;
// }
// type Reducer = (_state: State, _action: Action) => State;
// interface Props {
// children: JSX.Element;
// }
export const reducer = (state, action) => {
switch (action.type) {
case "SET_USER":
return { ...state, user: action.user };
case "SET_SESSION":
return { ...state, sessionId: action.sessionId };
case "SET_DEALER":
return { ...state, dealer: action.dealer };
case "SET_GAMEPLAY":
return { ...state, dealer: action.dealer, players: action.players };
case "SET_WORD_IN_PLAY":
return { ...state, wordInPlay: action.wordInPlay };
case "SET_MARRY_WORDS":
return { ...state, marryWords: action.marryWords };
case "SET_TURN":
return { ...state, myTurn: action.myTurn };
default:
return state;
}
};
const StoreContext = createContext(initialState);
export const StoreProvider = ({ children }) => {
return (
<StoreContext.Provider value={useReducer(reducer, initialState)}>
{children}
</StoreContext.Provider>
);
};
export const useStoreValue = () => useContext(StoreContext);