From ede8a3ea55a59859e88b5f7f2aa45134e9dcd5a8 Mon Sep 17 00:00:00 2001 From: Terri Archer Date: Wed, 7 Dec 2022 13:07:46 -0500 Subject: [PATCH] completed coursework --- frontend/components/App.js | 57 ++++++++++++++++++++++++++++++++- frontend/components/Form.js | 28 +++++++++++++++- frontend/components/Todo.js | 12 ++++++- frontend/components/TodoList.js | 12 ++++++- frontend/styles/styles.css | 13 ++++++-- 5 files changed, 116 insertions(+), 6 deletions(-) diff --git a/frontend/components/App.js b/frontend/components/App.js index 815684369..ecae76f95 100644 --- a/frontend/components/App.js +++ b/frontend/components/App.js @@ -1,9 +1,64 @@ 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() { + super(); + this.state= { + todo: [], + showCompleted: true, + shownTodo: [] + } + } + + + fetchData =() => { + axios.get(URL) + .then(({data}) => this.setState({...this.state, todo: data.data,shownTodo: data.data.filter(task => {return task.completed === false})})) + } + + postData = (newTask) => { + axios.post(URL, newTask) + .then(res => {this.fetchData()}) + } + + addTask =(task) => { + const newTask = { + name: task, + completed: false + } + this.postData(newTask) + } + + componentDidMount = () => { + this.fetchData() + } + + toggleCompleted = (taskId) => { + console.log(taskId) + axios.patch(`${URL}/${taskId}`) + .then(res => {this.fetchData()}) + } + + toggleView = () => { + this.setState({showCompleted: !this.state.showCompleted}) + } + + + + render() { - return null + const stateToUse = this.state.showCompleted ? this.state.todo : this.state.shownTodo + return ( + <> + +
+ + ) } } diff --git a/frontend/components/Form.js b/frontend/components/Form.js index 3932207be..2d5f51500 100644 --- a/frontend/components/Form.js +++ b/frontend/components/Form.js @@ -1,7 +1,33 @@ import React from 'react' export default class Form extends React.Component { + constructor() { + super(); + this.state = { + formValue: '' + } + } + + handleChanges = (e) => this.setState({formValue: e.target.value}) + + handleSubmit = (event) => { + event.preventDefault(); + if(this.state.formValue){ + this.props.submit(this.state.formValue); + this.setState({formValue: ""}) + } + } + + render() { - return null + return ( + <> + + + +
+ + + ) } } diff --git a/frontend/components/Todo.js b/frontend/components/Todo.js index aac687f6d..d532fda02 100644 --- a/frontend/components/Todo.js +++ b/frontend/components/Todo.js @@ -1,7 +1,17 @@ import React from 'react' export default class Todo extends React.Component { + + render() { - return null + return ( + <> +
this.props.toggleCompleted(this.props.task.id)}> +

{this.props.task.name}

+
+ + + ) } } diff --git a/frontend/components/TodoList.js b/frontend/components/TodoList.js index ce08a27ba..ec01ecbeb 100644 --- a/frontend/components/TodoList.js +++ b/frontend/components/TodoList.js @@ -1,7 +1,17 @@ import React from 'react' +import Todo from './Todo' export default class TodoList extends React.Component { + + render() { - return null + return ( + <> + {this.props.todo.map((task) => { + return + + })} + + ) } } diff --git a/frontend/styles/styles.css b/frontend/styles/styles.css index fda8a420d..73d61ca4f 100644 --- a/frontend/styles/styles.css +++ b/frontend/styles/styles.css @@ -76,10 +76,19 @@ ul, form { margin: 1rem 0 1rem 0; } -.todo { +.task { padding: 0.15rem; } -.todo:hover { +.task:hover { + cursor: pointer; +} + +.taskcompleted { + text-decoration: line-through; + padding: 0.15rem; +} + +.taskcompleted:hover { cursor: pointer; }