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

added a solution to the task #1071

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

import { Outlet } from 'react-router-dom';
import { Navigation as Navbar } from './components/Navigation';
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
22 changes: 22 additions & 0 deletions src/Root.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { HashRouter, Navigate, Route, Routes } from 'react-router-dom';
import { App } from './App';
import { HomePage } from './components/HomePage';
import { PeoplePage } from './components/PeoplePage';
import { NotFoundPage } from './components/NotFoundPage';

export const Root: React.FC = () => {
return (
<HashRouter>
<Routes>
<Route path="/" element={<App />}>
<Route index element={<HomePage />} />
<Route path="people" element={<PeoplePage />}>
<Route path=":slug?" element={<PeoplePage />} />
</Route>
<Route path="home" element={<Navigate to="/" replace />} />
<Route path="*" element={<NotFoundPage />} />
</Route>
</Routes>
</HashRouter>
);
};
3 changes: 3 additions & 0 deletions src/components/HomePage/HomePage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const HomePage: React.FC = () => {
return <h1 className="title">Home Page</h1>;
};
1 change: 1 addition & 0 deletions src/components/HomePage/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './HomePage';
26 changes: 0 additions & 26 deletions src/components/Navbar.tsx

This file was deleted.

28 changes: 28 additions & 0 deletions src/components/Navigation/Navigation.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import classNames from 'classnames';
import { NavLink } from 'react-router-dom';

export const Navigation: React.FC = () => {
const handleLinkClass = ({ isActive }: { isActive: boolean }) =>
classNames('navbar-item', { 'has-background-grey-lighter': isActive });

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">
<NavLink className={handleLinkClass} to="/">
Home
</NavLink>

<NavLink className={handleLinkClass} to="/people">
People
</NavLink>
</div>
</div>
</nav>
);
};
1 change: 1 addition & 0 deletions src/components/Navigation/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Navigation';
3 changes: 3 additions & 0 deletions src/components/NotFoundPage/NotFoundPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const NotFoundPage: React.FC = () => {
return <h1 className="title">Page not found</h1>;
};
1 change: 1 addition & 0 deletions src/components/NotFoundPage/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './NotFoundPage';
96 changes: 0 additions & 96 deletions src/components/PeopleFilters.tsx

This file was deleted.

124 changes: 124 additions & 0 deletions src/components/PeopleFilters/PeopleFilters.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import React from 'react';
import { useSearchParams } from 'react-router-dom';
import { SearchParams } from '../../types/SearchParams';
import { PersonSex } from '../../types/personSex';
import { getSearchWith } from '../../utils/searchHelper';
import classNames from 'classnames';
import { SearchLink } from '../SearchLink';
import { CenturyForSearch } from '../../types/CenturyForSearch';

type Props = {};

export const PeopleFilters: React.FC<Props> = () => {
const [searchParams, setSearchParams] = useSearchParams();

const query = searchParams.get(SearchParams.QUERY) || '';
const sex = searchParams.get(SearchParams.SEX) || PersonSex.ALL;
const centuries = searchParams.getAll(SearchParams.CENTURIES) || [];

const handleQuery = (event: React.ChangeEvent<HTMLInputElement>) => {
const params = new URLSearchParams(searchParams);
const newQuery = event.target.value || null;

if (newQuery) {
params.set(SearchParams.QUERY, newQuery);
} else {
params.delete(SearchParams.QUERY);
}

setSearchParams(getSearchWith(params, { query: newQuery }));
};

const handleCenturyOfBirth = (century: string) => {
return centuries.includes(century)
? centuries.filter(cent => cent !== century)
: [...centuries, century];
};

return (
<nav className="panel">
<p className="panel-heading">Filters</p>

<p className="panel-tabs" data-cy="SexFilter">
{Object.entries(PersonSex).map(([key, value]) => (
<SearchLink
key={key}
className={classNames({
'is-active': sex === value,
})}
params={{
sex: value,
}}
>
{key}
</SearchLink>
))}
</p>

<div className="panel-block">
<p className="control has-icons-left">
<input
data-cy="NameFilter"
type="search"
className="input"
placeholder="Search"
value={query}
onChange={handleQuery}
/>

<span className="icon is-left">
<i className="fas fa-search" aria-hidden="true" />
</span>
</p>
</div>

<div className="panel-block">
<div className="level is-flex-grow-1 is-mobile" data-cy="CenturyFilter">
<div className="level-left">
{Object.values(CenturyForSearch).map(century => (
<SearchLink
key={century}
data-cy="century"
className={classNames('button mr-1', {
'is-info': centuries.includes(century),
})}
params={{
centuries: handleCenturyOfBirth(century),
}}
>
{century}
</SearchLink>
))}
</div>

<div className="level-right ml-4">
<SearchLink
data-cy="centuryALL"
className={classNames('button is-success', {
'is-outlined': centuries.length,
})}
params={{
centuries: null,
}}
>
All
</SearchLink>
</div>
</div>
</div>

<div className="panel-block">
<SearchLink
className="button is-link is-outlined is-fullwidth"
params={{
centuries: null,
sex: null,
query: null,
}}
>
Reset all filters
</SearchLink>
</div>
</nav>
);
};
1 change: 1 addition & 0 deletions src/components/PeopleFilters/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './PeopleFilters';
33 changes: 0 additions & 33 deletions src/components/PeoplePage.tsx

This file was deleted.

Loading
Loading