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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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://denzzlle171.github.io/react_people-table-advanced/) and add it to the PR description.
33 changes: 18 additions & 15 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import { Routes, Route, Navigate } from 'react-router-dom';
import { PeoplePage } from './components/PeoplePage';
import { Navbar } from './components/Navbar';

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>
export const App = () => (
<div data-cy="app">
<Navbar />
<main className="section">
<div className="container">
<Routes>
<Route path="/home" element={<Navigate to="/" replace />} />
<Route path="/" element={<h1 className="title">Home Page</h1>} />
<Route path="people">
<Route index element={<PeoplePage />} />
<Route path=":userSlug" element={<PeoplePage />} />
</Route>
<Route path="*" element={<h1 className="title">Page not found</h1>} />
</Routes>
</div>
</div>
);
};
</main>
</div>
);
19 changes: 12 additions & 7 deletions src/components/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
import { NavLink } from 'react-router-dom';
import classNames from 'classnames';

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

export const Navbar = () => {
return (
<nav
Expand All @@ -8,15 +15,13 @@ export const Navbar = () => {
>
<div className="container">
<div className="navbar-brand">
<a className="navbar-item" href="#/">Home</a>
<NavLink className={getLinkClass} to="/">
Home
</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
118 changes: 69 additions & 49 deletions src/components/PeopleFilters.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,50 @@
import classnames from 'classnames';
import { useSearchParams } from 'react-router-dom';
import { SearchLink } from './SearchLink';
import { getSearchWith } from '../utils/searchHelper';

export const PeopleFilters = () => {
const [searchParams, setSearchParams] = useSearchParams();

const sex = searchParams.get('sex');
const query = searchParams.get('query') || '';
const centuries = searchParams.getAll('centuries') || [];

const handleQueryChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const newQueryParam = getSearchWith(searchParams, {
query: event.target.value.trim().toLocaleLowerCase() || null,
});

setSearchParams(newQueryParam);
};

const centuriesItem = ['16', '17', '18', '19', '20'];

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={classnames({ 'is-active': !sex })}
params={{ sex: null }}
>
All
</SearchLink>

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

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

Choose a reason for hiding this comment

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

Render as list

</p>

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

<span className="icon is-left">
Expand All @@ -27,66 +67,46 @@ 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>
{centuriesItem.map((century) => {
return (
<SearchLink
key={century}
params={{
centuries: centuries.includes(century)
? centuries.filter((cent) => cent !== 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 > 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 }}

Choose a reason for hiding this comment

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

Find a better way to reset all by just 1 line of code. If you had 20 filters, would you set null to all of them?

Copy link
Author

Choose a reason for hiding this comment

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

all the other options I can think of
will either just increase the amount of code or reset the sorting as well.

>
Reset all filters
</a>
</SearchLink>
</div>
</nav>
);
Expand Down
100 changes: 88 additions & 12 deletions src/components/PeoplePage.tsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,107 @@
import { 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/Person';

export const PeoplePage = () => {
const [people, setPeople] = useState<Person[] | null>(null);
const [isLoad, setIsLoad] = useState<boolean>(false);

Choose a reason for hiding this comment

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

It is better to make isLoad true by default. Now there's no need to set it true in line 23

const [error, setError] = useState<string | null>(null);
const [searchParams] = useSearchParams();

const sex = searchParams.get('sex');
const query = searchParams.get('query')?.trim().toLocaleLowerCase();
const centuries = searchParams.getAll('centuries') || [];
const sort = searchParams.get('sort');
const order = searchParams.get('order');

useEffect(() => {
setIsLoad(true);
getPeople()
.then((peopleFromServer) => setPeople(peopleFromServer))

Choose a reason for hiding this comment

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

.then(setPeople)

.catch(() => {

Choose a reason for hiding this comment

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

Try to catch an error here, maybe it will fix some of your failed tests

setError('Something went wrong');
})
.finally(() => setIsLoad(false));
}, []);

const getVisiblePeople = () => {

Choose a reason for hiding this comment

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

Better move this function outside the component and just call it here with your arguments.

if (!people) {
return [];
}

let visiblePeople = [...people];

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

if (query) {
visiblePeople = visiblePeople.filter((person) => person
.name.toLocaleLowerCase().includes(query)
|| person.motherName?.toLocaleLowerCase().includes(query)
|| person.fatherName?.toLocaleLowerCase().includes(query));
}

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

if (sort) {
visiblePeople.sort((a, b) => {
switch (sort) {
case 'name':
return a.name.localeCompare(b.name);

case 'sex':
return a.sex.localeCompare(b.sex);

case 'born':
return (a.born - b.born);

case 'died':
return (a.died - b.died);

default:
return 0;
}
});
}

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

return visiblePeople;
};

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 />
{people && <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>

<PeopleTable />
{isLoad && <Loader />}
{error && (
<p data-cy="peopleLoadingError" className="has-text-danger">
{error}
</p>
)}
{people && <PeopleTable people={getVisiblePeople()} />}
{!isLoad && !error && !people && (

Choose a reason for hiding this comment

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

no people appears before page is loaded for a moment. Create a variable for this and try to change your people condition.
image

Copy link
Author

Choose a reason for hiding this comment

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

Maybe I've already fixed it with lowder, because I don't see this problem now

<p data-cy="noPeopleMessage">no people</p>
)}
</div>
</div>
</div>
Expand Down
Loading
Loading