-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
first solution #528
base: master
Are you sure you want to change the base?
first solution #528
Conversation
src/components/PeoplePage.tsx
Outdated
const [loadingError, setLoadingError] = useState(false); | ||
|
||
const fetchData = () => { | ||
fetch('https://mate-academy.github.io/react_people-table/api/people.json') |
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.
please use getPeople
function provided
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.
Organize your code to minimize duplication by using mapping where indicated.
<SearchLink | ||
className={cn({ 'is-active': selectedGender === null })} | ||
params={{ sex: null }} | ||
> | ||
All | ||
|
||
</SearchLink> | ||
<SearchLink | ||
className={cn({ 'is-active': selectedGender === 'm' })} | ||
params={{ sex: 'm' }} | ||
> | ||
Male | ||
|
||
</SearchLink> | ||
<SearchLink | ||
className={cn({ 'is-active': selectedGender === 'f' })} | ||
params={{ sex: 'f' }} | ||
> | ||
Female | ||
|
||
</SearchLink> |
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.
- Try to avoid repetition of the
SearchLink
component - you can usemap
method to generate links dynamically. Same forSearchLink
with centuries - you can create array with possible centuries. - To make code more descriptive, it's a good practice to create an enum or variables for
'm'
,'f'
, andnull
. - delete blank lines to improve code readability.
}; | ||
|
||
const handleQueryChange = (newValue: string) => { | ||
if (newValue === '') { |
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.
if (newValue === '') { | |
if (!newValue) { |
|
||
<p>There are no people matching the current search criteria</p> | ||
{people?.length === 0 && ( |
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.
{people?.length === 0 && ( | |
{!people?.length && ( |
</table> | ||
return ( | ||
<> | ||
{filteredPeople.length === 0 |
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.
{filteredPeople.length === 0 | |
{!filteredPeople.length |
switch (sort) { | ||
case 'born': | ||
if (sortDirection === 'desc') { | ||
filteredPeople.sort((a, b) => b.born - a.born); | ||
} else { | ||
filteredPeople.sort((a, b) => a.born - b.born); | ||
} | ||
|
||
break; | ||
case 'died': | ||
if (sortDirection === 'desc') { | ||
filteredPeople.sort((a, b) => b.died - a.died); | ||
} else { | ||
filteredPeople.sort((a, b) => a.died - b.died); | ||
} | ||
|
||
break; | ||
case 'name': | ||
if (sortDirection === 'desc') { | ||
filteredPeople.sort((a, b) => b.name.localeCompare(a.name)); | ||
} else { | ||
filteredPeople.sort((a, b) => a.name.localeCompare(b.name)); | ||
} | ||
|
||
break; | ||
case 'sex': | ||
if (sortDirection === 'desc') { | ||
filteredPeople.sort((a, b) => b.sex.localeCompare(a.sex)); | ||
} else { | ||
filteredPeople.sort((a, b) => a.sex.localeCompare(b.sex)); | ||
} | ||
|
||
break; | ||
case null: | ||
default: | ||
} |
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.
- In order to simplify the code, you can consider reorganizing the switch statement to group similar sorting cases together (
name
andsex
) (born
anddied
) - delete blank lines before
break
<th> | ||
<span className="is-flex is-flex-wrap-nowrap"> | ||
Name | ||
<a onClick={() => setSortParams('name')}> | ||
<span className="icon"> | ||
<i className={cn({ | ||
'fas fa-sort': sortBy !== 'name', | ||
'fas fa-sort-up': sortBy === 'name' | ||
&& sortDirection !== 'desc', | ||
'fas fa-sort-down': sortBy === 'name' | ||
&& sortDirection === 'desc', | ||
})} | ||
/> | ||
</span> | ||
</a> | ||
</span> | ||
</th> | ||
|
||
<th> | ||
<span className="is-flex is-flex-wrap-nowrap"> | ||
Sex | ||
<a onClick={() => setSortParams('sex')}> | ||
<span className="icon"> | ||
<i className={cn({ | ||
'fas fa-sort': sortBy !== 'sex', | ||
'fas fa-sort-up': sortBy === 'sex' | ||
&& sortDirection !== 'desc', | ||
'fas fa-sort-down': sortBy === 'sex' | ||
&& sortDirection === 'desc', | ||
})} | ||
/> | ||
</span> | ||
</a> | ||
</span> | ||
</th> | ||
|
||
<th> | ||
<span className="is-flex is-flex-wrap-nowrap"> | ||
Born | ||
<a onClick={() => setSortParams('born')}> | ||
<span className="icon"> | ||
<i className={cn({ | ||
'fas fa-sort': sortBy !== 'born', | ||
'fas fa-sort-up': sortBy === 'born' | ||
&& sortDirection !== 'desc', | ||
'fas fa-sort-down': sortBy === 'born' | ||
&& sortDirection === 'desc', | ||
})} | ||
/> | ||
</span> | ||
</a> | ||
</span> | ||
</th> | ||
|
||
<th> | ||
<span className="is-flex is-flex-wrap-nowrap"> | ||
Died | ||
<a onClick={() => setSortParams('died')}> | ||
<span className="icon"> | ||
<i className={cn({ | ||
'fas fa-sort': sortBy !== 'died', | ||
'fas fa-sort-up': sortBy === 'died' | ||
&& sortDirection !== 'desc', | ||
'fas fa-sort-down': sortBy === 'died' | ||
&& sortDirection === 'desc', | ||
})} | ||
/> | ||
</span> | ||
</a> | ||
</span> | ||
</th> |
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.
You can utilize mapping for th
to iterate through possible sortings
{filteredPeople.map((person) => ( | ||
|
||
<tr |
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.
{filteredPeople.map((person) => ( | |
<tr | |
{filteredPeople.map((person) => ( | |
<tr |
: person.motherName || '-'} | ||
|
||
</td> |
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.
: person.motherName || '-'} | |
</td> | |
: person.motherName || '-'} | |
</td> |
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.
Waiting for any upadtes @vensecue 👀
DEMO LINK