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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

Develop #1136

Show file tree
Hide file tree
Changes from 5 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://italomagno.github.io/react_people-table-advanced/) and add it to the PR description.
230 changes: 131 additions & 99 deletions package-lock.json

Large diffs are not rendered by default.

27 changes: 15 additions & 12 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import { PeoplePage } from './components/PeoplePage';
import { Navbar } from './components/Navbar';

import './App.scss';
import React from 'react';
import { Navigate, Route, Routes } from 'react-router-dom';
import { Layout } from './components/Layout';
import { HomePageComponent } from './components/HomePageComponent';
import { NotFoundPage } from './components/NotFoundPage';

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>
</div>
</div>
<Layout>
<Routes>
<Route path="/" element={<HomePageComponent />} />
<Route path="/home" element={<Navigate to={'/'} />} />
<Route path="/people" element={<PeoplePage />}>
<Route path="/people/:slug" element={<PeoplePage />} />
</Route>
Comment on lines +16 to +18

Choose a reason for hiding this comment

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

The nested route inside /people is redundant since it uses the same component PeoplePage. If PeoplePage is supposed to handle both /people and /people/:slug, you might want to handle the logic for rendering different content based on the presence of :slug within the PeoplePage component itself. Consider removing the nested route or adjusting the component logic.

<Route path="*" element={<NotFoundPage />} />
</Routes>
</Layout>
);
};
25 changes: 25 additions & 0 deletions src/components/ErrorComponent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* eslint-disable max-len */
import React from 'react';
import { ErrorMessages } from './PeoplePage';

interface ErrorComponentProps {
message: ErrorMessages;
}

export function ErrorComponent({ message }: ErrorComponentProps) {
return (
<>
{message === ErrorMessages.SOMETHING_WENT_WRONG && (

Choose a reason for hiding this comment

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

The way you implemented this isn't wrong, but i would recommend to use dynamic property access to avoid repetition (and personally i think that's also improves code readability)!

Imagine if we have 50 types of error messages, what would we do?

an example implementation:

    <>
        {message && (
            <p data-cy={`${message}Message`}>
            {ErrorMessages[message]}
            </p>
        )}
    </>

<p data-cy="peopleLoadingError">{ErrorMessages.SOMETHING_WENT_WRONG}</p>
)}
{message === ErrorMessages.NO_PEOPLE && (
<p data-cy="noPeopleMessage">{ErrorMessages.NO_PEOPLE}</p>
)}
{message === ErrorMessages.NO_PEOPLE_MATCHING_CRITERIA && (
<p data-cy="noPeopleMessage">
{ErrorMessages.NO_PEOPLE_MATCHING_CRITERIA}
</p>
)}
</>
);
}
5 changes: 5 additions & 0 deletions src/components/HomePageComponent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import React from 'react';

export function HomePageComponent() {
return <h1 className="title">Home Page</h1>;
}
14 changes: 14 additions & 0 deletions src/components/Layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from 'react';
import { Navbar } from './Navbar';

export function Layout({ children }: { children: React.ReactNode }) {
return (
<div data-cy="app">
<Navbar />

<div className="section">
<div className="container">{children}</div>
</div>
</div>
);
}
12 changes: 10 additions & 2 deletions src/components/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import React from 'react';
import { useLocation } from 'react-router-dom';

