From 52f7d99b138bdf520809dedc8531072427a3db4e Mon Sep 17 00:00:00 2001 From: Anna Huang Date: Thu, 13 Mar 2025 00:10:57 -0700 Subject: [PATCH 1/2] auto reloads --- .DS_Store | Bin 6148 -> 6148 bytes frontend/src/api/tasks.ts | 128 ++++++++++--------- frontend/src/components/todolist/addTask.jsx | 5 +- frontend/src/components/todolist/page.tsx | 5 +- 4 files changed, 71 insertions(+), 67 deletions(-) diff --git a/.DS_Store b/.DS_Store index 8dd1bcb58df4582f3eb7bf3becfe7308721a9263..dd4b024eaaf8963b119c0781e3befb169358b542 100644 GIT binary patch delta 48 zcmZoMXfc@JFUrHfz`)4BAi%(o%8-)H?wp6 => { - try { - const res = await fetch(tasksURL, { - method: "GET", - headers: { - "Content-Type": "application/json", - }, - cache: "no-store", - }); + try { + const res = await fetch(tasksURL, { + method: "GET", + headers: { + "Content-Type": "application/json", + }, + cache: "no-store", + }); - // Check if the response is OK (status code 200-299) - if (!res.ok) { - throw new Error(`HTTP error! status: ${res.status}`); - } + // Check if the response is OK (status code 200-299) + if (!res.ok) { + throw new Error(`HTTP error! status: ${res.status}`); + } - // Parse the JSON response - const tasks: Task[] = await res.json(); - console.log("Fetched tasks:", tasks); // Remove this log in production if not necessary + // Parse the JSON response + const tasks: Task[] = await res.json(); + console.log("Fetched tasks:", tasks); // Remove this log in production if not necessary - return tasks; - } catch (error) { - console.error("Error fetching tasks:", error); - return null; - } + return tasks; + } catch (error) { + console.error("Error fetching tasks:", error); + return null; + } }; -export const addTask = async (task: Task) => { - try { - const res = await fetch(tasksURL, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - cache: "no-store", - body: JSON.stringify({ - _id: task._id, - date: task.date, - title: task.title, - label: task.label, - priority: task.priority, - description: task.description, - }), - }) - .then(async (res) => { - const response = await res.json(); - console.log(res.ok); - if (!res.ok) { - // check server response - throw new Error(res.status + "-" + res.statusText) - } - return true; - }) - .catch((error) => { - console.error("Error: ", error); - return false; - }); - } catch (error) { - console.error("Error fetching tasks:", error); - return null; - } - }; +export const addTask = async (task: Task): Promise => { + try { + const res = await fetch(tasksURL, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + cache: "no-store", + body: JSON.stringify({ + _id: task._id, + date: task.date, + title: task.title, + label: task.label, + priority: task.priority, + description: task.description, + }), + }); + + // Check if the response is OK (status code 200-299) + if (!res.ok) { + // If not, throw an error with the status and message + const response = await res.json(); + console.error( + `Failed to add task: ${response.message || res.statusText}` + ); + return false; // Return false if the task was not added successfully + } + + // If the task was added successfully + console.log("Task added successfully!"); + return true; + } catch (error) { + // Log any errors that occur during the fetch or handling process + console.error("Error adding task:", error); + return false; // Return false if an error occurs + } +}; diff --git a/frontend/src/components/todolist/addTask.jsx b/frontend/src/components/todolist/addTask.jsx index 5222288..b154162 100644 --- a/frontend/src/components/todolist/addTask.jsx +++ b/frontend/src/components/todolist/addTask.jsx @@ -2,7 +2,7 @@ import React, { useState } from "react"; import style from "./addTask.module.css"; // Assuming the correct path for your CSS import { addTask } from "../../api/tasks"; -const AddTask = () => { +const AddTask = ({ onTaskAdded }) => { const [title, setTitle] = useState(""); // Track task title const [date, setDate] = useState(""); // Track task date const [priority, setPriority] = useState(""); // Track task priority @@ -32,6 +32,8 @@ const AddTask = () => { const isTaskAdded = await addTask(newTask); if (isTaskAdded) { + console.log("here"); + onTaskAdded(newTask); // Clear form fields after submitting setTitle(""); setDate(""); @@ -42,7 +44,6 @@ const AddTask = () => { console.log("Task posted successfully!"); // Notify the parent component to update the task list - onTaskAdded(newTask); } } catch (err) { console.error("Error posting task:", err); diff --git a/frontend/src/components/todolist/page.tsx b/frontend/src/components/todolist/page.tsx index 525a99b..4a75541 100644 --- a/frontend/src/components/todolist/page.tsx +++ b/frontend/src/components/todolist/page.tsx @@ -19,7 +19,8 @@ function MyApp() { }); }, []); // Empty dependency array ensures this runs once on component mount - const handleTaskAdded = (newTask) => { + const handleTaskAdded = (newTask: Task) => { + console.log("hello") setTasks((prevTasks) => [...prevTasks, newTask]); // Add the new task to the task list }; return ( @@ -41,7 +42,7 @@ function MyApp() { )}
- +
); From c4583ac228f9f61434d7d5b929d159b08b10ec52 Mon Sep 17 00:00:00 2001 From: annahuang28 <156022139+annahuang28@users.noreply.github.com> Date: Thu, 13 Mar 2025 00:16:25 -0700 Subject: [PATCH 2/2] Update page.tsx --- frontend/src/components/todolist/page.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/frontend/src/components/todolist/page.tsx b/frontend/src/components/todolist/page.tsx index 4a75541..f95e411 100644 --- a/frontend/src/components/todolist/page.tsx +++ b/frontend/src/components/todolist/page.tsx @@ -20,7 +20,6 @@ function MyApp() { }, []); // Empty dependency array ensures this runs once on component mount const handleTaskAdded = (newTask: Task) => { - console.log("hello") setTasks((prevTasks) => [...prevTasks, newTask]); // Add the new task to the task list }; return (