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

implemented the ability to filter and sort people in the table #495

Open
wants to merge 4 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ implement the ability to filter and sort people in the table.
- Implement a solution following the [React task guideline](https://github.com/mate-academy/react_task-guideline#react-tasks-guideline).
- Use the [React TypeScript cheat sheet](https://mate-academy.github.io/fe-program/js/extra/react-typescript).
- Open one more terminal and run tests with `npm test` to ensure your solution is correct.
- Replace `<your_account>` with your Github username in the [DEMO LINK](https://<your_account>.github.io/react_people-table-advanced/) and add it to the PR description.
- Replace `<your_account>` with your Github username in the [DEMO LINK](https://OlhaMomot.github.io/react_people-table-advanced/) and add it to the PR description.
27 changes: 11 additions & 16 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
import { PeoplePage } from './components/PeoplePage';
import { Navbar } from './components/Navbar';

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

export const App = () => {
return (
<div data-cy="app">
<Navbar />
export const App = () => (
<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>
<main className="section">
<div className="container">
<Outlet />
</div>
</div>
);
};
</main>
</div>
);
27 changes: 27 additions & 0 deletions src/Root.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {
Navigate,
Route,
HashRouter as Router,
Routes,
} from 'react-router-dom';
import { App } from './App';
import { HomePage } from './pages/HomePage';
import { PeoplePage } from './pages/PeoplePage';
import { NotFoundPage } from './components/NotFoundPage';

export const Root = () => {
return (
<Router>
<Routes>
<Route path="/" element={<App />}>
<Route index element={<HomePage />} />
<Route path="people">
<Route path=":personSlug?" element={<PeoplePage />} />
</Route>
<Route path="home" element={<Navigate to=".." replace />} />
<Route path="*" element={<NotFoundPage />} />
</Route>
</Routes>
</Router>
);
};
54 changes: 32 additions & 22 deletions src/components/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
export const Navbar = () => {
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>
import { NavLink } from 'react-router-dom';
import classNames from 'classnames';

<a
aria-current="page"
className="navbar-item has-background-grey-lighter"
href="#/people"
>
People
</a>
</div>
const getLinkClass = ({ isActive }: { isActive: boolean }) => classNames(
'navbar-item',
{ 'has-background-grey-lighter': isActive },
);

Choose a reason for hiding this comment

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

You can move this to utils


export const Navbar = () => (
<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={getLinkClass}
to="/"
>
Home
</NavLink>

<NavLink
className={getLinkClass}
to="/people"
>
People
</NavLink>
</div>
</nav>
);
};
</div>
</nav>
);
5 changes: 5 additions & 0 deletions src/components/NotFoundPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const NotFoundPage = () => {
return (
<h1 className="title">Page not found</h1>
);
};
110 changes: 62 additions & 48 deletions src/components/PeopleFilters.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,46 @@
import classNames from 'classnames';
import { useSearchParams } from 'react-router-dom';
import { SearchLink } from './SearchLink';
import { CENTURIES, SEARCH_FILTER } from '../utils/constants';

export const PeopleFilters = () => {
const [searchParams, setSearchParams] = useSearchParams();
const params = new URLSearchParams(searchParams);
const selectedCenturies = params.getAll('century') || [];

const handleQueryChange = (e: React.ChangeEvent<HTMLInputElement>) => {
e.preventDefault();

const newQuery = e.target.value;

if (newQuery === '') {

Choose a reason for hiding this comment

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

Suggested change
if (newQuery === '') {
if (!newQuery) {

params.delete('query');
} else {
params.set('query', newQuery);
}

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">All</a>
<a className="" href="#/people?sex=m">Male</a>
<a className="" href="#/people?sex=f">Female</a>
{Object.keys(SEARCH_FILTER).map(
(key) => (
<SearchLink
key={key}
params={{ sex: SEARCH_FILTER[key as keyof typeof SEARCH_FILTER] }}
className={classNames({
'is-active': params.get('sex')
=== SEARCH_FILTER[key as keyof typeof SEARCH_FILTER],
})}
>
{key}
</SearchLink>
),
)}
</p>

<div className="panel-block">
Expand All @@ -16,6 +50,8 @@ export const PeopleFilters = () => {
type="search"
className="input"
placeholder="Search"
value={searchParams.get('query') || ''}
onChange={handleQueryChange}
/>

<span className="icon is-left">
Expand All @@ -27,66 +63,44 @@ 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>
{Object.values(CENTURIES).map(century => (
<SearchLink
key={century}
params={{
century: selectedCenturies.includes(century)
? selectedCenturies.filter(date => date !== century)
: [...selectedCenturies, century],
}}
data-cy="century"
className={classNames(
'button mr-1',

Choose a reason for hiding this comment

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

Suggested change
'button mr-1',
'button', 'mr-1',

{ 'is-info': selectedCenturies.includes(century) },
)}
>
{century}
</SearchLink>
))}
</div>

<div className="level-right ml-4">
<a
<SearchLink
data-cy="centuryALL"
className="button is-success is-outlined"
href="#/people"
params={{ century: [] }}
>
All
</a>
</SearchLink>
</div>
</div>
</div>

<div className="panel-block">
<a
<SearchLink
className="button is-link is-outlined is-fullwidth"
href="#/people"
params={{ query: null, sex: null, century: null }}
>
Reset all filters
</a>
</SearchLink>
</div>
</nav>
);
Expand Down
35 changes: 0 additions & 35 deletions src/components/PeoplePage.tsx

This file was deleted.

Loading