Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions graphite-demo/frontend.jsx
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('');
Expand All @@ -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>;
Expand All @@ -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>
))}
Comment on lines 49 to 53
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
{tasks.map(task => (
<li key={task.id}>
<p>{task.description}</p>
</li>
))}
{tasks.length > 0 ? (
tasks.map(task => (
<li key={task.id}>
<p>{task.description}</p>
</li>
))
) : (
<li>No tasks found</li>
)}

</ul>
<h3>Users</h3>
<ul>
{users.map(user => (
<li key={user.id}>
<p>{user.name}</p>
</li>
))}
Comment on lines +57 to +61
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Similar to the tasks list, consider showing a message when no users are found to provide better feedback to the user.

        {users.length > 0 ? (
          users.map(user => (
            <li key={user.id}>
              <p>{user.name}</p>
            </li>
          ))
        ) : (
          <li>No users found</li>
        )}

</ul>
</div>
);
};

export default TaskSearch;
export default TaskAndUserSearch;
29 changes: 25 additions & 4 deletions graphite-demo/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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, () => {
Expand Down
Loading