Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/create-update-todo-process #29

Merged
merged 17 commits into from Mar 13, 2019
123 changes: 123 additions & 0 deletions client/containers/TodoForm/UpdateTodoForm.js
@@ -0,0 +1,123 @@
import React, { Component } from "react";
import PropTypes from "prop-types";
import axios from "axios";

import "./TodoForm.css";

class UpdateTodoForm extends Component {
constructor(props) {
super(props);
this.state = {
id: null,
title: "",
body: "",
completed: false
};

this.handleInputChange = this.handleInputChange.bind(this);
this.updateTodoHandler = this.updateTodoHandler.bind(this);
}

// static getDerivedStateFromProps(nextProps, prevState) {
// console.log("props :", nextProps);
// console.log("state : ", prevState);
// if (nextProps.selectedTodoId !== prevState.id) {
// return {
// id: null
// };
// }
// return null;
// }

componentDidUpdate() {
if (this.props.selectedTodoId !== this.state.id) {
axios.get("/api/todos/" + this.props.selectedTodoId).then(response => {
this.setState({
id: response.data.id,
title: response.data.title,
body: response.data.body,
completed: response.data.completed
});
});
}
}

handleInputChange = event => {
const target = event.target;
const value = target.value;
const name = target.name;

this.setState({
[name]: value
});
};

updateTodoHandler = () => {
if (!this.state.title) return;
const todo = {
id: this.state.id,
title: this.state.title,
body: this.state.body,
completed: this.state.completed
};
this.props.updateTodo(todo);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#29 (comment)

同上

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.props.resetStateHandler();
this.setState({
id: null,
title: "",
body: "",
completed: false,
requestAdd: true
});
};

render() {
console.log(this.state.id);
console.log(this.state.title);
console.log(this.state.body);
console.log(this.state.completed);
return (
<div className="todoForm">
<h1>Update Todo</h1>
<label>
Title
<input
name="title"
type="text"
value={this.state.title}
onChange={this.handleInputChange}
/>
</label>
<label>
Body
<textarea
name="body"
rows="4"
value={this.state.body}
onChange={this.handleInputChange}
/>
</label>
<label>
completed
<select
name="completed"
value={this.state.completed}
onChange={this.handleInputChange}
>
<option value="false">false</option>
<option value="true">true</option>
</select>
</label>
<button onClick={this.updateTodoHandler}>Update Todo</button>
</div>
);
}
}

UpdateTodoForm.propTypes = {
updateTodo: PropTypes.func.isRequired,
selectedTodoId: PropTypes.number,
resetStateHandler: PropTypes.func.isRequired
};

export default UpdateTodoForm;