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

import { PeoplePage } from './components/PeoplePage';
import { Navbar } from './components/Navbar';

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>
<div className="section">
<div className="container">
<Routes>
<Route path="/" element={<h1 className="title mt-6">Home Page</h1>} />
<Route path="home" element={<Navigate to="/" replace />} />
<Route path="people">
<Route index element={<PeoplePage />} />

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Leaving console.log in production code is usually discouraged; consider removing it after you finish debugging to keep the console clean and avoid noise during automated checks.

<Route path=":slug" element={<PeoplePage />} />
</Route>
<Route path="*" element={<h1 className="title">Page not found</h1>} />
</Routes>
</div>
</div>
);
};
</div>
);
45 changes: 45 additions & 0 deletions src/components/CenturyFilter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import classNames from 'classnames';
import { useSearchParams } from 'react-router-dom';
import { SearchLink } from './SearchLink';

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

export const CenturyFilter = () => {
const [searchParams] = useSearchParams();
const centuries = searchParams.getAll('centuries');

return (
<div className="level is-flex-grow-1 is-mobile" data-cy="CenturyFilter">
<div className="level-left">
{AVAILABLE_CENTURIES.map(century => (
<SearchLink
data-cy="century"
key={century}
className={classNames('button', 'mr-1', {
'is-info': centuries.includes(century),
})}
params={{
centuries: centuries.includes(century) // if this century is already selected
? centuries.filter(c => c !== century) // we remove it from the selected
: [...centuries, century], // otherwise we add it to the selected
}}
>
{century}
</SearchLink>
))}
</div>

<div className="level-right ml-4">
<SearchLink
data-cy="centuryALL"
params={{ centuries: null }}
className={classNames('button', 'is-success', {
'is-outlined': centuries.length > 0,
})}
>
All
</SearchLink>
</div>
</div>
);
};
29 changes: 29 additions & 0 deletions src/components/NameFilter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { useSearchParams } from 'react-router-dom';
import { getSearchWith } from '../utils/searchHelper';

export const NameFilter = () => {
const [searchParams, setSearchParams] = useSearchParams();
const query = searchParams.get('query') || '';

return (
<p className="control has-icons-left">
<input
data-cy="NameFilter"
value={query}
onChange={event =>
setSearchParams(
getSearchWith(searchParams, {
query: event.target.value || null,
}),
)
}
className="input"
type="text"
placeholder="Search"
/>
<span className="icon is-left">
<i className="fas fa-search" aria-hidden="true" />
</span>
</p>
);
};
27 changes: 21 additions & 6 deletions src/components/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
import classNames from 'classnames';
import { NavLink, useLocation } from 'react-router-dom';

type Status = {
isActive: boolean;
};

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

export const Navbar = () => {
const location = useLocation();

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

<a
<NavLink
aria-current="page"
className="navbar-item has-background-grey-lighter"
href="#/people"
className={getNavLinkClassName}
to={{ pathname: 'people', search: location.search }}
>
People
</a>
</NavLink>
</div>
</div>
</nav>
Expand Down
107 changes: 34 additions & 73 deletions src/components/PeopleFilters.tsx
Original file line number Diff line number Diff line change
@@ -1,95 +1,56 @@
import { useSearchParams } from 'react-router-dom';
import classNames from 'classnames';

import { CenturyFilter } from './CenturyFilter';
import { NameFilter } from './NameFilter';
import { SearchLink } from './SearchLink';

export const PeopleFilters = () => {
const [searchParams] = useSearchParams();
const sex = searchParams.get('sex');

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': !sex })}
>
All
</a>
<a className="" href="#/people?sex=m">
</SearchLink>

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

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

<div className="panel-block">
<p className="control has-icons-left">
<input
data-cy="NameFilter"
type="search"
className="input"
placeholder="Search"
/>

<span className="icon is-left">
<i className="fas fa-search" aria-hidden="true" />
</span>
</p>
<NameFilter />
</div>

<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>
</div>

<div className="level-right ml-4">
<a
data-cy="centuryALL"
className="button is-success is-outlined"
href="#/people"
>
All
</a>
</div>
</div>
<CenturyFilter />
</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 }}
>
Reset all filters
</a>
</SearchLink>
</div>
</nav>
);
Expand Down
Loading
Loading