diff --git a/frontend/components/App.js b/frontend/components/App.js index 815684369..326c17004 100644 --- a/frontend/components/App.js +++ b/frontend/components/App.js @@ -1,9 +1,92 @@ -import React from 'react' +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 { - render() { - return null +export default class App extends React.Component { + + state = { + todos: [], + error: '', + todoNameInput: '', + displayCompleted: true, + } + + onNameChange = evt => { + const {value} = evt.target + this.setState({...this.state, todoNameInput: value}) + } + + resetForm = () =>{ + this.setState({...this.state, todoNameInput: ''}) + } + + setAxiosResponseError = err => this.setState({...this.state, error: err.response.data.message}) + + postNewTodo = () => { + + axios.post(URL, {name: this.state.todoNameInput}) + .then(res =>{ + this.setState({...this.state, todos: this.state.todos.concat(res.data.data)}) + this.resetForm() + }) + .catch(this.setAxiosResponseError) + } + + onTodoFormSubmit = evt => { + evt.preventdefault() + this.postNewTodo() + } + + fetchAllTodos = () => { + axios.get(URL) + .then(res => { + this.setState({...this.state, todos: res.data.data}) + }) + .catch(this.setAxiosResponseError) } + + toggleCompleted = id => evt => { + axios.patch(`${URL}/${id}`) + .then(res => { + this.setState({...this.state, todos: this.state.todos.map(td => { + if (td.id !== id) return td + return res.data.data + })}) + }) + .catch(err => { + + }) + } + toggleDisplayCompleted = () => { + this.setState({...this.state, displayCompleted: this.state.displayCompleted}) + } + + componentDidMount(){ + this.fetchAllTodos() + } + + + render() { + return ( + +