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
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,22 @@ implement the ability to filter and sort people in the table.
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.
- 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.
- 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.
- add `centuries` search params using `append` method `getAll` method;
- 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`;
- 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;
- use `sort` search param to save sort field;
- add `order=desc` (short for `descending`) if sorted in reversed order;
- if sorting is disabled there should not be `sort` and `order` search params;
- 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;
- use `sort` search param to save sort field;
- add `order=desc` (short for `descending`) if sorted in reversed order;
- if sorting is disabled there should not be `sort` and `order` search params;

## Instructions

- Install Prettier Extention and use this [VSCode settings](https://mate-academy.github.io/fe-program/tools/vscode/settings.json) to enable format on save.
- 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://valerij0412.github.io/react_people-table-advanced/) and add it to the PR description.
9 changes: 5 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"devDependencies": {
"@cypress/react18": "^2.0.1",
"@mate-academy/scripts": "^1.9.12",
"@mate-academy/scripts": "^2.1.3",
"@mate-academy/students-ts-config": "*",
"@mate-academy/stylelint-config": "*",
"@types/node": "^20.14.10",
Expand Down
19 changes: 16 additions & 3 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
import { Routes, Route, Navigate } from 'react-router-dom';
import { PeoplePage } from './components/PeoplePage';
import { Navbar } from './components/Navbar';

import './App.scss';

// Створюємо прості компоненти для сторінок прямо тут
const HomePage = () => <h1 className="title">Home Page</h1>;
const NotFoundPage = () => <h1 className="title">Page not found</h1>;

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 />
<Routes>
<Route path="/" element={<HomePage />} />
<Route path="/home" element={<Navigate to="/" replace />} />

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

<Route path="*" element={<NotFoundPage />} />
</Routes>
</div>
</div>
</div>
Expand Down
26 changes: 19 additions & 7 deletions src/components/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { NavLink, useLocation } from 'react-router-dom';

export const Navbar = () => {
// Дістаємо поточний рядок з параметрами (наприклад: "?sex=m&centuries=18")
const { search } = useLocation();

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

<a
aria-current="page"
className="navbar-item has-background-grey-lighter"
href="#/people"
<NavLink
// Передаємо збережені параметри в посилання
to={{ pathname: '/people', search }}
className={({ isActive }) =>
`navbar-item ${isActive ? 'has-background-grey-lighter' : ''}`
}
>
People
</a>
</NavLink>
</div>
</div>
</nav>
Expand Down
127 changes: 75 additions & 52 deletions src/components/PeopleFilters.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,65 @@
import { Link, useSearchParams } from 'react-router-dom';
import { getSearchWith } from '../utils/searchHelper';

export const PeopleFilters = () => {
const [searchParams, setSearchParams] = useSearchParams();

// Витягуємо поточні значення з URL для підсвічування активних кнопок/табів
const query = searchParams.get('query') || '';
const sex = searchParams.get('sex');
const centuries = searchParams.getAll('centuries');

// Обробник для текстового поля пошуку
// Як має бути:
// Як має бути:
const handleQueryChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const value = e.target.value;

setSearchParams(
new URLSearchParams(
getSearchWith(searchParams, { query: value || null }),
),
);
};

// Логіка для додавання/видалення століть
const getCenturySearch = (century: string) => {
let newCenturies = [...centuries];

if (newCenturies.includes(century)) {
newCenturies = newCenturies.filter(c => c !== century);
} else {
newCenturies.push(century);
Comment on lines +18 to +32

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The checklist requires: (1) first click = ascending with no order param, (2) second click = descending with order=desc, and (3) third click = sorting disabled with no sort and no order. Your handleSort correctly toggles sort and sets order='desc' on the second click, and clears both on the third click, so the URL behavior from the table side matches the requirements.

Comment on lines +31 to +32

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

isSortValid checks only that sortField is among name/sex/born/died, but it doesn’t validate order. Later you treat order as either 'desc' or “ascending” (any other value), so a stale or manually entered order=desc combined with a new sort value would make the first click appear descending, which violates the requirement that first click is always ascending. Consider validating order (only accept 'desc'), and treating anything else as “no order” so the first click is always ascending.

}

return getSearchWith(searchParams, {
centuries: newCenturies.length > 0 ? newCenturies : null,
});
};

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

<p className="panel-tabs" data-cy="SexFilter">
<a className="is-active" href="#/people">
<Link
to={{ search: getSearchWith(searchParams, { sex: null }) }}
className={!sex ? 'is-active' : ''}
>
All
</a>
<a className="" href="#/people?sex=m">
</Link>
<Link
to={{ search: getSearchWith(searchParams, { sex: 'm' }) }}
className={sex === 'm' ? 'is-active' : ''}
>
Male
</a>
<a className="" href="#/people?sex=f">
</Link>
<Link
to={{ search: getSearchWith(searchParams, { sex: 'f' }) }}
className={sex === 'f' ? 'is-active' : ''}
>
Female
</a>
</Link>
</p>

<div className="panel-block">
Expand All @@ -22,8 +69,9 @@ export const PeopleFilters = () => {
type="search"
className="input"
placeholder="Search"
value={query}
onChange={handleQueryChange}
/>

<span className="icon is-left">
<i className="fas fa-search" aria-hidden="true" />
</span>
Expand All @@ -33,63 +81,38 @@ 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>
{['16', '17', '18', '19', '20'].map(century => (
<Link
key={century}
data-cy="century"
className={`button mr-1 ${centuries.includes(century) ? 'is-info' : ''}`}
to={{ search: getCenturySearch(century) }}
>
{century}
</Link>
))}
</div>

<div className="level-right ml-4">
<a
<Link
data-cy="centuryALL"
className="button is-success is-outlined"
href="#/people"
className={`button ${centuries.length === 0 ? 'is-success' : ''} is-outlined`}
to={{ search: getSearchWith(searchParams, { centuries: null }) }}
>
All
</a>
</Link>
</div>
</div>
</div>

<div className="panel-block">
<a className="button is-link is-outlined is-fullwidth" href="#/people">
{/* Reset All просто скидає всі параметри пошуку (порожній рядок) */}
<Link
className="button is-link is-outlined is-fullwidth"
to={{ search: '' }}
>
Reset all filters
</a>
</Link>
</div>
</nav>
);
Expand Down
Loading
Loading