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://whomngmnt.github.io/react_people-table-advanced/) and add it to the PR description.
34 changes: 20 additions & 14 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
import { PeoplePage } from './components/PeoplePage';
import { Navigate, Route, Routes } from 'react-router-dom';
import { Navbar } from './components/Navbar';
import { HomePage } from './pages/HomePage';
import { NotFoundPage } from './pages/NotFoundPage';
import { PeoplePage } from './components/PeoplePage';

import './App.scss';

export const App = () => {
return (
<div data-cy="app">
<Navbar />
export const App = () => (
<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>
<main className="section">
<div className="container">
<Routes>
<Route path="/" element={<HomePage />} />
<Route path="/people" element={<PeoplePage />} />
<Route path="/people/:slug" element={<PeoplePage />} />
<Route path="/home" element={<Navigate to="/" replace />} />
<Route path="/not-found" element={<NotFoundPage />} />
<Route path="*" element={<NotFoundPage />} />
</Routes>
</div>
</div>
);
};
</main>
</div>
);
1 change: 0 additions & 1 deletion src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ function wait(delay: number) {
}

export async function getPeople(): Promise<Person[]> {
// keep this delay for testing purpose
return wait(500)
.then(() => fetch(API_URL))
.then(response => response.json());
Expand Down
32 changes: 25 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 { NavLink, useLocation } from 'react-router-dom';

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

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

<a
aria-current="page"
className="navbar-item has-background-grey-lighter"
href="#/people"
<NavLink
to={{
pathname: '/people',
search,
}}
className={({ isActive }) =>
classNames('navbar-item', {
'is-active has-background-grey-lighter': isActive,
})
}
>
People
</a>
</NavLink>
</div>
</div>
</nav>
Expand Down
133 changes: 82 additions & 51 deletions src/components/PeopleFilters.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,61 @@
import classNames from 'classnames';
import { ChangeEvent } from 'react';
import { useSearchParams } from 'react-router-dom';
import { SearchLink } from './SearchLink';
import { getSearchWith } from '../utils/searchHelper';

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

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

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

const handleQueryChange = (event: ChangeEvent<HTMLInputElement>) => {
const newQuery = event.target.value;

setSearchParams(
getSearchWith(searchParams, {
query: newQuery || null,
}),
);
};

const getToggledCenturies = (century: string) => {
if (selectedCenturies.includes(century)) {
return selectedCenturies.filter(item => item !== century);
}

return [...selectedCenturies, century];
};

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

<p className="panel-tabs" data-cy="SexFilter">
<a className="is-active" href="#/people">
<SearchLink
params={{ sex: null }}
className={classNames({ 'is-active': currentSex === null })}
>
All
</a>
<a className="" href="#/people?sex=m">
</SearchLink>

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

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

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

<span className="icon is-left">
Expand All @@ -33,63 +78,49 @@ 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>
{centuryOptions.map(century => (
<SearchLink
key={century}
data-cy="century"
className={classNames('button mr-1', {
'is-info': selectedCenturies.includes(century),
})}
params={{
centuries: getToggledCenturies(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', {
'is-info': selectedCenturies.length === 0,
})}
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={{
sex: null,
query: null,
centuries: null,
sort: null,
order: null,
}}
>
Reset all filters
</a>
</SearchLink>
</div>
</nav>
);
Expand Down
Loading
Loading