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

Practice solution #14

Open
wants to merge 1 commit into
base: main
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
331 changes: 97 additions & 234 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,248 +1,111 @@
/* eslint-disable jsx-a11y/accessible-emoji */
import React from 'react';
import React, { useState } from 'react';
import './App.scss';

// import usersFromServer from './api/users';
// import categoriesFromServer from './api/categories';
// import productsFromServer from './api/products';

// const products = productsFromServer.map((product) => {
// const category = null; // find by product.categoryId
// const user = null; // find by category.ownerId

// return null;
// });

export const App = () => (
<div className="section">
<div className="container">
<h1 className="title">Product Categories</h1>

<div className="block">
<nav className="panel">
<p className="panel-heading">Filters</p>

<p className="panel-tabs has-text-weight-bold">
<a
data-cy="FilterAllUsers"
href="#/"
>
All
</a>

<a
data-cy="FilterUser"
href="#/"
>
User 1
</a>

<a
data-cy="FilterUser"
href="#/"
className="is-active"
>
User 2
</a>

<a
data-cy="FilterUser"
href="#/"
>
User 3
</a>
</p>

<div className="panel-block">
<p className="control has-icons-left has-icons-right">
<input
data-cy="SearchField"
type="text"
className="input"
placeholder="Search"
value="qwe"
import { products, findOwners, filterByOwner, filterByName } from './utils';
import { Table } from './components/Table/Table';
import { FilterUserList } from './components/FilterUserList/FilterUserList';
import { FilterByInput } from './components/FilterByInput/FilterByInput';

export const App = () => {
const [selectedUser, setSelectedUser] = useState('All');
const [query, setQuery] = useState('');
const owners = findOwners(products);
const ownersList = ['All', ...owners, 'John'];
const filteredByUser = filterByOwner(products, selectedUser);
const filteredProducts = filterByName(filteredByUser, query);

return (
<div className="section">
<div className="container">
<h1 className="title">Product Categories</h1>

<div className="block">
<nav className="panel">
<p className="panel-heading">Filters</p>

<p className="panel-tabs has-text-weight-bold">
<FilterUserList
owners={ownersList}
selectedUser={selectedUser}
onSelectedUser={user => setSelectedUser(user)}
/>

<span className="icon is-left">
<i className="fas fa-search" aria-hidden="true" />
</span>

<span className="icon is-right">
{/* eslint-disable-next-line jsx-a11y/control-has-associated-label */}
<button
data-cy="ClearButton"
type="button"
className="delete"
/>
</span>
</p>
</div>

<div className="panel-block is-flex-wrap-wrap">
<a
href="#/"
data-cy="AllCategories"
className="button is-success mr-6 is-outlined"
>
All
</a>

<a
data-cy="Category"
className="button mr-2 my-1 is-info"
href="#/"
>
Category 1
</a>

<a
data-cy="Category"
className="button mr-2 my-1"
href="#/"
>
Category 2
</a>

<a
data-cy="Category"
className="button mr-2 my-1 is-info"
href="#/"
>
Category 3
</a>
<a
data-cy="Category"
className="button mr-2 my-1"
href="#/"
>
Category 4
</a>
</div>

<div className="panel-block">
<a
data-cy="ResetAllButton"
href="#/"
className="button is-link is-outlined is-fullwidth"
>
Reset all filters
</a>
</div>
</nav>
</div>

<div className="box table-container">
<p data-cy="NoMatchingMessage">
No products matching selected criteria
</p>

<table
data-cy="ProductTable"
className="table is-striped is-narrow is-fullwidth"
>
<thead>
<tr>
<th>
<span className="is-flex is-flex-wrap-nowrap">
ID

<a href="#/">
<span className="icon">
<i data-cy="SortIcon" className="fas fa-sort" />
</span>
</a>
</span>
</th>

<th>
<span className="is-flex is-flex-wrap-nowrap">
Product

<a href="#/">
<span className="icon">
<i data-cy="SortIcon" className="fas fa-sort-down" />
</span>
</a>
</span>
</th>

<th>
<span className="is-flex is-flex-wrap-nowrap">
Category

<a href="#/">
<span className="icon">
<i data-cy="SortIcon" className="fas fa-sort-up" />
</span>
</a>
</span>
</th>

<th>
<span className="is-flex is-flex-wrap-nowrap">
User

<a href="#/">
<span className="icon">
<i data-cy="SortIcon" className="fas fa-sort" />
</span>
</a>
</span>
</th>
</tr>
</thead>

<tbody>
<tr data-cy="Product">
<td className="has-text-weight-bold" data-cy="ProductId">
1
</td>

<td data-cy="ProductName">Milk</td>
<td data-cy="ProductCategory">🍺 - Drinks</td>

<td
data-cy="ProductUser"
className="has-text-link"
<FilterByInput
filterBy={newQuery => setQuery(newQuery)}
query={query}
onClearButton={() => setQuery('')}
/>

<div className="panel-block is-flex-wrap-wrap">
<a
href="#/"
data-cy="AllCategories"
className="button is-success mr-6 is-outlined"
>
Max
</td>
</tr>

<tr data-cy="Product">
<td className="has-text-weight-bold" data-cy="ProductId">
2
</td>

<td data-cy="ProductName">Bread</td>
<td data-cy="ProductCategory">🍞 - Grocery</td>
All
</a>

<td
data-cy="ProductUser"
className="has-text-danger"
<a
data-cy="Category"
className="button mr-2 my-1 is-info"
href="#/"
>
Anna
</td>
</tr>
Category 1
</a>

<tr data-cy="Product">
<td className="has-text-weight-bold" data-cy="ProductId">
3
</td>

<td data-cy="ProductName">iPhone</td>
<td data-cy="ProductCategory">💻 - Electronics</td>
<a
data-cy="Category"
className="button mr-2 my-1"
href="#/"
>
Category 2
</a>

<td
data-cy="ProductUser"
className="has-text-link"
<a
data-cy="Category"
className="button mr-2 my-1 is-info"
href="#/"
>
Category 3
</a>
<a
data-cy="Category"
className="button mr-2 my-1"
href="#/"
>
Category 4
</a>
</div>

<div className="panel-block">
<a
data-cy="ResetAllButton"
href="#/"
className="button is-link is-outlined is-fullwidth"
onClick={() => {
setSelectedUser('All');
setQuery('');
}}
Comment on lines +87 to +90

Choose a reason for hiding this comment

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

When function takes more than 1 line, make a separate function and pass it here

>
Roma
</td>
</tr>
</tbody>
</table>
Reset all filters
</a>
</div>
</nav>
</div>

<div className="box table-container">
{!filteredProducts.length
? (
<p data-cy="NoMatchingMessage">
No products matching selected criteria
</p>
)
: <Table products={filteredProducts} />
}
</div>
</div>
</div>
</div>
);
);
};
33 changes: 33 additions & 0 deletions src/components/FilterByInput/FilterByInput.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export const FilterByInput = ({ filterBy, query, onClearButton }) => (
<div className="panel-block">
<p className="control has-icons-left has-icons-right">
<input
data-cy="SearchField"
type="text"
className="input"
placeholder="Search"
value={query}
onChange={(event) => {
filterBy(event.target.value);
}}
/>

<span className="icon is-left">
<i className="fas fa-search" aria-hidden="true" />
</span>

{query
&& (
<span className="icon is-right">
{/* eslint-disable-next-line jsx-a11y/control-has-associated-label */}
<button
data-cy="ClearButton"
type="button"
className="delete"
onClick={onClearButton}
/>
</span>
)}
</p>
</div>
);
16 changes: 16 additions & 0 deletions src/components/FilterUserList/FilterUserList.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import cn from 'classnames';

export const FilterUserList = ({ owners, selectedUser, onSelectedUser }) => (
owners.map(user => (
<a
data-cy={user === 'All' ? 'FilterAllUsers' : 'FilterUser'}
href="#/"
className={cn({
'is-active': selectedUser === user,
})}
onClick={() => onSelectedUser(user)}
>
{user}
</a>
))
);
Loading