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 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://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 { PageNotFound } from './components/PageNotFound';
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={<PageNotFound />} />
<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/PageNotFound.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const PageNotFound: React.FC = () => (

Choose a reason for hiding this comment

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

Suggested change
export const PageNotFound: React.FC = () => (
export const NotFoundPage: React.FC = () => (

<h1 className="title">Page not found</h1>
);
54 changes: 54 additions & 0 deletions src/components/People.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import React from 'react';
import { useParams } from 'react-router-dom';
import classNames from 'classnames';
import { Person } from '../types';
import { PersonLink } from './PersonLink';
import { NOT_SET_VALUE } from '../utils/constants';

type Props = {
person: Person,
};

export const People: React.FC<Props> = ({ person }) => {

Choose a reason for hiding this comment

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

Suggested change
export const People: React.FC<Props> = ({ person }) => {
export const Person: React.FC<Props> = ({ person }) => {

const { selectedPerson = '' } = useParams();
const {
sex,
born,
died,
mother,
father,
motherName,
fatherName,
slug,
} = person;

return (
<tr
data-cy="person"
className={classNames({
'has-background-warning': selectedPerson === slug,
})}
>
<td>
<PersonLink person={person} />
</td>
<td>{sex}</td>
<td>{born}</td>
<td>{died}</td>
<td>
{mother ? (
<PersonLink person={mother} />
) : (
motherName || NOT_SET_VALUE
)}
</td>
<td>
{father ? (
<PersonLink person={father} />
) : (
fatherName || NOT_SET_VALUE
)}
</td>
</tr>
);
};
163 changes: 110 additions & 53 deletions src/components/PeopleFilters.tsx
Original file line number Diff line number Diff line change
@@ -1,89 +1,146 @@
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

}

function handleSexSelected(filterPath: string | null) {
const params = new URLSearchParams(searchParams);

params.set('sex', filterPath || '');
setSearchParams(params);
}

function toggleCenturies(cntr: string) {
const params = new URLSearchParams(searchParams);

const newCenturies = centuries.includes(cntr)
? centuries.filter(prevCentury => prevCentury !== cntr)
: [...centuries, cntr];

params.delete('centuries');
newCenturies.forEach(cent => params.append('centuries', cent));
setSearchParams(params);
}

function handleClearAllCenturies() {
const params = new URLSearchParams(searchParams);

params.delete('centuries');
setSearchParams(params);
}

function handleResetAllFilters() {
const params = new URLSearchParams(searchParams);

params.delete('sex');
params.delete('query');
setSearchParams(params);
}

Choose a reason for hiding this comment

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

You don't need these functions. You have params prop in your SearchLink for this


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

<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>
{CENTURIES.map(century => (
<button
key={century}
type="button"
data-cy="century"
className={classNames('button mr-1', {
'is-info': centuries.includes(century),
})}
onClick={() => toggleCenturies(century)}
>
{century}
</button>

Choose a reason for hiding this comment

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

You should use SearchLink here

))}
</div>

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

Choose a reason for hiding this comment

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

Same here, use SearchLink

</div>
</div>
</div>

<div className="panel-block">
<a
className="button is-link is-outlined is-fullwidth"
href="#/people"
onClick={() => handleResetAllFilters}
>
Reset all filters
</a>

Choose a reason for hiding this comment

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

And here, use SearchLink

Expand Down
Loading