Skip to content
Open

Develop #1779

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
9 changes: 5 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"devDependencies": {
"@cypress/react18": "^2.0.1",
"@mate-academy/scripts": "^1.9.12",
"@mate-academy/scripts": "^2.1.3",
"@mate-academy/students-ts-config": "*",
"@mate-academy/stylelint-config": "*",
"@types/node": "^20.14.10",
Expand Down
15 changes: 12 additions & 3 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { PeoplePage } from './components/PeoplePage';
import { Navbar } from './components/Navbar';

import './App.scss';
import { Navigate, Route, Routes } from 'react-router-dom';

const HomePage = () => <h1 className="title">Home Page</h1>;
const NotFoundPage = () => <h1 className="title">Page not found</h1>;

export const App = () => {
return (
Expand All @@ -10,9 +14,14 @@ 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/:slug?" element={<PeoplePage />} />

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

export const Navbar = () => {
const { search } = useLocation();

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

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

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

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

const handleQueryChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const newQuery = event.target.value || null;

const newSearch = getSearchWith(searchParams, { query: newQuery });

setSearchParams(newSearch);
};

const selectedCenturies = searchParams.getAll('centuries');

const toggleCentury = (century: string) => {
if (selectedCenturies.includes(century)) {
const updatedCenturies = selectedCenturies.filter(c => c !== century);

return updatedCenturies.length > 0 ? updatedCenturies : null;
}

return [...selectedCenturies, 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={classNames({ 'is-active': !sex })}
>
All
</a>
<a className="" href="#/people?sex=m">
</SearchLink>

<SearchLink
params={{ sex: 'm' }}
className={classNames({ 'is-active': sex === 'm' })}
>
Male
</a>
<a className="" href="#/people?sex=f">
</SearchLink>

<SearchLink
params={{ sex: 'f' }}
className={classNames({ 'is-active': sex === 'f' })}
>
Female
</a>
</SearchLink>
</p>

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

<span className="icon is-left">
Expand All @@ -33,63 +76,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>
{['16', '17', '18', '19', '20'].map(century => (
<SearchLink
key={century}
data-cy="century"
className={classNames('button mr-1', {
'is-info': selectedCenturies.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={classNames('button is-success', {
'is-outlined': selectedCenturies.length > 0, // Outlined, якщо вибрано хоча б одне століття
})}
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
params={{
sex: null,
query: null,
centuries: null,
sort: null,
order: null,
}}
className="button is-link is-outlined is-fullwidth"
>
Reset all filters
</a>
</SearchLink>
</div>
</nav>
);
Expand Down
97 changes: 91 additions & 6 deletions src/components/PeoplePage.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,114 @@
import { useEffect, useState } from 'react';
import { useSearchParams } from 'react-router-dom';
import { getPeople } from '../api';
import { Person } from '../types/Person';
import { PeopleFilters } from './PeopleFilters';
import { Loader } from './Loader';
import { PeopleTable } from './PeopleTable';

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

const [searchParams] = useSearchParams();

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

const sex = searchParams.get('sex');
const query = searchParams.get('query')?.toLowerCase().trim() || '';
const centuries = searchParams.getAll('centuries');
const sortField = searchParams.get('sort') as keyof Person | null;
const sortOrder = searchParams.get('order');

const visiblePeople = people.filter(person => {
if (sex && person.sex !== sex) {
return false;
}

if (query) {
const matchName = person.name.toLowerCase().includes(query);
const matchMother = person.motherName?.toLowerCase().includes(query);
const matchFather = person.fatherName?.toLowerCase().includes(query);

if (!matchName && !matchMother && !matchFather) {
return false;
}
}

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

if (!centuries.includes(personCentury)) {
return false;
}
}

return true;
});

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

switch (sortField) {
case 'name':
case 'sex':
result = String(a[sortField]).localeCompare(String(b[sortField]));
break;
case 'born':
case 'died':
result = Number(a[sortField]) - Number(b[sortField]);
break;
}

return sortOrder === 'desc' ? -result : result;
});
}

const showFilters = !isLoading && !hasError && people.length > 0;
const noPeopleOnServer = !isLoading && !hasError && people.length === 0;
const noMatch = showFilters && 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 />
{showFilters && <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" className="has-text-danger">
Something went wrong
</p>
)}

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

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

<PeopleTable />
{!isLoading && !hasError && visiblePeople.length > 0 && (
<PeopleTable people={visiblePeople} />
)}
</div>
</div>
</div>
Expand Down
Loading
Loading