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

Expand All @@ -10,9 +11,15 @@ 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/:slug?" element={<PeoplePage />} />
<Route
path="*"
element={<h1 className="title">Page not found</h1>}
/>
</Routes>
</div>
</div>
</div>
Expand Down
27 changes: 20 additions & 7 deletions src/components/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { Link, NavLink, useMatch, useSearchParams } from 'react-router-dom';
import cn from 'classnames';

export const Navbar = () => {
const [searchParams] = useSearchParams();
const isPeoplePage = useMatch('/people/*');

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

<a
aria-current="page"
className="navbar-item has-background-grey-lighter"
href="#/people"
<Link
className={cn('navbar-item', {
'has-background-grey-lighter': !!isPeoplePage,
})}
to={{ pathname: '/people', search: searchParams.toString() }}
>
People
Comment on lines +27 to 33
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Replace this Link with SearchLink using empty params as per requirements. Use: <SearchLink className={...} params={{}}>People</SearchLink>

</a>
</Link>
</div>
</div>
</nav>
Expand Down
123 changes: 72 additions & 51 deletions src/components/PeopleFilters.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,53 @@
import { useSearchParams } from 'react-router-dom';
import cn from 'classnames';
import { SearchLink } from './SearchLink';
import { getSearchWith } from '../utils/searchHelper';

const CENTURIES = ['16', '17', '18', '19', '20'];

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

const handleQueryChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const value = e.target.value;

setSearchParams(getSearchWith(searchParams, { query: value || null }));
};

const toggleCentury = (century: string): string[] => {
if (centuries.includes(century)) {
return centuries.filter(c => c !== century);
}

return [...centuries, century];
};

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

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

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

<span className="icon is-left">
Expand All @@ -33,63 +70,47 @@ 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>
{CENTURIES.map(century => (
<SearchLink
key={century}
data-cy="century"
className={cn('button mr-1', {
'is-info': centuries.includes(century),
})}
params={{ centuries: toggleCentury(century) }}
>
{century}
</SearchLink>
))}
</div>

<div className="level-right ml-4">
<a
<SearchLink
data-cy="centuryALL"
className="button is-success is-outlined"
href="#/people"
className={cn('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={{
sex: null,
query: null,
centuries: null,
sort: null,
order: null,
}}
>
Reset all filters
</a>
</SearchLink>
</div>
</nav>
);
Expand Down
112 changes: 104 additions & 8 deletions src/components/PeoplePage.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,125 @@
import { useEffect, useMemo, useState } from 'react';
import { useSearchParams } from 'react-router-dom';
import { PeopleFilters } from './PeopleFilters';
import { Loader } from './Loader';
import { PeopleTable } from './PeopleTable';
import { getPeople } from '../api';
import { Person } from '../types';

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

useEffect(() => {
setLoading(true);
setError(false);

getPeople()
.then(data => {
const peopleWithParents = data.map(person => ({
...person,
mother: data.find(p => p.name === person.motherName),
father: data.find(p => p.name === person.fatherName),
}));

setPeople(peopleWithParents);
})
.catch(() => setError(true))
.finally(() => setLoading(false));
}, []);

const sex = searchParams.get('sex');
Comment on lines +27 to +33
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The "People" link should use SearchLink with empty params (params={{}}) instead of Link with manual search params construction. This is required by the task implementation details.

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

const filteredPeople = useMemo(() => {
let result = [...people];

if (sex) {
result = result.filter(p => p.sex === sex);
}

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

result = result.filter(
p =>
p.name.toLowerCase().includes(q) ||
(p.motherName || '').toLowerCase().includes(q) ||
(p.fatherName || '').toLowerCase().includes(q),
);
}

if (centuries.length > 0) {
result = result.filter(p =>
centuries.includes(String(Math.ceil(p.born / 100))),
);
}

if (sort) {
result.sort((a, b) => {
let cmp = 0;

switch (sort) {
case 'name':
case 'sex':
cmp = a[sort].localeCompare(b[sort]);
break;
case 'born':
case 'died':
cmp = a[sort] - b[sort];
break;
}

return order === 'desc' ? -cmp : cmp;
});
}

return result;
}, [people, sex, query, centuries, sort, order]);

const loaded = !loading && !error;
const hasPeople = loaded && people.length > 0;
const noPeople = loaded && people.length === 0;
const noMatches = hasPeople && filteredPeople.length === 0;

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 />
</div>
{hasPeople && (
<div className="column is-7-tablet is-narrow-desktop">
<PeopleFilters />
</div>
)}

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

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

<p data-cy="noPeopleMessage">There are no people on the server</p>
{noPeople && (
<p data-cy="noPeopleMessage">
There are no people on the server
</p>
)}

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

<PeopleTable />
{hasPeople && !noMatches && (
<PeopleTable people={filteredPeople} />
)}
</div>
</div>
</div>
Expand Down
Loading
Loading