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 #636

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
14 changes: 11 additions & 3 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Routes, Route, Navigate } from 'react-router-dom';
import { PeoplePage } from './components/PeoplePage';
import { Navbar } from './components/Navbar';
import { HomePage } from './components/HomePage';
import { NotFoundPage } from './components/NotFoundPage';

import './App.scss';

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

<div className="section">
<div className="container">
<h1 className="title">Home Page</h1>
<h1 className="title">Page not found</h1>
<PeoplePage />
<Routes>
<Route path="/" element={<HomePage />} />
<Route path="/home" element={<Navigate to="/" />} />
<Route path="/people">
<Route path=":personSlug?" element={<PeoplePage />} />
</Route>
<Route path="*" element={<NotFoundPage />} />
</Routes>
</div>
</div>
</div>
Expand Down
7 changes: 7 additions & 0 deletions src/components/HomePage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from 'react';

export const HomePage: React.FC = () => {
return (
<h1 className="title">Home Page</h1>
);
};
26 changes: 19 additions & 7 deletions src/components/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
import { NavLink, useSearchParams } from 'react-router-dom';
import classNames from 'classnames';

const getNavLinkClass = ({ isActive }: { isActive: boolean }) => {
return classNames('navbar-item', {
'has-background-grey-lighter': isActive,
});
};

export const Navbar = () => {
const [searchParams] = useSearchParams();

return (
<nav
data-cy="nav"
Expand All @@ -8,15 +19,16 @@ export const Navbar = () => {
>
<div className="container">
<div className="navbar-brand">
<a className="navbar-item" href="#/">Home</a>

<a
aria-current="page"
className="navbar-item has-background-grey-lighter"
href="#/people"
<NavLink to="/" className={getNavLinkClass}>Home</NavLink>
<NavLink
to={{
pathname: 'people',
search: searchParams.toString(),
}}
className={getNavLinkClass}
>
People
</a>
</NavLink>
</div>
</div>
</nav>
Expand Down
7 changes: 7 additions & 0 deletions src/components/NotFoundPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from 'react';

export const NotFoundPage: React.FC = () => {
return (
<h1 className="title">Page not found</h1>
);
};
154 changes: 105 additions & 49 deletions src/components/PeopleFilters.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,90 @@
import { Link, useSearchParams } from 'react-router-dom';
import classNames from 'classnames';
import React, { useEffect, useState } from 'react';
import { FilterParams } from '../types';

const filterCenturies = [16, 17, 18, 19, 20];

export const PeopleFilters = () => {
const [searchParams, setSearchParams] = useSearchParams();
const centuries = searchParams.getAll(FilterParams.CENTURIES) || [];
const defaultQuery = searchParams.get(FilterParams.NAME) || '';
const [query, setQuery] = useState(defaultQuery);

const getParams = (key: FilterParams, value = '') => {
const params = new URLSearchParams(searchParams);

if (value.trim()) {
if (key === FilterParams.CENTURIES) {
const newCenturies = centuries.includes(value)
? centuries.filter(century => century !== value)
: [...centuries, value];

params.delete(FilterParams.CENTURIES);

newCenturies.forEach(century => {
params.append(FilterParams.CENTURIES, century);
});
} else {
params.set(key, value.trim());
}
} else {
params.delete(key);
}

return params.toString();
};

const getResetParams = () => {
const params = new URLSearchParams(searchParams);

params.delete(FilterParams.NAME);
params.delete(FilterParams.SEX);
params.delete(FilterParams.CENTURIES);

return params.toString();
};

const handleChangeQuery = (e: React.ChangeEvent<HTMLInputElement>) => {
setQuery(e.currentTarget.value);
setSearchParams(getParams(FilterParams.NAME, e.currentTarget.value));
};

useEffect(() => {
if (!defaultQuery) {
setQuery('');
}
}, [defaultQuery]);

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>
<Link
to={{ search: getParams(FilterParams.SEX) }}
className={classNames({
'is-active': !searchParams.has(FilterParams.SEX),
})}
>
All
</Link>
<Link
to={{ search: getParams(FilterParams.SEX, 'm') }}
className={classNames({
'is-active': searchParams.get(FilterParams.SEX) === 'm',
})}
>
Male
</Link>
<Link
to={{ search: getParams(FilterParams.SEX, 'f') }}
className={classNames({
'is-active': searchParams.get(FilterParams.SEX) === 'f',
})}
>
Female
</Link>
</p>

<div className="panel-block">
Expand All @@ -16,6 +94,8 @@ export const PeopleFilters = () => {
type="search"
className="input"
placeholder="Search"
value={query}
onChange={handleChangeQuery}
/>

<span className="icon is-left">
Expand All @@ -27,66 +107,42 @@ 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>
{filterCenturies.map(century => (
<Link
to={{
search: getParams(FilterParams.CENTURIES, String(century)),
}}
data-cy="century"
className={classNames('button mr-1', {
'is-info': centuries.includes(String(century)),
})}
>
{century}
</Link>
))}
</div>

<div className="level-right ml-4">
<a
<Link
to={{ search: getParams(FilterParams.CENTURIES) }}
data-cy="centuryALL"
className="button is-success is-outlined"
href="#/people"
className={classNames('button is-success', {
'is-outlined': !!centuries.length,
})}
>
All
</a>
</Link>
</div>
</div>
</div>

<div className="panel-block">
<a
<Link
to={{ search: getResetParams() }}
className="button is-link is-outlined is-fullwidth"
href="#/people"
>
Reset all filters
</a>
</Link>
</div>
</nav>
);
Expand Down
Loading
Loading