-
Notifications
You must be signed in to change notification settings - Fork 1
Add user search #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 01-11-demo_22d42e7a_add_frontend_for_search
Are you sure you want to change the base?
Add user search #47
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,8 @@ | ||
| import React, { useEffect, useState } from 'react'; | ||
|
|
||
| const TaskSearch = () => { | ||
| const TaskAndUserSearch = () => { | ||
| const [tasks, setTasks] = useState([]); | ||
| const [users, setUsers] = useState([]); | ||
| const [loading, setLoading] = useState(true); | ||
| const [error, setError] = useState(null); | ||
| const [searchQuery, setSearchQuery] = useState(''); | ||
|
|
@@ -16,14 +17,15 @@ const TaskSearch = () => { | |
| return response.json(); | ||
| }) | ||
| .then(data => { | ||
| setTasks(data); | ||
| setTasks(data.tasks); | ||
| setUsers(data.users); | ||
| setLoading(false); | ||
| }) | ||
| .catch(error => { | ||
| setError(error.message); | ||
| setLoading(false); | ||
| }); | ||
| }, [searchQuery]); // Depend on searchQuery | ||
| }, [searchQuery]); | ||
|
|
||
| if (loading) { | ||
| return <div>Loading...</div>; | ||
|
|
@@ -35,22 +37,31 @@ const TaskSearch = () => { | |
|
|
||
| return ( | ||
| <div> | ||
| <h2>Task Search</h2> | ||
| <h2>Search Tasks and Users</h2> | ||
| <input | ||
| type="text" | ||
| placeholder="Search tasks..." | ||
| placeholder="Search tasks and users..." | ||
| value={searchQuery} | ||
| onChange={(e) => setSearchQuery(e.target.value)} | ||
| /> | ||
| <h3>Tasks</h3> | ||
| <ul> | ||
| {tasks.map(task => ( | ||
| <li key={task.id}> | ||
| <p>{task.description}</p> | ||
| </li> | ||
| ))} | ||
| </ul> | ||
| <h3>Users</h3> | ||
| <ul> | ||
| {users.map(user => ( | ||
| <li key={user.id}> | ||
| <p>{user.name}</p> | ||
| </li> | ||
| ))} | ||
|
Comment on lines
+57
to
+61
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| </ul> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export default TaskSearch; | ||
| export default TaskAndUserSearch; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,17 +18,38 @@ const tasks = [ | |
| } | ||
| ]; | ||
|
|
||
| // Fake data for users | ||
| const users = [ | ||
| { | ||
| id: 101, | ||
| name: 'Alice Smith' | ||
| }, | ||
| { | ||
| id: 102, | ||
| name: 'Bob Johnson' | ||
| }, | ||
| { | ||
| id: 103, | ||
| name: 'Charlie Brown' | ||
| } | ||
| ]; | ||
|
|
||
| app.get('/search', (req, res) => { | ||
| // Retrieve the query parameter | ||
| const query = req.query.query?.toLowerCase() || ''; | ||
|
|
||
| // Filter tasks based on the query | ||
| const filteredTasks = tasks.filter(task => task.description.toLowerCase().includes(query)); | ||
| const filteredTasks = tasks.filter(task => | ||
| task.description.toLowerCase().includes(query) | ||
| ).sort((a, b) => a.description.localeCompare(b.description)); | ||
|
|
||
| // Sort the filtered tasks alphabetically by description | ||
| const sortedTasks = filteredTasks.sort((a, b) => a.description.localeCompare(b.description)); | ||
| // Filter users based on the query | ||
| const filteredUsers = users.filter(user => | ||
| user.name.toLowerCase().includes(query) | ||
| ).sort((a, b) => a.name.localeCompare(b.name)); | ||
|
Comment on lines
+42
to
+49
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The filtering and sorting logic for tasks and users is very similar. To adhere to the Don't Repeat Yourself (DRY) principle and improve maintainability, you could abstract this logic into a reusable helper function. const filterAndSort = (items, key) =>
items
.filter(item => item[key].toLowerCase().includes(query))
.sort((a, b) => a[key].localeCompare(b[key]));
const filteredTasks = filterAndSort(tasks, 'description');
const filteredUsers = filterAndSort(users, 'name'); |
||
|
|
||
| res.json(sortedTasks); | ||
| // Return both sets of results | ||
| res.json({ tasks: filteredTasks, users: filteredUsers }); | ||
| }); | ||
|
|
||
| app.listen(port, () => { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To improve the user experience, it's helpful to display a message when no tasks match the search query instead of showing an empty list.