diff --git a/package-lock.json b/package-lock.json index d365b3d..52e8957 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,11 +1,11 @@ { - "name": "technical-lesson-react-hooks-task-manager", + "name": "react-hooks-task-manager-lab", "version": "0.0.0", "lockfileVersion": 3, "requires": true, "packages": { "": { - "name": "technical-lesson-react-hooks-task-manager", + "name": "react-hooks-task-manager-lab", "version": "0.0.0", "dependencies": { "@vitejs/plugin-react": "^4.3.4", diff --git a/src/components/App.jsx b/src/components/App.jsx index 9394aab..a6c4eba 100644 --- a/src/components/App.jsx +++ b/src/components/App.jsx @@ -2,10 +2,16 @@ import React, { useEffect, useContext, useState } from "react"; import { TaskContext } from "../context/TaskContext"; import TaskForm from "./TaskForm"; import SearchBar from "./SearchBar"; +import TaskList from "./TaskList"; function App() { - const [tasks, setTasks] = useState([]); +// get global statee and updater from context + const { tasks, setTasks } = useContext(TaskContext); + //search state + const [query, setQuery] = useState(""); + + //lad tasks from db.json into context on page load useEffect(() => { fetch('http://localhost:6001/tasks') .then(r=>r.json()) @@ -17,7 +23,8 @@ function App() {

Task Manager

- + +
); } diff --git a/src/components/SearchBar.jsx b/src/components/SearchBar.jsx index ef44c20..01ba96a 100644 --- a/src/components/SearchBar.jsx +++ b/src/components/SearchBar.jsx @@ -1,25 +1,25 @@ -import React, { useRef, useState, useContext } from "react"; -import TaskList from "./TaskList"; -import { TaskContext } from "../context/TaskContext"; +import React, { useRef } from "react"; -function SearchBar() { - const [query, setQuery] = useState(""); + +function SearchBar({setQuery}) { + const searchRef = useRef(""); function handleSearch(e) { - setQuery(e.target.value); + const value = e.target.value; + searchRef.current = value; + setQuery(value); } return ( -
+ - -
+ ); } diff --git a/src/components/TaskForm.jsx b/src/components/TaskForm.jsx index ab2b6f9..7ab9ea0 100644 --- a/src/components/TaskForm.jsx +++ b/src/components/TaskForm.jsx @@ -4,16 +4,26 @@ import { TaskContext } from "../context/TaskContext"; function TaskForm() { const [taskName, setTaskName] = useState(""); + // required: useId for the input + const inputId = useId(); + + // get addTask from context + const { addTask } = useContext(TaskContext); + function handleSubmit(e) { e.preventDefault(); if (taskName.trim() === "") return; + // call addTask from context + addTask(taskName); + //clear imput setTaskName(""); } return (
- + setTaskName(e.target.value)} diff --git a/src/components/TaskList.jsx b/src/components/TaskList.jsx index 0bf6249..e7b7380 100644 --- a/src/components/TaskList.jsx +++ b/src/components/TaskList.jsx @@ -2,7 +2,8 @@ import React, { useContext,useState } from "react"; import { TaskContext } from "../context/TaskContext"; function TaskList({query}) { - const [tasks, setTasks] = useState([]); + //gets tasks and toggleComplete from context + const { tasks, toggleComplete } = useContext(TaskContext); const filteredTasks = tasks.filter(task => task.title.toLowerCase().includes(query.toLowerCase()) ); @@ -14,7 +15,7 @@ function TaskList({query}) { {task.title} - diff --git a/src/context/TaskContext.jsx b/src/context/TaskContext.jsx index 6781cdd..9ad4361 100644 --- a/src/context/TaskContext.jsx +++ b/src/context/TaskContext.jsx @@ -3,4 +3,51 @@ import React, { createContext, useState } from "react"; export const TaskContext = createContext(); export function TaskProvider({ children }) { + const [tasks, setTasks] = useState([]); + + //mark task complete or incomplete + async function toggleComplete(id) { + const taskToUpdate = tasks.find((task) => task.id === id); + if (!taskToUpdate) return; + + const updatedTask = { + ...taskToUpdate, + completed: !taskToUpdate.completed, + }; + + // update page + setTasks((prevTasks) => + prevTasks.map((task) => + task.id === id ? updatedTask : task + ) + ); + + // update db.json + await fetch(`http://localhost:6001/tasks/${id}`, { + method: "PATCH", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ completed: updatedTask.completed }), + }); + } + + //create a new task (only updates) + function addTask(title) { + const newTask = { + id: tasks.length + 1, + title, + completed: false, + }; + + setTasks((prevTasks) => [...prevTasks, newTask]); + } + + return ( + + {children} + + ); } diff --git a/src/main.jsx b/src/main.jsx index cb48fb7..7df66a3 100644 --- a/src/main.jsx +++ b/src/main.jsx @@ -7,4 +7,4 @@ createRoot(document.getElementById('root')).render( , -) +);