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 #1059

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

develop #1059

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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ implement the ability to filter and sort people in the table.
- Implement a solution following the [React task guideline](https://github.com/mate-academy/react_task-guideline#react-tasks-guideline).
- Use the [React TypeScript cheat sheet](https://mate-academy.github.io/fe-program/js/extra/react-typescript).
- Open one more terminal and run tests with `npm test` to ensure your solution is correct.
- Replace `<your_account>` with your Github username in the [DEMO LINK](https://<your_account>.github.io/react_people-table-advanced/) and add it to the PR description.
- Replace `<your_account>` with your Github username in the [DEMO LINK](https://mykyta01.github.io/react_people-table-advanced/) and add it to the PR description.
23 changes: 13 additions & 10 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.8.5",
"@mate-academy/scripts": "^1.9.12",
"@mate-academy/students-ts-config": "*",
"@mate-academy/stylelint-config": "*",
"@types/node": "^20.14.10",
Expand Down
20 changes: 2 additions & 18 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,4 @@
import { PeoplePage } from './components/PeoplePage';
import { Navbar } from './components/Navbar';

import { Root } from './Root';
import './App.scss';

export const App = () => {
return (
<div data-cy="app">
<Navbar />

<div className="section">
<div className="container">
<h1 className="title">Home Page</h1>
<h1 className="title">Page not found</h1>
<PeoplePage />
</div>
</div>
</div>
);
};
export const App = () => <Root />;
30 changes: 30 additions & 0 deletions src/Root.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React from 'react';
import { Navigate, Route, Routes } from 'react-router-dom';
import { Navbar } from './components/Navbar';
import { PeoplePage } from './components/PeoplePage';
import { Routes as AppRoutes } from './utils/constants';

export const Root: React.FC = () => (
<div data-cy="app">
<Navbar />
<main className="section">
<div className="container">
<Routes>
<Route
path={AppRoutes.HOME}
element={<h1 className="title">Home Page</h1>}
/>
<Route path={AppRoutes.NAVIGATE_HOME} element={<Navigate to="/" />} />
<Route path={AppRoutes.PEOPLE}>
<Route index element={<PeoplePage />} />
<Route path={AppRoutes.PERSON} element={<PeoplePage />} />
</Route>
<Route
path={AppRoutes.NOT_FOUND}
element={<h1 className="title">Page not found</h1>}
/>
</Routes>
</div>
</main>
</div>
);
31 changes: 31 additions & 0 deletions src/components/NameFilter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { useSearchParams } from 'react-router-dom';

export const NameFilter = () => {
const [searchParams, setSearchParams] = useSearchParams();
const query = searchParams.get('query') || '';

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

setSearchParams({ query: newQuery });
};

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

<span className="icon is-left">
<i className="fas fa-search" aria-hidden="true" />
</span>
</p>
</div>
);
};
25 changes: 11 additions & 14 deletions src/components/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
import classNames from 'classnames';
import { NavLink } from 'react-router-dom';

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

return (
<nav
data-cy="nav"
className="navbar is-fixed-top has-shadow"
role="navigation"
aria-label="main navigation"
>
<nav data-cy="nav">
<div className="container">
<div className="navbar-brand">
<a className="navbar-item" href="#/">
<NavLink className={hasActiveLink} to="/">
Home
</a>
</NavLink>

<a
aria-current="page"
className="navbar-item has-background-grey-lighter"
href="#/people"
>
<NavLink aria-current="page" className={hasActiveLink} to="/people">
People
</a>
</NavLink>
</div>
</div>
</nav>
Expand Down
89 changes: 7 additions & 82 deletions src/components/PeopleFilters.tsx
Original file line number Diff line number Diff line change
@@ -1,90 +1,15 @@
import { SexFilter } from './SexFilter';
import { NameFilter } from './NameFilter';
import { YearsFilter } from './YearsFilter';

export const PeopleFilters = () => {
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>
</p>

<div className="panel-block">
<p className="control has-icons-left">
<input
data-cy="NameFilter"
type="search"
className="input"
placeholder="Search"
/>

<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>
</div>

<div className="level-right ml-4">
<a
data-cy="centuryALL"
className="button is-success is-outlined"
href="#/people"
>
All
</a>
</div>
</div>
</div>
<SexFilter />
<NameFilter />
<YearsFilter />

<div className="panel-block">
<a className="button is-link is-outlined is-fullwidth" href="#/people">
Expand Down
82 changes: 76 additions & 6 deletions src/components/PeoplePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,64 @@ import { PeopleFilters } from './PeopleFilters';
import { Loader } from './Loader';
import { PeopleTable } from './PeopleTable';

import { useSearchParams } from 'react-router-dom';
import { useFetchPeople } from '../utils/useFetchPeople';
import { SearchParams, Order, SortField } from '../utils/constants';

export const PeoplePage = () => {
const { people, isLoading, errorMessage } = useFetchPeople();
const [searchParams] = useSearchParams();

const sex = searchParams.get(SearchParams.SEX);
const query = searchParams.get(SearchParams.QUERY);
const years = searchParams.getAll(SearchParams.YEARS);
const sortField = searchParams.get(SearchParams.SORT);
const isReversed = searchParams.get(SearchParams.ORDER) === Order.DESC;

let visiblePeople = [...people];

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

if (query) {
const normalQuery = query.toLocaleLowerCase();

visiblePeople = visiblePeople.filter(person => {
return [person.name, person.motherName || '', person.fatherName || '']
.join('\n')
.toLocaleLowerCase()
.includes(normalQuery);
});
}

if (years.length > 0) {
visiblePeople = visiblePeople.filter(person =>
years.includes(Math.ceil(person.born / 100).toString()),
);
}

if (sortField) {
visiblePeople.sort((a, b) => {
switch (sortField) {
case SortField.NAME:
case SortField.SEX:
return a[sortField].localeCompare(b[sortField]);

case SortField.BORN:
case SortField.DIED:
return a[sortField] - b[sortField];

default:
return 0;
}
});

if (isReversed) {
visiblePeople.reverse();
}
}

return (
<>
<h1 className="title">People Page</h1>
Expand All @@ -15,15 +72,28 @@ export const PeoplePage = () => {

<div className="column">
<div className="box table-container">
<Loader />

<p data-cy="peopleLoadingError">Something went wrong</p>
{isLoading && <Loader />}

<p data-cy="noPeopleMessage">There are no people on the server</p>
{errorMessage && (
<p data-cy="peopleLoadingError" className="has-text-danger">
Something went wrong
</p>
)}

<p>There are no people matching the current search criteria</p>
{!people.length && !isLoading ? (
<p data-cy="noPeopleMessage">
There are no people on the server
</p>
) : (
''
)}

<PeopleTable />
{!isLoading && errorMessage === '' && (
<PeopleTable
people={visiblePeople}
peopleFromServer={[...people]}
/>
)}
</div>
</div>
</div>
Expand Down
Loading
Loading