Skip to content
Open
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://Palfinger2002.github.io/react_people-table-advanced/) and add it to the PR description.
4 changes: 4 additions & 0 deletions src/App.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
iframe {
display: none;
}

h1 {
margin-top: 52px;
}
24 changes: 17 additions & 7 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
import { PeoplePage } from './components/PeoplePage';
import { Navbar } from './components/Navbar';

import './App.scss';
import { Navbar } from './components/Navbar';
import { HomePage } from './components/HomePage/HomePage';
import { PeoplePage } from './components/PeoplePage/PeoplePage';
import { Navigate, Route, Routes } from 'react-router-dom';
import { NotFoundPage } from './components/NotFoundPage';

export const App = () => {
export const App: React.FC = () => {
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 />
<Routes>
<Route path="/" element={<HomePage />} />

<Route path="people">
<Route index element={<PeoplePage />} />
<Route path=":slug?" element={<PeoplePage />} />
Comment on lines +15 to +20

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The href on line 20 is hardcoded to #/people?centuries=${century} which doesn't preserve other existing search params like query or sex. When clicked, the browser briefly shows this URL before the onClick handler updates it to include all params. Consider using the SearchLink component or building the proper URL string with all existing params.

</Route>

<Route path="/home" element={<Navigate to="/" replace />} />
<Route path="*" element={<NotFoundPage />} />
</Routes>
</div>
</div>
</div>
Expand Down
60 changes: 60 additions & 0 deletions src/components/CenturyFilter/CenturyFilter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import classNames from 'classnames';
import { useSearchParams } from 'react-router-dom';

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

const CENTURIES = ['16', '17', '18', '19', '20'];
const values = searchParams.getAll('centuries');

return (
<div className="level is-flex-grow-1 is-mobile" data-cy="CenturyFilter">
<div className="level-left">
{CENTURIES.map(century => {
return (
<a
key={century}
data-cy="century"
className={classNames('button mr-1', {
'is-info': values.includes(century),
})}
href={`#/people?centuries=${century}`}
onClick={event => {
event.preventDefault();
if (values.includes(century)) {
const newValues = values.filter(value => value !== century);

searchParams.delete('centuries');
newValues.forEach(value =>
searchParams.append('centuries', value),
);
} else {
searchParams.append('centuries', century);
}

setSearchParams(searchParams);
}}
>
{century}
</a>
);
})}
</div>

<div className="level-right ml-4">
<a
data-cy="centuryALL"
className="button is-success is-outlined"
href="#/people"
onClick={event => {
event.preventDefault();
searchParams.delete('centuries');
setSearchParams(searchParams);
}}
>
All
</a>
</div>
</div>
);
};
1 change: 1 addition & 0 deletions src/components/CenturyFilter/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './CenturyFilter';
3 changes: 3 additions & 0 deletions src/components/HomePage/HomePage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const HomePage = () => {
return <h1 className="title">Home Page</h1>;
};
1 change: 1 addition & 0 deletions src/components/HomePage/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './HomePage';
22 changes: 22 additions & 0 deletions src/components/NameFilter/NameFilter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { useSearchParams } from 'react-router-dom';
import { getSearchWith } from '../../helpers/getSearchWith';

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

return (
<input
data-cy="NameFilter"
type="search"
className="input"
placeholder="Search"
value={query}
onChange={event => {
const value = event.target.value;

setSearchParams(getSearchWith({ searchParams, key: 'query', value }));
}}
/>
);
};
1 change: 1 addition & 0 deletions src/components/NameFilter/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './NameFilter';
31 changes: 31 additions & 0 deletions src/components/Navbar/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import classNames from 'classnames';
import { NavLink } from 'react-router-dom';

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

export const Navbar: React.FC = () => {
return (
<nav
data-cy="nav"
className="navbar is-fixed-top has-shadow"
role="navigation"
aria-label="main navigation"
>
<div className="container">
<div className="navbar-brand">
<NavLink className={getLinkClass} to="/">
Home
</NavLink>

<NavLink className={getLinkClass} to="/people">
People
</NavLink>
</div>
</div>
</nav>
);
};
1 change: 1 addition & 0 deletions src/components/Navbar/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Navbar';
3 changes: 3 additions & 0 deletions src/components/NotFoundPage/NotFoundPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const NotFoundPage = () => {
return <h1 className="title">Page not found</h1>;
};
1 change: 1 addition & 0 deletions src/components/NotFoundPage/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './NotFoundPage';
105 changes: 38 additions & 67 deletions src/components/PeopleFilters.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,42 @@
import { NameFilter } from './NameFilter';
import { CenturyFilter } from './CenturyFilter';
import { useSearchParams } from 'react-router-dom';
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
className={sex !== 'm' && sex !== 'f' ? 'is-active' : ''}
params={{ sex: null }}
>
All
</a>
<a className="" href="#/people?sex=m">
</SearchLink>

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

<SearchLink
className={sex === 'f' ? 'is-active' : ''}
params={{ 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"
/>
<NameFilter />

<span className="icon is-left">
<i className="fas fa-search" aria-hidden="true" />
Expand All @@ -31,65 +45,22 @@ export const PeopleFilters = () => {
</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={{
query: null,
centuries: null,
sort: null,
order: null,
sex: null,
}}
>
Reset all filters
</a>
</SearchLink>
</div>
</nav>
);
Expand Down
Loading
Loading