Skip to content
This repository was archived by the owner on Feb 18, 2022. It is now read-only.

Commit 1ef9e26

Browse files
committed
Redux: dummy LIKE action and pictures list from Redux Store.
1 parent 3050353 commit 1ef9e26

File tree

7 files changed

+115
-27
lines changed

7 files changed

+115
-27
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
"dependencies": {
66
"react": "^15.5.4",
77
"react-dom": "^15.5.4",
8-
"react-router-dom": "^4.1.1"
8+
"react-redux": "^5.0.5",
9+
"react-router-dom": "^4.1.1",
10+
"redux": "^3.6.0"
911
},
1012
"devDependencies": {
1113
"enzyme": "^2.8.2",

src/actions/appActions.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const likePicture = (pictureIndex) => {
2+
console.log(`LIKE ${pictureIndex}!`)
3+
return {
4+
type: 'LIKE_PICTURE',
5+
pictureIndex
6+
}
7+
}
8+
9+
export {
10+
likePicture
11+
}

src/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
import React from 'react'
22
import ReactDOM from 'react-dom'
3+
import {createStore} from 'redux'
4+
import {Provider} from 'react-redux'
35
import App from './App'
6+
import reducers from './reducers'
47
import './index.css'
58

9+
const store = createStore(reducers)
10+
611
ReactDOM.render(
7-
<App />,
12+
<Provider store={store}>
13+
<App />
14+
</Provider>,
815
document.getElementById('mountMyApp')
916
)

src/pages/Pictures.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@
1919
font-weight: 400;
2020
}
2121

22+
.Picture-like {
23+
padding: 5px;
24+
}
25+
26+
.Picture-like > span {
27+
display: block;
28+
padding: 6px 10px;
29+
}
30+
2231
.Picture > a {
2332
display: block;
2433
}

src/pages/Pictures.js

Lines changed: 56 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,66 @@
1-
import React from 'react'
1+
import React, {Component} from 'react'
22
import {Link} from 'react-router-dom'
3-
import pictures from '../sampleData/pictures'
3+
import {connect} from 'react-redux'
4+
import {likePicture} from '../actions/appActions'
45
import './Pictures.css'
56

6-
const RenderPictures = (picturesList) => {
7-
return picturesList.map((picture, index) => {
8-
return (
9-
<div className="Picture" key={`MyPicture_${index}`}>
10-
<Link to={`/picture/${index}`}>
11-
<img src={picture.url} alt={picture.name}/>
12-
</Link>
13-
<div className="Picture-name">{picture.name}</div>
14-
</div>
15-
)
16-
})
17-
}
7+
export class Pictures extends Component {
188

19-
const Pictures = () => {
20-
return {
21-
render: () => {
9+
onLike = (index, event) => {
10+
event.preventDefault()
11+
this.props.onLikePicture(index)
12+
}
13+
14+
renderPictures(picturesList) {
15+
16+
return picturesList.map((picture, index) => {
2217
return (
23-
<div className="app-content-width">
24-
<article>
25-
<h2>Pictures</h2>
26-
<p>Simple picture gallery.</p>
27-
<div className="Pictures-gallery">{RenderPictures(pictures)}</div>
28-
</article>
18+
<div className="Picture" key={`MyPicture_${index}`}>
19+
<Link to={`/picture/${index}`}>
20+
<img src={picture.url} alt={picture.name}/>
21+
</Link>
22+
<div className="Picture-name">{picture.name}</div>
23+
<div className="Picture-like">
24+
{index !== this.props.liked
25+
? <button onClick={this.onLike.bind(this, index)}>Like</button>
26+
: <span>Liked! This is my favourite now.</span>
27+
}
28+
</div>
2929
</div>
3030
)
31-
}
31+
})
32+
}
33+
34+
render() {
35+
const { pictures } = this.props
36+
return (
37+
<div className="app-content-width">
38+
<article>
39+
<h2>Pictures</h2>
40+
<p>Simple picture gallery. Last like index: {this.props.liked}</p>
41+
<div className="Pictures-gallery">
42+
{this.renderPictures(pictures)}
43+
</div>
44+
</article>
45+
</div>
46+
)
3247
}
3348
}
3449

35-
export default Pictures
50+
const mapStateToProps = (state) => ({
51+
liked: state.appReducer.liked,
52+
pictures: state.appReducer.pictures
53+
})
54+
55+
const mapDispatchToProps = (dispatch) => ({
56+
onLikePicture: (pictureIndex) => {
57+
dispatch(likePicture(pictureIndex))
58+
}
59+
})
60+
61+
const ConnectedPictures = connect(
62+
mapStateToProps,
63+
mapDispatchToProps
64+
)(Pictures)
65+
66+
export default ConnectedPictures

src/reducers/appReducer.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import pictures from '../sampleData/pictures'
2+
3+
const defaultState = {
4+
pictures: pictures,
5+
liked: -1
6+
}
7+
8+
const appReducer = (state = defaultState, action) => {
9+
switch (action.type) {
10+
case "LIKE_PICTURE":
11+
return {
12+
...state,
13+
liked: action.pictureIndex
14+
}
15+
default:
16+
return state
17+
}
18+
}
19+
20+
export default appReducer

src/reducers/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { combineReducers } from 'redux'
2+
import appReducer from './appReducer'
3+
4+
const reducers = combineReducers({
5+
appReducer // ..., anotherReducer, moreReducers
6+
})
7+
8+
export default reducers

0 commit comments

Comments
 (0)