Skip to content
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
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# React People Table (Advanced) - Filterging and Sorting
# React People Table (Advanced) - Filtering and Sorting

> Here is [the working example](https://mate-academy.github.io/react_people-table-advanced/)

Expand All @@ -8,14 +8,14 @@ Using code from the [React People Table](https://github.com/mate-academy/react_p
implement the ability to filter and sort people in the table.

1. All the filters and sort params should be saved as URL Search Params, so you could share the link to show exactly what you see.
1. Keep search params when navigating within the `People` page (when selecting a person or clicking the `People` link).
1. The sidebar with the filters should appear only when people are loaded.
1. `NameFilter` should update the `query` search param with the text from the input.
2. Keep search params when navigating within the `People` page (when selecting a person or clicking the `People` link).
3. The sidebar with the filters should appear only when people are loaded.
4. `NameFilter` should update the `query` search param with the text from the input.
- show only people with the `name`, `motherName` or `fatherName` that match the query case insensitive;
- if the input is empty there should not be `query` in the search params.
1. `CenturyFilter` should allow to choose several centuries or all of them.
5. `CenturyFilter` should allow to choose several centuries or all of them.
- add `centuries` search params using `append` method `getAll` method;
1. Implement sorting by `name`, `sex`, `born` and `died` by clicking on arrows in a `th`;
6. Implement sorting by `name`, `sex`, `born` and `died` by clicking on arrows in a `th`;
- the first click on a column sorts people by the selected field ascending (`a -> z` or `0 -> 9`);
- the second click (when people are already sorted ascending by this field) reverses the order of sorting;
- the third click (when people are already sorted in reversed order by this field) disables sorting;
Expand All @@ -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://RVDotsenko.github.io/react_people-table-advanced/) and add it to the PR description.
11 changes: 7 additions & 4 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import { PeoplePage } from './components/PeoplePage';
import { Navbar } from './components/Navbar';

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

export const App = () => {
useEffect(() => {
document.documentElement.classList.add('has-navbar-fixed-top');
}, []);

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 />
<Outlet />
</div>
</div>
</div>
Expand Down
29 changes: 29 additions & 0 deletions src/Root.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {
HashRouter as Router,
Routes,
Route,
Navigate,
} from 'react-router-dom';
import { App } from './App';
import { HomePage } from './pages/HomePage';
import { PeoplePage } from './pages/PeoplePage';
import { PeopleTable } from './components/PeopleTable';
import { PageNotFound } from './pages/404';

export const Root = () => (
<Router>
<Routes>
<Route path="/" element={<App />}>
<Route index element={<HomePage />} />
<Route path="home" element={<Navigate to={'/'} replace />} />

<Route path="people" element={<PeoplePage />}>
<Route index element={<PeopleTable />} />
<Route path=":slug" element={<PeopleTable />} />
</Route>

<Route path="*" element={<PageNotFound />} />
</Route>
</Routes>
</Router>
);
26 changes: 20 additions & 6 deletions src/components/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { NavLink } from 'react-router-dom';

import cn from 'classnames';
import { useProjectParams } from '../utils/useProjectParams';

export const Navbar = () => {
const { searchParams } = useProjectParams();

return (
<nav
data-cy="nav"
Expand All @@ -8,17 +15,24 @@ export const Navbar = () => {
>
<div className="container">
<div className="navbar-brand">
<a className="navbar-item" href="#/">
<NavLink
className={({ isActive }) =>
cn('navbar-item', { 'has-background-grey-lighter': isActive })
}
to="/"
>
Home
</a>
</NavLink>

<a
<NavLink
aria-current="page"
className="navbar-item has-background-grey-lighter"
href="#/people"
className={({ isActive }) =>
cn('navbar-item', { 'has-background-grey-lighter': isActive })
}
to={{ pathname: '/people', search: searchParams.toString() }}
>
People
</a>
</NavLink>
</div>
</div>
</nav>
Expand Down
129 changes: 76 additions & 53 deletions src/components/PeopleFilters.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,45 @@
import { SearchLink } from './SearchLink';
import cn from 'classnames';
import { useProjectParams } from '../utils/useProjectParams';
import { useEffect, useState } from 'react';
import { getSearchWith } from '../utils/searchHelper';

const sexLinks: Record<string, string | null> = {
All: null,
Male: 'm',
Female: 'f',
};

const centuriesLinks = ['16', '17', '18', '19', '20'];

export const PeopleFilters = () => {
const { search, searchParams, setSearchParams } = useProjectParams();
const [inputValue, setInputValue] = useState(search.query || '');

useEffect(() => {
setInputValue(search.query || '');
}, [search.query]);

const resetFiltersPayload = {
query: null,
sex: null,
centuries: null,
};

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(sexLinks).map(link => (
<SearchLink
key={link}
params={{ sex: sexLinks[link] }}
className={cn({ 'is-active': search.sex === sexLinks[link] })}
>
{link}
</SearchLink>
))}
</p>

<div className="panel-block">
Expand All @@ -22,6 +49,15 @@ export const PeopleFilters = () => {
type="search"
className="input"
placeholder="Search"
value={inputValue}
onChange={e => {
const value = e.target.value;

setInputValue(value);
setSearchParams(
getSearchWith(searchParams, { query: value || null }),
);
}}
/>

<span className="icon is-left">
Expand All @@ -33,63 +69,50 @@ 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>
{centuriesLinks.map(century => {
const currentCenturies = search.centuries || [];

<a
data-cy="century"
className="button mr-1 is-info"
href="#/people?centuries=18"
>
18
</a>
const activeCenturies = currentCenturies.includes(century)
? currentCenturies.filter(cent => century !== cent)
: [...currentCenturies, 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
data-cy="century"
key={century}
params={{ centuries: [...activeCenturies] }}
className={cn('button mr-1', {
'is-info': search.centuries?.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={{ centuries: null }}
className={cn('button is-success', {
'is-outlined': search.centuries,
})}
>
All
</a>
{'All'}
</SearchLink>
</div>
</div>
</div>

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

This file was deleted.

Loading
Loading