Make Your Mini To-Do App Interactive: Mark Tasks as Done
Step 0: Understanding the Feature
Before coding, let’s understand what “mark as done” means:
-
Each task in the database will have a status: done (1) or not done (0).
-
On the frontend, tasks that are done will appear crossed out or in gray.
-
When you click the checkbox, the backend updates the status in the database.
Think of it like putting a checkmark on your real-life to-do list 📝.
Step 1: Update the Database
Open your database setup (routes/tasks.js) and make sure the tasks table has a done column:
-
done= 0 → task not done -
done= 1 → task done -
Default is 0 because new tasks are not done yet
Step 2: Add a Route to Mark Tasks as Done
In routes/tasks.js, add this new route:
-
This tells the backend to update the task as done when we call this route from the frontend.
Step 3: Update the Frontend (script.js)
Now let’s make the app interactive with checkboxes:
Step-by-step explanation for beginners:
-
We create a checkbox for each task.
-
If a task is done, we cross it out and gray it.
-
Clicking the checkbox calls
markDone(), which updates the database via our API. -
The task list refreshes automatically after every action.
Step 4: Test Your Interactive App
-
Run your backend server:
-
Open
http://localhost:3000/in your browser -
Add a few tasks → they appear in the list
-
Click the checkbox → task is crossed out and saved as done
-
Delete a task → it disappears from the list
🎉 Congratulations! You now have an interactive full-stack To-Do app