diff --git a/todo_list/package.json b/todo_list/package.json
index f3f0627ef8..d65843f1ac 100644
--- a/todo_list/package.json
+++ b/todo_list/package.json
@@ -12,8 +12,8 @@
"web-vitals": "^2.1.0"
},
"scripts": {
- "start": "react-scripts start",
- "build": "react-scripts build",
+ "start": "set NODE_OPTIONS=--openssl-legacy-provider && react-scripts start",
+ "build": "set NODE_OPTIONS=--openssl-legacy-provider && react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
diff --git a/todo_list/src/App.js b/todo_list/src/App.js
index 220a997d1c..0eb8ec7dab 100644
--- a/todo_list/src/App.js
+++ b/todo_list/src/App.js
@@ -1,28 +1,117 @@
-import React, {useState} from "react";
+import React, {useState,useEffect} from "react";
import "./App.css";
+
const App = () => {
- const [todos, setTodos] = useState([]);
-
- // Add the handlesubmit code here
-
-
- // Add the deleteToDo code here
-
-
- // Add the toggleComplete code here
-
-
- // Add the submitEdits code here
-
-
-return(
-
-
Todo List
-
-
-);
-};
+ const [todos, setTodos] = React.useState([]);
+
+ const [todoEditing, setTodoEditing] = React.useState(null);
+
+ useEffect(() => {
+ const json = localStorage.getItem("todos");
+ const loadedTodos = JSON.parse(json);
+ if (loadedTodos) {
+ setTodos(loadedTodos);
+ }
+ }, []);
+
+useEffect(() => {
+ if(todos.length > 0) {
+ const json = JSON.stringify(todos);
+ localStorage.setItem("todos", json);
+ }
+ }, [todos]);
+
+
+ function handleSubmit(e) {
+ e.preventDefault();
+
+ let todo = document.getElementById('todoAdd').value
+ const newTodo = {
+ id: new Date().getTime(),
+ text: todo.trim(),
+ completed: false,
+ };
+ if (newTodo.text.length > 0 ) {
+ setTodos([...todos].concat(newTodo));
+ } else {
+
+ alert("Enter Valid Task");
+ }
+ document.getElementById('todoAdd').value = ""
+ }
+ function deleteTodo(id) {
+ let updatedTodos = [...todos].filter((todo) => todo.id !== id);
+ setTodos(updatedTodos);
+ }
+
+ function toggleComplete(id) {
+ let updatedTodos = [...todos].map((todo) => {
+ if (todo.id === id) {
+ todo.completed = !todo.completed;
+ }
+ return todo;
+ });
+ setTodos(updatedTodos);
+ }
+
+ function submitEdits(newtodo) {
+ const updatedTodos = [...todos].map((todo) => {
+ if (todo.id === newtodo.id) {
+ todo.text = document.getElementById(newtodo.id).value;
+ }
+ return todo;
+ });
+ setTodos(updatedTodos);
+ setTodoEditing(null);
+ }
+
+ return (
+
+
Todo List
+
+ {todos.map((todo) => (
+
+
+
+
+ {/* if it is edit mode, allow submit edit, else allow edit */}
+ {todo.id === todoEditing ?
+ (
+
+ ) :
+ (
+
+ )}
+
+
+
+
+ ))}
+
+ );
+ };
+
export default App;