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

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
284 changes: 180 additions & 104 deletions package-lock.json

Large diffs are not rendered by default.

148 changes: 7 additions & 141 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Loader } from './components/Loader';

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

import { getLinkClass } from './utils/getLinkClass';
export const App = () => (
<div data-cy="app">
<nav
Expand All @@ -12,154 +11,21 @@ export const App = () => (
>
<div className="container">
<div className="navbar-brand">
<a className="navbar-item" href="#/">
<NavLink to="/" className={getLinkClass}>
Home
</a>
</NavLink>

<a
className="navbar-item has-background-grey-lighter"
href="#/people"
>
<NavLink to="/people" className={getLinkClass}>
People
</a>
</NavLink>
</div>
</div>
</nav>

<main className="section">
<div className="container">
<h1 className="title">Home Page</h1>
<h1 className="title">People Page</h1>
<h1 className="title">Page not found</h1>

<div className="block">
<div className="box table-container">
<Loader />

<p data-cy="peopleLoadingError" className="has-text-danger">
Something went wrong
</p>

<p data-cy="noPeopleMessage">There are no people on the server</p>

<table
data-cy="peopleTable"
className="table is-striped is-hoverable is-narrow is-fullwidth"
>
<thead>
<tr>
<th>Name</th>
<th>Sex</th>
<th>Born</th>
<th>Died</th>
<th>Mother</th>
<th>Father</th>
</tr>
</thead>

<tbody>
<tr data-cy="person">
<td>
<a href="#/people/jan-van-brussel-1714">Jan van Brussel</a>
</td>

<td>m</td>
<td>1714</td>
<td>1748</td>
<td>Joanna van Rooten</td>
<td>Jacobus van Brussel</td>
</tr>

<tr data-cy="person">
<td>
<a href="#/people/philibert-haverbeke-1907">
Philibert Haverbeke
</a>
</td>

<td>m</td>
<td>1907</td>
<td>1997</td>

<td>
<a
className="has-text-danger"
href="#/people/emma-de-milliano-1876"
>
Emma de Milliano
</a>
</td>

<td>
<a href="#/people/emile-haverbeke-1877">Emile Haverbeke</a>
</td>
</tr>

<tr data-cy="person" className="has-background-warning">
<td>
<a href="#/people/jan-frans-van-brussel-1761">
Jan Frans van Brussel
</a>
</td>

<td>m</td>
<td>1761</td>
<td>1833</td>
<td>-</td>

<td>
<a href="#/people/jacobus-bernardus-van-brussel-1736">
Jacobus Bernardus van Brussel
</a>
</td>
</tr>

<tr data-cy="person">
<td>
<a
className="has-text-danger"
href="#/people/lievijne-jans-1542"
>
Lievijne Jans
</a>
</td>

<td>f</td>
<td>1542</td>
<td>1582</td>
<td>-</td>
<td>-</td>
</tr>

<tr data-cy="person">
<td>
<a href="#/people/bernardus-de-causmaecker-1721">
Bernardus de Causmaecker
</a>
</td>

<td>m</td>
<td>1721</td>
<td>1789</td>

<td>
<a
className="has-text-danger"
href="#/people/livina-haverbeke-1692"
>
Livina Haverbeke
</a>
</td>

<td>
<a href="#/people/lieven-de-causmaecker-1696">
Lieven de Causmaecker
</a>
</td>
</tr>
</tbody>
</table>
</div>
<Outlet />
</div>
</div>
</main>
Expand Down
24 changes: 24 additions & 0 deletions src/Router/Root.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { HashRouter, Navigate, Route, Routes } from 'react-router-dom';
import { App } from '../App';
import { HomePage } from '../components/HomePage';
import { PeoplePage } from '../components/PeoplePage';
import { NotFound } from '../components/NotFound';

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

<Route path="people">
<Route index element={<PeoplePage />} />
<Route path=":slug?" element={<PeoplePage />} />
</Route>
<Route path="*" element={<NotFound />} />
</Route>
</Routes>
</HashRouter>
</>
);
1 change: 1 addition & 0 deletions src/components/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/components/NotFound.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const NotFound = () => <h1 className="title">Page not found</h1>;
20 changes: 20 additions & 0 deletions src/components/PeopleLink.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Link } from 'react-router-dom';
import { Person } from '../types';
import cn from 'classnames';

