Skip to content
Open

Sol #1811

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

import { Navbar } from './components/Navbar';
import { PeoplePage } from './components/PeoplePage';
import { HomePage } from './pages/HomePage';
import { NotFoundPage } from './pages/NotFoundPage';

import './App.scss';

Expand All @@ -10,9 +14,18 @@ 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={<HomePage />} />

<Route path="/home" element={<Navigate to="/" replace />} />

<Route path="/people">
<Route index element={<PeoplePage />} />
<Route path=":slug" element={<PeoplePage />} />
</Route>

<Route path="*" element={<NotFoundPage />} />
</Routes>
</div>
</div>
</div>
Expand Down
31 changes: 24 additions & 7 deletions src/components/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { NavLink, useSearchParams } from 'react-router-dom';

export const Navbar = () => {
const [searchParams] = useSearchParams();

return (
<nav
data-cy="nav"
Expand All @@ -8,17 +12,30 @@ 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
aria-current="page"
className="navbar-item has-background-grey-lighter"
href="#/people"
<NavLink
to={{
pathname: '/people',
search: searchParams.toString(),
}}
className={({ isActive }) =>
isActive
? 'navbar-item has-background-grey-lighter'
: 'navbar-item'
}
>
People
</a>
</NavLink>
</div>
</div>
</nav>
Expand Down
114 changes: 67 additions & 47 deletions src/components/PeopleFilters.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,51 @@
import { ChangeEvent } from 'react';
import { Link, useSearchParams } from 'react-router-dom';

import { SearchLink } from './SearchLink';

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

const query = searchParams.get('query') || '';
const selectedCenturies = searchParams.getAll('centuries');
const centuries = ['16', '17', '18', '19', '20'];
const sex = searchParams.get('sex') || '';

const handleQueryChange = (inputEvent: ChangeEvent<HTMLInputElement>) => {
const newParams = new URLSearchParams(searchParams);
const newQuery = inputEvent.target.value;

if (newQuery) {
newParams.set('query', newQuery);
} else {
newParams.delete('query');
}

setSearchParams(newParams);
};

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 +55,8 @@ export const PeopleFilters = () => {
type="search"
className="input"
placeholder="Search"
value={query}
onChange={handleQueryChange}
/>

<span className="icon is-left">
Expand All @@ -33,63 +68,48 @@ 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>
{centuries.map(century => {
const isSelected = selectedCenturies.includes(century);

<a
data-cy="century"
className="button mr-1 is-info"
href="#/people?centuries=17"
>
17
</a>
const filteredCenturies = selectedCenturies.filter(
selectedCentury => selectedCentury !== century,
);

<a
data-cy="century"
className="button mr-1 is-info"
href="#/people?centuries=18"
>
18
</a>
const nextCenturies = isSelected
? filteredCenturies
: [...selectedCenturies, century];

<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>
return (
<SearchLink
key={century}
data-cy="century"
className={`button mr-1 ${isSelected ? 'is-info' : ''}`}
params={{
centuries: nextCenturies.length > 0 ? nextCenturies : null,
}}
>
{century}
</SearchLink>
);
})}
</div>

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

<div className="panel-block">
<a className="button is-link is-outlined is-fullwidth" href="#/people">
<Link className="button is-link is-outlined is-fullwidth" to="/people">
Reset all filters
</a>
</Link>
</div>
</nav>
);
Expand Down
142 changes: 133 additions & 9 deletions src/components/PeoplePage.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,153 @@
import { PeopleFilters } from './PeopleFilters';
import { useEffect, useState } from 'react';
import { useParams, useSearchParams } from 'react-router-dom';

import { getPeople } from '../api';
import { Person } from '../types';

import { Loader } from './Loader';
import { PeopleFilters } from './PeopleFilters';
import { PeopleTable } from './PeopleTable';

function getVisiblePeople(
people: Person[],
query: string,
centuries: string[],
sort: string,
order: string,
sex: string,
): Person[] {
let visiblePeople = [...people];

const normalizedQuery = query.trim().toLowerCase();

if (normalizedQuery) {
visiblePeople = visiblePeople.filter(person => {
const name = person.name.toLowerCase();
const motherName = person.motherName?.toLowerCase() || '';
const fatherName = person.fatherName?.toLowerCase() || '';

return (
name.includes(normalizedQuery) ||
motherName.includes(normalizedQuery) ||
fatherName.includes(normalizedQuery)
);
});
}

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

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

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

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

case 'born':
case 'died':
return person1[sort] - person2[sort];

default:
return 0;
}
});

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

return visiblePeople;
}

export const PeoplePage = () => {
const { slug } = useParams();
const [searchParams] = useSearchParams();

const [people, setPeople] = useState<Person[]>([]);
const [isLoading, setIsLoading] = useState(true);
const [hasLoadingError, setHasLoadingError] = useState(false);

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

const visiblePeople = getVisiblePeople(
people,
query,
centuries,
sort,
order,
sex,
);

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

const shouldShowNoMatchingPeople =
!isLoading &&
!hasLoadingError &&
people.length > 0 &&
visiblePeople.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>
{!isLoading && !hasLoadingError && people.length > 0 && (
<div className="column is-7-tablet is-narrow-desktop">
<PeopleFilters />
</div>
)}

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

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

<p data-cy="noPeopleMessage">There are no people on the server</p>
{!isLoading && !hasLoadingError && 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>
{shouldShowNoMatchingPeople && (
<p>There are no people matching the current search criteria</p>
)}

<PeopleTable />
{!isLoading && !hasLoadingError && visiblePeople.length > 0 && (
<PeopleTable
people={visiblePeople}
selectedSlug={slug}
sort={sort}
order={order}
/>
)}
</div>
</div>
</div>
Expand Down
Loading
Loading