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
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://anastasiiadns.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
26 changes: 19 additions & 7 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,31 @@ import { PeoplePage } from './components/PeoplePage';
import { Navbar } from './components/Navbar';

import './App.scss';
import { useEffect } from 'react';
import { Navigate, Route, Routes } from 'react-router-dom';
import { HomePage } from './components/HomePage';
import { NotFoundPage } from './components/NotFoundPage';

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 />
</div>
</div>
<main className="section">
<Routes>
<Route path="/" element={<HomePage />} />

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

<Route path="/home" element={<Navigate to="/" replace />} />
<Route path="*" element={<NotFoundPage />} />
</Routes>
</main>
</div>
);
};
7 changes: 7 additions & 0 deletions src/components/HomePage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const HomePage = () => {
return (
<div className="container">
<h1 className="title">Home Page</h1>
</div>
);
};
32 changes: 25 additions & 7 deletions src/components/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { Link, useLocation } from 'react-router-dom';
import classNames from 'classnames';

export const Navbar = () => {
const location = useLocation();

return (
<nav
data-cy="nav"
Expand All @@ -8,17 +13,30 @@ export const Navbar = () => {
>
<div className="container">
<div className="navbar-brand">
<a className="navbar-item" href="#/">
<Link
className={classNames('navbar-item', {
'has-background-grey-lighter': location.pathname === '/',
})}
to={{
pathname: '/',
search: location.search,
}}
>
Home
</a>
</Link>

<a
aria-current="page"
className="navbar-item has-background-grey-lighter"
href="#/people"
<Link
className={classNames('navbar-item', {
'has-background-grey-lighter':
location.pathname.startsWith('/people'),
})}
to={{
pathname: '/people',
search: location.search,
}}
>
People
</a>
</Link>
</div>
</div>
</nav>
Expand Down
1 change: 1 addition & 0 deletions src/components/NotFoundPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const NotFoundPage = () => <h1 className="title">Page not found</h1>;
131 changes: 80 additions & 51 deletions src/components/PeopleFilters.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,58 @@
import { useSearchParams } from 'react-router-dom';
import React from 'react';
import { SearchLink } from './SearchLink';
import classNames from 'classnames';

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

const query = searchParams.get('query') || '';
const centuries = searchParams.getAll('centuries') || [];
const sex = searchParams.get('sex');
const allCenturies = ['16', '17', '18', '19', '20'];

Comment on lines +12 to +13

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

PersonLink uses a plain NavLink to /people/${person.slug} and does not preserve existing search params, so filters and sorting are lost when selecting a person. The requirements state that search params must be kept when navigating within the People page; consider using SearchLink or otherwise including the current location.search in the destination URL.

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

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

setSearchParams(params);
Comment on lines +14 to +24

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 name filter handler correctly updates the query search param and removes it when the input is empty, as required. Just ensure you don’t accidentally bypass SearchLink/getSearchWith elsewhere, since URL param logic is centralized there.

}

function getToggledCenturies(century: string) {
return centuries.includes(century)
Comment on lines +19 to +28

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

getNextSort correctly implements the three-step sort cycle (asc, desc, off) and uses null to signal removal of sort/order params, matching the URL param requirements for sorting. No changes needed here, but keep in mind this relies on getSearchWith to delete null keys.

? centuries.filter(c => c !== century)
: [...centuries, century];
}

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

<p className="panel-tabs" data-cy="SexFilter">
<a className="is-active" href="#/people">
<SearchLink
params={{ sex: null }}
className={classNames({ 'is-active': !sex })}
>
All
</a>
<a className="" href="#/people?sex=m">
</SearchLink>
<SearchLink
params={{ sex: 'm' }}
className={classNames({ 'is-active': sex === 'm' })}
>
Male
</a>
<a className="" href="#/people?sex=f">
</SearchLink>
<SearchLink
params={{ sex: 'f' }}
className={classNames({ 'is-active': sex === 'f' })}
>
Female
</a>
</SearchLink>
</p>

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

<span className="icon is-left">
Expand All @@ -33,63 +75,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>

<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>
{allCenturies.map(century => (
<SearchLink
key={century}
data-cy={century}
params={{
centuries: getToggledCenturies(century),
}}
className={classNames('button mr-1', {
'is-info': centuries.includes(century),
})}
>
{century}
</SearchLink>
))}
</div>

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

<div className="panel-block">
<a className="button is-link is-outlined is-fullwidth" href="#/people">
<SearchLink
params={{
query: null,
sex: null,
centuries: null,
sort: null,
order: null,
}}
className="button is-link is-outlined is-fullwidth"
>
Reset all filters
</a>
</SearchLink>
</div>
</nav>
);
Expand Down
Loading
Loading