Skip to content

Commit f730084

Browse files
author
Antoine Lelaisant
committed
feat: step 4 - introduced redux and react-redux
1 parent 31a0c7b commit f730084

File tree

13 files changed

+142
-77
lines changed

13 files changed

+142
-77
lines changed

jsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
"paths": {
99
"assets": ["./assets/*"],
1010
"components": ["./components/*"]
11+
"modules": ["./modules/*"]
1112
}
1213
}

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
"prop-types": "^15.7.2",
1010
"react": "^17.0.1",
1111
"react-dom": "^17.0.1",
12+
"react-redux": "^7.2.2",
1213
"react-router-dom": "^5.2.0",
1314
"react-scripts": "4.0.2",
15+
"redux": "^4.0.5",
1416
"web-vitals": "^1.0.1"
1517
},
1618
"scripts": {

src/components/App.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,29 @@ import {
44
Switch,
55
Route,
66
} from 'react-router-dom'
7+
import { Provider } from 'react-redux'
78
import CssBaseline from '@material-ui/core/CssBaseline'
89
import './App.css'
910
import { Game } from './Game'
1011
import { Home } from './Home'
12+
import configureStore from '../configureStore'
13+
14+
const store = configureStore()
1115

1216
export const App = () => {
1317
return (
14-
<>
18+
<Provider store={store}>
1519
<CssBaseline />
1620
<Router>
1721
<Switch>
1822
<Route exact path="/">
1923
<Home />
2024
</Route>
21-
<Route path="/">
25+
<Route path="/game">
2226
<Game />
2327
</Route>
2428
</Switch>
2529
</Router>
26-
</>
30+
</Provider>
2731
)
2832
}

src/components/Game/Game.js

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,40 @@
1-
import React, { useState, useEffect } from 'react'
2-
import './Game.css'
1+
import React, { useEffect } from 'react'
2+
import { useDispatch } from 'react-redux'
33
import Paper from '@material-ui/core/Paper'
4+
import './Game.css'
5+
import { loop } from 'modules/game'
46
import { Navbar } from 'components/layout/Navbar'
57
import { Gitcoin } from 'components/Gitcoin'
68
import { Score } from 'components/Score'
79
import { Store } from 'components/Store/Store'
810
import { Skills } from 'components/Skills'
9-
import items from '../../items'
1011

1112
export const Game = () => {
12-
const [lines, setLines] = useState(0)
13-
const [linesPerMillisecond, setLinesPerMillisecond] = useState(0)
14-
const [ownedItems, setOwnedItems] = useState({})
13+
const dispatch = useDispatch()
1514

1615
useEffect(() => {
1716
const interval = setInterval(() => {
18-
setLines(lines + linesPerMillisecond)
17+
dispatch(loop())
1918
}, 100)
2019

2120
return () => clearInterval(interval)
2221
})
2322

24-
useEffect(() => {
25-
let count = 0
26-
27-
Object.keys(ownedItems).forEach(name => {
28-
const item = items.find(element => element.name === name)
29-
count += item.multiplier * ownedItems[name]
30-
})
31-
32-
setLinesPerMillisecond(count)
33-
}, [ownedItems])
34-
35-
const handleClick = () => {
36-
setLines(lines + 1)
37-
}
38-
39-
const handleBuy = item => {
40-
setLines(lines - item.price)
41-
setOwnedItems({
42-
...ownedItems,
43-
[item.name]: (ownedItems[item.name] || 0) + 1
44-
})
45-
}
46-
4723
return (
4824
<>
4925
<Navbar />
5026
<main className="game">
5127
<Paper elevation={3} className="left">
52-
<Score
53-
lines={lines}
54-
linesPerSecond={parseInt(linesPerMillisecond * 10)}
55-
/>
56-
<Gitcoin onClick={handleClick} />
28+
<Score />
29+
<Gitcoin />
5730
</Paper>
5831

5932
<Paper elevation={3} className="center">
60-
<Skills skills={ownedItems} />
33+
<Skills />
6134
</Paper>
6235

6336
<Paper elevation={3} className="right">
64-
<Store
65-
lines={lines}
66-
onBuy={handleBuy}
67-
/>
37+
<Store />
6838
</Paper>
6939
</main>
7040
</>

src/components/Gitcoin/Gitcoin.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
import React from 'react'
2-
import PropTypes from 'prop-types'
2+
import { useDispatch } from 'react-redux'
33
import './Gitcoin.css'
4+
import { click } from 'modules/game'
45
import githubIcon from 'assets/github.svg'
56

6-
export const Gitcoin = ({ onClick }) => {
7+
export const Gitcoin = () => {
8+
const disptach = useDispatch()
9+
10+
const handleClick = () => disptach(click())
11+
712
return (
813
<button
914
className="gitcoin"
10-
onClick={onClick}
15+
onClick={handleClick}
1116
>
1217
<img src={githubIcon} alt="Gitcoin" />
1318
</button>
1419
)
1520
}
16-
17-
Gitcoin.propTypes = {
18-
onClick: PropTypes.func.isRequired,
19-
}

src/components/Home.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export const Home = () => {
3636
<div className={classes.heroButtons}>
3737
<Grid container spacing={2} justify="center">
3838
<Grid item>
39-
<Link to="/gitclicker">
39+
<Link to="/game">
4040
<Button variant="contained" color="primary">
4141
Play
4242
</Button>

src/components/Score.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import React from 'react'
2-
import PropTypes from 'prop-types'
2+
import { useSelector } from 'react-redux'
33

4-
export const Score = ({ lines, linesPerSecond }) => (
5-
<>
6-
<h3 style={{fontFamily: 'Orbitron'}}>{parseInt(lines)} lines</h3>
7-
<small>per second: {linesPerSecond}</small>
8-
</>
9-
)
4+
export const Score = () => {
5+
const lines = useSelector(state => parseInt(state.game.lines))
6+
const linesPerSecond = useSelector(state => parseInt(state.game.linesPerMillisecond * 10))
107

11-
Score.propTypes = {
12-
lines: PropTypes.number.isRequired,
13-
linesPerSecond: PropTypes.number.isRequired,
8+
return (
9+
<>
10+
<h3 style={{fontFamily: 'Orbitron'}}>{lines} lines</h3>
11+
<small>per second: {linesPerSecond}</small>
12+
</>
13+
)
1414
}

src/components/Skills/Skills.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import React from 'react'
2+
import { useSelector } from 'react-redux'
23
import './Skills.css'
3-
import PropTypes from 'prop-types'
44
import Typography from '@material-ui/core/Typography'
55
import { Section } from './Section'
66

7-
export const Skills = ({ skills }) => {
7+
export const Skills = () => {
8+
const skills = useSelector(state => state.game.skills)
9+
810
return (
911
<>
1012
<Typography variant="h5">Skills</Typography>
@@ -20,7 +22,3 @@ export const Skills = ({ skills }) => {
2022
</>
2123
)
2224
}
23-
24-
Skills.propTypes = {
25-
skills: PropTypes.objectOf(PropTypes.number).isRequired,
26-
}

src/components/Store/Store.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
import React from 'react'
2-
import PropTypes from 'prop-types'
2+
import { useSelector, useDispatch } from 'react-redux'
33
import Typography from '@material-ui/core/Typography'
44
import './Store.css'
5+
import { buyItem } from 'modules/game'
56
import { Item } from './Item'
67
import items from '../../items'
78

8-
export const Store = ({ lines, onBuy }) => {
9+
export const Store = () => {
10+
const dispatch = useDispatch()
11+
const lines = useSelector(state => state.game.lines)
12+
13+
const handleBuy = item => {
14+
dispatch(buyItem(item))
15+
}
16+
917
return (
1018
<div className="store">
1119
<Typography variant="h5">Store</Typography>
@@ -15,14 +23,9 @@ export const Store = ({ lines, onBuy }) => {
1523
key={key}
1624
item={item}
1725
lines={lines}
18-
onBuy={onBuy}
26+
onBuy={handleBuy}
1927
/>
2028
)}
2129
</div>
2230
)
2331
}
24-
25-
Store.propTypes = {
26-
lines: PropTypes.number.isRequired,
27-
onBuy: PropTypes.func.isRequired,
28-
}

src/configureStore.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { createStore } from 'redux'
2+
import { rootReducer } from './modules'
3+
4+
export default () => {
5+
const store = createStore(rootReducer)
6+
7+
return store
8+
}

0 commit comments

Comments
 (0)