From 3138e513dd2c2efe4bc31620a8201fda0e5ffd75 Mon Sep 17 00:00:00 2001 From: Aaron Belmore Date: Fri, 5 May 2023 12:16:52 -0400 Subject: [PATCH] Completed Steps --- frontend/components/App.js | 63 ++++++++++++++++++++++++++++++--- frontend/components/Form.js | 37 ++++++++++++++++--- frontend/components/Todo.js | 14 +++++--- frontend/components/TodoList.js | 15 +++++--- 4 files changed, 112 insertions(+), 17 deletions(-) diff --git a/frontend/components/App.js b/frontend/components/App.js index 38b7a2f5..b131a7c9 100644 --- a/frontend/components/App.js +++ b/frontend/components/App.js @@ -1,11 +1,66 @@ -import React from 'react' +import React from "react"; +import Form from "./Form"; +import TodoList from "./TodoList"; + +const list = [ + { + name: "Wash Dishes", + id: 100, + completed: false, + }, +]; export default class App extends React.Component { + constructor() { + super(); + this.state = { + list: list, + }; + } + + addItem = (e, item) => { + e.preventDefault(); + const newList = { + name: item, + id: Date.now(), + completed: false, + }; + this.setState({ + ...this.state, + list: [...this.state.list, newList], + }); + }; + + toggleItem = (itemId) => { + console.log(itemId); + this.setState({ + ...this.state, + list: this.state.list.map((items) => { + if (itemId === items.id) { + return { ...items, completed: !items.completed }; + } + return items; + }), + }); + }; + + clearCompleted = () => { + this.setState({ + ...this.state, + list: this.state.list.filter((item) => { + if (!item.completed) return item; + }), + }); + }; render() { return ( -
- Todo App +
+
+
+
+ +
- ) + ); } } diff --git a/frontend/components/Form.js b/frontend/components/Form.js index 30fbc48d..ce7dc070 100644 --- a/frontend/components/Form.js +++ b/frontend/components/Form.js @@ -1,11 +1,38 @@ -import React from 'react' +import React from "react"; export default class Form extends React.Component { + constructor() { + super(); + this.state = { + item: "", + }; + } + handleChange = (evt) => { + evt.preventDefault(); + this.setState({ ...this.state, item: evt.target.value }); + }; + + submitForm = (evt) => { + evt.preventDefault(); + this.props.addItem(evt, this.state.item); + this.setState({ ...this.state, item: "" }); + }; render() { return ( -
- Form -
- ) + +
+

Add Next To Do Item To List

+
+
+ + + + ); } } diff --git a/frontend/components/Todo.js b/frontend/components/Todo.js index ca86f981..d77f0aa5 100644 --- a/frontend/components/Todo.js +++ b/frontend/components/Todo.js @@ -1,11 +1,17 @@ -import React from 'react' +import React from "react"; export default class Todo extends React.Component { + constructor(props) { + super(props); + } render() { return ( -
- Todo +
this.props.toggleItem(this.props.item.id)} + > +

{this.props.item.name}

- ) + ); } } diff --git a/frontend/components/TodoList.js b/frontend/components/TodoList.js index 95410373..2d2db857 100644 --- a/frontend/components/TodoList.js +++ b/frontend/components/TodoList.js @@ -1,11 +1,18 @@ -import React from 'react' +import React from "react"; +import Todo from "./Todo"; export default class TodoList extends React.Component { + constructor(props) { + super(props); + } render() { return ( -
- TodoList +
+

TodoList

+ {this.props.list.map((item) => ( + + ))}
- ) + ); } }