Skip to content
Open
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ that will suggest people matching an entered text.
- Install Prettier Extention and use this [VSCode settings](https://mate-academy.github.io/fe-program/tools/vscode/settings.json) to enable format on save.
- Implement a solution following the [React task guideline](https://github.com/mate-academy/react_task-guideline#react-tasks-guideline).
- Use the [React TypeScript cheat sheet](https://mate-academy.github.io/fe-program/js/extra/react-typescript).
- Replace `<your_account>` with your Github username in the [DEMO LINK](https://<your_account>.github.io/react_autocomplete/) and add it to the PR description.
- Replace `<your_account>` with your Github username in the [DEMO LINK](https://AndreyKagaml.github.io/react_autocomplete/) and add it to the PR description.
- Don't remove the `data-qa` attributes. It is required for tests.

## Troubleshooting
Expand Down
106 changes: 59 additions & 47 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,46 @@
import React from 'react';
import React, { useCallback, useMemo, useState } from 'react';
import './App.scss';
import { peopleFromServer } from './data/people';
import { SuggestionList } from './components/SuggestionList';
import debounce from 'lodash.debounce';
import { Person } from './types/Person';

export const App: React.FC = () => {
const { name, born, died } = peopleFromServer[0];
//const [filteredPeople, setFilteredPeople] = useState([]);
const [query, setQuery] = useState('');
const [preparedQuery, setPreparedQuery] = useState('');
const [blured, setBlured] = useState(false);
const [selected, setSelected] = useState<Person | null>(null);
const { name, born, died } = selected ?? peopleFromServer[0];

const createdFilteredPeople = useMemo(
() =>
peopleFromServer.filter(person =>
person.name.includes(preparedQuery.trim()),
Comment on lines +18 to +19
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This violates checklist item #1: 'make sure that filter won't be called if user enred spaces only;'. The .filter() method is called even for an empty or spaces-only query. Consider adding a check to avoid filtering in this case and directly return the full list of people.

),
[preparedQuery],
);

const applyQuery = useCallback(debounce(setPreparedQuery, 300), []);

Check warning on line 24 in src/App.tsx

View workflow job for this annotation

GitHub Actions / run_linter (20.x)

React Hook useCallback received a function whose dependencies are unknown. Pass an inline function instead
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The task requires the debounce delay to be customizable via a prop with a default value of 300ms. Currently, the delay is hardcoded here. This suggests that the autocomplete logic should be encapsulated in a separate, reusable component that can accept props.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The debounce delay is hardcoded. According to the requirements, the delay should be a customizable prop with a default value of 300ms.

const loseQuery = useCallback(debounce(setBlured, 1000), []);

Check warning on line 25 in src/App.tsx

View workflow job for this annotation

GitHub Actions / run_linter (20.x)

React Hook useCallback received a function whose dependencies are unknown. Pass an inline function instead

const handleQueryChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setQuery(event.target.value);
applyQuery(event.target.value);
setSelected(null);
};
Comment thread
AndreyKagaml marked this conversation as resolved.

const handleSelect = (personSelected: Person) => {
setSelected(personSelected);
setBlured(false);
setQuery(personSelected.name);
};

return (
<div className="container">
<main className="section is-flex is-flex-direction-column">
<h1 className="title" data-cy="title">
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The task description explicitly states, "Don't remove the data-qa attributes. It is required for tests." All data-cy attributes throughout the application should be changed back to data-qa to ensure that the automated tests can run successfully.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The task requires using data-qa attributes for testing, but data-cy is used here. This was also pointed out in the previous review. Please update all test attributes to data-qa.

{`${name} (${born} - ${died})`}
{selected ? `${name} (${born} - ${died})` : `No selected person`}
</h1>

<div className="dropdown is-active">
Expand All @@ -19,55 +50,36 @@
placeholder="Enter a part of the name"
className="input"
data-cy="search-input"
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The task requires using data-qa attributes for testing, but data-cy is used here.

value={query}
onChange={handleQueryChange}
onFocus={() => setBlured(true)}
onBlur={() => loseQuery(false)}
Comment on lines +55 to +56
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

There's a subtle bug in the focus/blur handling. If you blur the input and then quickly re-focus it (within 1 second), the suggestions will appear and then disappear. This is because the debounced loseQuery call from onBlur isn't cancelled. You can fix this by calling loseQuery.cancel() inside the onFocus handler.

/>
</div>

<div className="dropdown-menu" role="menu" data-cy="suggestions-list">
<div className="dropdown-content">
<div className="dropdown-item" data-cy="suggestion-item">
<p className="has-text-link">Pieter Haverbeke</p>
</div>

<div className="dropdown-item" data-cy="suggestion-item">
<p className="has-text-link">Pieter Bernard Haverbeke</p>
</div>

<div className="dropdown-item" data-cy="suggestion-item">
<p className="has-text-link">Pieter Antone Haverbeke</p>
</div>

<div className="dropdown-item" data-cy="suggestion-item">
<p className="has-text-danger">Elisabeth Haverbeke</p>
</div>

<div className="dropdown-item" data-cy="suggestion-item">
<p className="has-text-link">Pieter de Decker</p>
</div>

<div className="dropdown-item" data-cy="suggestion-item">
<p className="has-text-danger">Petronella de Decker</p>
</div>

<div className="dropdown-item" data-cy="suggestion-item">
<p className="has-text-danger">Elisabeth Hercke</p>
</div>
</div>
</div>
{blured && (
<SuggestionList
filteredPeople={createdFilteredPeople}
onSelected={handleSelect}
/>
)}
</div>

<div
className="
notification
is-danger
is-light
mt-3
is-align-self-flex-start
"
role="alert"
data-cy="no-suggestions-message"
>
<p className="has-text-danger">No matching suggestions</p>
</div>
{createdFilteredPeople.length === 0 && preparedQuery && blured && (
<div
className="
notification
is-danger
is-light
mt-3
is-align-self-flex-start
"
role="alert"
data-cy="no-suggestions-message"
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The task requires using data-qa attributes for testing, but data-cy is used here.

>
<p className="has-text-danger">No matching suggestions</p>
</div>
)}
</main>
</div>
);
Expand Down
30 changes: 30 additions & 0 deletions src/components/SuggestionList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Person } from '../types/Person';

export const SuggestionList = ({
filteredPeople,
onSelected,
}: {
filteredPeople: Person[];
onSelected: (selected: Person) => void;
}) => {
//const field = useRef<HTMLInputElement>(null);

return (
<div className="dropdown-menu" role="menu" data-cy="suggestions-list">
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The task requirements specify using data-qa attributes for testing, not data-cy. Please update this attribute to data-qa="suggestions-list".

<div className="dropdown-content">
{filteredPeople.map((person: Person) => (
<div
className="dropdown-item"
data-cy="suggestion-item"
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

As per the task requirements, this should be a data-qa attribute for testing purposes. Please change it to data-qa="suggestion-item".

key={person.name}
onClick={() => {
onSelected(person);
Comment thread
AndreyKagaml marked this conversation as resolved.
}}
>
<p className="has-text-link">{person.name}</p>
</div>
))}
</div>
</div>
);
};