type Props = {
person: Person;
};

export const PersonLink: React.FC<Props> = ({ person }) => {
const { slug, sex, name } = person;

return (
<Link
to={`/people/${slug}`}
className={cn({ 'has-text-danger': sex === 'f' })}
>
{name}
</Link>
);
};
58 changes: 58 additions & 0 deletions src/components/PeoplePage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { useEffect, useState } from 'react';
import { Loader } from './Loader';
import { getPeople } from '../api';
import { Person } from '../types';
import { Error } from '../types/Error';
import { Table } from './PeopleTable';

export const PeoplePage = () => {
const [isLoading, setIsLoading] = useState(false);
const [people, setPeople] = useState<Person[]>([]);
const [error, setError] = useState(Error.Default);

const loadPeople = () => {
setIsLoading(true);
setError(Error.Default);

getPeople()
.then(peop => {
const peopleWithFamily = peop.map(person => {
const father = peop.find(pers => pers.name === person.fatherName);
const mother = peop.find(pers => pers.name === person.motherName);

return {
...person,
father,
mother,
};
});

setPeople(peopleWithFamily);
})
.catch(() => setError(Error.Load))
.finally(() => setIsLoading(false));
};

useEffect(() => {
loadPeople();
}, []);

return (
<>
<h1 className="title">People Page</h1>
<div className="box table-container">
{isLoading && !error && <Loader />}
{error && (
<p data-cy="peopleLoadingError" className="has-text-danger">
{error}
</p>
)}

{people.length === 0 && !error && !isLoading && (
<p data-cy="noPeopleMessage">There are no people on the server</p>
)}
{!isLoading && people.length > 0 && <Table people={people} />}
</div>
</>
);
};
73 changes: 73 additions & 0 deletions src/components/PeopleTable.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { useParams } from 'react-router-dom';
import { Person } from '../types';
import cn from 'classnames';
import { PersonLink } from './PeopleLink';

type Props = {
people: Person[];
};

export const Table: React.FC<Props> = ({ people }) => {
const { slug } = useParams();

const isSelected = people.find(person => person.slug === slug);

return (
<table
data-cy="peopleTable"
className="table is-striped is-hoverable is-narrow is-fullwidth"
>
<thead>
<tr>
<th>Name</th>
<th>Sex</th>
<th>Born</th>
<th>Died</th>
<th>Mother</th>
<th>Father</th>
</tr>
</thead>

<tbody>
{people.map(person => {
const {
sex,
born,
died,
fatherName,
motherName,
slug: personSlug,
mother,
father,
} = person;

Choose a reason for hiding this comment

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

You can put const isSelected = person.slug === slug; here instead

return (
<tr
key={person.slug}
data-cy="person"

Choose a reason for hiding this comment

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

Create a person component

className={cn({
'has-background-warning': isSelected?.slug === personSlug,
})}
>
<td>
<PersonLink person={person} />
</td>

<td>{sex}</td>
<td>{born}</td>
<td>{died}</td>

<td>
{mother ? <PersonLink person={mother} /> : motherName || '-'}
</td>

<td>
{father ? <PersonLink person={father} /> : fatherName || '-'}
</td>
</tr>
);
})}
</tbody>
</table>
);
};
10 changes: 2 additions & 8 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import { createRoot } from 'react-dom/client';
import { HashRouter as Router } from 'react-router-dom';

import 'bulma/css/bulma.css';
import '@fortawesome/fontawesome-free/css/all.css';
import { Root } from './Router/Root';

import { App } from './App';

createRoot(document.getElementById('root') as HTMLDivElement).render(
<Router>
<App />
</Router>,
);
createRoot(document.getElementById('root') as HTMLDivElement).render(<Root />);
4 changes: 4 additions & 0 deletions src/types/Error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum Error {
Default = '',
Load = 'Unable to load data',
}
6 changes: 6 additions & 0 deletions src/utils/getLinkClass.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import cn from 'classnames';

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