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

Develop #1178

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Develop #1178

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
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,22 @@ implement the ability to filter and sort people in the table.
1. Keep search params when navigating within the `People` page (when selecting a person or clicking the `People` link).
1. The sidebar with the filters should appear only when people are loaded.
1. `NameFilter` should update the `query` search param with the text from the input.
- show only people with the `name`, `motherName` or `fatherName` that match the query case insensitive;
- if the input is empty there should not be `query` in the search params.
- show only people with the `name`, `motherName` or `fatherName` that match the query case insensitive;
- if the input is empty there should not be `query` in the search params.
1. `CenturyFilter` should allow to choose several centuries or all of them.
- add `centuries` search params using `append` method `getAll` method;
- add `centuries` search params using `append` method `getAll` method;
1. Implement sorting by `name`, `sex`, `born` and `died` by clicking on arrows in a `th`;
- the first click on a column sorts people by the selected field ascending (`a -> z` or `0 -> 9`);
- the second click (when people are already sorted ascending by this field) reverses the order of sorting;
- the third click (when people are already sorted in reversed order by this field) disables sorting;
- use `sort` search param to save sort field;
- add `order=desc` (short for `descending`) if sorted in reversed order;
- if sorting is disabled there should not be `sort` and `order` search params;
- the first click on a column sorts people by the selected field ascending (`a -> z` or `0 -> 9`);
- the second click (when people are already sorted ascending by this field) reverses the order of sorting;
- the third click (when people are already sorted in reversed order by this field) disables sorting;
- use `sort` search param to save sort field;
- add `order=desc` (short for `descending`) if sorted in reversed order;
- if sorting is disabled there should not be `sort` and `order` search params;

## Instructions

- Install Prettier Extention and use this [VSCode settings](https://mate-academy.github.io/fe-program/tools/vscode/settings.json) to enable format on save.
- 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://Andrii-Yanchuk.github.io/react_people-table-advanced/) and add it to the PR description.
13 changes: 5 additions & 8 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import { PeoplePage } from './components/PeoplePage';
import { Navbar } from './components/Navbar';

import './App.scss';
import { Outlet } from 'react-router-dom';
import { Navbar } from './components/Navbar';

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 />
<Outlet />
</div>
</div>
</main>
</div>
);
};
30 changes: 30 additions & 0 deletions src/Root.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {
HashRouter as Router,
Navigate,
Route,
Routes,
} from 'react-router-dom';
import { App } from './App';
import { HomePage } from './pages/HomePage';
import { NotFoundPage } from './pages/NotFoundPage';
import { PeoplePage } from './pages/PeoplePage';

export const Root = () => {
return (
<Router>
<Routes>
<Route path="/" element={<App />}>
<Route index element={<HomePage />} />
<Route path="/home" element={<Navigate replace to="/" />} />

<Route path="people">
<Route index element={<PeoplePage />} />
<Route path=":slug?" element={<PeoplePage />} />
</Route>

<Route path="*" element={<NotFoundPage />} />
</Route>
</Routes>
</Router>
);
};
26 changes: 0 additions & 26 deletions src/components/Navbar.tsx

This file was deleted.

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

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

export const Navbar = () => {
const [searchParams] = useSearchParams();

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

<NavLink
to={{ pathname: 'people', search: searchParams.toString() }}
className={getLinkClass}
>
People
</NavLink>
</div>
</div>
</nav>
);
};
1 change: 1 addition & 0 deletions src/components/Navbar/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Navbar';
96 changes: 0 additions & 96 deletions src/components/PeopleFilters.tsx

This file was deleted.

110 changes: 110 additions & 0 deletions src/components/PeopleFilters/PeopleFilters.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { useSearchParams } from 'react-router-dom';
import cn from 'classnames';
import { SearchLink } from '../SearchLink';
import { getSearchWith, SearchParams } from '../../utils/searchHelper';
import { CENTURIES, PersonSex } from '../../utils/filters';

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

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

setSearchParams(search);
}

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

<p className="panel-tabs" data-cy="SexFilter">
{Object.values(PersonSex).map(sexFilter => {

Choose a reason for hiding this comment

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

That's not a big deal, but you could create an enum Sex { All = '', Female = 'f', Male = 'm' } and use Object.keys() to create the same tabs from the example. A key would play a role of tab's title (All. Female. Male) and key's value would be a search parameter ('', 'f', 'm').

const sexValue = sexFilter.charAt(0);
const filterTitle = sexValue.toUpperCase() + sexFilter.slice(1);

return (
<SearchLink
params={{ sex: sexFilter === PersonSex.All ? null : sexValue }}
key={sexFilter}
className={cn({
'is-active':
sex === sexValue ||
(sex === null && sexFilter === PersonSex.All),
})}
>
{filterTitle}
</SearchLink>
);
})}
</p>

<div className="panel-block">
<p className="control has-icons-left">
<input
data-cy="NameFilter"
type="search"
className="input"
placeholder="Search"
value={query}
onChange={event => {
setSearchWith({ query: event.target.value || null });
}}
/>

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

<div className="panel-block">
<div className="level is-flex-grow-1 is-mobile" data-cy="CenturyFilter">
<div className="level-left">
{CENTURIES.map(century => {
const currentCenturies = centuries.includes(century)

Choose a reason for hiding this comment

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

There is too much code here. I believe it would make sense to move some part of it to a separate function. To be more specific, the part where you filter your centuries array.

? centuries.filter(current => century !== current)
: [...centuries, century];

return (
<SearchLink
params={{ centuries: currentCenturies }}
key={century}
data-cy="century"
className={cn('button mr-1', {
'is-info': centuries.includes(century),
})}
>
{century}
</SearchLink>
);
})}
</div>

<div className="level-right ml-4">
<SearchLink
params={{ centuries: null }}
data-cy="centuryALL"
className={cn('button is-success', {
'is-outlined': centuries.length,
})}
>
All
</SearchLink>
</div>
</div>
</div>

<div className="panel-block">
<SearchLink
className="button is-link is-outlined is-fullwidth"
params={{ query: null, sex: null, centuries: null }}
>
Reset all filters
</SearchLink>
</div>
</nav>
);
};
1 change: 1 addition & 0 deletions src/components/PeopleFilters/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './PeopleFilters';
33 changes: 0 additions & 33 deletions src/components/PeoplePage.tsx

This file was deleted.

Loading
Loading