Skip to content

Commit dbdff68

Browse files
author
Antoine Lelaisant
committed
feat: step 6 - redux thunk
1 parent c696164 commit dbdff68

File tree

5 files changed

+58
-4
lines changed

5 files changed

+58
-4
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
"react-router-dom": "^5.2.0",
1414
"react-scripts": "4.0.2",
1515
"redux": "^4.0.5",
16+
"redux-devtools-extension": "^2.13.9",
17+
"redux-thunk": "^2.3.0",
1618
"web-vitals": "^1.0.1"
1719
},
1820
"scripts": {

src/components/Game/Game.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { useEffect } from 'react'
22
import { useDispatch } from 'react-redux'
33
import Paper from '@material-ui/core/Paper'
44
import './Game.css'
5-
import { loop } from 'modules/game'
5+
import { loop, start, stop } from 'modules/game'
66
import { Navbar } from 'components/layout/Navbar'
77
import { Gitcoin } from 'components/Gitcoin'
88
import { Score } from 'components/Score'
@@ -13,11 +13,16 @@ export const Game = () => {
1313
const dispatch = useDispatch()
1414

1515
useEffect(() => {
16+
dispatch(start())
17+
1618
const interval = setInterval(() => {
1719
dispatch(loop())
1820
}, 100)
1921

20-
return () => clearInterval(interval)
22+
return () => {
23+
clearInterval(interval)
24+
dispatch(stop())
25+
}
2126
})
2227

2328
return (

src/configureStore.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1-
import { createStore } from 'redux'
1+
import { createStore, applyMiddleware } from 'redux'
2+
import thunk from 'redux-thunk'
3+
import { composeWithDevTools } from 'redux-devtools-extension/logOnlyInProduction'
24
import { rootReducer } from './modules'
35

46
export default (initialState = {}) => {
5-
const store = createStore(rootReducer, initialState)
7+
const composeEnhancers = composeWithDevTools({})
8+
9+
const store = createStore(
10+
rootReducer,
11+
initialState,
12+
composeEnhancers(
13+
applyMiddleware(thunk)
14+
)
15+
)
616

717
return store
818
}

src/modules/game.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
export const CLICK = 'game::CLICK'
33
export const BUY_ITEM = 'game::BUY_ITEM'
44
export const LOOP = 'game::LOOP'
5+
export const START = 'game::START'
6+
export const STOP = 'game::STOP'
57

68
// Action creators
79
export const click = () => ({
@@ -17,6 +19,25 @@ export const loop = () => ({
1719
type: LOOP
1820
})
1921

22+
// Side Effects
23+
export const start = () => dispatch => {
24+
const loadedGame = JSON.parse(localStorage.getItem('game'))
25+
26+
return dispatch({
27+
type: START,
28+
loadedGame: loadedGame ?? {}
29+
})
30+
}
31+
32+
export const stop = () => (dispatch, getState) => {
33+
const serializedGame = JSON.stringify(getState().game)
34+
localStorage.setItem('game', serializedGame)
35+
36+
return dispatch({
37+
type: STOP
38+
})
39+
}
40+
2041
const INITIAL_STATE = {
2142
lines: 0,
2243
linesPerMillisecond: 0,
@@ -49,5 +70,11 @@ export const reducer = (state = INITIAL_STATE, action) => {
4970
}
5071
}
5172

73+
if (type === START) {
74+
const { loadedGame } = action
75+
76+
return { ...state, ...loadedGame }
77+
}
78+
5279
return state
5380
}

yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10008,6 +10008,16 @@ redent@^3.0.0:
1000810008
indent-string "^4.0.0"
1000910009
strip-indent "^3.0.0"
1001010010

10011+
redux-devtools-extension@^2.13.9:
10012+
version "2.13.9"
10013+
resolved "https://registry.yarnpkg.com/redux-devtools-extension/-/redux-devtools-extension-2.13.9.tgz#6b764e8028b507adcb75a1cae790f71e6be08ae7"
10014+
integrity sha512-cNJ8Q/EtjhQaZ71c8I9+BPySIBVEKssbPpskBfsXqb8HJ002A3KRVHfeRzwRo6mGPqsm7XuHTqNSNeS1Khig0A==
10015+
10016+
redux-thunk@^2.3.0:
10017+
version "2.3.0"
10018+
resolved "https://registry.yarnpkg.com/redux-thunk/-/redux-thunk-2.3.0.tgz#51c2c19a185ed5187aaa9a2d08b666d0d6467622"
10019+
integrity sha512-km6dclyFnmcvxhAcrQV2AkZmPQjzPDjgVlQtR0EQjxZPyJ0BnMf3in1ryuR8A2qU0HldVRfxYXbFSKlI3N7Slw==
10020+
1001110021
redux@^4.0.5:
1001210022
version "4.0.5"
1001310023
resolved "https://registry.yarnpkg.com/redux/-/redux-4.0.5.tgz#4db5de5816e17891de8a80c424232d06f051d93f"

0 commit comments

Comments
 (0)