/
game.js
46 lines (40 loc) · 1.09 KB
/
game.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
import {createAsyncThunk, createSlice} from '@reduxjs/toolkit'
import {getRoom} from "../firebase/game";
export const fetchGameRoom = createAsyncThunk(
'rooms/fetchByName',
async (roomName) => {
let game = await getRoom(roomName);
return game.val()
}
)
const slice = createSlice({
name: 'game',
initialState: {
game: {
started: false,
name: undefined,
players: []
},
loading: true
},
reducers: {
updateGame: (state, action) => {
state.game = action.payload
}
},
extraReducers: {
[fetchGameRoom.fulfilled]: (state, action) => {
if (action.payload) {
state.game = action.payload
}
state.loading = false
}
}
})
export const {updateGame} = slice.actions;
// The function below is called a selector and allows us to select a value from
// the state. Selectors can also be defined inline where they're used instead of
// in the slice file. For example: `useSelector((state) => state.counter.value)`
export const selectGame = state => !state.game.loading ? state.game.game : undefined;
export const isLoading = state => state.game.loading;
export default slice.reducer