|
| 1 | +import React, { PureComponent } from 'react' |
| 2 | +import { withStyles } from '@material-ui/core/styles' |
| 3 | +import Card from '@material-ui/core/Card' |
| 4 | +import CardActions from '@material-ui/core/CardActions' |
| 5 | +import CardContent from '@material-ui/core/CardContent' |
| 6 | +import Fab from '@material-ui/core/Fab' |
| 7 | +import AddIcon from '@material-ui/icons/Add' |
| 8 | +import RemoveIcon from '@material-ui/icons/Remove' |
| 9 | +import Typography from '@material-ui/core/Typography' |
| 10 | +import { connect } from 'react-redux' |
| 11 | +import { increment, decrement } from '../src/actions' |
| 12 | + |
| 13 | +const styles = theme => ({ |
| 14 | + container: { |
| 15 | + display: 'flex', |
| 16 | + flexWrap: 'wrap' |
| 17 | + }, |
| 18 | + title: { |
| 19 | + fontSize: 14 |
| 20 | + } |
| 21 | +}) |
| 22 | + |
| 23 | +class Index extends PureComponent { |
| 24 | + static getInitialProps ({ store, isServer }) { |
| 25 | + store.dispatch(increment(isServer)) |
| 26 | + |
| 27 | + return { isServer } |
| 28 | + } |
| 29 | + |
| 30 | + handleIncrement = () => { |
| 31 | + this.props.increment() |
| 32 | + } |
| 33 | + |
| 34 | + handleDecrement = () => { |
| 35 | + this.props.decrement() |
| 36 | + } |
| 37 | + |
| 38 | + render () { |
| 39 | + const { classes, counter } = this.props |
| 40 | + |
| 41 | + return ( |
| 42 | + <Card className={classes.card}> |
| 43 | + <CardContent> |
| 44 | + <Typography |
| 45 | + className={classes.title} |
| 46 | + color='textSecondary' |
| 47 | + gutterBottom |
| 48 | + > |
| 49 | + Dispatched from <b>{counter.from}</b> |
| 50 | + </Typography> |
| 51 | + <Typography variant='h3' component='h2'> |
| 52 | + {counter.value} |
| 53 | + </Typography> |
| 54 | + <Typography color='textSecondary'>{counter.action}</Typography> |
| 55 | + </CardContent> |
| 56 | + <CardActions> |
| 57 | + <Fab |
| 58 | + variant='round' |
| 59 | + color='primary' |
| 60 | + size='small' |
| 61 | + onClick={this.handleIncrement} |
| 62 | + > |
| 63 | + <AddIcon /> |
| 64 | + </Fab> |
| 65 | + <Fab |
| 66 | + variant='round' |
| 67 | + color='secondary' |
| 68 | + size='small' |
| 69 | + onClick={this.handleDecrement} |
| 70 | + > |
| 71 | + <RemoveIcon /> |
| 72 | + </Fab> |
| 73 | + </CardActions> |
| 74 | + </Card> |
| 75 | + ) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +const mapStateToProps = state => { |
| 80 | + return { |
| 81 | + counter: state |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +const mapDispatchToProps = dispatch => ({ |
| 86 | + increment: () => dispatch(increment()), |
| 87 | + decrement: () => dispatch(decrement()) |
| 88 | +}) |
| 89 | + |
| 90 | +export default connect( |
| 91 | + mapStateToProps, |
| 92 | + mapDispatchToProps |
| 93 | +)(withStyles(styles)(Index)) |
0 commit comments