Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solution #758

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

solution #758

wants to merge 3 commits into from

Conversation

hma-3
Copy link

@hma-3 hma-3 commented Oct 10, 2024

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

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

Great job! 🎉 There are no critical issues in your code. However, please consider the provided feedback for future improvements. Keep coding! 😊

✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨

Comment on lines 39 to 43
const handleInputChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setQuery(event.target.value.trimStart());
applyQuery(event.target.value);
onSelect(null);
};

Choose a reason for hiding this comment

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

The handleInputChange function is not following the recommended naming conventions for event handlers in React. According to the naming conventions, the function should be named onInputChange.

Comment on lines 35 to 37
const handleInputFocus = () => {
setIsActive(true);
};

Choose a reason for hiding this comment

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

The handleInputFocus function is not following the recommended naming conventions for event handlers in React. According to the naming conventions, the function should be named onInputFocus.

Comment on lines 45 to 49
const handleSelect = (person: Person) => {
onSelect(person);
setQuery(person.name);
setIsActive(false);
};

Choose a reason for hiding this comment

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

The handleSelect function is not following the recommended naming conventions for event handlers in React. According to the naming conventions, the function should be named onSelectPerson.

[appliedQuery],
);

const visiblePeople = selectedPerson ? peopleFromServer : filteredPeople;

Choose a reason for hiding this comment

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

The key for list items is generated during render which is not a good practice. As per the article, it's better to generate unique keys for list items when the list is created.


const handleInputChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setQuery(event.target.value.trimStart());
applyQuery(event.target.value);

Choose a reason for hiding this comment

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

You need to make sure that filter won't be called if user entered spaces only. In the current implementation, if the user only enters spaces, the filter function will still be called. You should trim the input value before applying the filter.

'has-background-grey-lighter': selectedPerson === person,
})}
data-cy="suggestion-item"
onClick={() => onSelect(person)}

Choose a reason for hiding this comment

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

This is a performance issue. You are creating a new function every time the component renders. This is not recommended. Instead, create a function inside the component that accepts a person as an argument and call this function in your onClick handler. This way, a new function is not created on every render.


return (
<div
key={person.slug}

Choose a reason for hiding this comment

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

Key generation on render is not recommended. This is because keys should be stable between multiple renders for performance reasons. You can read more about it here.

'has-background-grey-lighter': selectedPerson === person,
})}
data-cy="suggestion-item"
onClick={() => onSelect(person)}

Choose a reason for hiding this comment

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

The naming convention for event handlers in React suggests that the name should start with 'handle' and end with the type of event you are handling. In this case, a better name would be 'handleSelectPerson'. You can read more about the naming conventions here.

) {
let filteredPeople = [...people];

if (appliedQuery) {

Choose a reason for hiding this comment

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

You should check if appliedQuery is not only truthy, but also not just spaces. Currently, if a user enters spaces only, the filter function will be called unnecessarily. Consider using trim() method before the check to ensure that spaces only won't pass the condition.

let filteredPeople = [...people];

if (appliedQuery) {
const normalizedQuery = appliedQuery.toLocaleLowerCase().trim();

Choose a reason for hiding this comment

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

The toLocaleLowerCase() method is a good approach to make the search case-insensitive. However, please note that for some languages and locales, this method might not work as expected. If you plan to support multiple languages, you might need to use a library for more accurate case conversion.

Copy link

@VolodymyrKirichenko VolodymyrKirichenko left a comment

Choose a reason for hiding this comment

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

Great job 👍
But let's make ur code more better 🥹


type Props = {
selectedPerson: Person | null;
onSelect: React.Dispatch<React.SetStateAction<Person | null>>;

Choose a reason for hiding this comment

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

I think - this will be better and more readable, right?

Suggested change
onSelect: React.Dispatch<React.SetStateAction<Person | null>>;
onSelect: (person: Person | null) => void;

Comment on lines +28 to +31
const filteredPeople = useMemo(
() => filterPeople(peopleFromServer, { appliedQuery }),
[appliedQuery],
);

Choose a reason for hiding this comment

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

change syntax and use it in the future(more readable)

Suggested change
const filteredPeople = useMemo(
() => filterPeople(peopleFromServer, { appliedQuery }),
[appliedQuery],
);
const filteredPeople = useMemo(() => (
filterPeople(peopleFromServer, { appliedQuery })
), [appliedQuery]);

Copy link
Author

Choose a reason for hiding this comment

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

I'll remember it, thanks, but here prettier changes it back

[appliedQuery],
);

const visiblePeople = selectedPerson ? peopleFromServer : filteredPeople;

Choose a reason for hiding this comment

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

useMemo?

setIsActive(true);
};

const onInputChange = (event: React.ChangeEvent<HTMLInputElement>) => {

Choose a reason for hiding this comment

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

useCallback?

onSelect(null);
};

const onSelectPerson = (person: Person) => {

Choose a reason for hiding this comment

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

the same

@import '../../variables/colors';

.dropdown-item:hover {
background-color: $dropdown-backgrount-hover-color;

Choose a reason for hiding this comment

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

syntax mistake

Suggested change
background-color: $dropdown-backgrount-hover-color;
background-color: $dropdown-background-hover-color;

<DropdownMenu
people={visiblePeople}
selectedPerson={selectedPerson}
handleSelectPerson={onSelectPerson}

Choose a reason for hiding this comment

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

should be the other way round)

Suggested change
handleSelectPerson={onSelectPerson}
onSelectPerson={handleSelectPerson}

Copy link
Author

Choose a reason for hiding this comment

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

this was a correction from AI Buddy)

Comment on lines 7 to 16
let filteredPeople = [...people];
const normalizedQuery = appliedQuery.toLocaleLowerCase().trim();

if (normalizedQuery.length) {
filteredPeople = filteredPeople.filter(person =>
person.name.toLocaleLowerCase().includes(normalizedQuery),
);
}

return filteredPeople;

Choose a reason for hiding this comment

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

method filter returns a new array. u don't need the let variable here. Avoid creating let variables

Suggested change
let filteredPeople = [...people];
const normalizedQuery = appliedQuery.toLocaleLowerCase().trim();
if (normalizedQuery.length) {
filteredPeople = filteredPeople.filter(person =>
person.name.toLocaleLowerCase().includes(normalizedQuery),
);
}
return filteredPeople;
const normalizedQuery = appliedQuery.toLocaleLowerCase().trim();
return normalizedQuery.length
? people.filter((person) => (
person.name.toLocaleLowerCase().includes(normalizedQuery)
))
: [...people];

Copy link
Author

Choose a reason for hiding this comment

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

changed, but prettier removes brackets around the filter callback value

Copy link

@igorkruz igorkruz left a comment

Choose a reason for hiding this comment

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

Well done 🔥

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants