From 11601df6a3725e1c543f617a041c08f79aa6ac3c Mon Sep 17 00:00:00 2001 From: sosokker Date: Tue, 21 Nov 2023 12:36:10 +0700 Subject: [PATCH] Add checkbox false handle --- .../EisenhowerMatrix/Eisenhower.jsx | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/frontend/src/components/EisenhowerMatrix/Eisenhower.jsx b/frontend/src/components/EisenhowerMatrix/Eisenhower.jsx index 86958d6..374ac66 100644 --- a/frontend/src/components/EisenhowerMatrix/Eisenhower.jsx +++ b/frontend/src/components/EisenhowerMatrix/Eisenhower.jsx @@ -4,16 +4,24 @@ import { readTodoTasks } from "../../api/TaskApi"; import axiosInstance from "../../api/configs/AxiosConfig"; function EachBlog({ name, colorCode, contentList, icon }) { - const handleCheckboxChange = async (index) => { - const updatedTasks = [...contentList]; - const taskId = updatedTasks[index].id; + const [tasks, setTasks] = useState(contentList); + const handleCheckboxChange = async index => { try { - await axiosInstance.patch(`todo/${taskId}/`, { completed: true }); + setTasks(contentList) - updatedTasks[index].completed = true; + const updatedTasks = [...tasks]; + const taskId = updatedTasks[index].id; + + const response = await axiosInstance.patch(`todo/${taskId}/`, { + completed: !updatedTasks[index].completed, + }); + + updatedTasks[index].completed = response.data.completed; + + setTasks(updatedTasks); } catch (error) { - console.error('Error updating task:', error); + console.error("Error updating task:", error); } }; @@ -34,9 +42,7 @@ function EachBlog({ name, colorCode, contentList, icon }) { className="checkbox mt-1 mr-2" onChange={() => handleCheckboxChange(index)} /> -