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

1.0 #545

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

1.0 #545

Show file tree
Hide file tree
Changes from 3 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://victor-buldenko.github.io/react_people-table-advanced/) and add it to the PR description.
11 changes: 2 additions & 9 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PeoplePage } from './components/PeoplePage';
import { Outlet } from 'react-router-dom';
import { Navbar } from './components/Navbar';

import './App.scss';
Expand All @@ -7,14 +7,7 @@ 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>
</div>
<Outlet />
</div>
);
};
7 changes: 4 additions & 3 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { Person } from './types/Person';

// eslint-disable-next-line max-len
const API_URL = 'https://mate-academy.github.io/react_people-table/api/people.json';
const API_URL
= 'https://mate-academy.github.io/react_people-table/api/people.json';

function wait(delay: number) {
return new Promise(resolve => setTimeout(resolve, delay));
return new Promise((resolve) => setTimeout(resolve, delay));
}

export async function getPeople(): Promise<Person[]> {
// keep this delay for testing purpose
return wait(500)
.then(() => fetch(API_URL))
.then(response => response.json());
.then((response) => response.json());
}
52 changes: 52 additions & 0 deletions src/components/Century.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { SetURLSearchParams } from 'react-router-dom';
import cn from 'classnames';

type Props = {
centuries: string[];
searchParams: URLSearchParams;
setSearchParams: SetURLSearchParams;
century: string;
};

export const Century: React.FC<Props> = ({
centuries,
searchParams,
setSearchParams,
century,
}) => {
const findParams = (str: string) => {
return centuries.includes(str);
};

const centuriesHandler = (
e: React.MouseEvent<HTMLAnchorElement, MouseEvent>,
arg: string,
) => {
e.preventDefault();

const elmIndex = centuries.findIndex((a) => a === arg);

if (elmIndex > -1) {
centuries.splice(elmIndex, 1);
} else {
centuries.push(arg);
}

const params = new URLSearchParams(searchParams);

params.delete('centuries');
centuries.forEach((el) => params.append('centuries', el));
setSearchParams(params);
};

return (
<a
data-cy="century"
className={cn('button mr-1', { 'is-info': findParams(century) })}
onClick={(e) => centuriesHandler(e, century)}
href={`#/people?centuries=${century}`}
>
{century}
</a>
);
};
24 changes: 19 additions & 5 deletions src/components/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { NavLink } from 'react-router-dom';
import cn from 'classnames';

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

<a
<NavLink
aria-current="page"
className="navbar-item has-background-grey-lighter"
href="#/people"
className={({ isActive }) => cn(
'navbar-item',
{ 'has-background-grey-lighter': isActive },
)}
to="/people"
>
People
</a>
</NavLink>
</div>
</div>
</nav>
Expand Down
150 changes: 106 additions & 44 deletions src/components/PeopleFilters.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,95 @@
export const PeopleFilters = () => {
import {
Link,
SetURLSearchParams,
useNavigate,
useParams,
} from 'react-router-dom';
import cn from 'classnames';
import { Century } from './Century';

const centuriesButtons = ['16', '17', '18', '19', '20'];

type Props = {
searchParams: URLSearchParams;
setSearchParams: SetURLSearchParams;
};

export const PeopleFilters: React.FC<Props> = ({
searchParams,
setSearchParams,
}) => {
const { human } = useParams();
const query = searchParams.get('query') ?? '';
const centuries = searchParams.getAll('centuries') ?? [];
const navigate = useNavigate();

const resetFilter = (e: React.MouseEvent<HTMLAnchorElement, MouseEvent>) => {
e.preventDefault();
navigate(`/people/${human}`);
};

const pickAllCenturies = (
e: React.MouseEvent<HTMLAnchorElement, MouseEvent>,
) => {
e.preventDefault();

const params = new URLSearchParams(searchParams);

params.delete('centuries');
centuriesButtons.forEach((el) => params.append('centuries', el));
setSearchParams(params);
};

const queryHandler = (e: React.ChangeEvent<HTMLInputElement>) => {
const queryObj = new URLSearchParams(searchParams);

queryObj.set('query', e.target.value);
setSearchParams(queryObj);
};

const sexHandler = (
e: React.MouseEvent<HTMLAnchorElement, MouseEvent>,
arg: string,
) => {
e.preventDefault();

const params = new URLSearchParams(searchParams);

if (!arg) {
params.delete('sex');
} else {
params.set('sex', arg);
}

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>
<Link
className={cn({ 'is-active': searchParams.get('sex') === null })}
to=".."
onClick={(e) => sexHandler(e, '')}
>
All
</Link>
<Link
className={cn({ 'is-active': searchParams.get('sex') === 'm' })}
to="?sex=m"
onClick={(e) => sexHandler(e, 'm')}
>
Male
</Link>
<Link
className={cn({ 'is-active': searchParams.get('sex') === 'f' })}
to="?sex=f"
onClick={(e) => sexHandler(e, 'f')}
>
Female
</Link>
</p>

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

<span className="icon is-left">
Expand All @@ -27,52 +112,28 @@ 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>
{centuriesButtons.map((el) => (
<Century
key={el}
centuries={centuries}
searchParams={searchParams}
setSearchParams={setSearchParams}
century={el}
/>
))}
</div>

<div className="level-right ml-4">
<a
data-cy="centuryALL"
className="button is-success is-outlined"
// className="button is-success is-outlined"
className={`button ${
centuries.length === centuriesButtons.length
? 'is-success'
: 'is-outlined'
}`}
href="#/people"
onClick={(e) => pickAllCenturies(e)}
>
All
</a>
Expand All @@ -84,6 +145,7 @@ export const PeopleFilters = () => {
<a
className="button is-link is-outlined is-fullwidth"
href="#/people"
onClick={(e) => resetFilter(e)}
>
Reset all filters
</a>
Expand Down
Loading