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

Develop #641

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

import './App.scss';
import { HomePage } from './components/HomePage';
import { PageNotFound } from './components/PageNotFound';

export const App = () => {
return (
Expand All @@ -10,9 +12,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={<HomePage />} />
<Route path="/home" element={<Navigate to="/" replace />} />
<Route path="people">
<Route index element={<PeoplePage />} />
<Route path=":slug" element={<PeoplePage />} />
</Route>
<Route path="*" element={<PageNotFound />} />
</Routes>
</div>
</div>
</div>
Expand Down
5 changes: 5 additions & 0 deletions src/components/HomePage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const HomePage = () => {
return (
<h1 className="title">Home Page</h1>
);
};
21 changes: 16 additions & 5 deletions src/components/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import classNames from 'classnames';
import { NavLink } from 'react-router-dom';

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

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

<a
<NavLink
to="/people"
aria-current="page"
className="navbar-item has-background-grey-lighter"
href="#/people"
className={isActiveClass}
>
People
</a>
</NavLink>
</div>
</div>
</nav>
Expand Down
5 changes: 5 additions & 0 deletions src/components/PageNotFound.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const PageNotFound = () => {
return (
<h1 className="title">Page not found</h1>
);
};
116 changes: 67 additions & 49 deletions src/components/PeopleFilters.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,49 @@
import { ChangeEvent } from 'react';
import { useSearchParams } from 'react-router-dom';
import cn from 'classnames';
import { getSearchWith } from '../utils/searchHelper';
import { SearchLink } from './SearchLink';

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

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

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

function handleSearch(e: ChangeEvent<HTMLInputElement>): void {
const newParam = (e.target.value === '')
? getSearchWith(searchParams, { query: null })
: getSearchWith(searchParams, { query: e.target.value });

setSearchParams(newParam);
}

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>
<SearchLink
className={cn({ 'is-active': !sex })}
params={{ sex: null }}
>
All
</SearchLink>
<SearchLink
params={{ sex: 'm' }}
className={cn({ 'is-active': sex === 'm' })}
>
Male
</SearchLink>
<SearchLink
params={{ sex: 'f' }}
className={cn({ 'is-active': sex === 'f' })}
>
Female
</SearchLink>
</p>

<div className="panel-block">
Expand All @@ -16,77 +53,58 @@ export const PeopleFilters = () => {
type="search"
className="input"
placeholder="Search"
value={query}
onChange={e => handleSearch(e)}

Choose a reason for hiding this comment

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

Suggested change
onChange={e => handleSearch(e)}
onChange={handleSearch}

/>

<span className="icon is-left">
<i className="fas fa-search" aria-hidden="true" />
</span>
</p>
</div>

<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>
{centuriesList.map(century => (
<SearchLink
key={century}
params={{
centuries: centuries.includes(century)
? centuries.filter(c => c !== century)
: [...centuries, century],
}}
className={cn('button mr-1', {
'is-info': centuries.includes(century),
})}
>
{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
<SearchLink
className="button is-link is-outlined is-fullwidth"
href="#/people"
params={{
sex: null,
query: null,
centuries: null,
}}
>
Reset all filters
</a>
</SearchLink>
</div>
</nav>
);
Expand Down
109 changes: 101 additions & 8 deletions src/components/PeoplePage.tsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,124 @@
import { useEffect, useState } from 'react';
import { useParams, useSearchParams } from 'react-router-dom';
import { PeopleFilters } from './PeopleFilters';
import { Loader } from './Loader';
import { PeopleTable } from './PeopleTable';
import { Person } from '../types';
import { getPeople } from '../api';

export const PeoplePage = () => {
const [people, setPeople] = useState<Person[]>([]);
const [isLoader, setIsLoader] = useState(false);
const [errorMessage, setErrorMessage] = useState(false);
const [searchParams] = useSearchParams();

const { slug } = useParams();

useEffect(() => {
setIsLoader(true);
getPeople()
.then(response => {
const peopleServer: Person[] = response.map(person => {
const mother = response
.find(item => item.name === person.motherName);
const father = response
.find(item => item.name === person.fatherName);

return ({
...person,
mother,
father,
});
});

return peopleServer;
})
.then(preparedPeople => setPeople(preparedPeople))
.catch(() => setErrorMessage(true))
.finally(() => setIsLoader(false));
}, []);

const preparedPeople = () => {
const sort = searchParams.get('sort');
const order = searchParams.get('order');
const filterSex = searchParams.get('sex');
const query = searchParams.get('query')?.trim().toLocaleLowerCase();
const centuries = searchParams.getAll('centuries');
let prepared = [...people];

if (query) {
prepared = prepared.filter(elem => elem.name.toLowerCase()
.includes(query)
|| elem.motherName?.toLowerCase().includes(query)
|| elem.fatherName?.toLowerCase().includes(query));
}

if (filterSex) {
prepared = prepared.filter(elem => elem.sex === filterSex);
}

if (centuries && centuries.length > 0) {
prepared = prepared.filter(elem => (
centuries?.includes(Math.ceil(elem.born / 100).toString())));
}

if (sort) {
switch (sort) {
case 'name':
case 'sex':
{
prepared.sort((a, b) => a[sort].localeCompare(b[sort]));
break;
}

case 'born':
case 'died': {
prepared.sort((a, b) => a[sort] - b[sort]);
break;
}

default:
}
}

if (order) {
prepared.reverse();
}

return prepared;
};

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 />
{!isLoader && <PeopleFilters />}
</div>

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

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

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

Choose a reason for hiding this comment

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

consider moving long ternaries to a separate const with consistent name and use the const here

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

<p>There are no people matching the current search criteria</p>
{(!people.length && !isLoader) && (
<p>There are no people matching the current search criteria</p>
)}

<PeopleTable />
{!!people.length && (
<PeopleTable people={preparedPeople()} slugSelected={slug} />
)}
</div>
</div>
</div>
Expand Down
Loading
Loading