-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Solved task #1127
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
base: master
Are you sure you want to change the base?
Solved task #1127
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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()), | ||
| ), | ||
| [preparedQuery], | ||
| ); | ||
|
|
||
| const applyQuery = useCallback(debounce(setPreparedQuery, 300), []); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The task requires the debounce There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The debounce delay is hardcoded. According to the requirements, the |
||
| const loseQuery = useCallback(debounce(setBlured, 1000), []); | ||
|
|
||
| const handleQueryChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
| setQuery(event.target.value); | ||
| applyQuery(event.target.value); | ||
| setSelected(null); | ||
| }; | ||
|
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"> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The task description explicitly states, "Don't remove the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The task requires using |
||
| {`${name} (${born} - ${died})`} | ||
| {selected ? `${name} (${born} - ${died})` : `No selected person`} | ||
| </h1> | ||
|
|
||
| <div className="dropdown is-active"> | ||
|
|
@@ -19,55 +50,36 @@ | |
| placeholder="Enter a part of the name" | ||
| className="input" | ||
| data-cy="search-input" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The task requires using |
||
| value={query} | ||
| onChange={handleQueryChange} | ||
| onFocus={() => setBlured(true)} | ||
| onBlur={() => loseQuery(false)} | ||
|
Comment on lines
+55
to
+56
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| /> | ||
| </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" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The task requires using |
||
| > | ||
| <p className="has-text-danger">No matching suggestions</p> | ||
| </div> | ||
| )} | ||
| </main> | ||
| </div> | ||
| ); | ||
|
|
||
| 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"> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The task requirements specify using |
||
| <div className="dropdown-content"> | ||
| {filteredPeople.map((person: Person) => ( | ||
| <div | ||
| className="dropdown-item" | ||
| data-cy="suggestion-item" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As per the task requirements, this should be a |
||
| key={person.name} | ||
| onClick={() => { | ||
| onSelected(person); | ||
|
AndreyKagaml marked this conversation as resolved.
|
||
| }} | ||
| > | ||
| <p className="has-text-link">{person.name}</p> | ||
| </div> | ||
| ))} | ||
| </div> | ||
| </div> | ||
| ); | ||
| }; | ||
There was a problem hiding this comment.
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.