-
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
Develop #519
base: master
Are you sure you want to change the base?
Develop #519
Conversation
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 (sort) { | ||
params.set('sort', sort); | ||
} else { | ||
params.delete('sort'); | ||
} | ||
|
||
if (sort === oldSort && !oldOrder) { | ||
params.set('order', 'desc'); | ||
} else { | ||
params.delete('order'); |
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.
Consider using switch...case here instead
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.
I considered using switch...case here, but I feel the code looks too complicated with it. The thing that I should check not only the 'sort' variable, but the 'oldOrder' variable too. Still, I would appreciate it if you give me an example of how it can be done nicely.
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.
@cty2802 I would shorten the code like this:
if (sort === oldSort) {
params.delete('sort');
params.delete('order');
} else {
params.set('sort', sort);
params.delete('order');
if (oldOrder) {
params.set('order', 'desc');
}
}
but it is worth analyzing in practice whether everything will work as it should 😄
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.
Unfortunately, it doesn't work. Under such conditions, sorting has only two states - asc and start position, but there is no desc position.
I like my code, can I keep it? And we will assume that it's not a bug, it's a feature -:)
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.
@cty2802 of course, I only made assumptions based on your code, but if my approach does not work - then we leave it as you did))
One of the main rules of programming - if it works, don't touch it 😅
src/helper.tsx
Outdated
const humans: Person[] = [...people]; | ||
|
||
return humans.sort((humanA: Person, humanB: Person): number => { | ||
if (oldSort === SortBy.Name) { | ||
return oldOrder === SortOrder.Desc | ||
? (humanA.name.localeCompare(humanB.name) * -1) | ||
: humanA.name.localeCompare(humanB.name); | ||
} | ||
|
||
if (oldSort === SortBy.Sex) { |
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.
Same here
src/pages/PeoplePage.tsx
Outdated
export const PeoplePage: React.FC = () => { | ||
const [people, setPeople] = useState<Person[]>([]); | ||
const [isLoading, setIsLoading] = useState(true); | ||
const [errorMassage, setErrorMassage] = useState(''); |
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.
const [errorMassage, setErrorMassage] = useState(''); | |
const [errorMessage, setErrorMessage] = useState(''); |
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.
const setIconClass = (columnName: SortBy): string => { | ||
if (columnName === oldSort && !oldOrder) { | ||
return 'fas fa-sort-up'; | ||
} | ||
|
||
if (columnName === oldSort && oldOrder) { | ||
return 'fas fa-sort-down'; | ||
} | ||
|
||
return 'fas fa-sort'; | ||
}; |
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.
Use the classnames
library for this logic
}} | ||
> | ||
<span className="icon"> | ||
<i className={setIconClass(SortBy.Name)} /> |
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.
classnames
issue
if (sort) { | ||
params.set('sort', sort); | ||
} else { | ||
params.delete('sort'); | ||
} | ||
|
||
if (sort === oldSort && !oldOrder) { | ||
params.set('order', 'desc'); | ||
} else { | ||
params.delete('order'); |
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.
@cty2802 I would shorten the code like this:
if (sort === oldSort) {
params.delete('sort');
params.delete('order');
} else {
params.set('sort', sort);
params.delete('order');
if (oldOrder) {
params.set('order', 'desc');
}
}
but it is worth analyzing in practice whether everything will work as it should 😄
src/pages/PeoplePage.tsx
Outdated
return !centuries?.length | ||
? true | ||
: centuries.includes(Math.floor(person.born / 100).toString()); |
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.
return !centuries?.length | |
? true | |
: centuries.includes(Math.floor(person.born / 100).toString()); | |
return !centuries?.length || centuries.includes(Math.floor(person.born / 100).toString()); |
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.
But avoid usign "magic values" (100)
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.
Well done 👍
const setIconClass = (columnName: SortBy): string => { | ||
return classNames('fas', | ||
{ 'fa-sort-up': columnName === oldSort && !oldOrder }, | ||
{ 'fa-sort-down': columnName === oldSort && oldOrder }, | ||
{ 'fa-sort': columnName }); | ||
}; |
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.
It is better to define this function outside the component so that it is not created every time a new render is performed.
const oldSort = searchParams.get('sort'); | ||
const oldOrder = searchParams.get('order'); | ||
|
||
const setIconClass = (columnName: SortBy): string => { |
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.
const setIconClass = (columnName: SortBy): string => { | |
const getIconClass = (columnName: SortBy): string => { |
if (sort) { | ||
params.set('sort', sort); | ||
} else { | ||
params.delete('sort'); | ||
} | ||
|
||
if (sort === oldSort && !oldOrder) { | ||
params.set('order', 'desc'); | ||
} else { | ||
params.delete('order'); |
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.
@cty2802 of course, I only made assumptions based on your code, but if my approach does not work - then we leave it as you did))
One of the main rules of programming - if it works, don't touch it 😅
…outside the component
DEMO LINK