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

solution #1143

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

export const Root = () => (
<Router>
<Routes>
<Route path="/" element={<App />}>
<Route index element={<HomePage />} />
<Route path="home" element={<Navigate to=".." />} />
<Route path="people">
<Route index element={<PeoplePage />} />
<Route path=":slug?" element={<PeoplePage />} />
</Route>
<Route path="*" element={<NotFoundPage />} />
</Route>
</Routes>
</Router>
);
18 changes: 10 additions & 8 deletions src/components/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import classNames from 'classnames';
import { NavLink } from 'react-router-dom';

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

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

<a
aria-current="page"
className="navbar-item has-background-grey-lighter"
href="#/people"
>
<NavLink to="/people" aria-current="page" className={getLinkClass}>
People
</a>
</NavLink>
</div>
</div>
</nav>
Expand Down
128 changes: 77 additions & 51 deletions src/components/PeopleFilters.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,62 @@
import { useSearchParams } from 'react-router-dom';
import { SearchLink } from './SearchLink';
import classNames from 'classnames';
import { Gender } from '../types/Gender';

const centuriesValue: string[] = ['16', '17', '18', '19', '20'];

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

const resetParams = {
query: null,
sex: null,
centuries: null,
};

const isActiveSex = (value: string | null) => {
return (!sex && value === null) || value === sex;
};

const getCenturies = (century: string) => {
return centuries.includes(century)
? centuries.filter(currCentury => currCentury !== century)
: [...centuries, century];
};

const handleQueryChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const params = new URLSearchParams(searchParams);

params.set('query', e.target.value);
setSearchParams(params);
};

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

<p className="panel-tabs" data-cy="SexFilter">
<a className="is-active" href="#/people">
<SearchLink
className={classNames({ 'is-active': isActiveSex(null) })}
params={{ sex: null }}
>
All
</a>
<a className="" href="#/people?sex=m">
</SearchLink>
<SearchLink
className={classNames({ 'is-active': isActiveSex(Gender.Male) })}
params={{ sex: Gender.Male }}
>
Male
</a>
<a className="" href="#/people?sex=f">
</SearchLink>
<SearchLink
className={classNames({ 'is-active': isActiveSex(Gender.Female) })}
params={{ sex: Gender.Female }}
>
Female
</a>
</SearchLink>
</p>

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

<span className="icon is-left">
Expand All @@ -33,63 +79,43 @@ 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>
{centuriesValue.map(century => {
return (
<SearchLink
key={century}
data-cy="century"
className={classNames('button mr-1', {
'is-info': centuries.includes(century),
})}
params={{ centuries: getCenturies(century) }}
>
{century}
</SearchLink>
);
})}
</div>

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

<div className="panel-block">
<a className="button is-link is-outlined is-fullwidth" href="#/people">
<SearchLink
className="button is-link is-outlined is-fullwidth"
params={resetParams}
>
Reset all filters
</a>
</SearchLink>
</div>
</nav>
);
Expand Down
55 changes: 49 additions & 6 deletions src/components/PeoplePage.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,72 @@
import { PeopleFilters } from './PeopleFilters';
import { Loader } from './Loader';
import { PeopleTable } from './PeopleTable';
import { useEffect, useState } from 'react';
import { Person } from '../types';
import { getPeople } from '../api';
import { useSearchParams } from 'react-router-dom';
import { getFilteredPeople } from '../utils/getFilteredPeople';

export const PeoplePage = () => {
const [loading, setLoading] = useState(false);
const [error, setError] = useState('');
const [people, setPeople] = useState<Person[]>([]);

const [searchParams] = useSearchParams();

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

const filteredAndSortedPeople = getFilteredPeople(people, {
sex,
query,
centuries,
sort,
order,
});

useEffect(() => {
setLoading(true);
getPeople()
.then(setPeople)
.catch(() => {
setError('Something went wrong');
})
.finally(() => {
setLoading(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">
<PeopleFilters />
{!loading && <PeopleFilters />}
</div>

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

<p data-cy="peopleLoadingError">Something went wrong</p>
{error && !loading && <p data-cy="peopleLoadingError">{error}</p>}

<p data-cy="noPeopleMessage">There are no people on the server</p>
{!people.length && !error && !loading && (
<p data-cy="noPeopleMessage">
There are no people on the server
</p>
)}

<p>There are no people matching the current search criteria</p>
{/* <p>There are no people matching the current search criteria</p> */}

<PeopleTable />
{!loading && !error && !!people.length && (
<PeopleTable people={filteredAndSortedPeople} />
)}
</div>
</div>
</div>
Expand Down
Loading