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 #1154

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

Develop #1154

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://Krykunov.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;
}

.section {
margin-top: 3rem;
}
35 changes: 26 additions & 9 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,36 @@
import { PeoplePage } from './components/PeoplePage';
import { Navbar } from './components/Navbar';

import classNames from 'classnames';
import './App.scss';
import { NavLink, Outlet } from 'react-router-dom';

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

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

<div className="section">
<nav
data-cy="nav"
className="navbar is-fixed-top has-shadow"
role="navigation"
aria-label="main navigation"
>
<div className="container">
<h1 className="title">Home Page</h1>
<h1 className="title">Page not found</h1>
<PeoplePage />
<div className="navbar-brand">
<NavLink className={getLinkClass} to="/">
Home
</NavLink>

<NavLink className={getLinkClass} to="/people">
People
</NavLink>
</div>
</div>
</nav>

<div className="section">
<Outlet />
</div>
</div>
);
Expand Down
25 changes: 25 additions & 0 deletions src/Root.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Navigate, HashRouter as Router } from 'react-router-dom';
import { Routes, Route } from 'react-router-dom';
import { App } from './App';
import HomePage from './pages/HomePage';
import PeoplePage from './pages/PeoplePage';

const Root = () => {
return (
<Router>
<Routes>
<Route path="/" element={<App />}>
<Route index element={<HomePage />} />
<Route path="people/:slug?">
<Route index element={<PeoplePage />} />
<Route path=":slug" element={<PeoplePage />} />
</Route>
<Route path="*" element={<h1 className="title">Page not found</h1>} />
<Route path="home" element={<Navigate to="/" replace />} />
</Route>
</Routes>
</Router>
);
};

export default Root;
4 changes: 1 addition & 3 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Person } from './types/Person';
import { Person } from './types';

// eslint-disable-next-line operator-linebreak
const API_URL =
'https://mate-academy.github.io/react_people-table/api/people.json';

Expand All @@ -9,7 +8,6 @@ function wait(delay: number) {
}

export async function getPeople(): Promise<Person[]> {
// keep this delay for testing purpose
return wait(500)
.then(() => fetch(API_URL))
.then(response => response.json());
Expand Down
35 changes: 35 additions & 0 deletions src/components/ParentLink.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { FC } from 'react';
import classNames from 'classnames';
import { Link, useSearchParams } from 'react-router-dom';
import { Person } from '../types';

type Props = {
person: Person;
parent: 'mother' | 'father';
};

const ParentLink: FC<Props> = ({ person, parent }) => {
const [searchParams] = useSearchParams();

if (!person[parent] && !person[`${parent}Name`]) {
return '-';
}

return person[parent] ? (
<Link
to={{
pathname: `/people/${person[parent].slug}`,
search: searchParams.toString(),
}}
className={classNames({
'has-text-danger': parent === 'mother',
})}
>
{person[parent].name}
</Link>
) : (
person[`${parent}Name`]
);
};

export default ParentLink;
120 changes: 66 additions & 54 deletions src/components/PeopleFilters.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,42 @@
export const PeopleFilters = () => {
import { FC } from 'react';
import { SearchLink } from './SearchLink';
import { SexFilterValues } from '../types';

type Props = {
query: string;
handleQueryChange: (query: string) => void;
sex: string;
centuriesList: string[];
centuries: string[] | null;
};

export const PeopleFilters: FC<Props> = ({
query,
handleQueryChange,
sex,
centuriesList,
centuries,
}) => {
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.entries(SexFilterValues).map(([key, value]) => (
<SearchLink
params={{
sex: value === 'All' ? null : key.toLowerCase(),
}}
key={key}
className={
sex === key.toLowerCase() || (key === 'All' && sex === '')
? 'is-active'
: ''
}
>
{value}
</SearchLink>
))}
</p>

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

<span className="icon is-left">
Expand All @@ -33,63 +59,49 @@ 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>
{centuriesList.map(century => (
<SearchLink
params={{
centuries: centuries?.includes(century)
? centuries.filter(c => c !== century)
: [...(centuries || []), century],
}}
key={century}
data-cy="century"
className={`button mr-1 ${
centuries?.includes(century) ? 'is-info' : ''
}`}
>
{century}
</SearchLink>
))}
</div>

<div className="level-right ml-4">
<a
<SearchLink
params={{
centuries: null,
}}
data-cy="centuryALL"
className="button is-success is-outlined"
href="#/people"
>
All
</a>
</SearchLink>
</div>
</div>
</div>

<div className="panel-block">
<a className="button is-link is-outlined is-fullwidth" href="#/people">
<SearchLink
params={{
centuries: null,
query: null,
sex: null,
}}
className="button is-link is-outlined is-fullwidth"
>
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