Skip to content
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
14 changes: 11 additions & 3 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { PeoplePage } from './components/PeoplePage';
import { Navbar } from './components/Navbar';
import { Routes, Route, Navigate } from 'react-router-dom';

import './App.scss';

Expand All @@ -10,9 +11,16 @@ export const App = () => {

<div className="section">
<div className="container">
<h1 className="title">Home Page</h1>
<h1 className="title">Page not found</h1>
<PeoplePage />
<Routes>
<Route path="/" element={<h1 className="title">Home Page</h1>} />
<Route path="/home" element={<Navigate to="/" replace />} />
<Route path="/people" element={<PeoplePage />} />
<Route path="/people/:slug" element={<PeoplePage />} />
<Route
path="*"
element={<h1 className="title">Page not found</h1>}
/>
</Routes>
</div>
</div>
</div>
Expand Down
27 changes: 21 additions & 6 deletions src/components/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { NavLink, useLocation } from 'react-router-dom';

export const Navbar = () => {
const location = useLocation();

return (
<nav
data-cy="nav"
Expand All @@ -8,17 +12,28 @@ export const Navbar = () => {
>
<div className="container">
<div className="navbar-brand">
<a className="navbar-item" href="#/">
<NavLink
to="/"
className={({ isActive }) =>
isActive
? 'navbar-item has-background-grey-lighter'
: 'navbar-item'
}
>
Home
</a>
</NavLink>

<a
<NavLink
to={{ pathname: '/people', search: location.search }}
aria-current="page"
className="navbar-item has-background-grey-lighter"
href="#/people"
className={({ isActive }) =>
isActive
? 'navbar-item has-background-grey-lighter'
: 'navbar-item'
}
>
People
</a>
</NavLink>
</div>
</div>
</nav>
Expand Down
120 changes: 88 additions & 32 deletions src/components/PeopleFilters.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,45 @@
import { useSearchParams } from 'react-router-dom';
import { getSearchWith } from '../utils/searchHelper';
import { SearchLink } from './SearchLink';
import classNames from 'classnames';

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

const query = searchParams.get('query') || '';
const sex = searchParams.get('sex') || '';
const selectedCenturies = searchParams.getAll('centuries');

const getCenturyParams = (century: string) => {
const newCenturies = selectedCenturies.includes(century)
? selectedCenturies.filter(item => item !== century)
: [...selectedCenturies, century];

return {
centuries: newCenturies.length > 0 ? newCenturies : null,
};
};

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={!sex ? 'is-active' : ''} params={{ sex: null }}>
All
</a>
<a className="" href="#/people?sex=m">
</SearchLink>
<SearchLink
className={sex === 'm' ? 'is-active' : ''}
params={{ sex: 'm' }}
>
Male
</a>
<a className="" href="#/people?sex=f">
</SearchLink>
<SearchLink
className={sex === 'f' ? 'is-active' : ''}
params={{ sex: 'f' }}
>
Female
</a>
</SearchLink>
</p>

<div className="panel-block">
Expand All @@ -22,6 +49,14 @@ export const PeopleFilters = () => {
type="search"
className="input"
placeholder="Search"
value={query}
onChange={event => {
setSearchParams(
getSearchWith(searchParams, {
query: event.target.value || null,
}),
);
}}
/>

<span className="icon is-left">
Expand All @@ -33,63 +68,84 @@ export const PeopleFilters = () => {
<div className="panel-block">
<div className="level is-flex-grow-1 is-mobile" data-cy="CenturyFilter">
<div className="level-left">
<a
<SearchLink
data-cy="century"
className="button mr-1"
href="#/people?centuries=16"
className={classNames('button mr-1', {
'is-info': selectedCenturies.includes('16'),
})}
params={getCenturyParams('16')}
>
16
</a>
</SearchLink>

<a
<SearchLink
data-cy="century"
className="button mr-1 is-info"
href="#/people?centuries=17"
className={classNames('button mr-1', {
'is-info': selectedCenturies.includes('17'),
})}
params={getCenturyParams('17')}
>
17
</a>
</SearchLink>

<a
<SearchLink
data-cy="century"
className="button mr-1 is-info"
href="#/people?centuries=18"
className={classNames('button mr-1', {
'is-info': selectedCenturies.includes('18'),
})}
params={getCenturyParams('18')}
>
18
</a>
</SearchLink>

<a
<SearchLink
data-cy="century"
className="button mr-1 is-info"
href="#/people?centuries=19"
className={classNames('button mr-1', {
'is-info': selectedCenturies.includes('19'),
})}
params={getCenturyParams('19')}
>
19
</a>
</SearchLink>

<a
<SearchLink
data-cy="century"
className="button mr-1"
href="#/people?centuries=20"
className={classNames('button mr-1', {
'is-info': selectedCenturies.includes('20'),
})}
params={getCenturyParams('20')}
>
20
</a>
</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': selectedCenturies.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={{
query: null,
sex: null,
centuries: null,
sort: null,
order: null,
}}
>
Reset all filters
</a>
</SearchLink>
</div>
</nav>
);
Expand Down
102 changes: 96 additions & 6 deletions src/components/PeoplePage.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,119 @@
import { useEffect, useState } from 'react';
import { getPeople } from '../api';
import { PeopleFilters } from './PeopleFilters';
import { Loader } from './Loader';
import { PeopleTable } from './PeopleTable';
import { Person } from '../types';
import { useParams, useSearchParams } from 'react-router-dom';

export const PeoplePage = () => {
const { slug } = useParams();

const [people, setPeople] = useState<Person[]>([]);
const [isLoading, setIsLoading] = useState(false);
const [hasError, setHasError] = useState(false);

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');

let visiblePeople = [...people];

if (sex) {
visiblePeople = visiblePeople.filter(person => person.sex === sex);
}

if (query) {
const normalizedQuery = query.toLowerCase();

visiblePeople = visiblePeople.filter(person => {
return (
person.name.toLowerCase().includes(normalizedQuery) ||
person.motherName?.toLowerCase().includes(normalizedQuery) ||
person.fatherName?.toLowerCase().includes(normalizedQuery)
);
});
}

if (centuries.length > 0) {
visiblePeople = visiblePeople.filter(person => {
const century = String(Math.ceil(person.born / 100));

return centuries.includes(century);
});
}

if (sort) {
visiblePeople.sort((personA, personB) => {
switch (sort) {
case 'name':
case 'sex':
return personA[sort].localeCompare(personB[sort]);

case 'born':
case 'died':
return personA[sort] - personB[sort];

default:
return 0;
}
});

if (order === 'desc') {
visiblePeople.reverse();
}
}

const noPeopleMatching =
!isLoading && !hasError && people.length > 0 && visiblePeople.length === 0;

const tableOfPeople =
!isLoading && !hasError && people.length > 0 && visiblePeople.length > 0;

useEffect(() => {
setIsLoading(true);

getPeople()
.then(setPeople)
.catch(() => setHasError(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">
<PeopleFilters />
{!isLoading && !hasError && people.length > 0 && <PeopleFilters />}
</div>

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

<p data-cy="peopleLoadingError">Something went wrong</p>
{hasError && (
<p data-cy="peopleLoadingError">Something went wrong</p>
)}

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

<p>There are no people matching the current search criteria</p>
{noPeopleMatching && (
<p>There are no people matching the current search criteria</p>
)}

<PeopleTable />
{tableOfPeople && (
<PeopleTable people={visiblePeople} selectedSlug={slug} />
)}
</div>
</div>
</div>
Expand Down
Loading
Loading