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

After list manipulation example 2024 march preparation #140

Open
wants to merge 6 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
13 changes: 10 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
module.exports = {
extends: ['@mate-academy/eslint-config-react-typescript', 'plugin:cypress/recommended'],
extends: ['@mate-academy/eslint-config-react', 'plugin:cypress/recommended'],
rules: {
'import/no-extraneous-dependencies': ['error', {
devDependencies: true,
optionalDependencies: false,
peerDependencies: false,
}],
'react/prop-types': 0,
'max-len': ['error', {
ignoreTemplateLiterals: true,
ignoreComments: true,
}],
'jsx-a11y/label-has-associated-control': ["error", {
assert: "either",
'jsx-a11y/label-has-associated-control': ['error', {
assert: 'either',
}],
'jsx-a11y/control-has-associated-label': 'off',
},
};
17,629 changes: 10,274 additions & 7,355 deletions package-lock.json

Large diffs are not rendered by default.

36 changes: 21 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@
"author": "Mate Academy",
"license": "GPL-3.0",
"dependencies": {
"@fortawesome/fontawesome-free": "^6.1.2",
"@cypress/react": "^5.12.4",
"@fortawesome/fontawesome-free": "^6.2.0",
"bulma": "^0.9.4",
"classnames": "^2.3.1",
"postcss": "^8.4.16",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1"
"classnames": "^2.3.2",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "^4.0.3"
},
"devDependencies": {
"@cypress/react": "^5.12.4",
"@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.8",
"@mate-academy/eslint-config-react": "^0.0.11",
"@mate-academy/eslint-config-react-typescript": "^1.0.12",
"@mate-academy/scripts": "^1.7.3",
"@mate-academy/students-ts-config": "*",
"@mate-academy/stylelint-config": "*",
"@types/node": "^17.0.45",
"@types/react": "^18.0.15",
"@types/react-dom": "^18.0.6",
"@mate-academy/stylelint-config": "^0.0.11",
"@types/node": "^17.0.23",
"@types/react": "^17.0.43",
"@types/react-dom": "^17.0.14",
"cypress": "^9.5.3",
"eslint": "^7.32.0",
"eslint-plugin-cypress": "^2.11.2",
Expand All @@ -33,7 +33,8 @@
"mochawesome-merge": "^4.2.0",
"mochawesome-report-generator": "^6.2.0",
"node-sass": "^6.0.1",
"stylelint": "^13.13.1",
"postcss": "^8.4.12",
"stylelint": "^15.11.0",
"typescript": "^4.6.3"
},
"scripts": {
Expand Down Expand Up @@ -61,6 +62,11 @@
]
},
"mateAcademy": {
"projectType": "reactTypescript"
"_comment": "Replace 'react' with 'reactTypescript' if you want use React with Typescript",
"projectType": "react",
"tests": {
"_comment": "Add `cypressComponents: true` to enable component tests",
"cypress": true
}
}
}
141 changes: 141 additions & 0 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import React, { useState } from 'react';
import classNames from 'classnames';

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

import peopleFromServer from './people.json';

export function App() {
// #region People Selection Logic
const [selectedPeople, setSelectedPerson] = useState([]);

const isSelected = ({ slug }) => selectedPeople.some(p => p.slug === slug);

const addPerson = (person) => {
setSelectedPerson([...selectedPeople, person]);
};

const removePerson = (person) => {
setSelectedPerson(
selectedPeople.filter(p => p.slug !== person.slug),
);
};
// #endregion

const [query, setQuery] = useState('');

const [sex, setSex] = useState('all'); // 'm', 'f';

// Optional
// const sortField = ''; // 'name', 'sex', 'born'
// const sortOrder = 'asc'; // 'desc'

let visiblePeople = [...peopleFromServer];

const normalizedQuery = query.trim().toLowerCase();

if (normalizedQuery) {
visiblePeople = visiblePeople.filter(person => (
person.name.toLowerCase().includes(normalizedQuery)
));
}

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

return (
<div className="box">
<div className="block">
<div className="buttons has-addons">
<button
type="button"
className={classNames('button', {
'is-info': sex === 'all',
})}
onClick={() => setSex('all')}
>
all
</button>
<button
type="button"
className={classNames('button', {
'is-info': sex === 'm',
})}
onClick={() => setSex('m')}
>
m
</button>
<button
type="button"
className={classNames('button', {
'is-info': sex === 'f',
})}
onClick={() => setSex('f')}
>
f
</button>
</div>

<input type="search" onChange={event => setQuery(event.target.value)} />
</div>

<table className="table is-striped is-narrow">
<caption>
{selectedPeople.map(person => person.name)
.join(', ') || 'No one selected'
}
</caption>

<thead>
<tr>
<th> </th>
<th>name</th>
<th>sex</th>
<th>born</th>
</tr>
</thead>

<tbody>
{visiblePeople.map(person => (
<tr
key={person.slug}
className={isSelected(person)
? 'has-background-warning'
: ''}
>
<td>
{isSelected(person) ? (
<button
type="button"
className="button is-small is-rounded is-danger"
onClick={() => removePerson(person)}
>
<span className="icon is-small">
<i className="fas fa-minus" />
</span>
</button>
) : (
<button
type="button"
className="button is-small is-rounded is-success"
onClick={() => addPerson(person)}
>
<span className="icon is-small">
<i className="fas fa-plus" />
</span>
</button>
)}
</td>
<td>{person.name}</td>
<td>{person.sex}</td>
<td>{person.born}</td>
</tr>
))}
</tbody>
</table>
</div>
);
}
43 changes: 0 additions & 43 deletions src/App.tsx

This file was deleted.

19 changes: 19 additions & 0 deletions src/components/Button/Button.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import classNames from 'classnames';

export function Button({
children,
type = 'button',
className,
...restProps
}) {
return (
<button
// eslint-disable-next-line react/button-has-type
type={type}
className={classNames('button', className)}
{...restProps}
>
{children}
</button>
);
}
File renamed without changes.
File renamed without changes.
7 changes: 7 additions & 0 deletions src/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import ReactDOM from 'react-dom';
import { App } from './App';

ReactDOM.render(
<App />,
document.getElementById('root'),
);
8 changes: 0 additions & 8 deletions src/index.tsx

This file was deleted.

9 changes: 0 additions & 9 deletions src/types/Person.ts

This file was deleted.