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

added filters and sort logic #1073

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://OlehPhw.github.io/react_people-table-advanced/) and add it to the PR description.
6 changes: 2 additions & 4 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PeoplePage } from './components/PeoplePage';
import { Navbar } from './components/Navbar';

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

export const App = () => {
return (
Expand All @@ -10,9 +10,7 @@ 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>
</div>
Expand Down
3 changes: 3 additions & 0 deletions src/Pages/HomePage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const HomePage = () => {
return <h1 className="title">Home Page</h1>;
};
1 change: 1 addition & 0 deletions src/Pages/PageNotFound.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const PageNotFound = () => <h1 className="title">Page not found</h1>;
51 changes: 51 additions & 0 deletions src/Pages/PeoplePage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { PeopleFilters } from '../components/PeopleFilters';
import { Loader } from '../components/Loader';
import { PeopleTable } from '../components/PeopleTable';
import { useEffect, useState } from 'react';
import { Person } from '../types/Person';
import { getPeople } from '../api';

export const PeoplePage = () => {
const [people, setPeople] = useState<Person[] | []>([]);
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState(false);

useEffect(() => {
getPeople()
.then(result => setPeople(result))
.catch(() => setError(true))
.finally(() => setIsLoading(false));
}, []);

return (
<>
<h1 className="title">People Page</h1>

<div className="block">
<div className="columns is-desktop is-flex-direction-row-reverse">
<div className="column is-7-tablet is-narrow-desktop">
{!isLoading && <PeopleFilters />}
</div>

<div className="column">
<div className="box table-container">
{isLoading && <Loader />}

{!isLoading && error && (
<p data-cy="peopleLoadingError" className="has-text-danger">
Something went wrong
</p>
)}
{!isLoading && people.length === 0 && (
<p data-cy="noPeopleMessage">
There are no people on the server
</p>
)}
{!isLoading && <PeopleTable people={people} />}
</div>
</div>
</div>
</div>
</>
);
};
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,
Route,
HashRouter as Router,
Routes,
} from 'react-router-dom';
import { App } from './App';
import { HomePage } from './Pages/HomePage';
import { PeoplePage } from './Pages/PeoplePage';
import { PageNotFound } from './Pages/PageNotFound';

export const Root = () => (
<Router>
<Routes>
<Route path="/" element={<App />}>
<Route index element={<HomePage />} />
<Route path="/home" element={<Navigate to="/" replace={true} />} />
<Route path="people">
<Route path=":slug?" element={<PeoplePage />} />
</Route>
<Route path="*" element={<PageNotFound />} />
</Route>
</Routes>
</Router>
);
15 changes: 7 additions & 8 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 { getNavLinkClass } from '../utils/getNavLinkClass';

export const Navbar = () => {
return (
<nav
Expand All @@ -8,17 +11,13 @@ export const Navbar = () => {
>
<div className="container">
<div className="navbar-brand">
<a className="navbar-item" href="#/">
<NavLink to="/" className={getNavLinkClass}>
Home
</a>
</NavLink>

<a
aria-current="page"
className="navbar-item has-background-grey-lighter"
href="#/people"
>
<NavLink to="people" className={getNavLinkClass}>
People
</a>
</NavLink>
</div>
</div>
</nav>
Expand Down
130 changes: 74 additions & 56 deletions src/components/PeopleFilters.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,46 @@
import { useSearchParams } from 'react-router-dom';
import cn from 'classnames';
import { getSearchWith, SearchParams } from '../utils/searchHelper';
import { SearchLink } from './SearchLink';
import { FilterTypes } from '../types/FilterTypes';
import { SearchParamsTypes } from '../types/SearchParamsTypes';
import { CenturiesTypes } from '../types/CenturiesTypes';

export const PeopleFilters = () => {
const [searchParams, setSearchParams] = useSearchParams();

const query = searchParams.get(SearchParamsTypes.query) || '';
const centuries = searchParams.getAll(SearchParamsTypes.centuries || []);
const sex = searchParams.get(SearchParamsTypes.sex) || '';

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

setSearchParams(search);
}

const handleOnChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setSearchWith({ query: event.target.value || 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.entries(FilterTypes).map(([key, value]) => (
<SearchLink
params={{
sex: value || null,
}}
key={key}
className={cn({
'is-active': value === sex,
})}
>
{key}
</SearchLink>
))}
</p>

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

<span className="icon is-left">
Expand All @@ -33,63 +63,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>
{Object.values(CenturiesTypes).map(century => (
<SearchLink
params={{
centuries: !centuries.includes(century)
? [...centuries, century]
: centuries.filter(n => n !== century),
}}
key={+century}
data-cy="century"
className={cn('button', 'mr-1', {
'is-info': centuries.includes(century),
})}
>
{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"
className={cn('button', 'is-success', {
'is-outlined': centuries.length !== 0,
})}
>
All
</a>
{'All'}
</SearchLink>
</div>
</div>
</div>

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

This file was deleted.

Loading
Loading