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

add initial solution #493

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

import './App.scss';

export const App = () => {
return (
<div data-cy="app">
<Navbar />
export const App = () => (
<Routes>
<Route path="/" element={<Layout />}>
<Route index element={<HomePage />} />

<div className="section">
<div className="container">
<h1 className="title">Home Page</h1>
<h1 className="title">Page not found</h1>
<PeoplePage />
</div>
</div>
</div>
);
};
<Route path="people">
<Route path=":slug?" element={<PeoplePage />} />
</Route>

<Route path="home" element={<Navigate to="/" replace />} />
<Route path="*" element={<NotFoundPage />} />
</Route>
</Routes>
);
2 changes: 1 addition & 1 deletion src/api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Person } from './types/Person';

// eslint-disable-next-line max-len
const API_URL = 'https://mate-academy.github.io/react_people-table/api/people.json';
desevoker marked this conversation as resolved.
Show resolved Hide resolved
export const API_URL = 'https://mate-academy.github.io/react_people-table/api/people.json';

function wait(delay: number) {
return new Promise(resolve => setTimeout(resolve, delay));
Expand Down
3 changes: 3 additions & 0 deletions src/components/HomePage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const HomePage = () => (
<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 { Outlet } from 'react-router-dom';
import { Navbar } from './Navbar';

export const Layout = () => (
<div data-cy="app">
<Navbar />

<div className="section">
<div className="container">
<Outlet />
</div>
</div>
</div>
);
26 changes: 18 additions & 8 deletions src/components/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
export const Navbar = () => {
import { memo } from 'react';
import { NavLink, useLocation } from 'react-router-dom';
import { getNavLinkClass } from '../utils/helpers';

export const Navbar = memo(() => {
const { search } = useLocation();

return (
<nav
data-cy="nav"
Expand All @@ -8,17 +14,21 @@ export const Navbar = () => {
>
<div className="container">
<div className="navbar-brand">
<a className="navbar-item" href="#/">Home</a>
<NavLink
to="/"
className={getNavLinkClass}
>
Home
</NavLink>

<a
aria-current="page"
className="navbar-item has-background-grey-lighter"
href="#/people"
<NavLink
to={{ pathname: 'people', search }}
className={getNavLinkClass}
>
People
</a>
</NavLink>
</div>
</div>
</nav>
);
};
});
3 changes: 3 additions & 0 deletions src/components/NotFoundPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const NotFoundPage = () => (
<h1 className="title">Page not found</h1>
);
130 changes: 77 additions & 53 deletions src/components/PeopleFilters.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,68 @@
export const PeopleFilters = () => {
import { memo } from 'react';
import { useSearchParams } from 'react-router-dom';
import classNames from 'classnames';
import { CENTURIES } from '../utils/constants';
import { SearchParam, Sex } from '../types';
import { toggleCentury } from '../utils';
import { SearchLink } from './SearchLink';

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

const sex = searchParams.get(SearchParam.Sex) ?? '';
const query = searchParams.get(SearchParam.Query) ?? '';
const centuries = searchParams.getAll(SearchParam.Centuries) ?? [];

const handleQueryChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const inputValue = event.target.value;

setSearchParams((currentParams) => {
if (inputValue) {
currentParams.set(SearchParam.Query, inputValue);
} else {
currentParams.delete(SearchParam.Query);
}

return currentParams;
});
};

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>
{Object.entries(Sex).map(([option, value]) => {
const isActive = value === Sex.All
? !sex
: sex === value;
const sexValue = value === Sex.All
? null
: value;

return (
<SearchLink
key={value}
className={classNames({
'is-active': isActive,
})}
params={{ sex: sexValue }}
>
{option}
</SearchLink>
);
})}
</p>

<div className="panel-block">
<p className="control has-icons-left">
<input
data-cy="NameFilter"
type="search"
data-cy="NameFilter"
className="input"
placeholder="Search"
value={query}
onChange={handleQueryChange}
/>

<span className="icon is-left">
Expand All @@ -27,67 +74,44 @@ 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>
{CENTURIES.map(century => (
<SearchLink
key={century}
className={classNames('button', 'mr-1', {
'is-info': centuries.includes(century),
})}
params={{ centuries: toggleCentury(centuries, century) }}
>
{century}
</SearchLink>
))}
</div>

<div className="level-right ml-4">
<a
data-cy="centuryALL"
className="button is-success is-outlined"
href="#/people"
<SearchLink
className={classNames('button', 'is-success', {
'is-outlined': !!centuries.length,
})}
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={{
sex: null,
query: null,
centuries: null,
}}
>
Reset all filters
</a>
</SearchLink>
</div>
</nav>
);
};
});
87 changes: 76 additions & 11 deletions src/components/PeoplePage.tsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,96 @@
import { PeopleFilters } from './PeopleFilters';
import { useEffect, useMemo, useState } from 'react';
import { useParams, useSearchParams } from 'react-router-dom';
import { Person, SearchParam } from '../types';
import { getPeople } from '../api';
import {
getPreparedPeople,
getSortedPeople,
getFilteredPeople,
} from '../utils';
import { Loader } from './Loader';
import { PeopleTable } from './PeopleTable';
import { PeopleFilters } from './PeopleFilters';

export const PeoplePage = () => {
const [isLoading, setIsLoading] = useState(false);
const [isError, setIsError] = useState(false);
const [people, setPeople] = useState<Person[]>([]);
const { slug } = useParams();
const [searchParams] = useSearchParams();

const sort = searchParams.get(SearchParam.Sort) ?? '';
const order = searchParams.get(SearchParam.Order) ?? '';
const sex = searchParams.get(SearchParam.Sex) ?? '';
const query = searchParams.get(SearchParam.Query) ?? '';
const centuries = searchParams.getAll(SearchParam.Centuries) ?? [];

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

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

const sortedPeople = useMemo(
() => getSortedPeople(people, sort, order),
[people, sort, order],
);

const filteredPeople = useMemo(
() => getFilteredPeople(sortedPeople, sex, query, centuries),
[sortedPeople, sex, query, centuries],
);

const isRequestSuccessful = !isLoading && !isError;
const hasPeople = !!people.length;
const hasVisiblePeople = !!filteredPeople.length;

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>
{isRequestSuccessful && (
<div className="column is-7-tablet is-narrow-desktop">
<PeopleFilters />
</div>
)}

<div className="column">
<div className="box table-container">
<Loader />
{isLoading && <Loader />}

<p data-cy="peopleLoadingError">Something went wrong</p>
{isError && (
<p data-cy="peopleLoadingError" className="has-text-danger">
Something went wrong
</p>
)}

<p data-cy="noPeopleMessage">
There are no people on the server
</p>
{isRequestSuccessful && (
<>
{hasPeople && hasVisiblePeople && (
<PeopleTable
people={filteredPeople}
selectedPersonSlug={slug}
/>
)}

<p>There are no people matching the current search criteria</p>
{hasPeople && !hasVisiblePeople && (
<p>
There are no people matching the current search criteria
</p>
)}

<PeopleTable />
{!hasPeople && (
<p data-cy="noPeopleMessage">
There are no people on the server
</p>
)}
</>
)}
</div>
</div>
</div>
Expand Down
Loading