-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Implement filtering and sorting for People Table #1775
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?
Changes from 1 commit
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,3 +1,5 @@ | ||
| import { NavLink } from 'react-router-dom'; | ||
|
|
||
| export const Navbar = () => { | ||
| return ( | ||
| <nav | ||
|
|
@@ -8,17 +10,24 @@ export const Navbar = () => { | |
| > | ||
| <div className="container"> | ||
| <div className="navbar-brand"> | ||
| <a className="navbar-item" href="#/"> | ||
| <NavLink | ||
| className={({ isActive }) => | ||
| `navbar-item${isActive ? ' has-background-grey-lighter' : ''}` | ||
| } | ||
| to="/" | ||
| end | ||
| > | ||
| Home | ||
| </a> | ||
| </NavLink> | ||
|
|
||
| <a | ||
| aria-current="page" | ||
| className="navbar-item has-background-grey-lighter" | ||
| href="#/people" | ||
| <NavLink | ||
| className={({ isActive }) => | ||
| `navbar-item${isActive ? ' has-background-grey-lighter' : ''}` | ||
| } | ||
| to="/people" | ||
| > | ||
| People | ||
|
Comment on lines
+27
to
33
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. Replace this |
||
| </a> | ||
| </NavLink> | ||
| </div> | ||
| </div> | ||
| </nav> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,29 +1,125 @@ | ||
| import { useEffect, useMemo, useState } from 'react'; | ||
| import { useSearchParams } from 'react-router-dom'; | ||
| import { PeopleFilters } from './PeopleFilters'; | ||
| import { Loader } from './Loader'; | ||
| import { PeopleTable } from './PeopleTable'; | ||
| import { getPeople } from '../api'; | ||
| import { Person } from '../types'; | ||
|
|
||
| export const PeoplePage = () => { | ||
| const [people, setPeople] = useState<Person[]>([]); | ||
| const [loading, setLoading] = useState(true); | ||
| const [error, setError] = useState(false); | ||
| const [searchParams] = useSearchParams(); | ||
|
|
||
| useEffect(() => { | ||
| setLoading(true); | ||
| setError(false); | ||
|
|
||
| getPeople() | ||
| .then(data => { | ||
| const peopleWithParents = data.map(person => ({ | ||
| ...person, | ||
| mother: data.find(p => p.name === person.motherName), | ||
| father: data.find(p => p.name === person.fatherName), | ||
| })); | ||
|
|
||
| setPeople(peopleWithParents); | ||
| }) | ||
| .catch(() => setError(true)) | ||
| .finally(() => setLoading(false)); | ||
| }, []); | ||
|
|
||
| const sex = searchParams.get('sex'); | ||
|
Comment on lines
+27
to
+33
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 "People" link should use |
||
| const query = searchParams.get('query') || ''; | ||
| const centuries = searchParams.getAll('centuries'); | ||
| const sort = searchParams.get('sort'); | ||
| const order = searchParams.get('order'); | ||
|
|
||
| const filteredPeople = useMemo(() => { | ||
| let result = [...people]; | ||
|
|
||
| if (sex) { | ||
| result = result.filter(p => p.sex === sex); | ||
| } | ||
|
|
||
| if (query) { | ||
| const q = query.toLowerCase(); | ||
|
|
||
| result = result.filter( | ||
| p => | ||
| p.name.toLowerCase().includes(q) || | ||
| (p.motherName || '').toLowerCase().includes(q) || | ||
| (p.fatherName || '').toLowerCase().includes(q), | ||
| ); | ||
| } | ||
|
|
||
| if (centuries.length > 0) { | ||
| result = result.filter(p => | ||
| centuries.includes(String(Math.ceil(p.born / 100))), | ||
| ); | ||
| } | ||
|
|
||
| if (sort) { | ||
| result.sort((a, b) => { | ||
| let cmp = 0; | ||
|
|
||
| switch (sort) { | ||
| case 'name': | ||
| case 'sex': | ||
| cmp = a[sort].localeCompare(b[sort]); | ||
| break; | ||
| case 'born': | ||
| case 'died': | ||
| cmp = a[sort] - b[sort]; | ||
| break; | ||
| } | ||
|
|
||
| return order === 'desc' ? -cmp : cmp; | ||
| }); | ||
| } | ||
|
|
||
| return result; | ||
| }, [people, sex, query, centuries, sort, order]); | ||
|
|
||
| const loaded = !loading && !error; | ||
| const hasPeople = loaded && people.length > 0; | ||
| const noPeople = loaded && people.length === 0; | ||
| const noMatches = hasPeople && filteredPeople.length === 0; | ||
|
|
||
| return ( | ||
| <> | ||
| <h1 className="title">People Page</h1> | ||
|
|
||
| <div className="block"> | ||
| <div className="columns is-desktop is-flex-direction-row-reverse"> | ||
| <div className="column is-7-tablet is-narrow-desktop"> | ||
| <PeopleFilters /> | ||
| </div> | ||
| {hasPeople && ( | ||
| <div className="column is-7-tablet is-narrow-desktop"> | ||
| <PeopleFilters /> | ||
| </div> | ||
| )} | ||
|
|
||
| <div className="column"> | ||
| <div className="box table-container"> | ||
| <Loader /> | ||
| {loading && <Loader />} | ||
|
|
||
| <p data-cy="peopleLoadingError">Something went wrong</p> | ||
| {error && ( | ||
| <p data-cy="peopleLoadingError">Something went wrong</p> | ||
| )} | ||
|
|
||
| <p data-cy="noPeopleMessage">There are no people on the server</p> | ||
| {noPeople && ( | ||
| <p data-cy="noPeopleMessage"> | ||
| There are no people on the server | ||
| </p> | ||
| )} | ||
|
|
||
| <p>There are no people matching the current search criteria</p> | ||
| {noMatches && ( | ||
| <p>There are no people matching the current search criteria</p> | ||
| )} | ||
|
|
||
| <PeopleTable /> | ||
| {hasPeople && !noMatches && ( | ||
| <PeopleTable people={filteredPeople} /> | ||
| )} | ||
| </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.
The "People" link doesn't preserve search params when clicked. The task requires keeping filters and sort params when navigating to the People page. Currently using
NavLinkwhich doesn't maintain URL parameters. Consider usingSearchLinkwith empty params to preserve current search state.