-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Pronina-People-table-advanced #1831
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 all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import { PeoplePage } from './components/PeoplePage'; | ||
| import { Routes, Route, Navigate } from 'react-router-dom'; | ||
| import { Navbar } from './components/Navbar'; | ||
| import { PeoplePage } from './components/PeoplePage'; | ||
|
|
||
| import './App.scss'; | ||
|
|
||
|
|
@@ -10,9 +11,20 @@ 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={<h1 className="title">Home Page</h1>} /> | ||
|
|
||
| <Route path="/people" element={<PeoplePage />} /> | ||
|
|
||
| <Route path="/people/:slug" element={<PeoplePage />} /> | ||
|
|
||
| <Route path="/home" element={<Navigate to="/" replace />} /> | ||
|
|
||
| <Route | ||
| path="*" | ||
| element={<h1 className="title">Page not found</h1>} | ||
| /> | ||
|
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 nav link doesn’t preserve current search params, so navigating back to |
||
| </Routes> | ||
| </div> | ||
| </div> | ||
| </div> | ||
|
|
||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,9 @@ | ||
| import { NavLink, useLocation } from 'react-router-dom'; | ||
| import classNames from 'classnames'; | ||
|
|
||
| export const Navbar = () => { | ||
| const location = useLocation(); | ||
|
|
||
| return ( | ||
| <nav | ||
| data-cy="nav" | ||
|
|
@@ -8,17 +13,28 @@ export const Navbar = () => { | |
| > | ||
| <div className="container"> | ||
| <div className="navbar-brand"> | ||
| <a className="navbar-item" href="#/"> | ||
| <NavLink | ||
| to="/" | ||
| end | ||
| className={({ isActive }) => | ||
| classNames('navbar-item', { | ||
| 'has-background-grey-lighter': isActive, | ||
| }) | ||
| } | ||
| > | ||
| Home | ||
| </a> | ||
| </NavLink> | ||
|
|
||
| <a | ||
| aria-current="page" | ||
| className="navbar-item has-background-grey-lighter" | ||
| href="#/people" | ||
| <NavLink | ||
| to={{ pathname: '/people', search: location.search }} | ||
| className={({ isActive }) => | ||
| classNames('navbar-item', { | ||
| 'has-background-grey-lighter': isActive, | ||
| }) | ||
| } | ||
| > | ||
| People | ||
| </a> | ||
| </NavLink> | ||
| </div> | ||
|
Comment on lines
+37
to
38
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.
|
||
| </div> | ||
| </nav> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,29 +1,118 @@ | ||
| import { useEffect, useMemo, useState } from 'react'; | ||
| import { useSearchParams } from 'react-router-dom'; | ||
| import { Person } from '../types/Person'; | ||
| import { getPeople } from '../api'; | ||
| import { PeopleFilters } from './PeopleFilters'; | ||
| import { Loader } from './Loader'; | ||
| import { Loader } from './Loader/Loader'; | ||
| import { PeopleTable } from './PeopleTable'; | ||
|
|
||
| export const PeoplePage = () => { | ||
| const [people, setPeople] = useState<Person[]>([]); | ||
| const [loading, setLoading] = useState(false); | ||
| const [error, setError] = useState(false); | ||
| const [searchParams] = useSearchParams(); | ||
|
|
||
| const query = searchParams.get('query') || ''; | ||
| const sex = searchParams.get('sex') || ''; | ||
| const centuries = searchParams.getAll('centuries'); | ||
| const sort = searchParams.get('sort') || ''; | ||
| const order = searchParams.get('order') || ''; | ||
|
|
||
| useEffect(() => { | ||
| setLoading(true); | ||
| getPeople() | ||
| .then(data => { | ||
| const peopleWithParents = data.map(person => ({ | ||
|
Comment on lines
+22
to
+25
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 current routing setup correctly renders |
||
| ...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)); | ||
| }, []); | ||
|
Comment on lines
+34
to
+35
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. This |
||
|
|
||
| const visiblePeople = useMemo(() => { | ||
| let filtered = [...people]; | ||
|
|
||
| if (query) { | ||
| const q = query.toLowerCase(); | ||
|
|
||
| filtered = filtered.filter( | ||
| p => | ||
| p.name.toLowerCase().includes(q) || | ||
| p.motherName?.toLowerCase().includes(q) || | ||
| p.fatherName?.toLowerCase().includes(q), | ||
| ); | ||
| } | ||
|
|
||
| if (sex) { | ||
| filtered = filtered.filter(p => p.sex === sex); | ||
| } | ||
|
|
||
| if (centuries.length) { | ||
| filtered = filtered.filter(p => | ||
| centuries.includes(String(Math.ceil(p.born / 100))), | ||
| ); | ||
| } | ||
|
|
||
| if (sort) { | ||
| filtered.sort((a, b) => { | ||
| let result = 0; | ||
|
|
||
| switch (sort) { | ||
| case 'name': | ||
| case 'sex': | ||
| result = a[sort].localeCompare(b[sort]); | ||
| break; | ||
| case 'born': | ||
| case 'died': | ||
| result = a[sort] - b[sort]; | ||
| break; | ||
| } | ||
|
|
||
| return order === 'desc' ? -result : result; | ||
| }); | ||
| } | ||
|
|
||
| return filtered; | ||
| }, [people, query, sex, centuries, sort, order]); | ||
|
|
||
| const dataLoaded = !loading && !error && people.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 /> | ||
| {dataLoaded && <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> | ||
| {!loading && !error && people.length === 0 && ( | ||
| <p data-cy="noPeopleMessage"> | ||
| There are no people on the server | ||
| </p> | ||
| )} | ||
|
|
||
| <p>There are no people matching the current search criteria</p> | ||
| {dataLoaded && visiblePeople.length === 0 && ( | ||
| <p>There are no people matching the current search criteria</p> | ||
| )} | ||
|
|
||
| <PeopleTable /> | ||
| {dataLoaded && visiblePeople.length > 0 && ( | ||
| <PeopleTable people={visiblePeople} /> | ||
| )} | ||
| </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.
Current routing works, but nothing here ensures that search params are preserved when navigating within
/people(checklist #4/#16). When you later update links (e.g. People link in Navbar and person links in the table) to keep query/sort/centuries, make sure the router structure still supports that behavior.