Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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://ihorshtoiko.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
33 changes: 17 additions & 16 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import { PeoplePage } from './components/PeoplePage';
import { Route, Routes, Navigate } from 'react-router-dom';
import { Navbar } from './components/Navbar';

import { PeoplePage } from './components/PeoplePage';
import './App.scss';

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 />
</div>
export const App = () => (
<div data-cy="app">
<Navbar />
<main className="section">
<div className="container">
<Routes>
<Route path="/" element={<h1 className="title">Home Page</h1>} />
<Route path="/home" element={<Navigate to="/" replace />} />
<Route path="/people" element={<PeoplePage />} />
<Route path="/people/:slug" element={<PeoplePage />} />
<Route path="*" element={<h1 className="title">Page not found</h1>} />
</Routes>
</div>
</div>
);
};
</main>
</div>
);
21 changes: 15 additions & 6 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,24 @@ export const Navbar = () => {
>
<div className="container">
<div className="navbar-brand">
<a className="navbar-item" href="#/">
<NavLink
className={({ isActive }) =>
`navbar-item ${isActive ? 'has-background-grey-lighter' : ''}`
}
to="/"
>
Home
</a>
</NavLink>

<a
<NavLink
aria-current="page"
className="navbar-item has-background-grey-lighter"
href="#/people"
className={({ isActive }) =>
`navbar-item ${isActive ? 'has-background-grey-lighter' : ''}`
}
to="/people"
>
People
</a>
</NavLink>
</div>
</div>
</nav>
Expand Down
118 changes: 87 additions & 31 deletions src/components/PeopleFilters.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
import { useSearchParams } from 'react-router-dom';
import { getSearchWith } from '../utils/searchHelper';
import { SearchLink } from './SearchLink';

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

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

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

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

<div className="panel-block">
Expand All @@ -22,6 +41,14 @@ export const PeopleFilters = () => {
type="search"
className="input"
placeholder="Search"
value={query}
onChange={e =>
setSearchParams(
getSearchWith(searchParams, {
query: e.target.value || null,
}),
)
}
/>

<span className="icon is-left">
Expand All @@ -33,63 +60,92 @@ export const PeopleFilters = () => {
<div className="panel-block">
<div className="level is-flex-grow-1 is-mobile" data-cy="CenturyFilter">
<div className="level-left">
<a
<SearchLink
data-cy="century"
className="button mr-1"
href="#/people?centuries=16"
className={`button mr-1 ${century.includes('16') ? 'is-info' : ''}`}
params={{
centuries: century.includes('16')
? century.filter(c => c !== '16')
: [...century, '16'],
}}

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 sidebar should only appear when people are loaded. Currently PeopleFilters renders unconditionally. Consider adding a condition to only show the sidebar when !isLoading && people.length > 0.

>
16
</a>
</SearchLink>

<a
<SearchLink
data-cy="century"
className="button mr-1 is-info"
href="#/people?centuries=17"
className={`button mr-1 ${century.includes('17') ? 'is-info' : ''}`}
params={{
centuries: century.includes('17')
? century.filter(c => c !== '17')
: [...century, '17'],
}}
>
17
</a>
</SearchLink>

<a
<SearchLink
data-cy="century"
className="button mr-1 is-info"
href="#/people?centuries=18"
className={`button mr-1 ${century.includes('18') ? 'is-info' : ''}`}
params={{
centuries: century.includes('18')
? century.filter(c => c !== '18')
: [...century, '18'],
}}
>
18
</a>
</SearchLink>

<a
<SearchLink
data-cy="century"
className="button mr-1 is-info"
href="#/people?centuries=19"
className={`button mr-1 ${century.includes('19') ? 'is-info' : ''}`}
params={{
centuries: century.includes('19')
? century.filter(c => c !== '19')
: [...century, '19'],
}}
>
19
</a>
</SearchLink>

<a
<SearchLink
data-cy="century"
className="button mr-1"
href="#/people?centuries=20"
className={`button mr-1 ${century.includes('20') ? 'is-info' : ''}`}
params={{
centuries: century.includes('20')
? century.filter(c => c !== '20')
: [...century, '20'],
}}
>
20
</a>
</SearchLink>
</div>

<div className="level-right ml-4">
<a
<SearchLink
data-cy="centuryALL"
className="button is-success is-outlined"
href="#/people"
params={{ centuries: null }}
>
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={{
query: null,
sex: null,
centuries: null,
sort: null,
order: null,
}}
>
Reset all filters
</a>
</SearchLink>
</div>
</nav>
);
Expand Down
89 changes: 78 additions & 11 deletions src/components/PeoplePage.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,96 @@
import { PeopleFilters } from './PeopleFilters';
import { Loader } from './Loader';
import { getPeople } from '../api';
import { PeopleTable } from './PeopleTable';
import { useEffect, useState } from 'react';
import { Person } from '../types';
import { useSearchParams } from 'react-router-dom';

export const PeoplePage = () => {
const [people, setPeople] = useState<Person[]>([]);
const [error, setError] = useState(false);
const [isLoading, setIsLoading] = useState(false);
const [searchParams] = useSearchParams();

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

const visiblePeople = people.filter(person => {
const matchesQuery =
person.name.toLowerCase().includes(query.toLowerCase()) ||
person.motherName?.toLowerCase().includes(query.toLowerCase()) ||
person.fatherName?.toLowerCase().includes(query.toLowerCase());
const matchesSex = !sex || person.sex === sex;
const matchesCentury =
century.length === 0 ||
century.includes(String(Math.ceil(person.born / 100)));

return matchesQuery && matchesSex && matchesCentury;
});

let sortedPeople = [...visiblePeople].sort((a, b) => {
if (sort === 'name' || sort === 'sex') {
return (a[sort as keyof Person] as string).localeCompare(
b[sort as keyof Person] as string,
);
}

if (sort === 'born' || sort === 'died') {
return (
(a[sort as keyof Person] as number) -
(b[sort as keyof Person] as number)
);
}

return 0;
});

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

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

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 />
{!isLoading && people.length > 0 && <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 />}
{error && (
<p data-cy="peopleLoadingError">Something went wrong</p>
)}
{!isLoading && !error && people.length === 0 && (
<p data-cy="noPeopleMessage">
There are no people on the server
</p>
)}
{!isLoading && !error && people.length > 0 && (
<PeopleTable people={sortedPeople} />
)}
{!isLoading &&
!error &&
sortedPeople.length === 0 &&
people.length > 0 && (
<p>

Check failure on line 90 in src/components/PeoplePage.tsx

View workflow job for this annotation

GitHub Actions / run_linter (20.x)

Expected indentation of 16 spaces but found 18
There are no people matching the current search criteria
</p>

Check failure on line 92 in src/components/PeoplePage.tsx

View workflow job for this annotation

GitHub Actions / run_linter (20.x)

Expected indentation of 16 spaces but found 18
)}

Check failure on line 93 in src/components/PeoplePage.tsx

View workflow job for this annotation

GitHub Actions / run_linter (20.x)

Expected indentation of 14 spaces but found 16
</div>
</div>
</div>
Expand Down
Loading
Loading