|
1 | | -import React from 'react' |
| 1 | +import React, {Component} from 'react' |
2 | 2 | import {Link} from 'react-router-dom' |
3 | | -import pictures from '../sampleData/pictures' |
| 3 | +import {connect} from 'react-redux' |
| 4 | +import {likePicture} from '../actions/appActions' |
4 | 5 | import './Pictures.css' |
5 | 6 |
|
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 { |
18 | 8 |
|
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) => { |
22 | 17 | 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> |
29 | 29 | </div> |
30 | 30 | ) |
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 | + ) |
32 | 47 | } |
33 | 48 | } |
34 | 49 |
|
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 |
0 commit comments