Skip to content

Commit 4359323

Browse files
committed
contine with second fetch post request in reducer, try to get direction button to work in GameScreen
1 parent 3dddb5b commit 4359323

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

code/src/components/GameScreen.js

+17-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
1-
import React from 'react'
2-
import { useSelector } from 'react-redux'
1+
import React, { useState } from 'react'
2+
import { useSelector, useDispatch } from 'react-redux'
3+
4+
import { games, CarryOnGame } from '../reducers/games'
5+
6+
const GameScreen = ({ inputValue }) => {
7+
const [inputDirection, setInputDirection] = useState([])
38

4-
const GameScreen = () => {
59
const userActions = useSelector(store => store.games.username)
610
// console.log(userActions)
711

812
const games = useSelector(store => store.games.username.actions)
913
// const games = useSelector(store => store.games)
1014
console.log(games)
1115

16+
const dispatch = useDispatch()
17+
18+
/* const onNextDirection = () => {
19+
dispatch(games.actions.setInputDirection(inputDirection))
20+
dispatch(CarryOnGame(inputValue)) // ?
21+
} */
1222

1323
//The optional chaining operator (?.)
1424
//enables you to read the value of a property located deep within
@@ -20,7 +30,10 @@ const GameScreen = () => {
2030
{games?.map( (game, index) => (
2131
<div key={index}>
2232
<p>{game.description}</p>
23-
<button>{game.direction}</button>
33+
<button
34+
onClick={() => dispatch()}>
35+
{game.direction}
36+
</button>
2437
</div>
2538
)
2639
)}

code/src/reducers/games.js

+7-8
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,23 @@ export const createNewPlayer = (username) => {
3434
}
3535
}
3636

37-
// map over actions send in action ?
38-
// type: 'move',
39-
// direction: action.direction
40-
41-
/* export const CarryOnGame = () => {
37+
export const CarryOnGame = (direction, username) => {
4238
return (dispatch) => {
4339
fetch('https://wk16-backend.herokuapp.com/action', {
4440
method: 'POST',
4541
headers: {
4642
'Content-type': 'application/json'
4743
},
4844
body: JSON.stringify({
49-
direction
45+
username: `${username}`,
46+
type: 'move',
47+
direction: `${direction}`
5048
}),
5149
})
5250
.then(res => res.json())
51+
// .then(data => console.log(data))
5352
.then(data => dispatch(games.actions.setDescription(data)))
5453
}
5554
}
56-
*/
57-
export default games
55+
56+
//export default games

0 commit comments

Comments
 (0)