From d3662eab634a61246a3cec1013a924b0d2b52ba9 Mon Sep 17 00:00:00 2001 From: RealSadLemon Date: Tue, 7 Jun 2022 22:29:58 -0500 Subject: [PATCH] Finished Tasks --- frontend/components/App.js | 93 +++++++++++++++++++++++++++++++-- frontend/components/Form.js | 10 +++- frontend/components/Todo.js | 16 +++++- frontend/components/TodoList.js | 18 ++++++- frontend/styles/styles.css | 8 +++ 5 files changed, 136 insertions(+), 9 deletions(-) diff --git a/frontend/components/App.js b/frontend/components/App.js index 815684369..0a456a17a 100644 --- a/frontend/components/App.js +++ b/frontend/components/App.js @@ -1,9 +1,94 @@ -import React from 'react' +import React from 'react'; +import TodoList from './TodoList'; +import Form from './Form'; +import axios from 'axios'; -const URL = 'http://localhost:9000/api/todos' +const URL = 'http://localhost:9000/api/todos'; export default class App extends React.Component { + constructor(){ + super(); + this.state = { + todoList: [], + inputValue: '', + hideCompleted: false + } + } + + componentDidMount(){ + console.log('App: Component Mounted'); + axios.get(URL) + .then(res => { + this.setState({ + ...this.state, + todoList: res.data.data + }) + console.log(res); + }) + .catch(err => console.log('App: The following error occured', err)); + } + + toggleHidden = ()=>{ + this.setState({ + ...this.state, + hideCompleted: !this.state.hideCompleted + }) + } + + toggleCompleted = (itemId)=>{ + this.setState({ + todoList: this.state.todoList.map(item => { + if(itemId === item.id) { + return { + ...item, + completed: !item.completed + }; + }; + return {...item}; + }) + }); + axios.patch(`${URL}/${itemId}`, { data: [...this.state.todoList] }) + .then(res =>{ + console.log('App: Successful patch made', res) + }) + .catch(err =>{ + console.log('App: Failed patch', err); + }) + }; + + inputChange = (e)=>{ + this.setState({ + ...this.state, + inputValue: e.target.value + }) + } + + handleSubmit = (e)=>{ + e.preventDefault(); + const newListItem = { + name: this.state.inputValue, + id: new Date().getTime(), + completed: false + } + axios.post(URL, newListItem) + .then(res => { + this.setState({ + ...this.state, + todoList: [...this.state.todoList, res.data.data], + inputValue: '' + }) + }) + .catch(err => console.log('App: Failed to post to API', err)); + } + render() { - return null + console.log('App: Rendered Component', this.state.todoList); + return ( +
+ +
+ +
+ ) } -} +}; diff --git a/frontend/components/Form.js b/frontend/components/Form.js index 3932207be..1e4da4033 100644 --- a/frontend/components/Form.js +++ b/frontend/components/Form.js @@ -2,6 +2,14 @@ import React from 'react' export default class Form extends React.Component { render() { - return null + return ( + + + + + ) } } diff --git a/frontend/components/Todo.js b/frontend/components/Todo.js index aac687f6d..bc16cb1db 100644 --- a/frontend/components/Todo.js +++ b/frontend/components/Todo.js @@ -1,7 +1,19 @@ -import React from 'react' +import React, {useEffect} from 'react' export default class Todo extends React.Component { + constructor(){ + super(); + } render() { - return null + return ( +
this.props.toggleCompleted(this.props.todoItem.id)} + > + {this.props.todoItem.name}{this.props.todoItem.completed ? ' ✔️' : ''} +
+
+
+ ) } } diff --git a/frontend/components/TodoList.js b/frontend/components/TodoList.js index ce08a27ba..caafa7d64 100644 --- a/frontend/components/TodoList.js +++ b/frontend/components/TodoList.js @@ -1,7 +1,21 @@ -import React from 'react' +import React from 'react'; +import Todo from './Todo'; export default class TodoList extends React.Component { + constructor(){ + super(); + } + render() { - return null + return ( +
+ Todos: +
+
+ {this.props.todoItems.map(item => { + return + })} +
+ ) } } diff --git a/frontend/styles/styles.css b/frontend/styles/styles.css index fda8a420d..14583440f 100644 --- a/frontend/styles/styles.css +++ b/frontend/styles/styles.css @@ -83,3 +83,11 @@ ul, form { .todo:hover { cursor: pointer; } + +.hidden { + display: none; +} + +.list-item { + margin-left: 1rem; +} \ No newline at end of file