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

completed the task #637

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
51 changes: 37 additions & 14 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,43 @@
import { PeoplePage } from './components/PeoplePage';
import { Navbar } from './components/Navbar';
import { NavLink, Outlet } from 'react-router-dom';
import cn from 'classnames';

import './App.scss';

export const App = () => {
return (
<div data-cy="app">
<Navbar />
const getLinkClass = ({ isActive }: { isActive: boolean }) => {
return cn('navbar-item', { 'has-background-grey-lighter': isActive });
};

export const App = () => (
<div data-cy="app">
<nav
data-cy="nav"
className="navbar is-fixed-top has-shadow"
role="navigation"
aria-label="main navigation"
>
<div className="container">
<div className="navbar-brand">
<NavLink
className={getLinkClass}
to="/"
>
Home
</NavLink>

<div className="section">
<div className="container">
<h1 className="title">Home Page</h1>
<h1 className="title">Page not found</h1>
<PeoplePage />
<NavLink
className={getLinkClass}
to="/people"
>
People
</NavLink>
</div>
</div>
</div>
);
};
</nav>

<main className="section">
<div className="container">
<Outlet />
</div>
</main>
</div>
);
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>
);
};
139 changes: 91 additions & 48 deletions src/components/PeopleFilters.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,86 @@
export const PeopleFilters = () => {
import { useEffect, useMemo } from 'react';
import { useSearchParams } from 'react-router-dom';
import { SearchLink } from './SearchLink';
import { Filters } from '../types/Filters';
import { Sex } from '../utils/FilterPeople';

interface Props {
handleFilterChange: (newFilters: Filters) => void;
}

export const PeopleFilters: React.FC<Props> = ({ handleFilterChange }) => {
const [searchParams, setSearchParams] = useSearchParams();
const query = useMemo(() => searchParams.get('query') || '', [searchParams]);

const centuries = useMemo(
() => searchParams.getAll('centuries') || [], [searchParams],
);

const sex = useMemo(
() => (searchParams.get('sex') as Sex) || Sex.All, [searchParams],
);

function handleQueryChange(event: React.ChangeEvent<HTMLInputElement>) {
const newQuery = event.target.value;
const params = new URLSearchParams(searchParams);

if (newQuery.trim() !== '') {
params.set('query', newQuery);
} else {
params.delete('query');
}

setSearchParams(params);
}

useEffect(() => {
const applyFilters = () => {
const currentFilters = {
sex,
centuries,
query,
};

handleFilterChange(currentFilters);
};

applyFilters();
}, [searchParams, sex, centuries, query, handleFilterChange]);

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={`${sex === Sex.All ? 'is-active' : ''}`}
params={{ sex: Sex.All }}
>
All
</SearchLink>
<SearchLink
className={`${sex === Sex.Male ? 'is-active' : ''}`}
params={{ sex: Sex.Male }}
>
Male
</SearchLink>
<SearchLink
className={`${sex === Sex.Female ? 'is-active' : ''}`}
params={{ sex: Sex.Female }}
>
Female
</SearchLink>
</p>

<div className="panel-block">
<p className="control has-icons-left">
<input
data-cy="NameFilter"
type="search"
type="input"
className="input"
placeholder="Search"
onChange={handleQueryChange}
value={query}
/>

<span className="icon is-left">
Expand All @@ -27,55 +92,30 @@ 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>
{['16', '17', '18', '19', '20'].map(century => (
<SearchLink
key={century}
data-cy="century"
className={`button mr-1 ${centuries.includes(`${century}`) ? 'is-info' : ''}`}
params={{
centuries: !centuries.includes(`${century}`)
? [...centuries, century]
: centuries.filter(cen => cen !== century),
}}
>
{century}
</SearchLink>
))}
</div>

<div className="level-right ml-4">
<a
<SearchLink
data-cy="centuryALL"
className="button is-success is-outlined"
href="#/people"
className={`button is-success ${centuries.length === 0 ? 'is-outlined' : ''}`}
params={{ centuries: [] }}
>
All
</a>
</SearchLink>
</div>
</div>
</div>
Expand All @@ -84,6 +124,9 @@ export const PeopleFilters = () => {
<a
className="button is-link is-outlined is-fullwidth"
href="#/people"
onClick={() => {
setSearchParams(new URLSearchParams());
}}
>
Reset all filters
</a>
Expand Down
100 changes: 81 additions & 19 deletions src/components/PeoplePage.tsx
Original file line number Diff line number Diff line change
@@ -1,35 +1,97 @@
import { PeopleFilters } from './PeopleFilters';
import { useCallback, useEffect, useState } from 'react';
import { Loader } from './Loader';
import { Person } from '../types';
import { getPeople } from '../api';
import { PeopleTable } from './PeopleTable';
import { PeopleFilters } from './PeopleFilters';
import { filterPeople } from '../utils/FilterPeople';
import { Filters } from '../types/Filters';

enum ErrorType {
Type1 = 'type1',
Type2 = 'type2',
}

export const PeoplePage = () => {
const [people, setPeople] = useState<Person[]>([]);
const [filteredPeople, setFilteredPeople] = useState<Person[]>([]);
const [errorType, setErrorType] = useState<ErrorType | null>(null);
const [isLoading, setIsLoading] = useState<boolean>(true);
const [dataIsValid, setDataIsValid] = useState<boolean>(false);
const [filters, setFilters] = useState<Filters>({} as Filters);

useEffect(() => {
const fetchData = async () => {
try {
const data = await getPeople();

if (!data || data.length === 0) {
setErrorType(ErrorType.Type1);
setDataIsValid(false);
} else {
setPeople(data);
setDataIsValid(true);
}
} catch (error) {
setErrorType(ErrorType.Type2);
} finally {
setIsLoading(false);
}
};

fetchData();
}, []);

const applyFilters = useCallback(() => {
const filtered = filterPeople(people, filters);

setFilteredPeople(filtered);
}, [people, filters]);

useEffect(() => {
applyFilters();
}, [applyFilters]);

const handleFilterChange = (newFilters: Filters) => {
setFilters(newFilters);
};

return (
<>
<div className="column">
<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>

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

<p data-cy="peopleLoadingError">Something went wrong</p>
{errorType === 'type1' && (
<p data-cy="noPeopleMessage" className="has-text-danger">
It seems there are no people on the server.
</p>
)}

<p data-cy="noPeopleMessage">
There are no people on the server
</p>
{errorType === 'type2' && (
<p data-cy="peopleLoadingError">
Something went wrong while fetching people data.
</p>
)}

<p>There are no people matching the current search criteria</p>
{dataIsValid && (
<div className="block">
<div className="columns is-desktop is-flex-direction-row-reverse">
<div className="column is-7-tablet is-narrow-desktop">
<PeopleFilters
handleFilterChange={handleFilterChange}
/>
</div>

<PeopleTable />
<div className="column">
{dataIsValid && <PeopleTable people={filteredPeople} />}
</div>
</div>
</div>
</div>
)}
</div>
</div>
</>
</div>
);
};
Loading
Loading