Skip to content

Commit

Permalink
Se crea filterPanel y se ajustan estilos del panel
Browse files Browse the repository at this point in the history
  • Loading branch information
hendaniel committed Sep 7, 2020
1 parent 9cf530b commit f256fae
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 10 deletions.
File renamed without changes.
18 changes: 18 additions & 0 deletions src/Utils/filterHelper.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const priceFilter = (cost) => {
return (product) => {
switch (cost) {
case 1:
return product.cost <= 200;
case 2:
return product.cost > 200 && product.cost <= 500;
case 3:
return product.cost > 500 && product.cost <= 1000;
case 4:
return product.cost > 1001 && product.cost <= 2000;
case 5:
return product.cost > 2000;
default:
return product;
}
};
};
36 changes: 36 additions & 0 deletions src/components/Products/FilterPanel.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React, { useState } from "react";
import "./products.scss";

const optionsData = {
price: {
0: "Any price",
1: "0 - 200 coins",
2: "201 - 500 coins",
3: "501 - 1000 coins",
4: "1001 - 2000 coins",
5: "+ 2000 coins",
},
};

const FilterPanel = ({ currentAmount, totalAmount, onFilter }) => {
return (
<>
<div className="filter-container">
<div className="pagination-info">
Showing {currentAmount} of {totalAmount} products
</div>
<div className="filter">Filter by:</div>
<select className="filter-select" onChange={(e) => onFilter(e)}>
{Object.entries(optionsData.price).map(([key, value], index) => {
return (
<option key={index} value={key}>
{value}
</option>
);
})}
</select>
</div>
</>
);
};
export default FilterPanel;
25 changes: 21 additions & 4 deletions src/components/Products/ProductsList.jsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import React, { useEffect, useContext } from "react";
import React, { useEffect, useContext, useState } from "react";
import { usePagination } from "../../hooks/index";
import Product from "./Product";
import { ProductsContext } from "../../providers/index";
import Product from "./Product";
import FilterPanel from "./FilterPanel";
import "./products.scss";

const ITEMS_PER_PAGE = 16;

const ProductsList = () => {
const { products, setProductResponse } = useContext(ProductsContext);

const [filter, setFilter] = useState(0);

useEffect(() => {
if (!products) return;
}, [products]);

const { next, prev, currentData } = usePagination(products, 16);
const { next, prev, currentData, currentAmount } = usePagination(
products,
ITEMS_PER_PAGE
);

const onPrev = () => {
prev();
Expand All @@ -22,9 +30,18 @@ const ProductsList = () => {
window.scrollTo(0, 0);
};

if (products.length > 0) {
const handleFilter = (event) => {
console.log(event.target.value);
};

if (products.length) {
return (
<>
<FilterPanel
currentAmount={currentAmount()}
totalAmount={products.length}
onFilter={handleFilter}
/>
<div className="cards">
{currentData().map((item, key) => {
return (
Expand Down
29 changes: 26 additions & 3 deletions src/components/Products/products.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.cards {
margin: 0 40px;
.cards,
.filter-container {
margin: 10px 40px;
display: flex;
flex-wrap: wrap;
justify-content: center;
Expand All @@ -8,7 +9,7 @@
}
}
.card {
margin: 30px;
margin: 15px;
transition: all 0.4s cubic-bezier(0.175, 0.885, 0, 1);
background-color: #fff;
width: 250px;
Expand Down Expand Up @@ -143,3 +144,25 @@
font-weight: bold;
border: none;
}

.pagination-info,
.filter {
font-family: "Raleway", sans-serif;
text-transform: uppercase;
font-size: 15px;
letter-spacing: 2px;
font-weight: 1000;
color: #292727;
}

.filter {
margin-left: 70px;
margin-right: 10px;
}
.filter-select {
padding-left: 5px;
outline: none;
border: none;
border-radius: 100px;
height: 19px;
}
8 changes: 7 additions & 1 deletion src/hooks/usePagination.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ const usePagination = (data, itemsPerPage) => {
return data.slice(begin, end);
};

return { next, prev, jump, currentData, currentPage, maxPage };
const currentAmount = () => {
const accumulate = itemsPerPage * (currentPage - 1);
const current = currentData().length;
return accumulate + current;
};

return { next, prev, jump, currentData, currentAmount, currentPage, maxPage };
};
export default usePagination;
2 changes: 1 addition & 1 deletion src/services/productsService.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as API from "../api/API";
import * as API from "../Utils/API";

const headers = {
"Content-Type": "application/json",
Expand Down
2 changes: 1 addition & 1 deletion src/services/userService.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as API from "../api/API";
import * as API from "../Utils/API";

const headers = {
"Content-Type": "application/json",
Expand Down

0 comments on commit f256fae

Please sign in to comment.