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

implement filtering and sorting #507

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
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://serkrops.github.io/react_people-table-advanced/) and add it to the PR description.
28 changes: 11 additions & 17 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
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 />

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

export const Root = () => (
<Router>
<Routes>
<Route path="/" element={<App />}>
<Route index element={<HomePage />} />
<Route path="people">
<Route path=":selectedPerson?" element={<PeoplePage />} />
</Route>
<Route path="*" element={<NotFoundPage />} />
<Route path="/home" element={<Navigate to="/" replace />} />
</Route>
</Routes>
</Router>
);
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: React.FC = () => (
<h1 className="title">Home Page</h1>
);
18 changes: 10 additions & 8 deletions src/components/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export const Navbar = () => {
import React from 'react';
import { NavLink } from 'react-router-dom';
import { getLinkClass } from '../helpers/getLinkClass';

export const Navbar: React.FC = () => {
return (
<nav
data-cy="nav"
Expand All @@ -8,15 +12,13 @@ export const Navbar = () => {
>
<div className="container">
<div className="navbar-brand">
<a className="navbar-item" href="#/">Home</a>
<NavLink className={getLinkClass} to="/">
Home
</NavLink>

<a
aria-current="page"
className="navbar-item has-background-grey-lighter"
href="#/people"
>
<NavLink className={getLinkClass} to="people">
People
</a>
</NavLink>
</div>
</div>
</nav>
Expand Down
3 changes: 3 additions & 0 deletions src/components/NotFoundPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const NotFoundPage: React.FC = () => (
<h1 className="title">Page not found</h1>
);
139 changes: 84 additions & 55 deletions src/components/PeopleFilters.tsx
Original file line number Diff line number Diff line change
@@ -1,92 +1,121 @@
export const PeopleFilters = () => {
import classNames from 'classnames';
import React from 'react';
import { useSearchParams } from 'react-router-dom';
import { SearchLink } from './SearchLink';
import { CENTURIES, SEX_FILTER_LINKS } from '../utils/constants';

type Props = {
query: string,
sex: string,
centuries: string[],
};

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

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

params.set('query', event.target.value);
setSearchParams(params);
}
Comment on lines +21 to +24

Choose a reason for hiding this comment

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

You can get previous params in setSearchParams


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>
<SearchLink
className={classNames({
'is-active': sex === SEX_FILTER_LINKS.All,
})}
params={{ sex: null }}
>
All
</SearchLink>
<SearchLink
className={classNames({
'is-active': sex === SEX_FILTER_LINKS.Male,
})}
params={{ sex: SEX_FILTER_LINKS.Male }}
>
Male
</SearchLink>
<SearchLink
className={classNames({
'is-active': sex === SEX_FILTER_LINKS.Female,
})}
params={{ sex: SEX_FILTER_LINKS.Female }}
>
Female
</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={handleChangeQuery}
/>

<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">
<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>
{CENTURIES.map(century => {
const isCenturyIncluded = centuries.includes(century);
const updatedCenturies = isCenturyIncluded
? centuries.filter(cent => cent !== century)
: [...centuries, century];

<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>
return (
<SearchLink
key={century}
data-cy="century"
className={classNames('button mr-1', {
'is-info': isCenturyIncluded,
})}
params={{ centuries: updatedCenturies }}
>
{century}
</SearchLink>
);
})}
</div>

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

<div className="panel-block">
<a
<SearchLink
className="button is-link is-outlined is-fullwidth"
href="#/people"
params={{
centuries: null,
sex: null,
query: null,
}}
>
Reset all filters
</a>
</SearchLink>
</div>
</nav>
);
Expand Down
81 changes: 81 additions & 0 deletions src/components/PeopleList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import React from 'react';
import classNames from 'classnames';
import { Person } from '../types';
import { PersonTile } from './PersonTile';
import {
FATHER_THEAD_TITLE,
MOTHER_THEAD_TITLE,
THEAD_TITLES,
} from '../utils/constants';
import { SearchLink } from './SearchLink';
import { getSortedParams } from '../helpers/getSortedParams';

type Props = {
sort: string,
order: string,
persons: Person[],
};

export const PeopleList: React.FC<Props> = ({
sort,
order,
persons,
}) => {
const isParentsExist = (title: string) => {
return (MOTHER_THEAD_TITLE === title || FATHER_THEAD_TITLE === title);
};

return (
<table
data-cy="peopleTable"
className="table is-striped is-hoverable is-narrow is-fullwidth"
>
<thead>
<tr>
{THEAD_TITLES.map(title => {
const normalizedTitle = title.toLowerCase();
const isNotSorted = sort !== normalizedTitle;
const isSorted = !isNotSorted && !order;
const isReversed = sort === normalizedTitle && order;

return (
<th key={title}>
{isParentsExist(title)
? (
<span className="is-flex is-flex-wrap-nowrap">
{title}
</span>
) : (
<span className="is-flex is-flex-wrap-nowrap">
{title}
<SearchLink params={getSortedParams(
normalizedTitle,
sort,
order,
)}
>
<span className="icon">
<i
className={classNames('fas', {
'fa-sort': isNotSorted,
'fa-sort-up': isSorted,
'fa-sort-down': isReversed,
})}
/>
</span>
</SearchLink>
</span>
)}
</th>
);
})}
</tr>
</thead>
<tbody>
{persons.map(person => (
<PersonTile key={person.slug} person={person} />
))}
</tbody>
</table>
);
};
Loading
Loading