diff --git a/README.md b/README.md index b4fb45fce..add88e502 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ This module explored lifecycle methods in class components. In your project you - Use a submit event handler to `POST` a new resource to the server - Use a click handler to `PATCH` an existing resource on the server - Update the frontend to keep it in sync with the state of the server -- Use frontend filtering to display only some resources +- Use frontend filtering to display only some resources ## Introduction diff --git a/frontend/components/App.js b/frontend/components/App.js index 815684369..264bdd5ef 100644 --- a/frontend/components/App.js +++ b/frontend/components/App.js @@ -1,9 +1,79 @@ import React from 'react' +import axios from 'axios' +import Form from './Form' +import TodoList from './TodoList' const URL = 'http://localhost:9000/api/todos' export default class App extends React.Component { + constructor(props) { + super(props) + this.state = { + data: [], + text: '', + displayCompleted: true + } + } + + componentDidMount() { + axios.get(URL) + .then(res => this.setState({ ...this.state, data: res.data.data }) + ) + .catch(err => console.log(err)) + } + + onSubmit = e => { + e.preventDefault() + const payloadToSend = { name: this.state.text, completed: this.state.data.completed } + axios.post(URL, payloadToSend) + .then(res => { + this.setState({...this.state, data: [...this.state.data, res.data.data]}) + }) + } + + onClick = id => { + axios.patch(`${URL}/${id}`) + .then(res => { + this.setState({...this.state, data: this.state.data.map(e => { + if(e.id !== id ) return e + return res.data.data + }) }) + }) + } + + onChange = e => { + this.setState({...this.state, text: e.target.value}) + } + + filterCompleted = () => { + this.setState({ + ...this.state, data: this.state.data.filter(e => !e.completed + ) + }) + } + + hideCompleted = () => { + this.setState({ ...this.state, displayCompleted: !this.state.displayCompleted }) + } + render() { - return null + return ( +