From a9f34c67999b69fec0f0b5b216853a7a255691a9 Mon Sep 17 00:00:00 2001 From: Vedant Date: Sat, 27 Jun 2026 10:23:24 +0530 Subject: [PATCH] Fix duplicate task completion by assigning unique IDs --- src/Component/TodoList.js | 126 +++++++++++++++++++++++--------------- 1 file changed, 77 insertions(+), 49 deletions(-) diff --git a/src/Component/TodoList.js b/src/Component/TodoList.js index 0e5e62d..61c4fca 100644 --- a/src/Component/TodoList.js +++ b/src/Component/TodoList.js @@ -1,14 +1,14 @@ -import React, { useState, useEffect } from 'react'; -import { ToastContainer, toast } from 'react-toastify'; -import 'react-toastify/dist/ReactToastify.css'; -import './style.css'; -import todoImage from "../image/todo.png" +import React, { useState, useEffect } from "react"; +import { ToastContainer, toast } from "react-toastify"; +import "react-toastify/dist/ReactToastify.css"; +import "./style.css"; +import todoImage from "../image/todo.png"; const TodoList = () => { // State variables const [tasks, setTasks] = useState([]); // Holds the list of tasks - const [inputValue, setInputValue] = useState(''); // Holds the value of the input field - const [filter, setFilter] = useState('all'); // Holds the current filter type + const [inputValue, setInputValue] = useState(""); // Holds the value of the input field + const [filter, setFilter] = useState("all"); // Holds the current filter type const [isLoading, setIsLoading] = useState(true); // Indicates whether the data is being loaded const [editTaskId, setEditTaskId] = useState(null); // Holds the ID of the task being edited @@ -20,12 +20,14 @@ const TodoList = () => { // Fetch todos from an API const fetchTodos = async () => { try { - const response = await fetch('https://jsonplaceholder.typicode.com/todos?_limit=4'); + const response = await fetch( + "https://jsonplaceholder.typicode.com/todos?_limit=4", + ); const todos = await response.json(); setTasks(todos); setIsLoading(false); } catch (error) { - console.log('Error fetching todos:', error); + console.log("Error fetching todos:", error); setIsLoading(false); } }; @@ -37,30 +39,37 @@ const TodoList = () => { // Add a new task const handleAddTask = async () => { - if (inputValue.trim() === '') { + if (inputValue.trim() === "") { return; } const newTask = { title: inputValue, - completed: false + completed: false, }; try { - const response = await fetch('https://jsonplaceholder.typicode.com/todos', { - method: 'POST', - body: JSON.stringify(newTask), - headers: { - 'Content-type': 'application/json; charset=UTF-8', + const response = await fetch( + "https://jsonplaceholder.typicode.com/todos", + { + method: "POST", + body: JSON.stringify(newTask), + headers: { + "Content-type": "application/json; charset=UTF-8", + }, }, - }); + ); const addedTask = await response.json(); - setTasks((prevTasks) => [...prevTasks, addedTask]); - setInputValue(''); - toast.success('Task added successfully'); + const taskWithUniqueId = { + ...addedTask, + id: Date.now(), + }; + setTasks((prevTasks) => [...prevTasks, taskWithUniqueIdha]); + setInputValue(""); + toast.success("Task added successfully"); } catch (error) { - console.log('Error adding task:', error); - toast.error('Error adding task'); + console.log("Error adding task:", error); + toast.error("Error adding task"); } }; @@ -68,15 +77,15 @@ const TodoList = () => { const handleTaskCheckboxChange = (taskId) => { setTasks((prevTasks) => prevTasks.map((task) => - task.id === taskId ? { ...task, completed: !task.completed } : task - ) + task.id === taskId ? { ...task, completed: !task.completed } : task, + ), ); }; // Delete a task const handleDeleteTask = (taskId) => { setTasks((prevTasks) => prevTasks.filter((task) => task.id !== taskId)); - toast.success('Task deleted successfully'); + toast.success("Task deleted successfully"); }; // Edit a task @@ -88,42 +97,47 @@ const TodoList = () => { // Update a task const handleUpdateTask = async () => { - if (inputValue.trim() === '') { + if (inputValue.trim() === "") { return; } const updatedTask = { title: inputValue, - completed: false + completed: false, }; try { - const response = await fetch(`https://jsonplaceholder.typicode.com/todos/${editTaskId}`, { - method: 'PUT', - body: JSON.stringify(updatedTask), - headers: { - 'Content-type': 'application/json; charset=UTF-8', + const response = await fetch( + `https://jsonplaceholder.typicode.com/todos/${editTaskId}`, + { + method: "PUT", + body: JSON.stringify(updatedTask), + headers: { + "Content-type": "application/json; charset=UTF-8", + }, }, - }); + ); const updatedTaskData = await response.json(); setTasks((prevTasks) => prevTasks.map((task) => - task.id === editTaskId ? { ...task, title: updatedTaskData.title } : task - ) + task.id === editTaskId + ? { ...task, title: updatedTaskData.title } + : task, + ), ); - setInputValue(''); + setInputValue(""); setEditTaskId(null); - toast.success('Task updated successfully'); + toast.success("Task updated successfully"); } catch (error) { - console.log('Error updating task:', error); - toast.error('Error updating task'); + console.log("Error updating task:", error); + toast.error("Error updating task"); } }; // Mark all tasks as completed const handleCompleteAll = () => { setTasks((prevTasks) => - prevTasks.map((task) => ({ ...task, completed: true })) + prevTasks.map((task) => ({ ...task, completed: true })), ); }; @@ -139,11 +153,11 @@ const TodoList = () => { // Filter tasks based on the selected filter const filteredTasks = tasks.filter((task) => { - if (filter === 'all') { + if (filter === "all") { return true; - } else if (filter === 'completed') { + } else if (filter === "completed") { return task.completed; - } else if (filter === 'uncompleted') { + } else if (filter === "uncompleted") { return !task.completed; } return true; @@ -173,8 +187,11 @@ const TodoList = () => { value={inputValue} onChange={handleInputChange} /> - @@ -222,13 +239,21 @@ const TodoList = () => {
- handleFilterChange('all')}> + handleFilterChange("all")}> All - handleFilterChange('uncompleted')}> + handleFilterChange("uncompleted")} + > Uncompleted - handleFilterChange('completed')}> + handleFilterChange("completed")} + > Completed
@@ -236,7 +261,10 @@ const TodoList = () => {

- Completed: {tasks.filter((task) => task.completed).length} + Completed:{" "} + + {tasks.filter((task) => task.completed).length} +