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 task solution #1123

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://sophiasph.github.io/react_people-table-advanced/) and add it to the PR description.
12 changes: 4 additions & 8 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import { PeoplePage } from './components/PeoplePage';
import { Navbar } from './components/Navbar';

import './App.scss';
import { Outlet } from 'react-router-dom';

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>
<main className="section">
<Outlet />
</main>
</div>
);
};
33 changes: 33 additions & 0 deletions src/Root.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {
Navigate,
Route,
HashRouter as Router,
Routes,
} from 'react-router-dom';

import 'bulma/css/bulma.css';
import '@fortawesome/fontawesome-free/css/all.css';

import { App } from './App';
import { HomePage } from './components/HomePage';
import { PeoplePage } from './components/PeoplePage';

export const Root = () => {
return (
<Router>
<Routes>
<Route path="/" element={<App />}>
<Route path="/home" element={<Navigate to="/" replace />} />

<Route index element={<HomePage />} />
<Route path="/people">
<Route index element={<PeoplePage />} />
<Route path=":slug" element={<PeoplePage />} />
</Route>

<Route path="*" element={<h1 className="title">Page not found</h1>} />
</Route>
</Routes>
</Router>
);
};
9 changes: 9 additions & 0 deletions src/components/HomePage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react';

export const HomePage = () => {
return (
<div className="container">
<h1 className="title">Home Page</h1>
</div>
);
};
26 changes: 19 additions & 7 deletions src/components/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { NavLink } from 'react-router-dom';

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

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

const centuriesValue: string[] = ['16', '17', '18', '19', '20'];

export const PeopleFilters = () => {
const [searchParams, setSearchParams] = useSearchParams();
//const [sex, setSex] = useState();
const query = searchParams.get('query') || '';
const sex = searchParams.get('sex') || '';
const centuries = searchParams.getAll('centuries') || [];
const resetParams = {
query: null,
sex: null,
centuries: null,
};

const isActiveSex = (value: string | null) => {
return value === sex;
};

const getCenturies = (century: string) => {
return centuries.includes(century)
? centuries.filter(currentCentury => currentCentury !== century)
: [...centuries, century];
};

function handleQueryChange(event: React.ChangeEvent<HTMLInputElement>) {
const params = new URLSearchParams(searchParams);

params.set('query', event.target.value);
setSearchParams(params);
}

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

<p className="panel-tabs" data-cy="SexFilter">
<a className="is-active" href="#/people">
<SearchLink
className={classNames({ 'is-active': isActiveSex(null) })}
params={{ sex: null }}
>
All
</a>
<a className="" href="#/people?sex=m">
</SearchLink>
<SearchLink
className={classNames({ 'is-active': isActiveSex('m') })}
params={{ sex: 'm' }}
>
Male
</a>
<a className="" href="#/people?sex=f">
</SearchLink>
<SearchLink
className={classNames({ 'is-active': isActiveSex('f') })}
params={{ sex: 'f' }}
>
Female
</a>
</SearchLink>
</p>

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

<span className="icon is-left">
Expand All @@ -33,63 +79,43 @@ 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>
{centuriesValue.map(century => {
return (
<SearchLink
key={century}
data-cy="century"
className={classNames('button mr-1', {
'is-info': centuries.includes(century),
})}
params={{ centuries: getCenturies(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 className="button is-link is-outlined is-fullwidth" href="#/people">
<SearchLink
className="button is-link is-outlined is-fullwidth"
params={resetParams}
>
Reset all filters
</a>
</SearchLink>
</div>
</nav>
);
Expand Down
95 changes: 79 additions & 16 deletions src/components/PeoplePage.tsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,95 @@
import { PeopleFilters } from './PeopleFilters';
import { Loader } from './Loader';
import { PeopleTable } from './PeopleTable';
import { useEffect, useState } from 'react';
import { Person } from '../types';
import { getPeople } from '../api';
import { useSearchParams } from 'react-router-dom';
import { getFilteredPeople } from '../utils/getFilteredPeople';

export const PeoplePage = () => {
return (
<>
<h1 className="title">People Page</h1>
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const [people, setPeople] = useState<Person[]>([]);
const [searchParams] = useSearchParams();

<div className="block">
<div className="columns is-desktop is-flex-direction-row-reverse">
<div className="column is-7-tablet is-narrow-desktop">
<PeopleFilters />
</div>
const hasError = !loading && error;
const noPeople = !loading && people.length === 0;
const showContent = !loading && people.length > 0;
const sort = searchParams.get('sort') || '';
const order = searchParams.get('order') || '';
const query = searchParams.get('query') || '';
const sex = searchParams.get('sex') || '';
const centuries = searchParams.getAll('centuries') || [];

const peopleWithParents = people.reduce((newPeople: Person[], person) => {
const mother = people.find(mom => mom.name === person.motherName);
const father = people.find(dad => dad.name === person.fatherName);

const updatedPerson = {
...person,
mother,
father,
};

newPeople.push(updatedPerson);

return newPeople;
}, []);

<div className="column">
<div className="box table-container">
<Loader />
const filteredAndSortedPeople = getFilteredPeople(peopleWithParents, {
sex,
query,
centuries,
sort,
order,
});

<p data-cy="peopleLoadingError">Something went wrong</p>
useEffect(() => {
const fetchPeople = async () => {
setLoading(true);

<p data-cy="noPeopleMessage">There are no people on the server</p>
try {
const fetchedPeople = await getPeople();

<p>There are no people matching the current search criteria</p>
setPeople(fetchedPeople);
} catch {
setError('Something went wrong');
} finally {
setLoading(false);
}
};

fetchPeople();
}, []);

return (
<>
<h1 className="title">People Page</h1>

<div className="block">
{loading && <Loader />}
{hasError && (
<p data-cy="peopleLoadingError" className="has-text-danger">
Something went wrong
</p>
)}
{noPeople && (
<p data-cy="noPeopleMessage">There are no people on the server</p>
)}
{showContent && (
<div className="columns is-desktop is-flex-direction-row-reverse">
<div className="column is-7-tablet is-narrow-desktop">
<PeopleFilters />
</div>

<PeopleTable />
<div className="column">
<div className="box table-container">
<PeopleTable peopleList={filteredAndSortedPeople} />
</div>
</div>
</div>
</div>
)}
</div>
</>
);
Expand Down
Loading
Loading