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

my solution #1064

Open
wants to merge 3 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
7 changes: 2 additions & 5 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { PeoplePage } from './components/PeoplePage';
import { Outlet } from 'react-router-dom';
import { Navbar } from './components/Navbar';

import './App.scss';

export const App = () => {
Expand All @@ -10,9 +9,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
32 changes: 32 additions & 0 deletions src/Root.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {
Navigate,
Route,
HashRouter as Router,
Routes,
} from 'react-router-dom';
import { App } from './App';
import { HomePage } from './components/HomePage/HomePage';
import { PeoplePage } from './components/PeoplePage';

export const Root = () => (
<Router>
<Routes>
<Route path="/" element={<App />}>
<Route index element={<HomePage />} />
<Route path="people" element={<PeoplePage />} />
<Route path="home" element={<Navigate to="/" replace />} />
<Route path="people/:slug" element={<PeoplePage />} />
<Route
path="*"
element={
<main className="section">
<div className="container">
<h1 className="title">Page not found</h1>
</div>
</main>
}
/>
</Route>
</Routes>
</Router>
);
9 changes: 9 additions & 0 deletions src/components/HomePage/HomePage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react';

export const HomePage: React.FC = () => (
<main className="section">
<div className="container">
<h1 className="title">Home Page</h1>
</div>
</main>
);
57 changes: 36 additions & 21 deletions src/components/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,41 @@
export const Navbar = () => {
import React from 'react';
import cn from 'classnames';
import { Link, useLocation } from 'react-router-dom';

export const Navbar: React.FC = () => {
const location = useLocation();

return (
<nav
data-cy="nav"
className="navbar is-fixed-top has-shadow"
role="navigation"
aria-label="main navigation"
>
<div className="container">
<div className="navbar-brand">
<a className="navbar-item" href="#/">
Home
</a>
<>
<nav
data-cy="nav"
className="navbar is-fixed-top has-shadow"
role="navigation"
aria-label="main navigation"
>
<div className="container">
<div className="navbar-brand">
<Link
className={cn('navbar-item', {
'has-background-grey-lighter': location.pathname === '/',
})}
to="/"
>
Home
</Link>

<a
aria-current="page"
className="navbar-item has-background-grey-lighter"
href="#/people"
>
People
</a>
<Link
className={cn('navbar-item', {
'has-background-grey-lighter':
location.pathname.startsWith('/people'),
})}
to="/people"
>
People
</Link>
</div>
</div>
</div>
</nav>
</nav>
</>
);
};
156 changes: 113 additions & 43 deletions src/components/PeopleFilters.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,105 @@
import { useSearchParams } from 'react-router-dom';
import { SexEnum } from '../types/enums';
import cn from 'classnames';

export const PeopleFilters = () => {
const [searchParams, setSearchParams] = useSearchParams();
const query = searchParams.get('query') || '';
const centuries = searchParams.getAll('century') || [];
const allCenturies = [16, 17, 18, 19, 20];

function handleSexFiltering(
event: React.MouseEvent<HTMLAnchorElement>,
sex: string | null,
) {
event.preventDefault();

const params = new URLSearchParams(searchParams);

if (sex) {
params.set('sex', sex);
} else {
params.delete('sex');
}

setSearchParams(params);
}

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

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

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

const params = new URLSearchParams(searchParams);
const newCentury = centuries.includes(c)
? centuries.filter(century => century !== c)
: [...centuries, c];

params.delete('century');
newCentury.forEach(century => params.append('century', century));

setSearchParams(params);
};

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

const params = new URLSearchParams(searchParams);

params.delete('century');
allCenturies.forEach(century =>
params.append('century', century.toString()),
);

setSearchParams(params);
};

const resetAll = (event: React.MouseEvent<HTMLAnchorElement>) => {
event.preventDefault();

const params = new URLSearchParams(searchParams);

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

setSearchParams(params);
};

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="is-active"
href="#/people"
onClick={event => handleSexFiltering(event, null)}
>
All
</a>
<a className="" href="#/people?sex=m">
<a
className=""
href="#/people?sex=m"
onClick={e => handleSexFiltering(e, SexEnum.Male)}
>
Male
</a>
<a className="" href="#/people?sex=f">
<a
className=""
href="#/people?sex=f"
onClick={e => handleSexFiltering(e, SexEnum.Female)}
>
Female
</a>
</p>
Expand All @@ -22,6 +111,8 @@ export const PeopleFilters = () => {
type="search"
className="input"
placeholder="Search"
value={query}
onChange={handleQueryChange}
/>

<span className="icon is-left">
Expand All @@ -33,52 +124,27 @@ 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>
{allCenturies.map(century => (
<a
key={century}
data-cy="century"
className={cn('button mr-1', {
'is-info': centuries.includes(century.toString()),
})}
href={`#/people?${century}`}
onClick={e => handleCenturyFiltering(e, century.toString())}
>
{century}
</a>
))}
</div>

<div className="level-right ml-4">
<a
data-cy="centuryALL"
className="button is-success is-outlined"
href="#/people"
onClick={handleSelectAllCentury}
>
All
</a>
Expand All @@ -87,7 +153,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={resetAll}
>
Reset all filters
</a>
</div>
Expand Down
Loading
Loading