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

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://tania-kuzmenko.github.io/react_people-table-advanced/) and add it to the PR description.
4 changes: 4 additions & 0 deletions src/App.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
iframe {
display: none;
}

.section {
margin-top: 3.25em;
}
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
1 change: 1 addition & 0 deletions src/Pages/HomePage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const HomePage = () => <h1 className="title">Home Page</h1>;
1 change: 1 addition & 0 deletions src/Pages/PageNotFound.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const PageNotFound = () => <h1 className="title">Page not found</h1>;
10 changes: 10 additions & 0 deletions src/Pages/PeoplePage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { PeopleTable } from '../components/PeopleTable';

export const PeoplePage = () => {
return (
<>
<h1 className="title">People Page</h1>
<PeopleTable />
</>
);
};
32 changes: 32 additions & 0 deletions src/Root.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {
Navigate,
Route,
HashRouter as Router,
Routes,
} from 'react-router-dom';
import { App } from './App';
import { PeopleProvider } from './store/PeopleContext';
import { HomePage } from './Pages/HomePage';
import { PeoplePage } from './components/PeoplePage';

Choose a reason for hiding this comment

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

The import path for PeoplePage should be ../components/PeoplePage instead of ./components/PeoplePage to match the file structure.

import { PageNotFound } from './Pages/PageNotFound';
import { FilterProvider } from './store/FilterContext';

export const Root = () => (
<Router>
<FilterProvider>
<PeopleProvider>
<Routes>
<Route path="/" element={<App />}>
<Route path="home" element={<Navigate to="/" replace />} />
<Route index element={<HomePage />} />
<Route path="people" element={<PeoplePage />}>
<Route index element={<PeoplePage />} />
<Route path=":slug?" />
</Route>
<Route path="*" element={<PageNotFound />} />
</Route>
</Routes>
</PeopleProvider>
</FilterProvider>
</Router>
);
30 changes: 24 additions & 6 deletions src/components/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { NavLink } from 'react-router-dom';
import { getLinkClass } from '../utils/utils';

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

<a
<NavLink
to="/people"
aria-current="page"
className="navbar-item has-background-grey-lighter"
href="#/people"
className={({ isActive }) =>
getLinkClass(
isActive,
'navbar-item',
'has-background-grey-lighter',
)
}
>
People
</a>
</NavLink>
</div>
</div>
</nav>
Expand Down
145 changes: 90 additions & 55 deletions src/components/PeopleFilters.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,58 @@
import { Link, NavLink } from 'react-router-dom';
import { useFilters } from '../store/FilterContext';
import classNames from 'classnames';

export const PeopleFilters = () => {
const { query, sexs, centuries, setSearchWith, getSearchWith, searchParams } =
useFilters();

function handleQueryChange(event: React.ChangeEvent<HTMLInputElement>) {
setSearchWith({ query: event.target.value });
}

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>
{['All', 'Male', 'Female'].map(sex => {
let normalizedSex = 'All';

switch (sex) {
case 'Male':
normalizedSex = 'm';
break;
case 'Female':
normalizedSex = 'f';
}
Comment on lines +21 to +27

Choose a reason for hiding this comment

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

The switch statement does not have a default case. It's a good practice to include a default case to handle unexpected values, even if it's just to return the current state or log an error.


const isActive =
normalizedSex === 'All'
? sexs.length === 0
: sexs.includes(normalizedSex);

return (
<NavLink
key={sex}
to={{
search: getSearchWith(
{
sexs:
normalizedSex === 'All'
? null
: sexs.includes(normalizedSex)
? sexs.filter(ch => normalizedSex !== ch)
: [...sexs, normalizedSex],
},
searchParams,
),
}}
className={classNames('panel-tab', { 'is-active': isActive })}
>
{sex}
</NavLink>
);
})}
</p>

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

<span className="icon is-left">
Expand All @@ -33,63 +75,56 @@ 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 => (
<Link
key={century}
to={{
search: getSearchWith(
{
centuries: centuries.includes(century)
? centuries.filter(ch => century !== ch)
: [...centuries, century],
},
searchParams,
),
}}
data-cy="century"
className={classNames('button mr-1', {
'is-info': centuries.includes(century),
})}
>
{century}
</Link>
))}
</div>

<div className="level-right ml-4">
<a
<Link
to={{ search: getSearchWith({ centuries: null }, searchParams) }}
data-cy="centuryALL"
className="button is-success is-outlined"
href="#/people"
className={classNames('button is-success ', {
'is-outlined': centuries.length > 0,
})}
>
All
</a>
</Link>
</div>
</div>
</div>

<div className="panel-block">
<a className="button is-link is-outlined is-fullwidth" href="#/people">
<Link
to={{
search: getSearchWith(
{ centuries: null, sexs: null, query: '' },
searchParams,
),
}}
className={classNames('button is-link is-fullwidth', {
'is-outlined ': sexs.length > 0 || centuries.length > 0 || query,
})}
>
Reset all filters
</a>
</Link>
</div>
</nav>
);
Expand Down
33 changes: 21 additions & 12 deletions src/components/PeoplePage.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,38 @@
import { PeopleFilters } from './PeopleFilters';
import { Loader } from './Loader';
import { PeopleTable } from './PeopleTable';
import { usePeople } from '../store/PeopleContext';

export const PeoplePage = () => {
const { isLoading, errorMsg, filteredPeople } = usePeople();

if (isLoading) {
return <Loader />;
}

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

<PeopleTable />
{!isLoading && errorMsg && (
<p data-cy="peopleLoadingError" className="has-text-danger">
{errorMsg}
</p>
)}
{!isLoading && !errorMsg && filteredPeople.length === 0 && (
<p data-cy="noPeopleMessage">{errorMsg}</p>
)}
Comment on lines +32 to +34

Choose a reason for hiding this comment

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

The condition for displaying the 'no people' message uses errorMsg, which might not be appropriate if there is no error but simply no people to display. Consider using a different message or condition to handle this case.

{filteredPeople.length > 0 && <PeopleTable />}
</div>
</div>
</div>
Expand Down
Loading
Loading