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

Task solution #1069

Open
wants to merge 2 commits 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://Poviakalo.github.io/react_people-table-advanced/) and add it to the PR description.
9 changes: 4 additions & 5 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { PeoplePage } from './components/PeoplePage';
import { Navbar } from './components/Navbar';
import { Outlet } from 'react-router-dom';

import './App.scss';

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

export const App = () => {
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 />
<Outlet />
</div>
</div>
</div>
Expand Down
26 changes: 26 additions & 0 deletions src/Root.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {
HashRouter as Router,
Routes,
Route,
Navigate,
} from 'react-router-dom';

import { App } from './App';
import { PeoplePage } from './pages/PeoplePage';
import { PageNotFound } from './pages/PageNotFound';
import { HomePage } from './pages/HomePage';

export const Root = () => (
<Router>
<Routes>
<Route path="/" element={<App />}>
<Route path="/home" element={<Navigate to="/" />} />
<Route index element={<HomePage />} />
<Route path="people">
<Route path=":personSlug?" element={<PeoplePage />} />
</Route>
<Route path="*" element={<PageNotFound />} />
</Route>
</Routes>
</Router>
);
26 changes: 0 additions & 26 deletions src/components/Navbar.tsx

This file was deleted.

32 changes: 32 additions & 0 deletions src/components/Navbar/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import classNames from 'classnames';
import React from 'react';
import { NavLink } from 'react-router-dom';

export const Navbar: React.FC = () => {
const getClassNameLink = ({ isActive }: { isActive: boolean }) => {
return classNames('navbar-item', {
'has-background-grey-lighter': isActive,
});
};

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={getClassNameLink} to="/">
Home
</NavLink>

<NavLink className={getClassNameLink} 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';
150 changes: 97 additions & 53 deletions src/components/PeopleFilters.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,72 @@
import { Link, useSearchParams } from 'react-router-dom';
import classnames from 'classnames';
import { getSearchWith, SearchParams } from '../utils/searchHelper';
import { FilterSex } from '../types';
import { SearchLink } from './SearchLink';

export const PeopleFilters = () => {
const [searchParams, setSearchParams] = useSearchParams();
const query = searchParams.get('query') || '';
const centuries = searchParams.getAll('centuries') || [];
const sexSearch = searchParams.get('sex');

function setSearchWith(params: SearchParams) {
const search = getSearchWith(searchParams, params);

setSearchParams(search);
}

function getCenturies(min: number, max: number) {
const centuryNumbers = [];

for (let i = min; i <= max; i++) {
centuryNumbers.push(i);
}

return centuryNumbers;
}

function handleQueryChange(e: React.ChangeEvent<HTMLInputElement>) {
setSearchWith({ query: e.target.value || null });
}

const getLinkStyle = (item: FilterSex) => {
if (
(item === FilterSex.All && !sexSearch) ||
(item === FilterSex.Male && sexSearch === 'm') ||
(item === FilterSex.Female && sexSearch === 'f')
) {
return 'is-active';
}

return '';
};

const getSexFilter = (sexFilter: FilterSex) => {
switch (sexFilter) {
case 'Female':
return 'f';
case 'Male':
return 'm';
default:
return null;
}
};

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>
{Object.values(FilterSex).map(item => (
<SearchLink
key={item}
params={{ sex: getSexFilter(item) }}
className={getLinkStyle(item)}
>
{item}
</SearchLink>
))}
</p>

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

<span className="icon is-left">
Expand All @@ -33,63 +89,51 @@ 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>
{getCenturies(16, 20).map(century => (
<Link
to={{
search: getSearchWith(searchParams, {
centuries: centuries.includes(`${century}`)
? centuries.filter(cent => cent !== `${century}`)
: [...centuries, `${century}`],
}),
}}
data-cy="century"
className={classnames('button', 'mr-1', {
'is-info': centuries.includes(`${century}`),
})}
key={century}
>
{century}
</Link>
))}
</div>

<div className="level-right ml-4">
<a
<Link
to={{ search: getSearchWith(searchParams, { centuries: null }) }}
data-cy="centuryALL"
className="button is-success is-outlined"
href="#/people"
>
All
</a>
</Link>
</div>
</div>
</div>

<div className="panel-block">
<a className="button is-link is-outlined is-fullwidth" href="#/people">
<Link
to={{
search: getSearchWith(searchParams, {
centuries: null,
query: null,
sex: null,
}),
}}
className="button is-link is-outlined is-fullwidth"
>
Reset all filters
</a>
</Link>
</div>
</nav>
);
Expand Down
33 changes: 0 additions & 33 deletions src/components/PeoplePage.tsx

This file was deleted.

Loading
Loading