Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solution #538

Closed
wants to merge 2 commits into from
Closed
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://patryk-babon.github.io/react_people-table-advanced/) and add it to the PR description.
16 changes: 13 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,18 @@ 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=":slugPerson" element={<PeoplePage />} />
</Route>
<Route
path="*"
element={<h1 className="title">Page not found</h1>}
/>
</Routes>
</div>
</div>
</div>
Expand Down
22 changes: 14 additions & 8 deletions src/components/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import classNames from 'classnames';
import { NavLink } from 'react-router-dom';

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

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

<a
aria-current="page"
className="navbar-item has-background-grey-lighter"
href="#/people"
>
<NavLink to="/" className={getLinkClass}>
Home
</NavLink>
<NavLink to="/people" className={getLinkClass}>
People
</a>
</NavLink>
</div>
</div>
</nav>
Expand Down
124 changes: 72 additions & 52 deletions src/components/PeopleFilters.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,49 @@
import classNames from 'classnames';
import { useSearchParams } from 'react-router-dom';
import { SearchLink } from './SearchLink';

export const PeopleFilters = () => {
const [searchParams, setSearchParams] = useSearchParams();
const query = searchParams.get('query') || '';
const centuries = searchParams.getAll('centuries') || '';
const sex = searchParams.get('sex') || '';
const arrayCentury = ['16', '17', '18', '19', '20'];
Copy link

Choose a reason for hiding this comment

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

this should be outside of component

Suggested change
const arrayCentury = ['16', '17', '18', '19', '20'];
const FILTER_CENTURIES = ['16', '17', '18', '19', '20'];


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

params.set('query', event.target.value);
setSearchParams(params);
}

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>
<p
className="panel-tabs"
data-cy="SexFilter"
>
<SearchLink
params={{ sex: null }}
className={classNames({ 'is-active': !sex })}
>
All
</SearchLink>

<SearchLink
params={{ sex: 'm' }}
className={classNames({ 'is-active': sex === 'm' })}
>
Male
</SearchLink>

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

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

<span className="icon is-left">
Expand All @@ -27,66 +66,47 @@ 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>
{arrayCentury.map((century) => (
<SearchLink
key={century}
data-cy="century"
className={classNames(
'button mr-1',
{ 'is-info': centuries.includes(century) },
)}
params={{
centuries: centuries.includes(century)
? centuries.filter(c => c !== century)
: [...centuries, 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,
})}
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={classNames('button is-link is-fullwidth', {
'is-outlined': !!centuries.length || sex || query,
})}
params={{ sex: null, centuries: null, query: null }}
>
Reset all filters
</a>
</SearchLink>
</div>
</nav>
);
Expand Down
100 changes: 88 additions & 12 deletions src/components/PeoplePage.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,82 @@
import { useSearchParams } from 'react-router-dom';
import { useEffect, useMemo, useState } from 'react';
import { PeopleFilters } from './PeopleFilters';
import { Loader } from './Loader';
import { PeopleTable } from './PeopleTable';
import { Person, SortCategories } from '../types';
import { getPeople } from '../api';

export const PeoplePage = () => {
const [people, setPeople] = useState <Person[]>([]);
const [isLoading, setIsLoading] = useState(true);
const [isError, setIsError] = useState(false);
const [searchParams] = useSearchParams();
const query = searchParams.get('query') || '';
const centuries = searchParams.getAll('centuries') || '';
const sex = searchParams.get('sex') || '';
const order = searchParams.get('order') || null;
const sort = searchParams.get('sort') || null;

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

const visiblePeople = useMemo(() => {
let preparedPeople = people;

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

if (centuries.length > 0) {
preparedPeople = preparedPeople.filter(person => (
centuries.includes(Math.ceil(person.born / 100).toString())
));
}

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

if (sort) {
preparedPeople = [...preparedPeople].sort((person1, person2) => {
switch (sort) {
case SortCategories.Name:
case SortCategories.Sex:
return person1[sort].localeCompare(person2[sort]);
case SortCategories.Born:
case SortCategories.Died:
return person2[sort] - person1[sort];
default:
return 0;
}
});
}

if (order) {
preparedPeople.reverse();
}

return preparedPeople;
}, [people, query, centuries, sex, sort, order]);

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

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">
Expand All @@ -15,18 +85,24 @@ export const PeoplePage = () => {

<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 />
{!people.length ? (
<p data-cy="noPeopleMessage">
There are no people on the server
</p>
)
: <PeopleTable people={visiblePeople} />}
</div>
{!visiblePeople.length && (
<p>
There are no people matching the current search
criteria
</p>
)}
{!isLoading && isError && (
<p data-cy="peopleLoadingError" className="has-text-danger">
Something went wrong
</p>
)}
</div>
</div>
</div>
Expand Down
Loading
Loading