-
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
implement filtering and sorting #507
base: master
Are you sure you want to change the base?
Changes from 2 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 |
---|---|---|
@@ -1,20 +1,14 @@ | ||
import { PeoplePage } from './components/PeoplePage'; | ||
import { Navbar } from './components/Navbar'; | ||
|
||
import './App.scss'; | ||
import { Outlet } from 'react-router-dom'; | ||
import { Navbar } from './components/Navbar'; | ||
|
||
export const App = () => { | ||
return ( | ||
<div data-cy="app"> | ||
<Navbar /> | ||
|
||
<div className="section"> | ||
<div className="container"> | ||
<h1 className="title">Home Page</h1> | ||
<h1 className="title">Page not found</h1> | ||
<PeoplePage /> | ||
</div> | ||
export const App = () => ( | ||
<div data-cy="app"> | ||
<Navbar /> | ||
<main className="section"> | ||
<div className="container"> | ||
<Outlet /> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
</main> | ||
</div> | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { | ||
Navigate, | ||
Route, | ||
HashRouter as Router, | ||
Routes, | ||
} from 'react-router-dom'; | ||
import { App } from './App'; | ||
import { PageNotFound } from './components/PageNotFound'; | ||
import { PeoplePage } from './components/PeoplePage'; | ||
import { HomePage } from './components/HomePage'; | ||
|
||
export const Root = () => ( | ||
<Router> | ||
<Routes> | ||
<Route path="/" element={<App />}> | ||
<Route index element={<HomePage />} /> | ||
<Route path="people"> | ||
<Route path=":selectedPerson?" element={<PeoplePage />} /> | ||
</Route> | ||
<Route path="*" element={<PageNotFound />} /> | ||
<Route path="/home" element={<Navigate to="/" replace />} /> | ||
</Route> | ||
</Routes> | ||
</Router> | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const HomePage: React.FC = () => ( | ||
<h1 className="title">Home Page</h1> | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const PageNotFound: React.FC = () => ( | ||
<h1 className="title">Page not found</h1> | ||
); |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,54 @@ | ||||||
import React from 'react'; | ||||||
import { useParams } from 'react-router-dom'; | ||||||
import classNames from 'classnames'; | ||||||
import { Person } from '../types'; | ||||||
import { PersonLink } from './PersonLink'; | ||||||
import { NOT_SET_VALUE } from '../utils/constants'; | ||||||
|
||||||
type Props = { | ||||||
person: Person, | ||||||
}; | ||||||
|
||||||
export const People: React.FC<Props> = ({ person }) => { | ||||||
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.
Suggested change
|
||||||
const { selectedPerson = '' } = useParams(); | ||||||
const { | ||||||
sex, | ||||||
born, | ||||||
died, | ||||||
mother, | ||||||
father, | ||||||
motherName, | ||||||
fatherName, | ||||||
slug, | ||||||
} = person; | ||||||
|
||||||
return ( | ||||||
<tr | ||||||
data-cy="person" | ||||||
className={classNames({ | ||||||
'has-background-warning': selectedPerson === slug, | ||||||
})} | ||||||
> | ||||||
<td> | ||||||
<PersonLink person={person} /> | ||||||
</td> | ||||||
<td>{sex}</td> | ||||||
<td>{born}</td> | ||||||
<td>{died}</td> | ||||||
<td> | ||||||
{mother ? ( | ||||||
<PersonLink person={mother} /> | ||||||
) : ( | ||||||
motherName || NOT_SET_VALUE | ||||||
)} | ||||||
</td> | ||||||
<td> | ||||||
{father ? ( | ||||||
<PersonLink person={father} /> | ||||||
) : ( | ||||||
fatherName || NOT_SET_VALUE | ||||||
)} | ||||||
</td> | ||||||
</tr> | ||||||
); | ||||||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,146 @@ | ||
export const PeopleFilters = () => { | ||
import classNames from 'classnames'; | ||
import React from 'react'; | ||
import { useSearchParams } from 'react-router-dom'; | ||
import { SearchLink } from './SearchLink'; | ||
import { CENTURIES, SEX_FILTER_LINKS } from '../utils/constants'; | ||
|
||
type Props = { | ||
query: string, | ||
sex: string, | ||
centuries: string[], | ||
}; | ||
|
||
export const PeopleFilters: React.FC<Props> = ({ | ||
query, | ||
sex, | ||
centuries, | ||
}) => { | ||
const [searchParams, setSearchParams] = useSearchParams(); | ||
|
||
function handleChangeQuery(event: React.ChangeEvent<HTMLInputElement>) { | ||
const params = new URLSearchParams(searchParams); | ||
|
||
params.set('query', event.target.value); | ||
setSearchParams(params); | ||
Comment on lines
+21
to
+24
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. You can get previous params in |
||
} | ||
|
||
function handleSexSelected(filterPath: string | null) { | ||
const params = new URLSearchParams(searchParams); | ||
|
||
params.set('sex', filterPath || ''); | ||
setSearchParams(params); | ||
} | ||
|
||
function toggleCenturies(cntr: string) { | ||
const params = new URLSearchParams(searchParams); | ||
|
||
const newCenturies = centuries.includes(cntr) | ||
? centuries.filter(prevCentury => prevCentury !== cntr) | ||
: [...centuries, cntr]; | ||
|
||
params.delete('centuries'); | ||
newCenturies.forEach(cent => params.append('centuries', cent)); | ||
setSearchParams(params); | ||
} | ||
|
||
function handleClearAllCenturies() { | ||
const params = new URLSearchParams(searchParams); | ||
|
||
params.delete('centuries'); | ||
setSearchParams(params); | ||
} | ||
|
||
function handleResetAllFilters() { | ||
const params = new URLSearchParams(searchParams); | ||
|
||
params.delete('sex'); | ||
params.delete('query'); | ||
setSearchParams(params); | ||
} | ||
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. You don't need these functions. You have |
||
|
||
return ( | ||
<nav className="panel"> | ||
<p className="panel-heading">Filters</p> | ||
|
||
<p className="panel-tabs" data-cy="SexFilter"> | ||
<a className="is-active" href="#/people">All</a> | ||
<a className="" href="#/people?sex=m">Male</a> | ||
<a className="" href="#/people?sex=f">Female</a> | ||
<SearchLink | ||
className={classNames({ | ||
'is-active': sex === SEX_FILTER_LINKS.All, | ||
})} | ||
params={{ sex: null }} | ||
onClick={() => handleSexSelected(null)} | ||
> | ||
All | ||
</SearchLink> | ||
<SearchLink | ||
className={classNames({ | ||
'is-active': sex === SEX_FILTER_LINKS.Male, | ||
})} | ||
params={{ sex: SEX_FILTER_LINKS.Male }} | ||
onClick={() => handleSexSelected(SEX_FILTER_LINKS.Male)} | ||
> | ||
Male | ||
</SearchLink> | ||
<SearchLink | ||
className={classNames({ | ||
'is-active': sex === SEX_FILTER_LINKS.Female, | ||
})} | ||
params={{ sex: SEX_FILTER_LINKS.Female }} | ||
onClick={() => handleSexSelected(SEX_FILTER_LINKS.Female)} | ||
> | ||
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={handleChangeQuery} | ||
/> | ||
|
||
<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"> | ||
<a | ||
data-cy="century" | ||
className="button mr-1" | ||
href="#/people?centuries=16" | ||
> | ||
16 | ||
</a> | ||
|
||
<a | ||
data-cy="century" | ||
className="button mr-1 is-info" | ||
href="#/people?centuries=17" | ||
> | ||
17 | ||
</a> | ||
|
||
<a | ||
data-cy="century" | ||
className="button mr-1 is-info" | ||
href="#/people?centuries=18" | ||
> | ||
18 | ||
</a> | ||
|
||
<a | ||
data-cy="century" | ||
className="button mr-1 is-info" | ||
href="#/people?centuries=19" | ||
> | ||
19 | ||
</a> | ||
|
||
<a | ||
data-cy="century" | ||
className="button mr-1" | ||
href="#/people?centuries=20" | ||
> | ||
20 | ||
</a> | ||
{CENTURIES.map(century => ( | ||
<button | ||
key={century} | ||
type="button" | ||
data-cy="century" | ||
className={classNames('button mr-1', { | ||
'is-info': centuries.includes(century), | ||
})} | ||
onClick={() => toggleCenturies(century)} | ||
> | ||
{century} | ||
</button> | ||
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. You should use |
||
))} | ||
</div> | ||
|
||
<div className="level-right ml-4"> | ||
<a | ||
<button | ||
type="button" | ||
data-cy="centuryALL" | ||
className="button is-success is-outlined" | ||
href="#/people" | ||
className={classNames('button is-success', { | ||
'is-outlined': !!centuries.length, | ||
})} | ||
onClick={handleClearAllCenturies} | ||
> | ||
All | ||
</a> | ||
</button> | ||
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. Same here, use |
||
</div> | ||
</div> | ||
</div> | ||
|
||
<div className="panel-block"> | ||
<a | ||
className="button is-link is-outlined is-fullwidth" | ||
href="#/people" | ||
onClick={() => handleResetAllFilters} | ||
> | ||
Reset all filters | ||
</a> | ||
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. And here, use |
||
|
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.