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

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://k0tlik02.github.io/react_people-table-advanced/) and add it to the PR description.
6 changes: 2 additions & 4 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PeoplePage } from './components/PeoplePage';
import { Navbar } from './components/Navbar';

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

export const App = () => {
return (
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
27 changes: 27 additions & 0 deletions src/Root.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {
Navigate,
Route,
HashRouter as Router,
Routes,
} from 'react-router-dom';
import { App } from './App';
import { HomePage } from './components/HomePage/HomePage';
import { NotFoundPage } from './components/NotFoundPage/NotFoundPage';
import { PeoplePage } from './components/PeoplePage';

export const Root = () => {
return (
<Router>
<Routes>
<Route path="/" element={<App />}>
<Route index element={<HomePage />} />
<Route path="home" element={<Navigate to=".." replace={true} />} />
<Route path="people" element={<PeoplePage />}>
<Route path=":slug?" element={<PeoplePage />} />
Comment on lines +19 to +20

Choose a reason for hiding this comment

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

The nested route for PeoplePage seems to render PeoplePage twice: once for the people path and again for the optional :slug? parameter. This might lead to rendering issues or unexpected behavior. Consider reviewing the intended behavior and adjusting the route structure if necessary.

</Route>
<Route path="*" element={<NotFoundPage />} />
</Route>
</Routes>
</Router>
);
};
7 changes: 7 additions & 0 deletions src/components/ErrorMessage/ErrorMessage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const ErrorMessage = () => {
return (
<p data-cy="peopleLoadingError" className="has-text-danger">
Something went wrong
</p>
);
};
3 changes: 3 additions & 0 deletions src/components/HomePage/HomePage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const HomePage = () => {
return <h1 className="title">Home Page</h1>;
};
20 changes: 12 additions & 8 deletions src/components/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import classNames from 'classnames';
import { NavLink } from 'react-router-dom';

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

return (
<nav
data-cy="nav"
Expand All @@ -8,17 +16,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 className={getLinkClass} to="/people">
People
</a>
</NavLink>
</div>
</div>
</nav>
Expand Down
3 changes: 3 additions & 0 deletions src/components/NotFoundPage/NotFoundPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const NotFoundPage = () => {
return <h1 className="title">Page not found</h1>;
};
123 changes: 71 additions & 52 deletions src/components/PeopleFilters.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,45 @@
import { useSearchParams } from 'react-router-dom';
import { getSearchWith, SearchParams } from '../utils/searchHelper';
import { SearchLink } from './SearchLink';
import { Sex } from '../types/Sex';
import classNames from 'classnames';
import { CENTURIES } from '../constants/constants';

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

function setSearchWith(params: SearchParams) {
const search = getSearchWith(searchParams, params);

setSearchParams(search);
}

return (
<nav className="panel">
<p className="panel-heading">Filters</p>

<p className="panel-tabs" data-cy="SexFilter">
<a className="is-active" href="#/people">
<SearchLink
className={classNames({ 'is-active': sex === null })}
params={{ sex: null }}
>
All
</a>
<a className="" href="#/people?sex=m">
</SearchLink>
<SearchLink
className={classNames({ 'is-active ': sex === Sex.Male })}

Choose a reason for hiding this comment

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

There is an extra space in the class name 'is-active ' which might cause styling issues. Consider removing the trailing space to ensure consistent styling.

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

<div className="panel-block">
Expand All @@ -22,74 +49,66 @@ export const PeopleFilters = () => {
type="search"
className="input"
placeholder="Search"
value={query}
onChange={event =>
setSearchWith({ query: event.target.value || null })
}
/>

<span className="icon is-left">
<i className="fas fa-search" aria-hidden="true" />
<i
className="fas fa-search"
aria-hidden="true"
onClick={() => setSearchWith({ query })}
/>
</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>
{CENTURIES.map(century => (
<>
<SearchLink
data-cy="century"
key={century}
params={{
centuries: centuries.includes(century)
? centuries.filter(selCentury => selCentury !== century)
: [...centuries, century],
}}
className={classNames('button mr-1', {
'is-info': centuries.includes(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': !!centuries.length,
})}
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
className="button is-link is-outlined is-fullwidth"
params={{ query: null, centuries: null, sex: null }}
>
Reset all filters
</a>
</SearchLink>
</div>
</nav>
);
Expand Down
92 changes: 81 additions & 11 deletions src/components/PeoplePage.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,99 @@
import { PeopleFilters } from './PeopleFilters';
import { Loader } from './Loader';
import { PeopleTable } from './PeopleTable';
import { ErrorMessage } from './ErrorMessage/ErrorMessage';
import { useEffect, useState } from 'react';
import { Person } from '../types';
import { getPeople } from '../api';
import { useSearchParams } from 'react-router-dom';
import { Parameter } from '../types/Parameter';
import { Order } from '../types/Order';

export const PeoplePage = () => {
const [searchParams] = useSearchParams();
const query = searchParams.get('query') || '';
const sex = searchParams.get('sex');
const centuries = searchParams.getAll('centuries');

const order = searchParams.get('order');
const sort = searchParams.get('sort');

const [people, setPeople] = useState<Person[]>([]);
const [isLoading, setIsLoading] = useState(false);
const [isError, setIsError] = useState(false);

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

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

const filteredPeople = people
.filter(
person =>
person.name.toLocaleLowerCase().includes(query.toLowerCase()) ||
person.fatherName?.toLocaleLowerCase().includes(query.toLowerCase()) ||
person.motherName?.toLowerCase().includes(query.toLowerCase()),
Comment on lines +37 to +39

Choose a reason for hiding this comment

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

Consider using toLowerCase() consistently for string comparison to avoid potential issues with locale-specific behavior. Replace toLocaleLowerCase() with toLowerCase() for uniformity.

)
.filter(person => {
if (!sex) {
return true;
}

return sex === person.sex;
})
.filter(person => {
if (!centuries.length) {
return true;
}

return centuries.includes(Math.ceil(person.born / 100).toString());

Choose a reason for hiding this comment

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

Ensure that the Math.ceil(person.born / 100).toString() calculation correctly represents the century. If there are any edge cases (e.g., year 2000), verify that the logic handles them as expected.

})
.sort((person1: Person, person2: Person): number => {
let coefficient = 0;

if (sort === Parameter.Name.toLowerCase()) {
coefficient = person1.name.localeCompare(person2.name);
} else if (sort === Parameter.Sex.toLowerCase()) {
coefficient = person1.sex.localeCompare(person2.sex);
} else if (sort === Parameter.Born.toLowerCase()) {
coefficient = person1.born - person2.born;
} else if (sort === Parameter.Died.toLowerCase()) {
coefficient = person1.died - person2.died;
}

return order === Order.desc ? -coefficient : coefficient;
});

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>
{!isLoading && !isError && (
<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>

<p>There are no people matching the current search criteria</p>
{isLoading && <Loader />}
{isError && <ErrorMessage />}

<PeopleTable />
{!isLoading &&
!isError &&
(people.length > 0 ? (
<PeopleTable people={filteredPeople} />
) : (
<p data-cy="noPeopleMessage">
There are no people on the server
</p>
))}
</div>
</div>
</div>
Expand Down
Loading
Loading