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

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

export const App = () => {
return (
Expand All @@ -10,9 +11,17 @@ 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="/home" element={<Navigate to="/" replace />} />
<Route index element={<h1 className="title">Home Page</h1>} />
<Route path="people">
<Route path=":slug?" element={<PeoplePage />} />
</Route>
<Route
path="*"
element={<h1 className="title">Page not found</h1>}
/>
</Routes>
</div>
</div>
</div>
Expand Down
28 changes: 21 additions & 7 deletions src/components/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import classNames from 'classnames';
import { NavLink } from 'react-router-dom';

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

<a
aria-current="page"
className="navbar-item has-background-grey-lighter"
href="#/people"
{/* aria-current="page" */}
<NavLink
className={({ isActive }) => {
return classNames('navbar-item', {
'has-background-grey-lighter': isActive,
});
}}
to="/people"
>
People
</a>
</NavLink>
</div>
</div>
</nav>
Expand Down
160 changes: 108 additions & 52 deletions src/components/PeopleFilters.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,80 @@
export const PeopleFilters = () => {
import classNames from 'classnames';
import React from 'react';
import { Link, useSearchParams } from 'react-router-dom';

type Param = string | number;
type Params = {
[key: string]: Param[] | Param | null;
};

export function getSearchWith(
params: Params,
search?: string | URLSearchParams,
) {
const newParams = new URLSearchParams(search);

for (const [key, value] of Object.entries(params)) {
if (value === null) {
newParams.delete(key);
} else if (Array.isArray(value)) {
newParams.delete(key);
value.forEach(item => newParams.append(key, item.toString()));
} else {
newParams.set(key, value.toString());
}
}

return newParams.toString();
}

type Props = {
query: string;
centuries: string[];
sex: string;
};

export const PeopleFilters: React.FC<Props> = ({ query, centuries, sex }) => {
const [searchParams, setSearchParams] = useSearchParams();

function setSearchWith(params: Params) {
const search = getSearchWith(params, searchParams);

setSearchParams(search);
}

function handleQueryChange(event: React.ChangeEvent<HTMLInputElement>) {
setSearchWith({ query: event.target.value || null });
}

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

<p className="panel-tabs" data-cy="SexFilter">
<a className="is-active" href="#/people">
<Link
className={classNames({
'is-active': sex === '',
})}
to={{ search: getSearchWith({ sex: null }, searchParams) }}
>
All
</a>
<a className="" href="#/people?sex=m">
</Link>
<Link
className={classNames({
'is-active': sex === 'm',
})}
to={{ search: getSearchWith({ sex: 'm' }, searchParams) }}
>
Male
</a>
<a className="" href="#/people?sex=f">
</Link>
<Link
className={classNames({
'is-active': sex === 'f',
})}
to={{ search: getSearchWith({ sex: 'f' }, searchParams) }}
>
Female
</a>
</Link>
</p>

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

<span className="icon is-left">
Expand All @@ -33,63 +97,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>

<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': centuries.includes(String(century)),
})}
to={{
search: getSearchWith(
{
centuries: centuries.includes(String(century))
? centuries.filter(cent => String(century) !== cent)
: [...centuries, century],
},
searchParams,
),
}}
>
{century}
</Link>
))}
</div>

<div className="level-right ml-4">
<a
<Link
data-cy="centuryALL"
className="button is-success is-outlined"
href="#/people"
className={classNames('button is-success', {
'is-outlined': centuries.length !== 0,
})}
to={{ search: getSearchWith({ centuries: null }, searchParams) }}
>
All
</a>
</Link>
</div>
</div>
</div>

<div className="panel-block">
<a className="button is-link is-outlined is-fullwidth" href="#/people">
<Link
className="button is-link is-outlined is-fullwidth"
to={{
search: getSearchWith(
{ centuries: null, sex: null, query: null },
searchParams,
),
}}
>
Reset all filters
</a>
</Link>
</div>
</nav>
);
Expand Down
Loading
Loading