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

add task solution #542

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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://AngerDESTROYS.github.io/react_people-table-advanced/) and add it to the PR description.
21 changes: 18 additions & 3 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
import { Navigate, Route, Routes } from 'react-router-dom';
import { PeoplePage } from './components/PeoplePage';
import { Navbar } from './components/Navbar';

import './App.scss';

const NotFoundPage = () => <h1 className="title">Page not found</h1>;

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 className="block">
<div className="box table-container">

<Routes>
<Route
path="/"
element={<h1 className="title">Home Page</h1>}
/>
<Route path="/home" element={<Navigate to="/" />} />
<Route path="/people" element={<PeoplePage />} />
<Route path="/people/:slug" element={<PeoplePage />} />
<Route path="*" element={<NotFoundPage />} />
</Routes>
</div>
</div>
</div>
</div>
</div>
Expand Down
23 changes: 17 additions & 6 deletions src/components/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
import { NavLink } from 'react-router-dom';
import classNames from 'classnames';

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

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

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

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

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

const isActiveCentury = (century: string) => centuries.includes(century);

const isActive = (newSex: string) => {
if (newSex === '') {
return !location.search || !location.search.includes('sex');
}

return location.search.includes(`sex=${newSex}`);
};

function toggleCenturies(century: string) {
const params = new URLSearchParams(searchParams);
const newCenturies = centuries.includes(century)
? centuries.filter(centur => centur !== century)
: [...centuries, century];

params.delete('centuries');
newCenturies.forEach(centur => params.append('centuries', centur));

setSearchParams(params);
}

function handleQueryChange(event: React.ChangeEvent<HTMLInputElement>) {
const paramsToUpdate = { query: event.target.value || null };
const newSearchParams = getSearchWith(searchParams, paramsToUpdate);

setSearchParams(newSearchParams);
}

function handleSexChange(newSex: string) {
const paramsToUpdate = { sex: newSex || null };
const newSearchParams = getSearchWith(searchParams, paramsToUpdate);

setSearchParams(newSearchParams);
}

function clearCenturies() {
const paramsToUpdate = { centuries: [] };
const newSearchParams = getSearchWith(searchParams, paramsToUpdate);

setSearchParams(newSearchParams);
}

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>
<Link
className={classNames(
{ 'is-active': sex === '' },
)}
to={{
pathname: '/people',
search: getSearchWith(searchParams, { sex: null }),
}}
onClick={() => handleSexChange('')}
>
All
</Link>
<Link
className={classNames(
{ 'is-active': isActive('m') },
)}
to={{
pathname: '/people',
search: getSearchWith(searchParams, { sex: 'm' }),
}}
onClick={() => handleSexChange('m')}
>
Male
</Link>
<Link
className={classNames(
{ 'is-active': isActive('f') },
)}
to={{
pathname: '/people',
search: getSearchWith(searchParams, { sex: 'f' }),
}}
onClick={() => handleSexChange('f')}
>
Female
</Link>
</p>

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

<span className="icon is-left">
Expand All @@ -27,55 +118,40 @@ 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>
{[16, 17, 18, 19, 20].map(century => (
<Link
key={century}
data-cy="century"
className={classNames('button', 'mr-1', {
'is-info': isActiveCentury(century.toString()),
})}
to={{
pathname: '/people',
search: getSearchWith(searchParams, {
centuries: isActiveCentury(century.toString())
? centuries.filter(c => c !== century.toString())
: [...centuries, century.toString()],
}),
}}
onClick={() => toggleCenturies(century.toString())}
>
{century}
</Link>
))}
</div>

<div className="level-right ml-4">
<a
<Link
data-cy="centuryALL"
className="button is-success is-outlined"
href="#/people"
to={{
pathname: '/people',
search: getSearchWith(searchParams, { centuries: [] }),
}}
onClick={() => clearCenturies()}
>
All
</a>
</Link>
</div>
</div>
</div>
Expand Down
97 changes: 78 additions & 19 deletions src/components/PeoplePage.tsx
Original file line number Diff line number Diff line change
@@ -1,35 +1,94 @@
import { useLocation } from 'react-router-dom';
import { useEffect, useState } from 'react';
import { PeopleFilters } from './PeopleFilters';
import { Loader } from './Loader';
import { PeopleTable } from './PeopleTable';
import { Person } from '../types';
import { getPeople } from '../api';

export const PeoplePage = () => {
return (
<>
<h1 className="title">People Page</h1>
const [people, setPeople] = useState<Person[]>([]);
const [filtredPeople, setFiltredPeople] = useState<Person[]>([]);
const [isLoading, setIsLoading] = useState(true);
const [loadingError, setLoadingError] = useState(false);
const location = useLocation();

<div className="block">
<div className="columns is-desktop is-flex-direction-row-reverse">
<div className="column is-7-tablet is-narrow-desktop">
<PeopleFilters />
</div>
function getCentury(bornYear: number) {
const century = Math.ceil(bornYear / 100);

return century;
}

useEffect(() => {
getPeople()
.then((data) => {
setPeople(data);
setIsLoading(false);
})
.catch((error) => {
// eslint-disable-next-line no-console
console.error('Error fetching people data:', error);
setIsLoading(false);
setLoadingError(true);
});
}, []);

<div className="column">
<div className="box table-container">
<Loader />
useEffect(() => {
const urlSearchParams = new URLSearchParams(location.search);
const query = urlSearchParams.get('query');
const sex = urlSearchParams.get('sex');
const centuries = urlSearchParams.getAll('centuries');

<p data-cy="peopleLoadingError">Something went wrong</p>
const newPeople = people.filter(person => (
(query === null || person.name.toLowerCase().includes(query.toLowerCase())
|| person.motherName?.toLowerCase().includes(query.toLowerCase())
|| person.fatherName?.toLowerCase().includes(query.toLowerCase()))
&& (sex === null || person.sex === sex)
&& (centuries.length === 0 || centuries
.includes(getCentury(person.born).toString()))
));

<p data-cy="noPeopleMessage">
There are no people on the server
</p>
setFiltredPeople(newPeople);
}, [location.search, people]);

<p>There are no people matching the current search criteria</p>
return (
<div data-cy="peoplePage">
<h1 className="title">People Page</h1>

{isLoading ? (
<Loader />
) : (
<div className="block">
<div className="columns is-desktop is-flex-direction-row-reverse">
<div className="column is-7-tablet is-narrow-desktop">
<PeopleFilters />
</div>

<PeopleTable />
<div className="column">
<div className="box table-container">
{loadingError ? (
<p data-cy="peopleLoadingError" className="has-text-danger">
Something went wrong
</p>
) : (
<div>
{people.length === 0 ? (
<p data-cy="noPeopleMessage">
There are no people on the server
</p>
) : (
<PeopleTable
people={people}
filtredPeople={filtredPeople}
/>
)}
</div>
)}
</div>
</div>
</div>
</div>
</div>
</>
)}
</div>
);
};
Loading