Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ module.exports = {
// Typescript
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-unused-vars': ['error'],
'@typescript-eslint/indent': ['error', 2],
'@typescript-eslint/no-unused-vars': ['error'],
'@typescript-eslint/indent': 'off',
'@typescript-eslint/ban-types': ['error', {
extendDefaults: true,
types: {
Expand Down
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://tavokina.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
12 changes: 7 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"devDependencies": {
"@cypress/react18": "^2.0.1",
"@faker-js/faker": "^8.4.1",
"@mate-academy/scripts": "^1.8.5",
"@mate-academy/scripts": "^2.1.3",
"@mate-academy/students-ts-config": "*",
"@mate-academy/stylelint-config": "*",
"@types/lodash.debounce": "^4.0.9",
Expand Down
81 changes: 22 additions & 59 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,73 +1,36 @@
import React from 'react';
import React, { useState } from 'react';
import './App.scss';
import { peopleFromServer } from './data/people';
import { Autocomplete } from './Autocomplete';
import { Person } from './types/Person';

export const App: React.FC = () => {
const { name, born, died } = peopleFromServer[0];
const [selectedPerson, setSelectedPerson] = useState<Person | null>(null);
const [query, setQuery] = useState('');
const delayValue = 300;

return (
<div className="container">
<main className="section is-flex is-flex-direction-column">
<h1 className="title" data-cy="title">
{`${name} (${born} - ${died})`}
{selectedPerson
? `${selectedPerson.name} (${selectedPerson.born} - ${selectedPerson.died})`
: 'No selected person'}
</h1>

<div className="dropdown is-active">
<div className="dropdown-trigger">
<input
type="text"
placeholder="Enter a part of the name"
className="input"
data-cy="search-input"
/>
</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>
</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>
<Autocomplete
people={peopleFromServer}
onSelected={person => {
setSelectedPerson(person);
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 #4: 'follow naming conventions for methods'. The function name isAppliedQuery implies it returns a boolean, but it returns undefined with side effects. Rename it to something action-oriented like applyQueryIfChanged.

setQuery(person.name);
}}
delay={delayValue}
query={query}
onQueryChange={value => {
setQuery(value);
setSelectedPerson(null);
Comment on lines +24 to +31
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Checklist item #4 violation: Function name isAppliedQuery implies a boolean return value, but it returns undefined (early return) and performs side effects. Rename to an action-oriented name like applyQueryIfChanged per naming conventions for methods that return undefined and perform actions.

}}
/>
</main>
</div>
);
Expand Down
105 changes: 105 additions & 0 deletions src/Autocomplete.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { useCallback, useRef, useState } from 'react';
import { Person } from './types/Person';
import debounce from 'lodash.debounce';
import classNames from 'classnames';

type Props = {
people: Person[];
onSelected: (person: Person) => void;
delay?: number;
query: string;
onQueryChange: (value: string) => void;
};
export const Autocomplete: React.FC<Props> = ({
people,
onSelected,
delay,
query,
onQueryChange,
}) => {
const [appliedQuery, setAppliedQuery] = useState('');
const [isOpen, setIsOpen] = useState(false);

const lastAppliedQuery = useRef('');

const isAppliedQuery = (value: string) => {
if (lastAppliedQuery.current === value) {
return;
}

lastAppliedQuery.current = value;
setAppliedQuery(value);
Comment on lines +24 to +31
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 function name implies it returns a boolean (is prefix), but it returns undefined and performs side effects. Per the naming conventions requirement, functions that perform actions should have action-oriented names like applyQueryIfChanged.

Comment on lines +24 to +31
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 #4 'follow naming conventions for methods'. Function name isAppliedQuery implies it returns a boolean, but it returns undefined and performs side effects. Rename to action-oriented name like applyQueryIfChanged.

};

// eslint-disable-next-line react-hooks/exhaustive-deps
const applyQuery = useCallback(debounce(isAppliedQuery, delay), [delay]);
Comment on lines +34 to +35
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 debounced function should be cancelled on unmount to prevent memory leaks. Add a useEffect cleanup: useEffect(() => { return () => applyQuery.cancel(); }, []);


const handleQueryChange = (event: React.ChangeEvent<HTMLInputElement>) => {
onQueryChange(event.target.value);
applyQuery(event.target.value);
};

const normalizedQuery = appliedQuery.trim().toLowerCase();

const filteredPeople = appliedQuery
? people.filter(person =>
person.name.toLowerCase().includes(normalizedQuery))
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 #4: 'make sure that filter won't be called if user entered spaces only'. When appliedQuery is only spaces (e.g., ' '), appliedQuery is truthy so people.filter() still runs. Use normalizedQuery in the condition instead: normalizedQuery ? people.filter(...) : people

: people;

return (
<div
className={classNames('dropdown', {
'is-active': isOpen,
})}
>
<div className="dropdown-trigger">
<input
type="text"
placeholder="Enter a part of the name"
className="input"
data-cy="search-input"
value={query}
onChange={e => {
handleQueryChange(e);
Comment on lines +68 to +70
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Redundant wrapper - can be simplified to onChange={handleQueryChange}

Comment on lines +68 to +70
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 #4 - redundant wrapper. Change onChange={e => { handleQueryChange(e); }} to onChange={handleQueryChange}.

}}
onFocus={() => {
setIsOpen(true);
}}
/>
</div>

<div className="dropdown-menu" role="menu" data-cy="suggestions-list">
<div className="dropdown-content">
{filteredPeople.map(person => (
<div
key={person.slug}
className="dropdown-item"
data-cy="suggestion-item"
onClick={() => {
onSelected(person);
setIsOpen(false);
}}
>
<p className="has-text-link">{person.name}</p>
</div>
))}
</div>
</div>

{isOpen && appliedQuery && filteredPeople.length === 0 && (
<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>
)}
</div>
);
};
Loading