-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Implement filtering and sorting with URL search params #1801
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import { | ||
| Navigate, | ||
| Route, | ||
| HashRouter as Router, | ||
| Routes, | ||
| } from 'react-router-dom'; | ||
| import { App } from './App'; | ||
| import { HomePage } from './pages/HomePage'; | ||
| import { PeoplePage } from './pages/PeoplePage'; | ||
| import { NotFoundPage } from './pages/NotFoundPage'; | ||
|
|
||
| export const Root = () => ( | ||
| <Router> | ||
| <Routes> | ||
| <Route path="/" element={<App />}> | ||
| <Route index 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 />} /> | ||
| </Route> | ||
| </Routes> | ||
| </Router> | ||
| ); |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import { NavLink, useSearchParams } from 'react-router-dom'; | ||
| import classNames from 'classnames'; | ||
|
|
||
| export const Navbar = () => { | ||
| const getLinkClass = ({ isActive }: { isActive: boolean }) => | ||
| classNames('navbar-item', { 'has-background-grey-lighter': isActive }); | ||
|
|
||
| const [searchParams] = useSearchParams(); | ||
|
|
||
| return ( | ||
| <nav | ||
| data-cy="nav" | ||
| className="navbar is-fixed-top has-shadow" | ||
| role="navigation" | ||
| aria-label="main navigation" | ||
| > | ||
| <div className="container"> | ||
| <div className="navbar-brand"> | ||
| <NavLink to="/" className={getLinkClass}> | ||
| Home | ||
| </NavLink> | ||
|
|
||
| <NavLink | ||
| to={{ | ||
| pathname: '/people', | ||
| search: searchParams.toString(), | ||
| }} | ||
| aria-current="page" | ||
| className={getLinkClass} | ||
| > | ||
| People | ||
| </NavLink> | ||
| </div> | ||
| </div> | ||
| </nav> | ||
| ); | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './Navbar'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| import { ChangeEvent } from 'react'; | ||
| import { useSearchParams } from 'react-router-dom'; | ||
| import classNames from 'classnames'; | ||
| import { SearchLink } from '../SearchLink'; | ||
| import { getSearchWith } from '../../utils/searchHelper'; | ||
| import { CENTURIES } from '../../constants/centuries'; | ||
|
|
||
| export const PeopleFilters = () => { | ||
| const [searchParams, setSearchParams] = useSearchParams(); | ||
|
|
||
| const query = searchParams.get('query') || ''; | ||
| const sex = searchParams.get('sex') || ''; | ||
| const centuries = searchParams.getAll('centuries'); | ||
|
|
||
| const handleQueryChange = (event: ChangeEvent<HTMLInputElement>) => { | ||
| const search = getSearchWith(searchParams, { | ||
| query: event.target.value || null, | ||
| }); | ||
|
|
||
| setSearchParams(search); | ||
| }; | ||
|
|
||
| return ( | ||
| <nav className="panel"> | ||
| <p className="panel-heading">Filters</p> | ||
|
|
||
| <p className="panel-tabs" data-cy="SexFilter"> | ||
| <SearchLink | ||
| className={classNames({ 'is-active': !sex })} | ||
| params={{ sex: null }} | ||
| > | ||
| All | ||
| </SearchLink> | ||
|
|
||
| <SearchLink | ||
| className={classNames({ 'is-active': sex === 'm' })} | ||
| params={{ sex: 'm' }} | ||
| > | ||
| Male | ||
| </SearchLink> | ||
|
|
||
| <SearchLink | ||
| className={classNames({ 'is-active': sex === 'f' })} | ||
| params={{ sex: 'f' }} | ||
| > | ||
| Female | ||
| </SearchLink> | ||
| </p> | ||
|
|
||
| <div className="panel-block"> | ||
| <p className="control has-icons-left"> | ||
| <input | ||
| data-cy="NameFilter" | ||
| type="search" | ||
| className="input" | ||
| placeholder="Search" | ||
| value={query} | ||
| onChange={handleQueryChange} | ||
| /> | ||
|
|
||
| <span className="icon is-left"> | ||
| <i className="fas fa-search" aria-hidden="true" /> | ||
| </span> | ||
| </p> | ||
| </div> | ||
|
|
||
| <div className="panel-block"> | ||
| <div className="level is-flex-grow-1 is-mobile" data-cy="CenturyFilter"> | ||
| <div className="level-left"> | ||
| {CENTURIES.map(century => ( | ||
| <SearchLink | ||
| key={century} | ||
| data-cy="century" | ||
| className={classNames('button mr-1', { | ||
| 'is-info': centuries.includes(century), | ||
| })} | ||
| params={{ | ||
| centuries: centuries.includes(century) | ||
| ? centuries.filter(num => century !== num) | ||
| : [...centuries, century], | ||
| }} | ||
|
Comment on lines
+76
to
+81
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 century filter removes centuries from the array when clicked again (line 78). According to the requirements, it should use the |
||
| > | ||
| {century} | ||
| </SearchLink> | ||
| ))} | ||
| </div> | ||
|
|
||
| <div className="level-right ml-4"> | ||
| <SearchLink | ||
| data-cy="centuryALL" | ||
| className={classNames('button is-success', { | ||
| 'is-outlined': centuries.length !== 0, | ||
| })} | ||
| params={{ centuries: null }} | ||
| > | ||
| All | ||
| </SearchLink> | ||
| </div> | ||
| </div> | ||
| </div> | ||
|
|
||
| <div className="panel-block"> | ||
| <SearchLink | ||
| className="button is-link is-outlined is-fullwidth" | ||
| params={{ query: null, centuries: null, sex: null }} | ||
| > | ||
| Reset all filters | ||
| </SearchLink> | ||
| </div> | ||
| </nav> | ||
| ); | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './PeopleFilters'; |
This file was deleted.
This file was deleted.
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 NavLink always navigates to
/peoplewithout preserving search params. Per requirement #2, search params should be kept when navigating within the People page (when clicking the People link). Consider usinguseSearchParamsto append current search params to the link.