Created
December 21, 2017 16:06
-
-
Save ydatech/c23b87fba1c838021a3c3c78f2924134 to your computer and use it in GitHub Desktop.
Edit Todo Page for tutorial create React Router Redux on https://learnreact.blogspot.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React, { Component } from 'react'; | |
| // material-ui components | |
| import Grid from 'material-ui/Grid'; | |
| import Typography from 'material-ui/Typography'; | |
| import Paper from 'material-ui/Paper'; | |
| import TextField from 'material-ui/TextField'; | |
| import IconButton from 'material-ui/IconButton'; | |
| import CheckIcon from 'material-ui-icons/Check'; | |
| import BackIcon from 'material-ui-icons/ArrowBack'; | |
| import { InputAdornment } from 'material-ui/Input'; | |
| // react redux | |
| import { connect } from 'react-redux'; | |
| import { bindActionCreators } from 'redux'; | |
| // action creators | |
| import { actions as todoActions } from '../reducers/todo'; | |
| class EditTodo extends Component { | |
| state = { | |
| item: { | |
| title: '' | |
| } | |
| } | |
| // get active item by ID from router (match.params.id) | |
| componentDidMount() { | |
| const { match, todo } = this.props; | |
| const item = todo.items.find(item => item.id === match.params.id) || { title: '' }; | |
| this.setState({ item }); | |
| } | |
| // handle change active item data | |
| handleChange = name => e => { | |
| this.setState({ | |
| item: { | |
| ...this.state.item, | |
| [name]: e.target.value | |
| } | |
| }) | |
| } | |
| //handle submit to update todo on redux state and go back to home | |
| handleSubmit = (e) => { | |
| e.preventDefault(); | |
| const { item } = this.state; | |
| if (item.title) { | |
| const { todoActions } = this.props; | |
| todoActions.update(item); | |
| this.goBack(); | |
| } | |
| } | |
| //go back home | |
| goBack = () => { | |
| const { history } = this.props; | |
| history.goBack(); | |
| } | |
| render() { | |
| const { item } = this.state; | |
| return ( | |
| <Grid item xs={12} sm={6}> | |
| <Typography align="center" type="display3">Edit Todo</Typography> | |
| <Paper style={{ paddingLeft: 16, paddingRight: 16 }}> | |
| <form onSubmit={this.handleSubmit}> | |
| <TextField | |
| id="todo" | |
| label="What needs to be done?" | |
| onChange={this.handleChange('title')} | |
| fullWidth | |
| margin="normal" | |
| value={item.title} | |
| autoComplete="off" | |
| autoFocus={true} | |
| InputProps={{ | |
| endAdornment: | |
| (<InputAdornment position="end"> | |
| <IconButton | |
| onClick={this.handleSubmit} | |
| > | |
| <CheckIcon /> | |
| </IconButton> | |
| </InputAdornment>) | |
| }} | |
| /> | |
| <IconButton aria-label="Edit" onClick={this.handleSubmit}> | |
| <BackIcon /> | |
| </IconButton> | |
| </form> | |
| </Paper> | |
| </Grid > | |
| ); | |
| } | |
| } | |
| const mapStateToProps = ({ todo }) => ({ todo }); | |
| const mapDispatchToProps = (dispatch) => ({ todoActions: bindActionCreators(todoActions, dispatch) }); | |
| export default connect(mapStateToProps, mapDispatchToProps)(EditTodo); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment