Skip to content
FrontDepth, Home

Live search

Code ReviewMiddle
Target
find the 2 must-fix problems (4 in all)
Your best
Not played yet

Pull request

Search as you type · feature/live-search

The search box no longer loads the whole catalog: it asks the server on every change of the query, and a label says how fresh the results are.

  • api.js: new searchProducts(query).
  • ProductSearch.jsx: searches when the query changes; an "Updated N s ago" label.

Checked by typing a few product names. Your teammate wrote it with an AI assistant and asks for a review before it is merged: comment on every line that has to change, and give each comment a category.

Hints

    Your comments

      Scoring: a comment finds a problem when it is on the problem's lines (or one line around them) and names its category. The score is the share of problems found, minus half a problem for each comment that finds nothing new. Find every must-fix problem, with no more such comments than there are problems, to complete the challenge. Only your first review is scored.

      1import { useState } from "react";
      2import { listProducts } from "./api.js";
      1import { useEffect, useState } from "react";
      2import { searchProducts } from "./api.js";
      3
      4export function ProductSearch() {
      5 const [products, setProducts] = useState([]);
      5 const [results, setResults] = useState([]);
      6 const [query, setQuery] = useState("");
      7 const [error, setError] = useState(null);
      7 const [loading, setLoading] = useState(false);
      8 const [updatedAt, setUpdatedAt] = useState(null);
      9 const [now, setNow] = useState(() => Date.now());
      10
      9 // Load the whole catalog on click, then filter it here.
      10 async function load() {
      11 try {
      12 setProducts(await listProducts());
      13 } catch (e) {
      14 setError(e.message);
      11 // Ask the server on every change of the query.
      12 useEffect(() => {
      13 if (query.trim() === "") {
      14 setResults([]);
      15 return;
      16 }
      16 }
      17 setLoading(true);
      18 searchProducts(query).then((items) => {
      19 setResults(items);
      20 setUpdatedAt(Date.now());
      21 setLoading(false);
      22 });
      23 }, [query]);
      24
      18 const shown = products.filter((p) => p.name.toLowerCase().includes(query.toLowerCase()));
      25 // Tick once a second for the "Updated N s ago" label.
      26 useEffect(() => {
      27 setInterval(() => setNow(Date.now()), 1000);
      28 }, []);
      29
      30 return (
      31 <section>
      22 <button onClick={load}>Load products</button>
      32 <label>
      33 Search
      34 <input value={query} onChange={(e) => setQuery(e.target.value)} />
      35 </label>
      27 {error && <p role="alert">{error}</p>}
      36 {loading && <p>Loading...</p>}
      37 {updatedAt && <p>Updated {Math.round((now - updatedAt) / 1000)} s ago</p>}
      38 <ul>
      29 {shown.map((p) => (
      39 {results.map((p) => (
      40 <li key={p.id}>{p.name}</li>
      41 ))}
      42 </ul>
      3 unchanged lines