Skip to content

Commit e76d954

Browse files
committed
Add user search
1 parent a0793e6 commit e76d954

2 files changed

Lines changed: 42 additions & 10 deletions

File tree

graphite-demo/frontend.jsx

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import React, { useEffect, useState } from 'react';
22

3-
const TaskSearch = () => {
3+
const TaskAndUserSearch = () => {
44
const [tasks, setTasks] = useState([]);
5+
const [users, setUsers] = useState([]);
56
const [loading, setLoading] = useState(true);
67
const [error, setError] = useState(null);
78
const [searchQuery, setSearchQuery] = useState('');
@@ -16,14 +17,15 @@ const TaskSearch = () => {
1617
return response.json();
1718
})
1819
.then(data => {
19-
setTasks(data);
20+
setTasks(data.tasks);
21+
setUsers(data.users);
2022
setLoading(false);
2123
})
2224
.catch(error => {
2325
setError(error.message);
2426
setLoading(false);
2527
});
26-
}, [searchQuery]); // Depend on searchQuery
28+
}, [searchQuery]);
2729

2830
if (loading) {
2931
return <div>Loading...</div>;
@@ -35,22 +37,31 @@ const TaskSearch = () => {
3537

3638
return (
3739
<div>
38-
<h2>Task Search</h2>
40+
<h2>Search Tasks and Users</h2>
3941
<input
4042
type="text"
41-
placeholder="Search tasks..."
43+
placeholder="Search tasks and users..."
4244
value={searchQuery}
4345
onChange={(e) => setSearchQuery(e.target.value)}
4446
/>
47+
<h3>Tasks</h3>
4548
<ul>
4649
{tasks.map(task => (
4750
<li key={task.id}>
4851
<p>{task.description}</p>
4952
</li>
5053
))}
5154
</ul>
55+
<h3>Users</h3>
56+
<ul>
57+
{users.map(user => (
58+
<li key={user.id}>
59+
<p>{user.name}</p>
60+
</li>
61+
))}
62+
</ul>
5263
</div>
5364
);
5465
};
5566

56-
export default TaskSearch;
67+
export default TaskAndUserSearch;

graphite-demo/server.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,38 @@ const tasks = [
1818
}
1919
];
2020

21+
// Fake data for users
22+
const users = [
23+
{
24+
id: 101,
25+
name: 'Alice Smith'
26+
},
27+
{
28+
id: 102,
29+
name: 'Bob Johnson'
30+
},
31+
{
32+
id: 103,
33+
name: 'Charlie Brown'
34+
}
35+
];
36+
2137
app.get('/search', (req, res) => {
2238
// Retrieve the query parameter
2339
const query = req.query.query?.toLowerCase() || '';
2440

2541
// Filter tasks based on the query
26-
const filteredTasks = tasks.filter(task => task.description.toLowerCase().includes(query));
42+
const filteredTasks = tasks.filter(task =>
43+
task.description.toLowerCase().includes(query)
44+
).sort((a, b) => a.description.localeCompare(b.description));
2745

28-
// Sort the filtered tasks alphabetically by description
29-
const sortedTasks = filteredTasks.sort((a, b) => a.description.localeCompare(b.description));
46+
// Filter users based on the query
47+
const filteredUsers = users.filter(user =>
48+
user.name.toLowerCase().includes(query)
49+
).sort((a, b) => a.name.localeCompare(b.name));
3050

31-
res.json(sortedTasks);
51+
// Return both sets of results
52+
res.json({ tasks: filteredTasks, users: filteredUsers });
3253
});
3354

3455
app.listen(port, () => {

0 commit comments

Comments
 (0)