diff --git a/react-express-mongodb/frontend/src/App.js b/react-express-mongodb/frontend/src/App.js index 78df4d0..faaa127 100644 --- a/react-express-mongodb/frontend/src/App.js +++ b/react-express-mongodb/frontend/src/App.js @@ -1,55 +1,47 @@ -import React from "react"; -import axios from "axios"; +import React, { useState, useEffect } from "react"; import "./App.scss"; import AddTodo from "./components/AddTodo"; import TodoList from "./components/TodoList"; -export default class App extends React.Component { - constructor(props) { - super(props); +const MainApp = () => { + const [todos, setTodos] = useState([]); - this.state = { - todos: [], - }; - } + async function getTodos() { + try { + const todoListResponse = await fetch("/api", { + method: "GET", + }); - componentDidMount() { - axios - .get("/api") - .then((response) => { - this.setState({ - todos: response.data.data, - }); - }) - .catch((e) => console.log("Error : ", e)); + if (!todoListResponse.ok) { + console.log("Fetch failed"); + } else { + const updatedTodos = await todoListResponse.json(); + setTodos([...updatedTodos.data]); + } + } catch (error) { + console.log("Error :", error); + } } - handleAddTodo = (value) => { - axios - .post("/api/todos", { text: value }) - .then(() => { - this.setState({ - todos: [...this.state.todos, { text: value }], - }); - }) - .catch((e) => console.log("Error : ", e)); - }; + useEffect(() => { + getTodos(); + }, []); - render() { - return ( -
-
-
-
-

Todos

-
- - -
+ return ( +
+
+
+
+

Todos

+
+ +
- ); - } -} +
+ ); +}; + +export default MainApp; diff --git a/react-express-mongodb/frontend/src/components/AddTodo.js b/react-express-mongodb/frontend/src/components/AddTodo.js index 4a2691a..4765701 100644 --- a/react-express-mongodb/frontend/src/components/AddTodo.js +++ b/react-express-mongodb/frontend/src/components/AddTodo.js @@ -1,33 +1,52 @@ import React from "react"; -export default class AddTodo extends React.Component { - handleSubmit = (e) => { +const AddTodo = (props) => { + const { todos, setTodos } = props; + + async function handleSubmit(e) { e.preventDefault(); const { value } = e.target.elements.value; if (value.length > 0) { - this.props.handleAddTodo(value); - e.target.reset(); - } - }; + try { + const newTodoResponse = await fetch("/api/todos", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ text: value }), + }); - render() { - return ( -
- - -
- ); + if (!newTodoResponse.ok) { + console.log("Fetch failed"); + } else { + const newTodo = await newTodoResponse.json(); + setTodos([...todos, newTodo.data]); + e.target.reset(); + } + } catch (error) { + console.log("Error :", error); + } + } } -} + + return ( +
handleSubmit(e)} + className="new-todo form-group" + > + + +
+ ); +}; + +export default AddTodo; diff --git a/react-express-mongodb/frontend/src/components/TodoList.js b/react-express-mongodb/frontend/src/components/TodoList.js index 8551ed0..71f329e 100644 --- a/react-express-mongodb/frontend/src/components/TodoList.js +++ b/react-express-mongodb/frontend/src/components/TodoList.js @@ -1,49 +1,31 @@ -import React from "react"; +import React, { useState } from "react"; -export default class TodoList extends React.Component { - constructor(props) { - super(props); +const TodoList = (props) => { + const { todos } = props; + const [activeIndex, setActiveIndex] = useState(0); - this.state = { - activeIndex: 0, - }; - } + return todos.length > 0 ? ( +
    + {todos.map((todo, index) => ( +
  • { + setActiveIndex(index); + }} + > + {todo.text} +
  • + ))} +
+ ) : ( +
+ No Todos to display +
+ ); +}; - handleActive(index) { - this.setState({ - activeIndex: index, - }); - } - - renderTodos(todos) { - return ( -
    - {todos.map((todo, i) => ( -
  • { - this.handleActive(i); - }} - > - {todo.text} -
  • - ))} -
- ); - } - - render() { - let { todos } = this.props; - return todos.length > 0 ? ( - this.renderTodos(todos) - ) : ( -
- No Todos to display -
- ); - } -} +export default TodoList; diff --git a/react-express-mongodb/frontend/src/custom.scss b/react-express-mongodb/frontend/src/custom.scss index eefdfa8..6d494f7 100644 --- a/react-express-mongodb/frontend/src/custom.scss +++ b/react-express-mongodb/frontend/src/custom.scss @@ -5,4 +5,8 @@ $body-bg: #fff; .cursor-pointer { cursor: pointer; +} + +.list-group { + margin-top: 1.2em; } \ No newline at end of file