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

working versiun #530

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 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://andrzejpec.github.io/react_people-table-advanced/) and add it to the PR description.
23 changes: 16 additions & 7 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
import './App.scss';
import { Routes, Route, Navigate } from 'react-router-dom';
import { HomePage } from './components/HomePage';
import { PeoplePage } from './components/PeoplePage';
import { PageNotFound } from './components/PageNotFound';
import { Navbar } from './components/Navbar';

import './App.scss';

export const App = () => {
return (
<div data-cy="app">

<Navbar />

<div className="section">
<main 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="/home" element={<Navigate to="/" replace />} />
<Route path="/people" element={<PeoplePage />} />
<Route path="/people/:slug" element={<PeoplePage />} />
<Route path="/*" element={<PageNotFound />} />
</Routes>

</div>
</div>
</main>
</div>
);
};
37 changes: 37 additions & 0 deletions src/SearchParamsContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { createContext, useContext, ReactNode } from 'react';
import { useSearchParams } from 'react-router-dom';

interface SearchParamsContextProps {
searchParams: URLSearchParams;
setSearchParams: (params: URLSearchParams) => void;
}

const SearchParamsContext = createContext<SearchParamsContextProps
| undefined>(undefined);

export const useSearchParamsContext = () => {
const context = useContext(SearchParamsContext);

if (!context) {
throw new
Error('useSearchParamsContext must be used within a SearchParamsProvider');
}

return context;
};

interface SearchParamsProviderProps {
children: ReactNode;
}

export const SearchParamsProvider: React.FC<SearchParamsProviderProps> = ({
children,
}) => {
const [searchParams, setSearchParams] = useSearchParams();

return (
<SearchParamsContext.Provider value={{ searchParams, setSearchParams }}>
{children}
</SearchParamsContext.Provider>
);
};
5 changes: 5 additions & 0 deletions src/components/HomePage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const HomePage = () => {
return (
<h1 className="title">Home Page</h1>
);
};
24 changes: 18 additions & 6 deletions src/components/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { NavLink } from 'react-router-dom';
import classNames from 'classnames';

export const Navbar = () => {
return (
<nav
Expand All @@ -7,16 +10,25 @@ export const Navbar = () => {
aria-label="main navigation"
>
<div className="container">

<div className="navbar-brand">
<a className="navbar-item" href="#/">Home</a>
<NavLink
className={({ isActive }) => classNames('navbar-item', {
'has-background-grey-lighter': isActive,
})}
to="/"
>
Home
</NavLink>

<a
aria-current="page"
className="navbar-item has-background-grey-lighter"
href="#/people"
<NavLink
className={({ isActive }) => classNames('navbar-item', {
'has-background-grey-lighter': isActive,
})}
to="/people"
>
People
</a>
</NavLink>
</div>
</div>
</nav>
Expand Down
5 changes: 5 additions & 0 deletions src/components/PageNotFound.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const PageNotFound = () => {
return (
<h1 className="title">Page not found</h1>
);
};
138 changes: 89 additions & 49 deletions src/components/PeopleFilters.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,77 @@
import { useSearchParamsContext } from '../SearchParamsContext';
import { SexFilter } from './SexFilter';

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

export const PeopleFilters = () => {
const { searchParams, setSearchParams } = useSearchParamsContext();

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

// const selectedSex = searchParams.get('sex') || 'all';

// const handleSexFilterChange = (sex: string) => {
// if (sex === 'all') {
// searchParams.delete('sex');
// } else {
// searchParams.set('sex', sex);
// }

// setSearchParams(new URLSearchParams(searchParams.toString()));
// };
AndrzejPec marked this conversation as resolved.
Show resolved Hide resolved

const handleNameFilterChange = (event: React.ChangeEvent<
HTMLInputElement
>) => {
AndrzejPec marked this conversation as resolved.
Show resolved Hide resolved
const query = event.target.value;

if (query) {
searchParams.set('query', query);
} else {
searchParams.delete('query');
}

setSearchParams(new URLSearchParams(searchParams.toString()));
};

const handleResetCenturyFilters = () => {
const query = searchParams.get('query');

searchParams.delete('centuries');

const newParams = new URLSearchParams(searchParams.toString());

if (query) {
newParams.set('query', query);
}

setSearchParams(newParams);
};

const handleCenturyFilterChange = (century: string) => {
const currentCenturies = searchParams.getAll('centuries');

if (currentCenturies.includes(century)) {
const newCenturies = currentCenturies.filter(c => c !== century);

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

setSearchParams(new URLSearchParams(searchParams.toString()));
};

const handleResetFilters = () => {
setSearchParams(new URLSearchParams());
};

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

<p className="panel-tabs" data-cy="SexFilter">
<a className="is-active" href="#/people">All</a>
<a className="" href="#/people?sex=m">Male</a>
<a className="" href="#/people?sex=f">Female</a>
</p>
<SexFilter />

<div className="panel-block">
<p className="control has-icons-left">
Expand All @@ -16,6 +80,8 @@ export const PeopleFilters = () => {
type="search"
className="input"
placeholder="Search"
onChange={handleNameFilterChange}
value={inputValue}
/>

<span className="icon is-left">
Expand All @@ -27,55 +93,28 @@ 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>
{CENTURIES.map((century) => (
<button
key={century}
data-cy="century"
type="button"
className={`button mr-1 ${selectedCenturies.includes(century) ? 'is-info' : ''}`}
onClick={() => handleCenturyFilterChange(century)}
>
{century}
</button>
))}
</div>

<div className="level-right ml-4">
<a
<button
data-cy="centuryALL"
className="button is-success is-outlined"
href="#/people"
type="button"
className={`button is-outlined ${selectedCenturies.length === 0 ? 'is-success' : 'is-success'}`}
onClick={handleResetCenturyFilters}
>
All
</a>
</button>
</div>
</div>
</div>
Expand All @@ -84,6 +123,7 @@ export const PeopleFilters = () => {
<a
className="button is-link is-outlined is-fullwidth"
href="#/people"
onClick={handleResetFilters}
>
Reset all filters
</a>
Expand Down
Loading
Loading