export const Navbar = () => {
const path = useLocation().pathname;

return (
<nav
data-cy="nav"
Expand All @@ -8,13 +13,16 @@ export const Navbar = () => {
>
<div className="container">
<div className="navbar-brand">
<a className="navbar-item" href="#/">
<a
className={`navbar-item ${path.includes('home') || path === '/' ? 'has-background-grey-lighter' : ''}`}

Choose a reason for hiding this comment

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

Suggested change
className={`navbar-item ${path.includes('home') || path === '/' ? 'has-background-grey-lighter' : ''}`}
className={`navbar-item ${path.includes('home') || path === '/' ? 'has-background-grey-lighter' : ''}`}

We can extract it to a variable (or a function) to avoid complexity in the JSX =)

    const getHomeLinkClassNames = () => {
        const isHome = path.includes('home') || path === '/'

        return isHome ? 'has-background-grey-lighter' : ''
    }

    ...

    <a className={getHomeLinkClassNames()}>

href="#/"
>
Home
</a>

<a
aria-current="page"
className="navbar-item has-background-grey-lighter"
className={`navbar-item ${path.includes('people') ? 'has-background-grey-lighter' : ''}`}
href="#/people"
>
People
Expand Down
5 changes: 5 additions & 0 deletions src/components/NotFoundPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import React from 'react';

export function NotFoundPage() {
return <h1 className="title">Page not found</h1>;
}
145 changes: 93 additions & 52 deletions src/components/PeopleFilters.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,72 @@
import React from 'react';
import { Link, useLocation, useSearchParams } from 'react-router-dom';
import { getSearchWith, SearchParams } from '../utils/searchHelper';

export const PeopleFilters = () => {
const availableCenturies = [16, 17, 18, 19, 20];

Choose a reason for hiding this comment

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

Suggested change
const availableCenturies = [16, 17, 18, 19, 20];
const availableCenturies = [16, 17, 18, 19, 20];

We can move this variable to outside of the component, since we aren't changing it's values anywhere in the code flow, so we can avoid it's re-creation across re-renders =)


const pathName = useLocation().pathname;
const [searchParams, setSearchParams] = useSearchParams();
const sex = searchParams.get('sex');
const searchInputValue = searchParams.get('query');
const centuries = searchParams.getAll('centuries');

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

setSearchParams(search);

return search;
}

function handleSearchInputChange(event: React.ChangeEvent<HTMLInputElement>) {
const query = event.target.value === '' ? null : event.target.value;

const search = setParamsWith({ query });

setSearchParams(search);
}

function handleAddNewCentury(century: number) {
const newCenturies = centuries.includes(century.toString())
? centuries.filter(c => c !== century.toString())
: [...centuries, century.toString()];

return newCenturies;

Choose a reason for hiding this comment

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

The handleAddNewCentury function returns newCenturies, but this value is not used directly in the component. Instead, it's passed to getSearchWith in the to prop of the Link component. Ensure that the logic for updating the centuries is correctly handled in the getSearchWith function and that the returned value is used appropriately.

}

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

<p className="panel-tabs" data-cy="SexFilter">
<a className="is-active" href="#/people">
<Link
className={`${sex ? '' : 'is-active'}`}
to={{
pathname: pathName,
search: `${getSearchWith(searchParams, { sex: null })}`,
}}
>
All
</a>
<a className="" href="#/people?sex=m">
</Link>
<Link
className={`${sex === 'm' ? 'is-active' : ''}`}
to={{
pathname: pathName,
search: `${getSearchWith(searchParams, { sex: 'm' })}`,
}}
>
Male
</a>
<a className="" href="#/people?sex=f">
</Link>
<Link
className={`${sex === 'f' ? 'is-active' : ''}`}
to={{
pathname: pathName,
search: `${getSearchWith(searchParams, { sex: 'f' })}`,
}}
>
Female
</a>
</Link>
</p>

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

<span className="icon is-left">
Expand All @@ -33,63 +89,48 @@ 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>
{availableCenturies.map(century => (
<Link
key={`century-${century}`}
className={`button mr-1 ${centuries.includes(century.toString()) ? 'is-info' : ''}`}
to={{
pathname: pathName,
search: `${getSearchWith(searchParams, { centuries: handleAddNewCentury(century) })}`,
}}
>
{century}
</Link>
))}
</div>

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

<div className="panel-block">
<a className="button is-link is-outlined is-fullwidth" href="#/people">
<Link
className="button is-link is-outlined is-fullwidth"
to={{
pathname: pathName,
search: getSearchWith(searchParams, {
centuries: [],
sex: null,
query: null,
}),
}}
>
Reset all filters
</a>
</Link>
</div>
</nav>
);
Expand Down
Loading
Loading