-
Notifications
You must be signed in to change notification settings - Fork 1.9k
People Table app with routing, filters and sorting #1817
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,7 +1,11 @@ | ||
| import { Routes, Route, Navigate } from 'react-router-dom'; | ||
|
|
||
| import { PeoplePage } from './components/PeoplePage'; | ||
| import { Navbar } from './components/Navbar'; | ||
|
|
||
| import './App.scss'; | ||
| import { HomePage } from './pages/HomePage'; | ||
| import { NotFoundPage } from './pages/NotFoundPage'; | ||
|
|
||
| export const App = () => { | ||
| return ( | ||
|
|
@@ -10,9 +14,21 @@ export const App = () => { | |
|
|
||
| <div className="section"> | ||
| <div className="container"> | ||
| <h1 className="title">Home Page</h1> | ||
| <h1 className="title">Page not found</h1> | ||
| <PeoplePage /> | ||
| <Routes> | ||
| <Route path="/" element={<HomePage />} /> | ||
|
|
||
| <Route path="/home" element={<Navigate to="/" replace />} /> | ||
|
|
||
| <Route path="/people"> | ||
| <Route index element={<PeoplePage />} /> | ||
| <Route path=":slug" element={<PeoplePage />} /> | ||
| </Route> | ||
|
|
||
| <Route path="*" element={<NotFoundPage />} /> | ||
| </Routes> | ||
| {/* <h1 className="title">Home Page</h1> | ||
| <h1 className="title">Page not found</h1> */} | ||
| {/* <PeoplePage /> */} | ||
| </div> | ||
|
Comment on lines
+27
to
32
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 |
||
| </div> | ||
| </div> | ||
|
|
||
| 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,27 @@ export const Navbar = () => { | |
| > | ||
| <div className="container"> | ||
| <div className="navbar-brand"> | ||
| <a className="navbar-item" href="#/"> | ||
| <NavLink | ||
| to="/" | ||
| className={({ isActive }) => | ||
| isActive | ||
| ? 'navbar-item has-background-grey-lighter' | ||
| : 'navbar-item' | ||
| } | ||
| > | ||
| Home | ||
| </a> | ||
| </NavLink> | ||
|
|
||
|
Comment on lines
+13
to
23
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 SexFilter links (lines 14-23) use plain |
||
| <a | ||
| aria-current="page" | ||
| className="navbar-item has-background-grey-lighter" | ||
| href="#/people" | ||
| <NavLink | ||
| to="/people" | ||
| className={({ isActive }) => | ||
| isActive | ||
| ? 'navbar-item has-background-grey-lighter' | ||
| : 'navbar-item' | ||
| } | ||
| > | ||
| People | ||
|
Comment on lines
+27
to
32
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 |
||
| </a> | ||
| </NavLink> | ||
| </div> | ||
| </div> | ||
| </nav> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,103 @@ | ||
| import { PeopleFilters } from './PeopleFilters'; | ||
| import { Loader } from './Loader'; | ||
| import { PeopleTable } from './PeopleTable'; | ||
| import { useParams, useSearchParams } from 'react-router-dom'; | ||
| import { useEffect, useMemo, useState } from 'react'; | ||
| import { Person } from '../types'; | ||
| import { getPeople } from '../api'; | ||
|
|
||
| export const PeoplePage = () => { | ||
| const { slug } = useParams(); | ||
|
|
||
| const [searchParams] = useSearchParams(); | ||
|
|
||
| const [people, setPeople] = useState<Person[]>([]); | ||
| const [loading, setLoading] = useState(true); | ||
| const [error, setError] = useState(false); | ||
|
|
||
| useEffect(() => { | ||
| getPeople() | ||
| .then(setPeople) | ||
| .catch(() => setError(true)) | ||
| .finally(() => setLoading(false)); | ||
| }, []); | ||
|
|
||
| const visiblePeople = useMemo(() => { | ||
| let result = [...people]; | ||
|
|
||
| const query = searchParams.get('query')?.toLowerCase() || ''; | ||
|
|
||
| const centuries = searchParams.getAll('centuries'); | ||
|
|
||
| const sort = searchParams.get('sort'); | ||
|
Comment on lines
+27
to
+32
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 |
||
| const order = searchParams.get('order'); | ||
| const sex = searchParams.get('sex'); | ||
|
|
||
| if (sex) { | ||
| result = result.filter(person => person.sex === sex); | ||
| } | ||
|
|
||
| if (query) { | ||
| result = result.filter(person => | ||
| [person.name, person.motherName, person.fatherName] | ||
| .filter(Boolean) | ||
| .some(value => value!.toLowerCase().includes(query)), | ||
| ); | ||
| } | ||
|
|
||
| if (centuries.length) { | ||
| result = result.filter(person => { | ||
| const century = Math.ceil(person.born / 100); | ||
|
|
||
| return centuries.includes(String(century)); | ||
| }); | ||
| } | ||
|
|
||
| if (sort) { | ||
| result.sort((a, b) => { | ||
| let res = 0; | ||
|
|
||
| switch (sort) { | ||
| case 'name': | ||
| res = a.name.localeCompare(b.name); | ||
| break; | ||
|
|
||
| case 'sex': | ||
| res = a.sex.localeCompare(b.sex); | ||
| break; | ||
|
|
||
| case 'born': | ||
|
Comment on lines
+64
to
+69
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 'All' century button uses static href that resets all URL params. According to the requirements, this should use SearchLink with |
||
| res = a.born - b.born; | ||
| break; | ||
|
|
||
| case 'died': | ||
| res = a.died - b.died; | ||
| break; | ||
| } | ||
|
|
||
| return order === 'desc' ? -res : res; | ||
|
Comment on lines
+76
to
+78
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 |
||
| }); | ||
|
|
||
| if (order === 'desc') { | ||
| result.reverse(); | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| }, [people, searchParams]); | ||
|
|
||
| if (loading) { | ||
| return <Loader />; | ||
| } | ||
|
|
||
| if (error) { | ||
| return ( | ||
| <p data-cy="peopleLoadingError" className="has-text-danger"> | ||
| Something went wrong | ||
| </p> | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
|
Comment on lines
+100
to
101
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 "Reset all filters" link doesn't clear sorting params. Add |
||
| <> | ||
| <h1 className="title">People Page</h1> | ||
|
|
@@ -15,15 +110,24 @@ export const PeoplePage = () => { | |
|
|
||
| <div className="column"> | ||
| <div className="box table-container"> | ||
| <Loader /> | ||
| {people.length === 0 ? ( | ||
| <p data-cy="noPeopleMessage"> | ||
| There are no people on the server | ||
| </p> | ||
| ) : !visiblePeople.length ? ( | ||
| <p>There are no people matching the current search criteria</p> | ||
| ) : ( | ||
| <PeopleTable people={visiblePeople} selectedSlug={slug} /> | ||
| )} | ||
| {/* <Loader /> | ||
|
|
||
| <p data-cy="peopleLoadingError">Something went wrong</p> | ||
|
|
||
| <p data-cy="noPeopleMessage">There are no people on the server</p> | ||
| <p data-cy="noPeopleMessage">There are no people on the server</p> */} | ||
|
|
||
| <p>There are no people matching the current search criteria</p> | ||
| {/* <p>There are no people matching the current search criteria</p> */} | ||
|
|
||
| <PeopleTable /> | ||
| {/* <PeopleTable /> */} | ||
| </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.
SexFilter links use hardcoded
hrefattributes instead ofSearchLink. This means clicking these links won't preserve other search params (like query or centuries). UseSearchLinkwithparams={{ sex: 'm' }}orparams={{ sex: null }}for 'All'.