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

first solution #528

Open
wants to merge 2 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://vensecue.github.io/react_people-table-advanced/) and add it to the PR description.
16 changes: 13 additions & 3 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Navigate, Route, Routes } from 'react-router-dom';
import { PeoplePage } from './components/PeoplePage';
import { Navbar } from './components/Navbar';

Expand All @@ -10,9 +11,18 @@ 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={<h1 className="title">Home Page</h1>} />
<Route
path="*"
element={<h1 className="title">Page not found</h1>}
/>
<Route path="/home" element={<Navigate to="/" replace />} />
<Route path="/people" element={<PeoplePage />}>
<Route path=":slug" element={<PeoplePage />} />
</Route>
</Routes>

</div>
</div>
</div>
Expand Down
20 changes: 15 additions & 5 deletions src/components/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import cn from 'classnames';
import { NavLink } from 'react-router-dom';

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

<a
<NavLink
aria-current="page"
className="navbar-item has-background-grey-lighter"
href="#/people"
className={({ isActive }) => cn('navbar-item',
{ 'has-background-grey-lighter': isActive })}
to="/people"
>
People
</a>
</NavLink>
</div>
</div>
</nav>
Expand Down
131 changes: 99 additions & 32 deletions src/components/PeopleFilters.tsx
Original file line number Diff line number Diff line change
@@ -1,92 +1,159 @@
import { useSearchParams } from 'react-router-dom';
import cn from 'classnames';
import { SearchLink } from './SearchLink';

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

const centurySetter = (century: string) => {
if (allCenturies.includes(century)) {
return allCenturies.filter(age => age !== century);
}

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

const handleQueryChange = (newValue: string) => {
if (newValue === '') {

Choose a reason for hiding this comment

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

Suggested change
if (newValue === '') {
if (!newValue) {

searchParams.delete('query');
} else {
searchParams.set('query', newValue);
}

setSearchParams(searchParams);
};

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>
<SearchLink
className={cn({ 'is-active': selectedGender === null })}
params={{ sex: null }}
>
All

</SearchLink>
<SearchLink
className={cn({ 'is-active': selectedGender === 'm' })}
params={{ sex: 'm' }}
>
Male

</SearchLink>
<SearchLink
className={cn({ 'is-active': selectedGender === 'f' })}
params={{ sex: 'f' }}
>
Female

</SearchLink>
Comment on lines +34 to +54
Copy link

@nataliaklonowska nataliaklonowska Oct 11, 2023

Choose a reason for hiding this comment

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

  • Try to avoid repetition of the SearchLink component - you can use map method to generate links dynamically. Same for SearchLink with centuries - you can create array with possible centuries.
  • To make code more descriptive, it's a good practice to create an enum or variables for 'm', 'f', and null.
  • delete blank lines to improve code readability.

</p>

<div className="panel-block">
<p className="control has-icons-left">
<input
value={query || ''}
onChange={(e) => handleQueryChange(e.target.value)}
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
<SearchLink
data-cy="century"
className="button mr-1"
href="#/people?centuries=16"
className={cn('button mr-1',
{ 'is-info': allCenturies.includes('16') })}
params={{
centuries: centurySetter('16'),
}}
>
16
</a>
</SearchLink>

<a
<SearchLink
data-cy="century"
className="button mr-1 is-info"
href="#/people?centuries=17"
className={cn('button mr-1',
{ 'is-info': allCenturies.includes('17') })}
params={{
centuries: centurySetter('17'),
}}
>
17
</a>
</SearchLink>

<a
<SearchLink
data-cy="century"
className="button mr-1 is-info"
href="#/people?centuries=18"
className={cn('button mr-1',
{ 'is-info': allCenturies.includes('18') })}
params={{
centuries: centurySetter('18'),
}}
>
18
</a>
</SearchLink>

<a
<SearchLink
data-cy="century"
className="button mr-1 is-info"
href="#/people?centuries=19"
className={cn('button mr-1',
{ 'is-info': allCenturies.includes('19') })}
params={{
centuries: centurySetter('19'),
}}
>
19
</a>
</SearchLink>

<a
<SearchLink
data-cy="century"
className="button mr-1"
href="#/people?centuries=20"
className={cn('button mr-1',
{ 'is-info': allCenturies.includes('20') })}
params={{
centuries: centurySetter('20'),
}}
>
20
</a>
</SearchLink>
</div>

<div className="level-right ml-4">
<a
<SearchLink
data-cy="centuryALL"
className="button is-success is-outlined"
href="#/people"
className={cn('button is-success',
{ 'is-outlined': allCenturies.length !== 0 })}
params={{
centuries: null,
}}
>
All
</a>
</SearchLink>
</div>
</div>
</div>

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

const preparePeople = (peop: Person[]) => {
return peop.map(person => {
const mother = peop.find(human => human.name === person.motherName);
const father = peop.find(human => human.name === person.fatherName);

return ({
...person,
mother,
father,
});
});
};

export const PeoplePage = () => {
const [people, setPeople] = useState<Person[] | null>(null);
const [loading, setLoading] = useState(true);
const [loadingError, setLoadingError] = useState(false);

const fetchData = () => {
getPeople()
.then((result) => {
setPeople(preparePeople(result));
})
.catch(() => {
setLoadingError(true);
})
.finally(() => {
setLoading(false);
});
};

useEffect(() => {
fetchData();
}, []);

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 />
{!loading && <PeopleFilters />}
</div>

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

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

<p data-cy="noPeopleMessage">
There are no people on the server
</p>
{loadingError && (
<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 === 0 && (

Choose a reason for hiding this comment

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

Suggested change
{people?.length === 0 && (
{!people?.length && (

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

<PeopleTable />
{people && <PeopleTable people={people} />}
</div>
</div>
</div>
Expand Down
Loading
Loading