-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Develop #1136
base: master
Are you sure you want to change the base?
Develop #1136
Changes from 5 commits
66719aa
fffd0e8
d81a8e2
682b83b
62058a2
e90f94f
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,20 +1,23 @@ | ||
import { PeoplePage } from './components/PeoplePage'; | ||
import { Navbar } from './components/Navbar'; | ||
|
||
import './App.scss'; | ||
import React from 'react'; | ||
import { Navigate, Route, Routes } from 'react-router-dom'; | ||
import { Layout } from './components/Layout'; | ||
import { HomePageComponent } from './components/HomePageComponent'; | ||
import { NotFoundPage } from './components/NotFoundPage'; | ||
|
||
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> | ||
</div> | ||
</div> | ||
<Layout> | ||
<Routes> | ||
<Route path="/" element={<HomePageComponent />} /> | ||
<Route path="/home" element={<Navigate to={'/'} />} /> | ||
<Route path="/people" element={<PeoplePage />}> | ||
<Route path="/people/:slug" element={<PeoplePage />} /> | ||
</Route> | ||
<Route path="*" element={<NotFoundPage />} /> | ||
</Routes> | ||
</Layout> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* eslint-disable max-len */ | ||
import React from 'react'; | ||
import { ErrorMessages } from './PeoplePage'; | ||
|
||
interface ErrorComponentProps { | ||
message: ErrorMessages; | ||
} | ||
|
||
export function ErrorComponent({ message }: ErrorComponentProps) { | ||
return ( | ||
<> | ||
{message === ErrorMessages.SOMETHING_WENT_WRONG && ( | ||
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 way you implemented this isn't wrong, but i would recommend to use dynamic property access to avoid repetition (and personally i think that's also improves code readability)! Imagine if we have 50 types of error messages, what would we do? an example implementation: <>
{message && (
<p data-cy={`${message}Message`}>
{ErrorMessages[message]}
</p>
)}
</> |
||
<p data-cy="peopleLoadingError">{ErrorMessages.SOMETHING_WENT_WRONG}</p> | ||
)} | ||
{message === ErrorMessages.NO_PEOPLE && ( | ||
<p data-cy="noPeopleMessage">{ErrorMessages.NO_PEOPLE}</p> | ||
)} | ||
{message === ErrorMessages.NO_PEOPLE_MATCHING_CRITERIA && ( | ||
<p data-cy="noPeopleMessage"> | ||
{ErrorMessages.NO_PEOPLE_MATCHING_CRITERIA} | ||
</p> | ||
)} | ||
</> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import React from 'react'; | ||
|
||
export function HomePageComponent() { | ||
return <h1 className="title">Home Page</h1>; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import React from 'react'; | ||
import { Navbar } from './Navbar'; | ||
|
||
export function Layout({ children }: { children: React.ReactNode }) { | ||
return ( | ||
<div data-cy="app"> | ||
<Navbar /> | ||
|
||
<div className="section"> | ||
<div className="container">{children}</div> | ||
</div> | ||
</div> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,4 +1,9 @@ | ||||||
import React from 'react'; | ||||||
import { useLocation } from 'react-router-dom'; | ||||||
|
||||||
export const Navbar = () => { | ||||||
const path = useLocation().pathname; | ||||||
|
||||||
return ( | ||||||
<nav | ||||||
data-cy="nav" | ||||||
|
@@ -8,13 +13,16 @@ export const Navbar = () => { | |||||
> | ||||||
<div className="container"> | ||||||
<div className="navbar-brand"> | ||||||
<a className="navbar-item" href="#/"> | ||||||
<a | ||||||
className={`navbar-item ${path.includes('home') || path === '/' ? 'has-background-grey-lighter' : ''}`} | ||||||
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
We can extract it to a variable (or a function) to avoid complexity in the JSX =) const getHomeLinkClassNames = () => {
const isHome = path.includes('home') || path === '/'
return isHome ? 'has-background-grey-lighter' : ''
}
...
<a className={getHomeLinkClassNames()}> |
||||||
href="#/" | ||||||
> | ||||||
Home | ||||||
</a> | ||||||
|
||||||
<a | ||||||
aria-current="page" | ||||||
className="navbar-item has-background-grey-lighter" | ||||||
className={`navbar-item ${path.includes('people') ? 'has-background-grey-lighter' : ''}`} | ||||||
href="#/people" | ||||||
> | ||||||
People | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import React from 'react'; | ||
|
||
export function NotFoundPage() { | ||
return <h1 className="title">Page not found</h1>; | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,18 +1,72 @@ | ||||||
import React from 'react'; | ||||||
import { Link, useLocation, useSearchParams } from 'react-router-dom'; | ||||||
import { getSearchWith, SearchParams } from '../utils/searchHelper'; | ||||||
|
||||||
export const PeopleFilters = () => { | ||||||
const availableCenturies = [16, 17, 18, 19, 20]; | ||||||
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
We can move this variable to outside of the component, since we aren't changing it's values anywhere in the code flow, so we can avoid it's re-creation across re-renders =) |
||||||
|
||||||
const pathName = useLocation().pathname; | ||||||
const [searchParams, setSearchParams] = useSearchParams(); | ||||||
const sex = searchParams.get('sex'); | ||||||
const searchInputValue = searchParams.get('query'); | ||||||
const centuries = searchParams.getAll('centuries'); | ||||||
|
||||||
function setParamsWith(params: SearchParams) { | ||||||
const search = getSearchWith(searchParams, params); | ||||||
|
||||||
setSearchParams(search); | ||||||
|
||||||
return search; | ||||||
} | ||||||
|
||||||
function handleSearchInputChange(event: React.ChangeEvent<HTMLInputElement>) { | ||||||
const query = event.target.value === '' ? null : event.target.value; | ||||||
|
||||||
const search = setParamsWith({ query }); | ||||||
|
||||||
setSearchParams(search); | ||||||
} | ||||||
|
||||||
function handleAddNewCentury(century: number) { | ||||||
const newCenturies = centuries.includes(century.toString()) | ||||||
? centuries.filter(c => c !== century.toString()) | ||||||
: [...centuries, century.toString()]; | ||||||
|
||||||
return newCenturies; | ||||||
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 |
||||||
} | ||||||
|
||||||
return ( | ||||||
<nav className="panel"> | ||||||
<p className="panel-heading">Filters</p> | ||||||
|
||||||
<p className="panel-tabs" data-cy="SexFilter"> | ||||||
<a className="is-active" href="#/people"> | ||||||
<Link | ||||||
className={`${sex ? '' : 'is-active'}`} | ||||||
to={{ | ||||||
pathname: pathName, | ||||||
search: `${getSearchWith(searchParams, { sex: null })}`, | ||||||
}} | ||||||
> | ||||||
All | ||||||
</a> | ||||||
<a className="" href="#/people?sex=m"> | ||||||
</Link> | ||||||
<Link | ||||||
className={`${sex === 'm' ? 'is-active' : ''}`} | ||||||
to={{ | ||||||
pathname: pathName, | ||||||
search: `${getSearchWith(searchParams, { sex: 'm' })}`, | ||||||
}} | ||||||
> | ||||||
Male | ||||||
</a> | ||||||
<a className="" href="#/people?sex=f"> | ||||||
</Link> | ||||||
<Link | ||||||
className={`${sex === 'f' ? 'is-active' : ''}`} | ||||||
to={{ | ||||||
pathname: pathName, | ||||||
search: `${getSearchWith(searchParams, { sex: 'f' })}`, | ||||||
}} | ||||||
> | ||||||
Female | ||||||
</a> | ||||||
</Link> | ||||||
</p> | ||||||
|
||||||
<div className="panel-block"> | ||||||
|
@@ -22,6 +76,8 @@ export const PeopleFilters = () => { | |||||
type="search" | ||||||
className="input" | ||||||
placeholder="Search" | ||||||
value={searchInputValue || ''} | ||||||
onChange={handleSearchInputChange} | ||||||
/> | ||||||
|
||||||
<span className="icon is-left"> | ||||||
|
@@ -33,63 +89,48 @@ export const PeopleFilters = () => { | |||||
<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> | ||||||
{availableCenturies.map(century => ( | ||||||
<Link | ||||||
key={`century-${century}`} | ||||||
className={`button mr-1 ${centuries.includes(century.toString()) ? 'is-info' : ''}`} | ||||||
to={{ | ||||||
pathname: pathName, | ||||||
search: `${getSearchWith(searchParams, { centuries: handleAddNewCentury(century) })}`, | ||||||
}} | ||||||
> | ||||||
{century} | ||||||
</Link> | ||||||
))} | ||||||
</div> | ||||||
|
||||||
<div className="level-right ml-4"> | ||||||
<a | ||||||
<Link | ||||||
data-cy="centuryALL" | ||||||
className="button is-success is-outlined" | ||||||
href="#/people" | ||||||
className={`button is-success ${centuries.length !== 0 ? 'is-outlined' : ''}`} | ||||||
to={{ | ||||||
pathname: pathName, | ||||||
search: `${getSearchWith(searchParams, { centuries: [] })}`, | ||||||
}} | ||||||
> | ||||||
All | ||||||
</a> | ||||||
</Link> | ||||||
</div> | ||||||
</div> | ||||||
</div> | ||||||
|
||||||
<div className="panel-block"> | ||||||
<a className="button is-link is-outlined is-fullwidth" href="#/people"> | ||||||
<Link | ||||||
className="button is-link is-outlined is-fullwidth" | ||||||
to={{ | ||||||
pathname: pathName, | ||||||
search: getSearchWith(searchParams, { | ||||||
centuries: [], | ||||||
sex: null, | ||||||
query: null, | ||||||
}), | ||||||
}} | ||||||
> | ||||||
Reset all filters | ||||||
</a> | ||||||
</Link> | ||||||
</div> | ||||||
</nav> | ||||||
); | ||||||
|
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 nested route inside
/people
is redundant since it uses the same componentPeoplePage
. IfPeoplePage
is supposed to handle both/people
and/people/:slug
, you might want to handle the logic for rendering different content based on the presence of:slug
within thePeoplePage
component itself. Consider removing the nested route or adjusting the component logic.