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://aholubko.github.io/react_people-table-advanced/) and add it to the PR description.
19 changes: 14 additions & 5 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,28 @@ import { PeoplePage } from './components/PeoplePage';
import { Navbar } from './components/Navbar';

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

export const App = () => {
return (
<div data-cy="app">
<Navbar />

<div className="section">
<main 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" element={<PeoplePage />} />
<Route path="/people/:slug" element={<PeoplePage />} />

<Route path="*" element={<NotFoundPage />} />
</Routes>
</div>
</div>
</main>
</div>
);
};
25 changes: 18 additions & 7 deletions src/components/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import classNames from 'classnames';
import { Link, useLocation } from 'react-router-dom';

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

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

<a
aria-current="page"
className="navbar-item has-background-grey-lighter"
href="#/people"
<Link
to="/people"
className={classNames('navbar-item', {
'has-background-grey-lighter': pathname.startsWith('/people'),
})}
>
People
</a>
</Link>
</div>
</div>
</nav>
Expand Down
128 changes: 81 additions & 47 deletions src/components/PeopleFilters.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,46 @@
import { useSearchParams } from 'react-router-dom';
import { SearchLink } from './SearchLink';
import classNames from 'classnames';

export const PeopleFilters = () => {
const [searchParams, setSearchParams] = useSearchParams();
const centuryButtons = [16, 17, 18, 19, 20];

const sex = searchParams.get('sex') || '';
const query = searchParams.get('query') || '';
const centuries = 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={classNames({
'is-active': sex === '',
})}
params={{ sex: null }}
>
All
</a>
<a className="" href="#/people?sex=m">
</SearchLink>

<SearchLink
className={classNames({
'is-active': sex === 'm',
})}
params={{ sex: 'm' }}
>
Male
</a>
<a className="" href="#/people?sex=f">
</SearchLink>

<SearchLink
className={classNames({
'is-active': sex === 'f',
})}
params={{ sex: 'f' }}
>
Female
</a>
</SearchLink>
</p>

<div className="panel-block">
Expand All @@ -22,6 +50,20 @@ export const PeopleFilters = () => {
type="search"
className="input"
placeholder="Search"
value={query}
onChange={event => {
const value = event.target.value;

const params = new URLSearchParams(searchParams);

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

setSearchParams(params);
}}
/>

<span className="icon is-left">
Expand All @@ -33,63 +75,55 @@ 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>
{centuryButtons.map(century => {
const centuryString = century.toString();

<a
data-cy="century"
className="button mr-1 is-info"
href="#/people?centuries=17"
>
17
</a>
const isSelected = centuries.includes(centuryString);

<a
data-cy="century"
className="button mr-1 is-info"
href="#/people?centuries=18"
>
18
</a>
const updatedCenturies = isSelected
? centuries.filter(selectCen => selectCen !== centuryString)
: [...centuries, centuryString];

<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>
return (
<SearchLink
key={century}
data-cy="century"
className={classNames('button mr-1', {
'is-info': isSelected,
})}
params={{ centuries: updatedCenturies }}
>
{century}
</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
110 changes: 102 additions & 8 deletions src/components/PeoplePage.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,123 @@
import { PeopleFilters } from './PeopleFilters';
import { useEffect, useState } from 'react';
import { Person } from '../types';
import { getPeople } from '../api';
import { Loader } from './Loader';
import { PeopleTable } from './PeopleTable';
import { useSearchParams } from 'react-router-dom';

export const PeoplePage = () => {
const noMatchingPeopleMessage =
'There are no people matching the current search criteria';
const [people, setPeople] = useState<Person[]>([]);
const [isLoading, setIsLoading] = useState(false);
const [errorMessage, setErrorMessage] = useState(false);

const [searchParams] = useSearchParams();

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

const sort = searchParams.get('sort') || '';
const order = searchParams.get('order') || '';

let visiblePeople = people;
const normalizedQuery = query?.trim().toLowerCase();

if (query.trim() !== '') {
visiblePeople = visiblePeople.filter(
person =>
person.name.toLowerCase().includes(normalizedQuery) ||
person.motherName?.toLowerCase().includes(normalizedQuery) ||
person.fatherName?.toLowerCase().includes(normalizedQuery),
);
}

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

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

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

if (sort) {
const sortedPeople = [...visiblePeople];

sortedPeople.sort((person1, person2) => {
switch (sort) {
case 'name':

case 'sex':
return person1[sort].localeCompare(person2[sort]);

case 'born':

case 'died':
return person1[sort] - person2[sort];

default:
return 0;
}
});

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

visiblePeople = sortedPeople;
}

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

getPeople()
.then(setPeople)
.catch(() => setErrorMessage(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 />
</div>
{!isLoading && !errorMessage && people.length > 0 && (
<div className="column is-7-tablet is-narrow-desktop">
<PeopleFilters />
</div>
)}

<div className="column">
<div className="box table-container">
<Loader />
{isLoading && <Loader />}

<p data-cy="peopleLoadingError">Something went wrong</p>
{errorMessage && (
<p data-cy="peopleLoadingError" className="has-text-danger">
Something went wrong
</p>
)}

<p data-cy="noPeopleMessage">There are no people on the server</p>
{!isLoading && !errorMessage && people.length === 0 && (
<p data-cy="noPeopleMessage">
There are no people on the server
</p>
)}

<p>There are no people matching the current search criteria</p>
{!isLoading &&
!errorMessage &&
people.length > 0 &&
visiblePeople.length === 0 && <p>{noMatchingPeopleMessage}</p>}

<PeopleTable />
{!isLoading && !errorMessage && people.length > 0 && (
<PeopleTable people={visiblePeople} />
)}
</div>
</div>
</div>
Expand Down
Loading
Loading