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 SelectedPeople and PeopleList components #122

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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"@cypress/webpack-dev-server": "^1.8.4",
"@mate-academy/cypress-tools": "^1.0.4",
"@mate-academy/eslint-config-react-typescript": "^1.0.11",
"@mate-academy/scripts": "^1.2.3",
"@mate-academy/scripts": "^1.2.8",
"@mate-academy/students-ts-config": "*",
"@mate-academy/stylelint-config": "*",
"@types/node": "^17.0.45",
Expand Down
8 changes: 8 additions & 0 deletions src/App.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,11 @@
iframe {
display: none;
}

button {
cursor: pointer;
}

tr {
transition: 0.3s background-color;
}
49 changes: 19 additions & 30 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,39 +1,28 @@
import React from 'react';
import React, { useEffect, useState } from 'react';

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

import peopleFromServer from './people.json';
import { Loader } from './components/Loader';
import { PeopleList } from './components/PeopleList';

export class App extends React.Component {
state = {};
export const App: React.FC = () => {
const [isLoaded, setIsLoaded] = useState(false);

render() {
return (
<div className="box">
<h1 className="title">People table</h1>
useEffect(() => {
setTimeout(() => {
setIsLoaded(true);
}, 1000);
}, []);

<table className="table is-striped is-narrow">
<thead>
<tr>
<th>name</th>
<th>sex</th>
<th>born</th>
</tr>
</thead>
return (
<div className="box">
<h1 className="title">People table</h1>

<tbody>
{peopleFromServer.map(person => (
<tr key={person.slug}>
<td>{person.name}</td>
<td>{person.sex}</td>
<td>{person.born}</td>
</tr>
))}
</tbody>
</table>
</div>
);
}
}
{isLoaded
? <PeopleList />
: <Loader />}
</div>
);
};
15 changes: 15 additions & 0 deletions src/components/Button/Button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react';

type Props = React.ButtonHTMLAttributes<HTMLButtonElement> & {
children: React.ReactNode;
};

export const Button: React.FC<Props> = ({ children, className, ...props }) => (
<button
type="button"
className={`button ${className}`}
{...props}
>
{children}
</button>
);
1 change: 1 addition & 0 deletions src/components/Button/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Button';
118 changes: 118 additions & 0 deletions src/components/PeopleList/PeopleList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import React, { useState } from 'react';
import { Person } from '../../types/Person';
import peopleFromServer from '../../people.json';
import { SelectedPeople } from '../SelectedPeople';
import { PeopleTable } from '../PeopleTable';
import { SortField } from '../../types/SortFields';

function sortByField(items: Person[], field: SortField) {
return [...items].sort((personA, personB) => {
switch (field) {
case SortField.Born:
case SortField.Died:
return personA[field] - personB[field];

case SortField.Name:
case SortField.Slug:
return personA[field].localeCompare(personB[field]);

default:
return 0;
}
});
}

export const PeopleList: React.FC<{}> = () => {
const [people, setPeople] = useState<Person[]>(peopleFromServer);

const moveDown = (person: Person) => {
setPeople(currentPeople => {
const peopleCopy = [...currentPeople];

const personIndex = peopleCopy.findIndex(
personA => personA.slug === person.slug,
);

if (personIndex === peopleCopy.length - 1) {
return currentPeople;
}

peopleCopy[personIndex + 1] = person;
peopleCopy[personIndex] = currentPeople[personIndex + 1];

return peopleCopy;
});
};

const moveUp = (person: Person) => {
setPeople(currentPeople => {
const peopleCopy = [...currentPeople];

const personIndex = peopleCopy.findIndex(
personA => personA.slug === person.slug,
);

if (personIndex === 0) {
return currentPeople;
}

peopleCopy[personIndex - 1] = person;
peopleCopy[personIndex] = currentPeople[personIndex - 1];

return peopleCopy;
});
};

const selectionToggler = (personId: string) => {
setPeople(currentPeople => (
currentPeople.map(person => {
if (person.id === personId) {
return {
...person,
selected: !person.selected,
};
}

return person;
})
));
};

const clearSelectedPeople = () => {
setPeople(currentPeople => (
currentPeople.map(person => ({
...person,
selected: false,
}))
));
};

const sortPeople = (field: SortField) => {
setPeople(currentPeople => (
sortByField(currentPeople, field)
));
};

if (people.length === 0) {
return (
<p>No people data yet</p>
);
}

return (
<>
<SelectedPeople
people={people}
clearSelectedPeople={clearSelectedPeople}
/>

<PeopleTable
people={people}
moveDown={moveDown}
moveUp={moveUp}
selectionToggler={selectionToggler}
sortPeople={sortPeople}
/>
</>
);
};
1 change: 1 addition & 0 deletions src/components/PeopleList/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './PeopleList';
101 changes: 101 additions & 0 deletions src/components/PeopleTable/PeopleTable.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import React from 'react';
import cn from 'classnames';
import { Button } from '../Button';
import { SortField } from '../../types/SortFields';
import { Person } from '../../types/Person';

type Props = {
people: Person[];
moveUp: (person: Person) => void;
moveDown: (person: Person) => void;
selectionToggler: (personId: string) => void;
sortPeople: (field: SortField) => void;
};

export const PeopleTable: React.FC<Props> = ({
people,
moveUp,
moveDown,
selectionToggler,
sortPeople,
}) => (
<table className="table is-striped is-narrow">
<thead>
<tr>
<th>select</th>
<th>
name

<a
href="#sort"
onClick={() => sortPeople(SortField.Name)}
>
<span className="icon">
<i className="fas fa-sort" />
</span>
</a>
</th>
<th>sex</th>
<th>born</th>
</tr>
</thead>

<tbody>
{people.map(person => (
<tr
key={person.slug}
className={cn({
'has-background-warning': person.selected,
})}
>
<td>
<Button
className={cn({
'is-danger': person.selected,
'is-success': !person.selected,
})}
onClick={() => selectionToggler(person.id)}
>
<span className="icon is-small">
<i className={
cn('fas', {
'fa-minus': person.selected,
'fa-plus': !person.selected,
})
}
/>
</span>
</Button>
</td>
<td
className={cn({
'has-text-link': person.sex === 'm',
'has-text-danger': person.sex === 'f',
})}
>
{person.name}
</td>
<td>{person.sex}</td>
<td>{person.born}</td>
<td>
<button
type="button"
onClick={() => moveDown(person)}
>
&darr;
</button>
</td>

<td>
<button
type="button"
onClick={() => moveUp(person)}
>
&uarr;
</button>
</td>
</tr>
))}
</tbody>
</table>
);
1 change: 1 addition & 0 deletions src/components/PeopleTable/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './PeopleTable';
39 changes: 39 additions & 0 deletions src/components/SelectedPeople/SelectedPeople.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import React from 'react';
import { Person } from '../../types/Person';

type Props = {
people: Person[];
clearSelectedPeople: () => void;
};

export const SelectedPeople: React.FC<Props> = ({
people,
clearSelectedPeople,
}) => {
const selectedPeople = people.filter(person => person.selected);

if (selectedPeople.length === 0) {
return (
<p>
No selected people
</p>
);
}

return (
<article className="title is-5 has-text-info">
<button
aria-label="Remove"
type="button"
className="delete"
onClick={clearSelectedPeople}
/>

<ul>
{selectedPeople.map(person => (
<li key={person.id}>{person.name}</li>
))}
</ul>
</article>
);
};
1 change: 1 addition & 0 deletions src/components/SelectedPeople/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './SelectedPeople';
Loading