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

solution #1133

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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://tonni004.github.io/react_people-table-advanced/) and add it to the PR description.
9 changes: 5 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"devDependencies": {
"@cypress/react18": "^2.0.1",
"@mate-academy/scripts": "^1.8.5",
"@mate-academy/scripts": "^1.9.12",
"@mate-academy/students-ts-config": "*",
"@mate-academy/stylelint-config": "*",
"@types/node": "^20.14.10",
Expand Down
18 changes: 14 additions & 4 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { PeoplePage } from './components/PeoplePage';
import { Routes, Route, Outlet, Navigate } from 'react-router-dom';
import { PeoplePage } from './pages/PeoplePage';
import { HomePage } from './pages/HomePage';
import { PageNotFound } from './pages/PageNotFound';
import { Navbar } from './components/Navbar';

import './App.scss';
Expand All @@ -10,11 +13,18 @@ export const App = () => {

<div className="section">
<div className="container">
<h1 className="title">Home Page</h1>
<h1 className="title">Page not found</h1>
<PeoplePage />
<Outlet />
</div>
</div>

<Routes>
<Route path="/" element={<HomePage />} />
<Route path="/home" element={<Navigate to="/" />} />
<Route path="people" element={<PeoplePage />}>
<Route path=":slug" element={<PeoplePage />} />
</Route>
<Route path="*" element={<PageNotFound />} />
Comment on lines +23 to +25

Choose a reason for hiding this comment

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

The nested route for PeoplePage is defined, but it might be worth checking if the PeoplePage component is designed to handle both the base path /people and the nested :slug path. If PeoplePage is not intended to handle both, consider creating a separate component for the nested route.

</Routes>
</div>
);
};
26 changes: 19 additions & 7 deletions src/components/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { NavLink } from 'react-router-dom';

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

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

const centuriesValue: string[] = ['16', '17', '18', '19', '20'];

export const PeopleFilters: React.FC = () => {
const [searchParams, setSearchParams] = useSearchParams();
const query = searchParams.get('query') || '';
const sex = searchParams.get('sex') || '';
const centuries = searchParams.getAll('centuries') || [];
const resetParams = {
query: null,
sex: null,
centuries: null,
};

Comment on lines +13 to +17

Choose a reason for hiding this comment

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

The resetParams object sets query, sex, and centuries to null. However, when using URLSearchParams, setting a parameter to null does not remove it from the URL. Consider using params.delete('key') to remove parameters or set them to an empty string if you want to clear them.

const isActiveSex = (value: string | null) => {
return value === sex;
};

const getCenturies = (century: string) => {
return centuries.includes(century)
? centuries.filter(currentCentury => currentCentury !== century)
: [...centuries, century];
};

function handleQueryChange(event: React.ChangeEvent<HTMLInputElement>) {
const params = new URLSearchParams(searchParams);

params.set('query', event.target.value);
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">
<SearchLink
className={classNames({ 'is-active': isActiveSex(null) })}
params={{ sex: null }}
>
All
</a>
<a className="" href="#/people?sex=m">
</SearchLink>
<SearchLink
className={classNames({ 'is-active': isActiveSex('m') })}
params={{ sex: 'm' }}
>
Male
</a>
<a className="" href="#/people?sex=f">
</SearchLink>
<SearchLink
className={classNames({ 'is-active': isActiveSex('f') })}
params={{ sex: '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,43 @@ 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>
{centuriesValue.map(century => {
return (
<SearchLink
key={century}
data-cy="century"
className={classNames('button mr-1', {
'is-info': centuries.includes(century),
})}
params={{ centuries: getCenturies(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': centuries.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={resetParams}
>
Reset all filters
</a>
</SearchLink>
</div>
</nav>
);
Expand Down
33 changes: 0 additions & 33 deletions src/components/PeoplePage.tsx

This file was deleted.

Loading
Loading