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

add task solution #1135

Open
wants to merge 1 commit 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://roman-logos-frontend.github.io/react_people-table-advanced/) and add it to the PR description.
7 changes: 3 additions & 4 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { PeoplePage } from './components/PeoplePage';
import { Navbar } from './components/Navbar';
import React from 'react';

import './App.scss';
import { Outlet } from 'react-router-dom';

export const App = () => {
return (
Expand All @@ -10,9 +11,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
1 change: 1 addition & 0 deletions src/components/Loader/Loader.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import './Loader.scss';
import React from 'react';

export const Loader = () => (
<div className="Loader" data-cy="loader">
Expand Down
21 changes: 13 additions & 8 deletions src/components/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
import classNames from 'classnames';
import React from 'react';
import { NavLink } from 'react-router-dom';

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

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

<a
aria-current="page"
className="navbar-item has-background-grey-lighter"
href="#/people"
>
<NavLink aria-current="page" className={getLinkClass} to="/people">
People
</a>
</NavLink>
</div>
</div>
</nav>
Expand Down
131 changes: 79 additions & 52 deletions src/components/PeopleFilters.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,62 @@
import React, { useState } from 'react';
import { SearchLink } from './SearchLink';
import { useSearchParams } from 'react-router-dom';

export const PeopleFilters = () => {
const [searchParams, setSearchParams] = useSearchParams();
const selectedCenturies = searchParams.getAll('centuries');
const [query, setQuery] = useState(searchParams.get('query') || '');

const toggleCentury = (century: string): string[] => {
if (selectedCenturies.includes(century)) {
return selectedCenturies.filter(c => c !== century);
}

return [...selectedCenturies, century];
};

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

setQuery(newQuery);
const newSearchParams = new URLSearchParams(searchParams);

if (newQuery) {
newSearchParams.set('query', newQuery);
} else {
newSearchParams.delete('query');
}

setSearchParams(newSearchParams);
};

const isAllActive = selectedCenturies.length === 0;

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={searchParams.get('sex') === null ? 'is-active' : ''}
>
All
</a>
<a className="" href="#/people?sex=m">
</SearchLink>

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

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

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

<span className="icon is-left">
<i className="fas fa-search" aria-hidden="true" />
</span>
Expand All @@ -33,63 +78,45 @@ 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}
params={{ centuries: toggleCentury(century) }}

Choose a reason for hiding this comment

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

The toggleCentury function is called directly within the params prop of SearchLink. This means it will be executed on every render, which could lead to unexpected behavior. Consider refactoring this logic to ensure toggleCentury is only called when necessary, such as within an event handler.

className={`button mr-1 ${
selectedCenturies.includes(century) ? 'is-info' : ''
}`}
data-cy="century"
>
{century}
</SearchLink>
))}
</div>

<div className="level-right ml-4">
<a
<SearchLink
params={{ centuries: null }}
className={`button is-success ${
isAllActive ? '' : 'is-outlined'
}`}
data-cy="centuryALL"
className="button is-success is-outlined"
href="#/people"
>
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,
centuries: null,
query: null,
}}
className="button is-link is-outlined is-fullwidth"
>
Reset all filters
</a>
</SearchLink>
</div>
</nav>
);
Expand Down
74 changes: 65 additions & 9 deletions src/components/PeoplePage.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,60 @@
import React, { useEffect, useState } from 'react';
import { PeopleFilters } from './PeopleFilters';
import { Loader } from './Loader';
import { PeopleTable } from './PeopleTable';
import { getPeople } from '../api';
import { Person } from '../types';
import { useSearchParams } from 'react-router-dom';

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

useEffect(() => {
const fetchPeople = async () => {
try {
setLoading(true);
const data = await getPeople();

setPeople(data);
setLoading(false);
} catch (e) {
setError('Something went wrong');

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 information to the user, if available. This can help users understand what went wrong and potentially how to fix it. You might want to log the actual error message or display it conditionally.

setLoading(false);
}
};

fetchPeople();
}, []);

const [searchParams] = useSearchParams();

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

const isInSelectedCenturies = (person: Person) => {
if (selectedCenturies.length === 0) {
return true;
}

return selectedCenturies.some(century => {
const startYear = (parseInt(century, 10) - 1) * 100 + 1;
const endYear = startYear + 99;

return person.born >= startYear && person.born <= endYear;
});
};

const filteredPeople = people.filter(person => {
const matchesQuery = person.name.toLowerCase().includes(query);
const matchesSex = !sex || person.sex === sex;
const matchesCentury = isInSelectedCenturies(person);

return matchesQuery && matchesSex && matchesCentury;
});

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

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

<p>There are no people matching the current search criteria</p>

<PeopleTable />
{loading && <Loader />}
{error && <p data-cy="peopleLoadingError">{error}</p>}
{!loading && !error && people.length === 0 && (
<p data-cy="noPeopleMessage">
There are no people on the server
</p>
)}
{!loading && !error && filteredPeople.length > 0 && (
<PeopleTable people={filteredPeople} />
)}
{!loading && !error && filteredPeople.length === 0 && (
<p>There are no people matching the current search criteria</p>
)}
</div>
</div>
</div>
Expand Down
Loading
Loading