-
Notifications
You must be signed in to change notification settings - Fork 1.9k
solution, But I think I made some mistakes. #1823
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,23 @@ | ||
| import { | ||
| Navigate, | ||
| Route, | ||
| HashRouter as Router, | ||
| Routes, | ||
| } from 'react-router-dom'; | ||
| import { App } from './App'; | ||
| import { PageNotFound } from './components/Pages/PageNotFound'; | ||
| import { HomePage } from './components/Pages/HomePage'; | ||
| import { PeoplePage } from './components/PeoplePage'; | ||
|
|
||
| export const Root = () => ( | ||
| <Router> | ||
| <Routes> | ||
| <Route path="/" element={<App />}> | ||
| <Route path="*" element={<PageNotFound />} /> | ||
| <Route path="home" element={<Navigate to="/" replace />} /> | ||
| <Route index element={<HomePage />} /> | ||
| <Route path="people/:peopleSlug?" element={<PeoplePage />} /> | ||
| </Route> | ||
| </Routes> | ||
| </Router> | ||
| ); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import React from 'react'; | ||
|
|
||
| export const HomePage: React.FC = () => { | ||
| return <h1 className="title">Home Page</h1>; | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import React from 'react'; | ||
|
|
||
| export const PageNotFound: React.FC = () => { | ||
| return <h1 className="title">Page not found</h1>; | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,126 @@ | ||
| export const PeopleFilters = () => { | ||
| import React, { useEffect } from 'react'; | ||
| import { Person } from '../types'; | ||
| import { useSearchParams } from 'react-router-dom'; | ||
| import cn from 'classnames'; | ||
|
|
||
| type Props = { | ||
| peoples: Person[]; | ||
| onFilter: (persons: Person[]) => void; | ||
| }; | ||
|
|
||
| export const PeopleFilters: React.FC<Props> = ({ peoples, onFilter }) => { | ||
| const [serchParams, setSerchParams] = useSearchParams(); | ||
| const query = serchParams.get('query') || ''; | ||
| const centuries = serchParams.getAll('centuries') || []; | ||
| const sex = serchParams.get('sex') || ''; | ||
|
|
||
| function hangeChangeQuery(event: React.ChangeEvent<HTMLInputElement>) { | ||
| const params = new URLSearchParams(serchParams); | ||
| const trueQuery = event.target.value.trim(); | ||
|
|
||
| if (!trueQuery) { | ||
| params.delete('query'); | ||
| } else { | ||
| params.set('query', event.target.value.trim()); | ||
| } | ||
|
|
||
| setSerchParams(params); | ||
| } | ||
|
|
||
| function hangeChangeSex(sexe: string) { | ||
| const params = new URLSearchParams(serchParams); | ||
|
|
||
| params.set('sex', sexe); | ||
| setSerchParams(params); | ||
| } | ||
|
|
||
| function hangeChangeAges(n: string) { | ||
| const params = new URLSearchParams(serchParams); | ||
|
|
||
| if (n === 'All') { | ||
| params.delete('centuries'); | ||
|
|
||
| return setSerchParams(params); | ||
| } | ||
|
|
||
| const newCenturies = centuries.includes(n) | ||
| ? centuries.filter(centurie => centurie !== n) | ||
| : [...centuries, n]; | ||
|
|
||
| params.delete('centuries'); | ||
| newCenturies.forEach(ages => params.append('centuries', ages)); | ||
|
|
||
| return setSerchParams(params); | ||
| } | ||
|
|
||
| const handleClearAll = () => { | ||
| const params = new URLSearchParams(serchParams); | ||
|
|
||
| params.delete('centuries'); | ||
| params.delete('query'); | ||
| params.delete('sex'); | ||
| setSerchParams(params); | ||
|
|
||
| return; | ||
| }; | ||
|
|
||
| useEffect(() => { | ||
| let filtered = [...peoples]; | ||
|
|
||
| if (centuries.length > 0) { | ||
| filtered = filtered.filter(person => { | ||
|
Comment on lines
+39
to
+71
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.
|
||
| const century = Math.ceil(person.born / 100); | ||
|
|
||
| return centuries.includes(String(century)); | ||
| }); | ||
| } | ||
|
|
||
| if (sex) { | ||
| filtered = filtered.filter(person => person.sex === sex); | ||
| } | ||
|
|
||
| if (query.trim()) { | ||
| filtered = filtered.filter( | ||
| person => | ||
| person.name | ||
| .toLowerCase() | ||
| .trim() | ||
| .includes(query.trim().toLowerCase()) || | ||
| person.fatherName | ||
| ?.toLowerCase() | ||
| .trim() | ||
| .includes(query.trim().toLowerCase()) || | ||
| person.motherName | ||
| ?.trim() | ||
| .toLowerCase() | ||
| .includes(query.trim().toLowerCase()), | ||
| ); | ||
| } | ||
|
|
||
| onFilter(filtered); | ||
| }, [peoples, serchParams, sex, query, onFilter]); | ||
|
Comment on lines
+67
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. This effect recalculates |
||
|
|
||
| return ( | ||
| <nav className="panel"> | ||
| <p className="panel-heading">Filters</p> | ||
|
|
||
| <p className="panel-tabs" data-cy="SexFilter"> | ||
| <a className="is-active" href="#/people"> | ||
| <a | ||
| className={cn(sex === '' ? 'is-active' : '')} | ||
| onClick={() => hangeChangeSex('')} | ||
| > | ||
| All | ||
| </a> | ||
| <a className="" href="#/people?sex=m"> | ||
| <a | ||
| className={cn(sex === 'm' ? 'is-active' : '')} | ||
| onClick={() => hangeChangeSex('m')} | ||
| > | ||
| Male | ||
| </a> | ||
| <a className="" href="#/people?sex=f"> | ||
| <a | ||
| className={cn(sex === 'f' ? 'is-active' : '')} | ||
| onClick={() => hangeChangeSex('f')} | ||
| > | ||
| Female | ||
| </a> | ||
| </p> | ||
|
|
@@ -22,6 +132,8 @@ | |
| type="search" | ||
| className="input" | ||
| placeholder="Search" | ||
| value={query} | ||
| onChange={event => hangeChangeQuery(event)} | ||
| /> | ||
|
|
||
| <span className="icon is-left"> | ||
|
|
@@ -35,40 +147,55 @@ | |
| <div className="level-left"> | ||
| <a | ||
| data-cy="century" | ||
| className="button mr-1" | ||
| href="#/people?centuries=16" | ||
| className={cn( | ||
| 'button mr-1', | ||
| centuries.includes('16') ? 'is-info' : '', | ||
| )} | ||
| onClick={() => hangeChangeAges('16')} | ||
| > | ||
| 16 | ||
| </a> | ||
|
|
||
| <a | ||
| data-cy="century" | ||
| className="button mr-1 is-info" | ||
| href="#/people?centuries=17" | ||
| className={cn( | ||
| 'button mr-1', | ||
| centuries.includes('17') ? 'is-info' : '', | ||
| )} | ||
| onClick={() => hangeChangeAges('17')} | ||
| > | ||
| 17 | ||
| </a> | ||
|
|
||
| <a | ||
| data-cy="century" | ||
| className="button mr-1 is-info" | ||
| href="#/people?centuries=18" | ||
| className={cn( | ||
| 'button mr-1', | ||
| centuries.includes('18') ? 'is-info' : '', | ||
| )} | ||
| onClick={() => hangeChangeAges('18')} | ||
| > | ||
| 18 | ||
| </a> | ||
|
|
||
| <a | ||
| data-cy="century" | ||
| className="button mr-1 is-info" | ||
| href="#/people?centuries=19" | ||
| className={cn( | ||
| 'button mr-1', | ||
| centuries.includes('19') ? 'is-info' : '', | ||
| )} | ||
| onClick={() => hangeChangeAges('19')} | ||
| > | ||
| 19 | ||
| </a> | ||
|
|
||
| <a | ||
| data-cy="century" | ||
| className="button mr-1" | ||
| href="#/people?centuries=20" | ||
| className={cn( | ||
| 'button mr-1', | ||
| centuries.includes('20') ? 'is-info' : '', | ||
| )} | ||
| onClick={() => hangeChangeAges('20')} | ||
| > | ||
| 20 | ||
| </a> | ||
|
|
@@ -77,8 +204,11 @@ | |
| <div className="level-right ml-4"> | ||
| <a | ||
| data-cy="centuryALL" | ||
| className="button is-success is-outlined" | ||
| href="#/people" | ||
| className={cn( | ||
| 'button is-success', | ||
| centuries.length !== 0 ? 'is-outlined' : '', | ||
| )} | ||
| onClick={() => hangeChangeAges('All')} | ||
| > | ||
| All | ||
| </a> | ||
|
|
@@ -87,7 +217,13 @@ | |
| </div> | ||
|
|
||
| <div className="panel-block"> | ||
| <a className="button is-link is-outlined is-fullwidth" href="#/people"> | ||
| <a | ||
| className={cn( | ||
| 'button is-link is-fullwidth', | ||
| serchParams.size !== 0 ? 'is-outlined' : '', | ||
| )} | ||
| onClick={handleClearAll} | ||
| > | ||
| Reset all filters | ||
| </a> | ||
| </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.
Here you always set the
queryparam, even when the input is empty; the requirements (checklist #8/#18) say that when the input is empty there should be noqueryin the search params. Consider deletingqueryfromparamsifevent.target.value.trim()is an empty string instead of setting it.