-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c90c621
commit 6e5fc7e
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
|
||
const TaskSearch = () => { | ||
const [tasks, setTasks] = useState([]); | ||
const [loading, setLoading] = useState(true); | ||
const [error, setError] = useState(null); | ||
const [searchQuery, setSearchQuery] = useState(''); | ||
|
||
useEffect(() => { | ||
setLoading(true); | ||
fetch(`/search?query=${encodeURIComponent(searchQuery)}`) | ||
.then(response => { | ||
if (!response.ok) { | ||
throw new Error('Network response was not ok'); | ||
} | ||
return response.json(); | ||
}) | ||
.then(data => { | ||
setTasks(data); | ||
setLoading(false); | ||
}) | ||
.catch(error => { | ||
setError(error.message); | ||
setLoading(false); | ||
}); | ||
}, [searchQuery]); // Depend on searchQuery | ||
|
||
if (loading) { | ||
return <div>Loading...</div>; | ||
} | ||
|
||
if (error) { | ||
return <div>Error: {error}</div>; | ||
} | ||
|
||
return ( | ||
<div> | ||
<h2>Task Search</h2> | ||
<input | ||
type="text" | ||
placeholder="Search tasks..." | ||
value={searchQuery} | ||
onChange={(e) => setSearchQuery(e.target.value)} | ||
/> | ||
<ul> | ||
{tasks.map(task => ( | ||
<li key={task.id}> | ||
<p>{task.description}</p> | ||
</li> | ||
))} | ||
</ul> | ||
</div> | ||
); | ||
}; | ||
|
||
export default TaskSearch; |