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

solution #1183

Open
wants to merge 3 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
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://KostiantynBond.github.io/react_people-table-advanced/) and add it to the PR description.
5,357 changes: 3,169 additions & 2,188 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PeoplePage } from './components/PeoplePage';
import { Outlet } from 'react-router-dom';
import { Navbar } from './components/Navbar';

import './App.scss';
Expand All @@ -10,9 +10,7 @@ export const App = () => {

<div className="section">
<div className="container">
<h1 className="title">Home Page</h1>
<h1 className="title">Page not found</h1>
<PeoplePage />
<Outlet />
</div>
</div>
</div>
Expand Down
26 changes: 26 additions & 0 deletions src/Root.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {
HashRouter as Router,
Routes,
Route,
Navigate,
} from 'react-router-dom';
import { App } from './App';
import { HomePage } from './components/HomePage';
import { PeoplePage } from './components/PeoplePage';

export const Root = () => (
<Router>
<Routes>
<Route path="/" element={<App />}>
<Route index element={<HomePage />} />
<Route path="home" element={<Navigate to="/" />} />
<Route path="people" element={<PeoplePage />}>
<Route index />

Choose a reason for hiding this comment

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

The <Route index /> inside the people route does not specify an element. This will result in a route that does nothing when accessed. Consider providing a component or redirect for this index route.

Choose a reason for hiding this comment

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

The <Route index /> is missing an element. It should specify a component or redirect to ensure it performs an action when accessed.

<Route path=":slug" />

Choose a reason for hiding this comment

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

The <Route path=":slug" /> does not specify an element. This will result in a route that does nothing when accessed. Consider providing a component to render when this route is matched.

Choose a reason for hiding this comment

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

The <Route path=":slug" /> is missing an element. Ensure to provide a component or redirect to handle this route properly.

</Route>

<Route path="*" element={<h1 className="title">Page not found</h1>} />
</Route>
</Routes>
</Router>
);
5 changes: 5 additions & 0 deletions src/components/HomePage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import React from 'react';

export const HomePage: React.FC = () => {
return <h1 className="title">Home Page</h1>;
};
19 changes: 10 additions & 9 deletions src/components/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { NavLink } from 'react-router-dom';
import cn from 'classnames';

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

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

<a
aria-current="page"
className="navbar-item has-background-grey-lighter"
href="#/people"
>
</NavLink>
<NavLink to="/people" className={getLinkClass}>
People
</a>
</NavLink>
</div>
</div>
</nav>
Expand Down
5 changes: 5 additions & 0 deletions src/components/NotFoundPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import React from 'react';

export const NotFoundPage: React.FC = () => {
return <h1 className="title">Page not found</h1>;
};
130 changes: 77 additions & 53 deletions src/components/PeopleFilters.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,60 @@
export const PeopleFilters = () => {
import React from 'react';
import { useSearchParams } from 'react-router-dom';
import classNames from 'classnames';
import { SearchLink } from './SearchLink';

const centuries: number[] = [16, 17, 18, 19, 20];

export const PeopleFilters: React.FC = () => {
const [searchParams, setSearchParams] = useSearchParams();
const selectedCenturies = searchParams.getAll('centuries') || [];

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

params.set('query', event.target.value.trim().toLowerCase());
setSearchParams(params);
};

const handleToggleCentury = (century: string) => {
const updatedCenturies = selectedCenturies.includes(century)
? selectedCenturies.filter(centuryYear => centuryYear !== century)
: [...selectedCenturies, century];

const params = new URLSearchParams(searchParams);

params.delete('centuries');
updatedCenturies.forEach(cent => params.append('centuries', cent));
setSearchParams(params);
};

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': !searchParams.get('sex') })}
>
All
</a>
<a className="" href="#/people?sex=m">
</SearchLink>
<SearchLink
params={{ sex: 'm' }}
className={classNames({
'is-active': searchParams.get('sex') === 'm',
})}
>
Male
</a>
<a className="" href="#/people?sex=f">
</SearchLink>
<SearchLink
params={{ sex: 'f' }}
className={classNames({
'is-active': searchParams.get('sex') === 'f',
})}
>
Female
</a>
</SearchLink>
</p>

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

Choose a reason for hiding this comment

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

The onChange event handler handleQueryChange should prevent default behavior to avoid any unexpected behavior during input changes.

/>

<span className="icon is-left">
Expand All @@ -33,63 +76,44 @@ 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>
{centuries.map(century => (
<button
key={century}
data-cy="century"
className={classNames('button mr-1', {
'is-info': selectedCenturies.includes(`${century}`),
})}
onClick={() => handleToggleCentury(`${century}`)}
>
{century}
</button>
))}
</div>

<div className="level-right ml-4">
<a
<button
data-cy="centuryALL"
className="button is-success is-outlined"
href="#/people"
className={classNames('button is-success', {
'is-outlined': selectedCenturies.length !== 0,
})}
onClick={() => setSearchParams(new URLSearchParams(searchParams))}

Choose a reason for hiding this comment

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

The onClick event handler for the 'All' button should reset the centuries filter. Consider clearing the 'centuries' parameter in the URLSearchParams to ensure all filters are reset.

>
All
</a>
</button>
</div>
</div>
</div>

<div className="panel-block">
<a className="button is-link is-outlined is-fullwidth" href="#/people">
<SearchLink
className="button is-link is-outlined is-fullwidth"
params={{
centuries: [],
sex: null,
query: null,
}}
>
Reset all filters
</a>
</SearchLink>
</div>
</nav>
);
Expand Down
72 changes: 62 additions & 10 deletions src/components/PeoplePage.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,81 @@
import { useMemo, useEffect, useState } from 'react';
import { useSearchParams } from 'react-router-dom';
import { PeopleFilters } from './PeopleFilters';
import { Loader } from './Loader';
import { PeopleTable } from './PeopleTable';
import { getPeople } from '../api';
import { Person } from '../types';
import { getFilteredPeople } from '../utils/getFilteredPeople';

export const PeoplePage = () => {
const [people, setPeople] = useState<Person[]>([]);
const [loading, setLoading] = useState(false);
const [error, setError] = useState(false);

const [searchParams] = useSearchParams();

const filteredPeople = useMemo(() => {
const filters = {
sex: searchParams.get('sex'),
name: searchParams.get('query'),
centuries: searchParams.getAll('centuries'),
};

return getFilteredPeople(people, filters);
}, [searchParams, people]);

useEffect(() => {
setLoading(true);

getPeople()
.then(response => setPeople(response))
.catch(() => setError(true))

Choose a reason for hiding this comment

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

Consider providing more detailed error handling to inform the user about the nature of the error. This can help in debugging and improving user experience.

.finally(() => setLoading(false));
}, []);

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>
{!loading && !error && (
<div className="column is-7-tablet is-narrow-desktop">
<PeopleFilters />
</div>
)}

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

<p data-cy="peopleLoadingError">Something went wrong</p>

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

<p>There are no people matching the current search criteria</p>
{error && (
<p data-cy="peopleLoadingError">
Something went wrong.
<button
onClick={() => window.location.reload()}
className="button is-link ml-2"
>
Retry
</button>
</p>
)}

<PeopleTable />
{!loading && !error && (
<>
{people.length === 0 ? (
<p data-cy="noPeopleMessage">
There are no people on the server
</p>
) : filteredPeople.length === 0 ? (
<p>
There are no people matching the current search criteria
</p>
) : (
<PeopleTable people={filteredPeople} />
)}
Comment on lines +66 to +76

Choose a reason for hiding this comment

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

The conditional rendering for no data and no matching criteria is overlapping. Consider consolidating these conditions to avoid redundancy and ensure clarity.

</>
)}
</div>
</div>
</div>
Expand Down
Loading