Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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://ValyaMeln.github.io/react_people-table-advanced/) and add it to the PR description.
22 changes: 19 additions & 3 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { Routes, Route, Navigate } from 'react-router-dom';

import { PeoplePage } from './components/PeoplePage';
import { Navbar } from './components/Navbar';

import './App.scss';
import { HomePage } from './pages/HomePage';
import { NotFoundPage } from './pages/NotFoundPage';

export const App = () => {
return (
Expand All @@ -10,9 +14,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={<HomePage />} />

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

<Route path="/people">
<Route index element={<PeoplePage />} />
Comment on lines 14 to +23

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

SexFilter links use hardcoded href attributes instead of SearchLink. This means clicking these links won't preserve other search params (like query or centuries). Use SearchLink with params={{ sex: 'm' }} or params={{ sex: null }} for 'All'.

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

<Route path="*" element={<NotFoundPage />} />
</Routes>
{/* <h1 className="title">Home Page</h1>
<h1 className="title">Page not found</h1> */}
{/* <PeoplePage /> */}
</div>
Comment on lines +27 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 NameFilter input is missing value and onChange handlers. According to requirement #4, it should update the query search param when text changes, and the input should reflect the current query value. Consider reading from searchParams.get('query') and using a callback to update it.

</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,3 +1,5 @@
import { NavLink } from 'react-router-dom';

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

Comment on lines +13 to 23

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 SexFilter links (lines 14-23) use plain <a> tags with static href values instead of SearchLink. They should update the sex search param to make them functional and maintain existing search params.

<a
aria-current="page"
className="navbar-item has-background-grey-lighter"
href="#/people"
<NavLink
to="/people"
className={({ isActive }) =>
isActive
? 'navbar-item has-background-grey-lighter'
: 'navbar-item'
}
>
People
Comment on lines +27 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 NameFilter input is missing onChange handler and value binding. The input needs to update the query search param when typing. Add useState or controlled input with SearchLink that updates the query param when text is entered.

</a>
</NavLink>
</div>
</div>
</nav>
Expand Down
61 changes: 24 additions & 37 deletions src/components/PeopleFilters.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { useSearchParams } from 'react-router-dom';
import { SearchLink } from './SearchLink';

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

const selected = searchParams.getAll('centuries');
const centuries = ['16', '17', '18', '19', '20'];

return (
<nav className="panel">
<p className="panel-heading">Filters</p>
Expand Down Expand Up @@ -33,45 +41,24 @@ 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>
{centuries.map(c => {
const isActive = selected.includes(c);

<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>
const updated = isActive
? selected.filter(x => x !== c)
: [...selected, c];

<a
data-cy="century"
className="button mr-1"
href="#/people?centuries=20"
>
20
</a>
return (
<SearchLink
key={c}
params={{ centuries: updated }}
className={`button mr-1 ${isActive ? 'is-info' : ''}`}
data-cy="century"
>
{c}
</SearchLink>
);
})}
</div>

<div className="level-right ml-4">
Expand Down
112 changes: 108 additions & 4 deletions src/components/PeoplePage.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,103 @@
import { PeopleFilters } from './PeopleFilters';
import { Loader } from './Loader';
import { PeopleTable } from './PeopleTable';
import { useParams, useSearchParams } from 'react-router-dom';
import { useEffect, useMemo, useState } from 'react';
import { Person } from '../types';
import { getPeople } from '../api';

export const PeoplePage = () => {
const { slug } = useParams();

const [searchParams] = useSearchParams();

const [people, setPeople] = useState<Person[]>([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(false);

useEffect(() => {
getPeople()
.then(setPeople)
.catch(() => setError(true))
.finally(() => setLoading(false));
}, []);

const visiblePeople = useMemo(() => {
let result = [...people];

const query = searchParams.get('query')?.toLowerCase() || '';

const centuries = searchParams.getAll('centuries');

const sort = searchParams.get('sort');
Comment on lines +27 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 NameFilter input is missing the onChange handler and value from searchParams. According to requirements, this input must update the query search param. Add value={searchParams.get('query') || ''}, an onChange handler, and use SearchLink to update the query param.

const order = searchParams.get('order');
const sex = searchParams.get('sex');

if (sex) {
result = result.filter(person => person.sex === sex);
}

if (query) {
result = result.filter(person =>
[person.name, person.motherName, person.fatherName]
.filter(Boolean)
.some(value => value!.toLowerCase().includes(query)),
);
}

if (centuries.length) {
result = result.filter(person => {
const century = Math.ceil(person.born / 100);

return centuries.includes(String(century));
});
}

if (sort) {
result.sort((a, b) => {
let res = 0;

switch (sort) {
case 'name':
res = a.name.localeCompare(b.name);
break;

case 'sex':
res = a.sex.localeCompare(b.sex);
break;

case 'born':
Comment on lines +64 to +69

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 'All' century button uses static href that resets all URL params. According to the requirements, this should use SearchLink with centuries: null to only clear the centuries filter while preserving other params.

res = a.born - b.born;
break;

case 'died':
res = a.died - b.died;
break;
}

return order === 'desc' ? -res : res;
Comment on lines +76 to +78

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 Reset all filters link uses a static href that resets all URL params. According to the requirements, this should properly update search params using SearchLink with individual null values for each filter (query, sex, centuries, sort, order).

});

if (order === 'desc') {
result.reverse();
}
}

return result;
}, [people, searchParams]);

if (loading) {
return <Loader />;
}

if (error) {
return (
<p data-cy="peopleLoadingError" className="has-text-danger">
Something went wrong
</p>
);
}

return (
Comment on lines +100 to 101

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 "Reset all filters" link doesn't clear sorting params. Add sort: null and order: null to the params object so that clicking reset also clears any active sorting, not just the filter options.

<>
<h1 className="title">People Page</h1>
Expand All @@ -15,15 +110,24 @@ export const PeoplePage = () => {

<div className="column">
<div className="box table-container">
<Loader />
{people.length === 0 ? (
<p data-cy="noPeopleMessage">
There are no people on the server
</p>
) : !visiblePeople.length ? (
<p>There are no people matching the current search criteria</p>
) : (
<PeopleTable people={visiblePeople} selectedSlug={slug} />
)}
{/* <Loader />

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

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

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

<PeopleTable />
{/* <PeopleTable /> */}
</div>
</div>
</div>
Expand Down
Loading
Loading