Skip to content
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

Solution #1160

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PeoplePage } from './components/PeoplePage';
import { Navbar } from './components/Navbar';

import './App.scss';
import { Outlet } from 'react-router-dom';

export const App = () => {
return (
Expand All @@ -10,9 +10,7 @@ export const App = () => {

<div className="section">
<div className="container">
<h1 className="title">Home Page</h1>
<h1 className="title">Page not found</h1>
<PeoplePage />
<Outlet />
</div>
</div>
</div>
Expand Down
3 changes: 3 additions & 0 deletions src/components/HomePage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const HomePage = () => {
return <h1 className="title">Home Page</h1>;
};
25 changes: 19 additions & 6 deletions src/components/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { NavLink } from 'react-router-dom';
import cn from 'classnames';
export const Navbar = () => {
const isTabActive = ({ isActive }: { isActive: boolean }) => {
return isActive ? 'has-background-grey-lighter' : '';
};

return (
<nav
data-cy="nav"
Expand All @@ -8,17 +14,24 @@ export const Navbar = () => {
>
<div className="container">
<div className="navbar-brand">
<a className="navbar-item" href="#/">
<NavLink
className={({ isActive }) =>
cn('navbar-item', isTabActive({ isActive }))
}
to="/"
>
Home
</a>
</NavLink>

<a
<NavLink
aria-current="page"
className="navbar-item has-background-grey-lighter"
href="#/people"
className={({ isActive }) =>
cn('navbar-item', isTabActive({ isActive }))
}
to="people"
>
People
</a>
</NavLink>
Comment on lines +26 to +34

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here you forgot to add this. make sure to do this here and in all People link in table

"Keep search params when navigating within the People page (when selecting a person or clicking the People link)."

</div>
</div>
</nav>
Expand Down
3 changes: 3 additions & 0 deletions src/components/PageNotFound.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const PageNotFound = () => {
return <h1 className="title">Page not found</h1>;
};
185 changes: 135 additions & 50 deletions src/components/PeopleFilters.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,122 @@
import { useSearchParams } from 'react-router-dom';
import { Sex } from '../types';
import cn from 'classnames';

export const PeopleFilters = () => {
const [searchParams, setSearchParams] = useSearchParams();

const query = searchParams.get('query') || '';

const handleQuery = (event: React.ChangeEvent<HTMLInputElement>) => {
const params = new URLSearchParams(searchParams);

params.set('query', event.target.value);
setSearchParams(params);
};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you need to make sure, that if query is empty, it should be deleted from searchParams


const handleGenderChange = (
gender: Sex,
event: React.MouseEvent<HTMLAnchorElement, MouseEvent>,
) => {
event.preventDefault();
const params = new URLSearchParams(searchParams);

params.set('sex', gender);
setSearchParams(params);
};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and for sex as well as for query - empty parameter should not be in search params


const genderTabActive = (gender: Sex) => {
return searchParams.get('sex') === gender ? 'is-active' : '';
};

const handleCenturyChange = (
century: string,
event: React.MouseEvent<HTMLAnchorElement, MouseEvent>,
) => {
event.preventDefault();

const params = new URLSearchParams(searchParams);
const centuries = params.getAll('centuries');

if (centuries.includes(century)) {
const newCenturies = centuries.filter(
currCentury => currCentury !== century,
);

params.delete('centuries');
newCenturies.forEach(currCentury =>
params.append('centuries', currCentury),
);
} else {
params.append('centuries', century);
}

setSearchParams(params);
};

const handleAll = (
event: React.MouseEvent<HTMLAnchorElement, MouseEvent>,
) => {
event.preventDefault();
const params = new URLSearchParams(searchParams);

params.delete('centuries');
setSearchParams(params);
};

const centuryTabActive = (century: string) => {
const centuries = searchParams.getAll('centuries');

return centuries.includes(century) ? 'is-info' : '';
};

const handleReset = (
event: React.MouseEvent<HTMLAnchorElement, MouseEvent>,
) => {
event.preventDefault();
const params = new URLSearchParams(searchParams);

params.delete('query');
params.delete('sex');
params.delete('centuries');

setSearchParams(params);
};

const genderSelection = [
{
label: 'All',
link: '#/people',
sex: Sex.All,
},
{
label: 'Male',
link: '#/people?sex=m',
sex: Sex.Male,
},
{
label: 'Female',
link: '#/people?sex=f',
sex: Sex.Female,
},
];

const centuries = ['16', '17', '18', '19', '20'];

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>
{genderSelection.map(({ label, link, sex }) => (
<a
key={label}
className={cn('panel-block', genderTabActive(sex))}
href={link}
onClick={event => handleGenderChange(sex, event)}
>
{label}
</a>
))}
</p>

<div className="panel-block">
Expand All @@ -22,6 +126,10 @@ export const PeopleFilters = () => {
type="search"
className="input"
placeholder="Search"
value={query}
onChange={event => {
handleQuery(event);
}}
/>

<span className="icon is-left">
Expand All @@ -33,52 +141,25 @@ 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>
{centuries.map(century => (
<a
key={century}
data-cy="century"
className={cn('button mr-1', centuryTabActive(century))}
href={`#/people?centuries=${century}`}
onClick={event => handleCenturyChange(century, event)}
>
{century}
</a>
))}
</div>

<div className="level-right ml-4">
<a
data-cy="centuryALL"
className="button is-success is-outlined"
href="#/people"
onClick={event => handleAll(event)}
>
All
</a>
Expand All @@ -87,7 +168,11 @@ export const PeopleFilters = () => {
</div>

<div className="panel-block">
<a className="button is-link is-outlined is-fullwidth" href="#/people">
<a
className="button is-link is-outlined is-fullwidth"
href="#/people"
onClick={event => handleReset(event)}
>
Reset all filters
</a>
</div>
Expand Down
Loading
Loading