Skip to content
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
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 { FC, 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 { PeopleTable } from './components/PeopleTable/PeopleTable';
import { Loader } from './components/Loader';

export class App extends React.Component {
state = {};
export const App: 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 && <Loader />}

{isLoaded && <PeopleTable />}
</div>
);
};
3 changes: 3 additions & 0 deletions src/components/PeopleTable/PeopleTable.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.isSelected {
background-color: yellowgreen;
}
231 changes: 231 additions & 0 deletions src/components/PeopleTable/PeopleTable.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
import { ChangeEvent, useState } from 'react';
import classNames from 'classnames';

import peopleFromServer from '../../people.json';
import { Person } from '../../types/Person';
import './PeopleTable.scss';

export const PeopleTable = () => {
const [people, setPeople] = useState<Person[]>(peopleFromServer);
const [selectedPeople, setSelectedPeople] = useState<Person[]>([]);
const [query, setQuery] = useState('');
const [sex, setSex] = useState('');

// region Methods
const handlePersonSelection = (personToAdd: Person) => {
setSelectedPeople((prevSelectedPeople) => ([
...prevSelectedPeople,
personToAdd,
]));
};

const handlePersonUnselection = (personToRemove: Person) => {
setSelectedPeople((prevSelectedPeople) => prevSelectedPeople
.filter(
person => person.slug !== personToRemove.slug,
));
};

const handleMoveDown = (personToMoveDown: Person) => {
const peopleCopy = [...people];

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

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

setPeople((prevPeople) => ([
...prevPeople.slice(0, personIndex),
prevPeople[personIndex + 1],
prevPeople[personIndex],
...prevPeople.slice(personIndex + 2),
]));
};

const handleMoveUp = (personToMoveDown: Person) => {
const peopleCopy = [...people];

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

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

const swap = peopleCopy[personIndex];

peopleCopy[personIndex] = peopleCopy[personIndex - 1];
peopleCopy[personIndex - 1] = swap;

setPeople(peopleCopy);
};

const isSelected = (personToCheck: Person) => {
return selectedPeople.some(
person => person.slug === personToCheck.slug,
);
};

const clearSelectedPeople = () => {
setSelectedPeople([]);
};
// endregion

const handleInputChange = (event: ChangeEvent<HTMLInputElement>) => {
setQuery(event.target.value);
};

let visiblePeople = [...people];

if (sex !== '') {
visiblePeople = visiblePeople.filter(person => person.sex === sex);
}

if (query !== '') {
const normalizedQuery = query.toLocaleLowerCase();

visiblePeople = visiblePeople.filter(person => {
person.motherName?.toLocaleLowerCase(); // === null

if (person.motherName) {
person.motherName.toLocaleLowerCase();
} else {
return person.motherName;
}


const stringToCheck = `
${person.name}
${person.motherName || ''}
${person?.fatherName || ''}
`;

return stringToCheck.toLocaleLowerCase().includes(normalizedQuery);
});
}

return (
<table className="table is-striped is-narrow">
<caption
className="title is-5 has-text-info"
>
<p className="block">
<button
type="button"
className="delete"
onClick={clearSelectedPeople}
>
Clear all
</button>
{selectedPeople.map(person => person.name).join(', ')}
</p>

<div className="field has-addons">
<div className="control has-icons-left is-expanded">
<input
type="search"
className="input"
placeholder="Search by name"
onChange={handleInputChange}
/>

<span className="icon is-small is-left">
<i className="fas fa-search" />
</span>
</div>

<div className="control">
<div className="select">
<select onChange={(event) => setSex(event.target.value)}>
<option value="">All</option>
<option value="m">Men</option>
<option value="f">Women</option>
</select>
</div>
</div>
</div>
</caption>
<thead>
<tr>
<th>-</th>
<th>name</th>
<th>Mother name</th>
<th>Father name</th>
<th>sex</th>
<th>born</th>
<th>reorder</th>
</tr>
</thead>

<tbody>
{visiblePeople.map(person => (
<tr
key={person.slug}
className={classNames('Person', {
'has-background-warning': isSelected(person),
})}
>
<td>
{isSelected(person)
? (
<button
type="button"
onClick={() => handlePersonUnselection(person)}
className="button is-small is-rounded is-danger"
>
<span className="icon is-small">
<i className="fas fa-minus" />
</span>
</button>
)
: (
<button
type="button"
onClick={() => handlePersonSelection(person)}
className="button is-small is-rounded is-success"
>
<span className="icon is-small">
<i className="fas fa-plus" />
</span>
</button>
)}
</td>
<td className={classNames({
'has-text-link': person.sex === 'm',
'has-text-danger': person.sex === 'f',
})}
>
{person.name}
</td>

<td>{person.motherName || '-'}</td>
<td>{person.fatherName || '-'}</td>
<td>{person.sex}</td>
<td>{person.born}</td>
<td className="is-flex is-flex-wrap-nowrap">
<button
type="button"
onClick={() => handleMoveDown(person)}
className="button mr-2"
>
&darr;
</button>

<button
type="button"
className="button"
onClick={() => handleMoveUp(person)}
>
&uarr;
</button>
</td>
</tr>
))}
</tbody>
</table>
);
};