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://denis-denis718.github.io/react_people-table-advanced/) and add it to the PR description.
129 changes: 39 additions & 90 deletions package-lock.json

Large diffs are not rendered by default.

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,3 +1,4 @@
import { Navigate, Route, Routes } from 'react-router-dom';
import { PeoplePage } from './components/PeoplePage';
import { Navbar } from './components/Navbar';

Expand All @@ -10,9 +11,21 @@ export const App = () => {

<div className="section">
<div className="container">
<h1 className="title">Home Page</h1>
<h1 className="title">Page not found</h1>
<PeoplePage />
<Routes>
<Route path="/" element={<h1 className="title">Home Page</h1>} />

<Route path="home" element={<Navigate to="/" replace />} />

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

<Route
path="*"
element={<h1 className="title">Page not found</h1>}
/>
</Routes>
</div>
</div>
</div>
Expand Down
21 changes: 14 additions & 7 deletions src/components/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { NavLink, useLocation } from 'react-router-dom';
import classNames from 'classnames';

const getLinkClass = ({ isActive }: { isActive: boolean }) =>
classNames('navbar-item', { 'has-background-grey-lighter': isActive });

export const Navbar = () => {
const { search } = useLocation();

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

<a
aria-current="page"
className="navbar-item has-background-grey-lighter"
href="#/people"
<NavLink
className={getLinkClass}
to={{ pathname: '/people', search }}
>
People
</a>
</NavLink>

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

setSearchParams(getSearchWith(searchParams, { query: newQuery || null })) correctly updates the query param and removes it when the input is empty, matching the NameFilter URL behavior requirement. Ensure similar || null handling for any future optional params you might introduce.

</div>
</div>
</nav>
Expand Down
124 changes: 70 additions & 54 deletions src/components/PeopleFilters.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,54 @@
import { ChangeEvent } from 'react';
import { useSearchParams } from 'react-router-dom';
import classNames from 'classnames';
import { SearchLink } from './SearchLink';
import { getSearchWith } from '../utils/searchHelper';

const SEX_FILTERS = [
{ value: null, label: 'All' },
{ value: 'm', label: 'Male' },
{ value: 'f', label: 'Female' },
];

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

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

const sex = searchParams.get('sex');
const query = searchParams.get('query') || '';
const centuries = searchParams.getAll('centuries');

const handleQueryChange = (event: ChangeEvent<HTMLInputElement>) => {
const newQuery = event.target.value;

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

const toggleCentury = (century: string) => {
return centuries.includes(century)
? 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">
All
</a>
<a className="" href="#/people?sex=m">
Male
</a>
<a className="" href="#/people?sex=f">
Female
</a>
{SEX_FILTERS.map(({ value, label }) => (
<SearchLink
key={label}
params={{ sex: value }}
className={classNames({ 'is-active': sex === value })}
>
{label}
</SearchLink>
))}
</p>

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

<span className="icon is-left">
Expand All @@ -33,63 +71,41 @@ 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>
{CENTURIES.map(century => (
<SearchLink
key={century}
data-cy="century"
className={classNames('button mr-1', {
'is-info': centuries.includes(century),
})}
params={{ centuries: toggleCentury(century) }}
>
{century}
</SearchLink>
))}
</div>

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

<div className="panel-block">
<a className="button is-link is-outlined is-fullwidth" href="#/people">
<SearchLink
className="button is-link is-outlined is-fullwidth"
params={{ sex: null, query: null, centuries: [] }}
>
Reset all filters
</a>
</SearchLink>
</div>
</nav>
);
Expand Down
68 changes: 57 additions & 11 deletions src/components/PeoplePage.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,75 @@
import { PeopleFilters } from './PeopleFilters';
import { useState, useEffect } from 'react';
import { useSearchParams } from 'react-router-dom';
import { getPreparedPeople } from '../utils/getPreparedPeople';
import { getPeople } from '../api';
import { Person } from '../types/Person';
import { Loader } from './Loader';
import { PeopleFilters } from './PeopleFilters';
import { PeopleTable } from './PeopleTable';

export const PeoplePage = () => {
const [people, setPeople] = useState<Person[]>([]);
const [isLoading, setIsLoading] = useState(false);
const [hasError, setHasError] = useState(false);

useEffect(() => {
setIsLoading(true);

getPeople()
.then(setPeople)
.catch(() => setHasError(true))
.finally(() => setIsLoading(false));
}, []);

const [searchParams] = useSearchParams();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

setSearchParams expects either an object mapping keys to string values or a URLSearchParams instance, but getSearchWith returns a plain string here. This means the URL search params are not being updated in the way React Router expects, which can break the NameFilter behavior (checklist items #3, #6, #8, #17). Consider creating a new URLSearchParams from getSearchWith(...) and passing that into setSearchParams.

const sex = searchParams.get('sex');
const query = searchParams.get('query');
const centuries = searchParams.getAll('centuries');
const sort = searchParams.get('sort');
const order = searchParams.get('order');

const visiblePeople = getPreparedPeople(people, {
sex,
query,
centuries,
sort,
order,
});

const isLoaded = !isLoading && !hasError;
const noPeopleOnServer = isLoaded && people.length === 0;
const showContent = isLoaded && people.length > 0;

return (
<>
<h1 className="title">People Page</h1>

<div className="block">
<div className="columns is-desktop is-flex-direction-row-reverse">
<div className="column is-7-tablet is-narrow-desktop">
<PeopleFilters />
{showContent && <PeopleFilters />}
</div>

<div className="column">
<div className="box table-container">
<Loader />

<p data-cy="peopleLoadingError">Something went wrong</p>

<p data-cy="noPeopleMessage">There are no people on the server</p>

<p>There are no people matching the current search criteria</p>

<PeopleTable />
{isLoading && <Loader />}
{!isLoading && hasError && (
<p data-cy="peopleLoadingError" className="has-text-danger">
Something went wrong
</p>
)}
{noPeopleOnServer && (
<p data-cy="noPeopleMessage">
There are no people on the server
</p>
)}
{showContent && visiblePeople.length === 0 && (
<p>There are no people matching the current search criteria</p>
)}
{showContent && visiblePeople.length > 0 && (
<PeopleTable people={visiblePeople} />
)}{' '}
</div>
</div>
</div>
Expand Down
Loading
Loading