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

removed-map-from-initial-code #133

Open
wants to merge 2 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
103 changes: 103 additions & 0 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/* eslint-disable import/order */
import React, { useState } from 'react';

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

import peopleFromServer from './people.json';
import { PeopleHeader } from './components/PeopleHeader';
import { PeopleFilters } from './components/PeopleFilters';
import { PeopleTable } from './components/PeopleTable';
import { FILTERBYSEX, SORTBY } from './constants';

function getFilteredPeople(people, sex, query) {
let filteredPeople = [...people];

if (query) {
const normalizedQuery = query.toLowerCase();

filteredPeople = filteredPeople.filter(person => (
person.name.toLowerCase().startsWith(normalizedQuery)
));
}

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

return filteredPeople;
}

function sortPeople(people, sortBy) {
const sortedPeople = [...people];

if (!Object.keys(sortBy).length) {
return sortedPeople;
}

const [property] = Object.entries(sortBy);

const [key, value] = property;

switch (key) {
case SORTBY.BORN:
sortedPeople.sort((a, b) => {
return value === 'ASC'
? a.born - b.born
: b.born - a.born;
});
break;

case SORTBY.NAME:
case SORTBY.SEX:
sortedPeople.sort((a, b) => {
return value === 'ASC'
? a[key].localeCompare(b[key])
: b[key].localeCompare(a[key]);
});
break;

default:
return sortedPeople;
}

return sortedPeople;
}

export const App = () => {
const [selectedPeople, setSelectedPeople] = useState([]);
const [filterBy, setFilterBy] = useState(FILTERBYSEX.ALL);
const [query, setQuery] = useState('');
const [sortBy, setSortBy] = useState({});

const peopleForRendering = getFilteredPeople(
peopleFromServer, filterBy, query,
);
const visiblePeople = sortPeople(peopleForRendering, sortBy);

const selectedPeopleNames = selectedPeople.map(person => person.name)
.join(', ');

return (
<div className="box">
<PeopleHeader
selectedPeopleNames={selectedPeopleNames}
/>

<PeopleFilters
filterBy={filterBy}
setFilterBy={setFilterBy}
setQuery={setQuery}
/>

<PeopleTable
sortBy={sortBy}
setSortBy={setSortBy}
peopleToRender={visiblePeople}
selectedPeople={selectedPeople}
setSelectedPeople={setSelectedPeople}
/>
</div>
);
};
43 changes: 0 additions & 43 deletions src/App.tsx

This file was deleted.

19 changes: 19 additions & 0 deletions src/components/Button.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export const Button = ({
type = 'button',
onClick,
className = 'button',
children,
...props
}) => {
return (
<button
{...props}
// eslint-disable-next-line react/button-has-type
type={type}
onClick={onClick}
className={className}
>
{children}
</button>
);
};
54 changes: 54 additions & 0 deletions src/components/PeopleFilters.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import React from 'react';
import cn from 'classnames';
import { Button } from './Button';
import { FILTERBYSEX } from '../constants';

export const PeopleFilters = ({
setFilterBy,
filterBy,
setQuery,
}) => {
return (
<>
<Button
onClick={() => {
setFilterBy(FILTERBYSEX.ALL);
}}
className={cn({
'has-text-danger': filterBy === FILTERBYSEX.ALL,
})}
>
all
</Button>

<Button
onClick={() => {
setFilterBy(FILTERBYSEX.M);
}}
className={cn({
'has-text-danger': filterBy === FILTERBYSEX.M,
})}
>
m
</Button>

<Button
onClick={() => {
setFilterBy(FILTERBYSEX.F);
}}
className={cn({
'has-text-danger': filterBy === FILTERBYSEX.F,
})}
>
f
</Button>

<input
type="search"
onChange={(event) => {
setQuery(event.target.value);
}}
/>
</>
);
};
11 changes: 11 additions & 0 deletions src/components/PeopleHeader.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const PeopleHeader = ({ selectedPeopleNames }) => {
return (
<h1 className="title">
{selectedPeopleNames.length === 0 ? (
'People table'
) : (
`Selected person is ${selectedPeopleNames}`
)}
</h1>
);
};
115 changes: 115 additions & 0 deletions src/components/PeopleTable.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import React from 'react';
import cn from 'classnames';
import { SORTBY } from '../constants';
import { Button } from './Button';

function isSelected(selected, current) {
return selected === current;
}

export const PeopleTable = ({
setSortBy,
sortBy,
peopleToRender,
selectedPeople,
setSelectedPeople,
}) => (
<table className="table is-striped is-narrow">
<thead>
<th>
<Button
className={cn({
'has-background-warning': isSelected(
Object.keys(sortBy)[0],
SORTBY.NAME,
),
})}
onClick={() => {
setSortBy({
[SORTBY.NAME]: sortBy[SORTBY.NAME] ? 'DESC' : 'ASC',
});
}}
>
name
</Button>
</th>
<th>
<Button
className={cn({
'has-background-warning': isSelected(
Object.keys(sortBy)[0],
SORTBY.SEX,
),
})}
onClick={() => {
setSortBy({
[SORTBY.SEX]: sortBy[SORTBY.SEX] ? 'DESC' : 'ASC',
});
}}
>
sex
</Button>
</th>
<th>
<Button
className={cn({
'has-background-warning': isSelected(
Object.keys(sortBy)[0],
SORTBY.BORN,
),
})}
onClick={() => {
setSortBy({
[SORTBY.BORN]: sortBy[SORTBY.BORN] ? 'DESC' : 'ASC',
});
}}
>
born
</Button>
</th>
</thead>

<tbody>
{peopleToRender.map((person) => {
const isPersonSelected = selectedPeople.includes(person);

return (
<tr
key={person.slug}
onClick={() => {
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
isPersonSelected
? setSelectedPeople(selectedPeople.filter(p => (
p.slug !== person.slug
)))
: setSelectedPeople([...selectedPeople, person]);
}}
className={cn({
'has-background-warning': isPersonSelected,
})}
>
<td>{person.name}</td>
<td
className={cn({
'has-text-danger': person.sex === 'f',
'has-text-link': person.sex === 'm',
})}
>
{person.sex}
</td>
<td>{person.born}</td>
{isSelected ? (
<Button>
<i className="fa-solid fa-skull-crossbones" />
</Button>
) : (
<Button>
<i className="fa-solid fa-circle-plus" />
</Button>
)}
</tr>
);
})}
</tbody>
</table>
);
11 changes: 11 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const FILTERBYSEX = {
ALL: 'all',
M: 'm',
F: 'f',
};

export const SORTBY = {
NAME: 'name',
SEX: 'sex',
BORN: 'born',
};
2 changes: 1 addition & 1 deletion src/index.tsx → src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createRoot } from 'react-dom/client';
import { App } from './App';

const root = createRoot(
document.getElementById('root') as HTMLDivElement,
document.getElementById('root'),
);

root.render(<App />);