File tree Expand file tree Collapse file tree 5 files changed +58
-4
lines changed Expand file tree Collapse file tree 5 files changed +58
-4
lines changed Original file line number Diff line number Diff line change 13
13
"react-router-dom" : " ^5.2.0" ,
14
14
"react-scripts" : " 4.0.2" ,
15
15
"redux" : " ^4.0.5" ,
16
+ "redux-devtools-extension" : " ^2.13.9" ,
17
+ "redux-thunk" : " ^2.3.0" ,
16
18
"web-vitals" : " ^1.0.1"
17
19
},
18
20
"scripts" : {
Original file line number Diff line number Diff line change @@ -2,7 +2,7 @@ import React, { useEffect } from 'react'
2
2
import { useDispatch } from 'react-redux'
3
3
import Paper from '@material-ui/core/Paper'
4
4
import './Game.css'
5
- import { loop } from 'modules/game'
5
+ import { loop , start , stop } from 'modules/game'
6
6
import { Navbar } from 'components/layout/Navbar'
7
7
import { Gitcoin } from 'components/Gitcoin'
8
8
import { Score } from 'components/Score'
@@ -13,11 +13,16 @@ export const Game = () => {
13
13
const dispatch = useDispatch ( )
14
14
15
15
useEffect ( ( ) => {
16
+ dispatch ( start ( ) )
17
+
16
18
const interval = setInterval ( ( ) => {
17
19
dispatch ( loop ( ) )
18
20
} , 100 )
19
21
20
- return ( ) => clearInterval ( interval )
22
+ return ( ) => {
23
+ clearInterval ( interval )
24
+ dispatch ( stop ( ) )
25
+ }
21
26
} )
22
27
23
28
return (
Original file line number Diff line number Diff line change 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'
2
4
import { rootReducer } from './modules'
3
5
4
6
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
+ )
6
16
7
17
return store
8
18
}
Original file line number Diff line number Diff line change 2
2
export const CLICK = 'game::CLICK'
3
3
export const BUY_ITEM = 'game::BUY_ITEM'
4
4
export const LOOP = 'game::LOOP'
5
+ export const START = 'game::START'
6
+ export const STOP = 'game::STOP'
5
7
6
8
// Action creators
7
9
export const click = ( ) => ( {
@@ -17,6 +19,25 @@ export const loop = () => ({
17
19
type : LOOP
18
20
} )
19
21
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
+
20
41
const INITIAL_STATE = {
21
42
lines : 0 ,
22
43
linesPerMillisecond : 0 ,
@@ -49,5 +70,11 @@ export const reducer = (state = INITIAL_STATE, action) => {
49
70
}
50
71
}
51
72
73
+ if ( type === START ) {
74
+ const { loadedGame } = action
75
+
76
+ return { ...state , ...loadedGame }
77
+ }
78
+
52
79
return state
53
80
}
Original file line number Diff line number Diff line change @@ -10008,6 +10008,16 @@ redent@^3.0.0:
10008
10008
indent-string "^4.0.0"
10009
10009
strip-indent "^3.0.0"
10010
10010
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
+
10011
10021
redux@^4.0.5:
10012
10022
version "4.0.5"
10013
10023
resolved "https://registry.yarnpkg.com/redux/-/redux-4.0.5.tgz#4db5de5816e17891de8a80c424232d06f051d93f"
You can’t perform that action at this time.
0 commit comments