Skip to content

Commit

Permalink
Till colors filter done
Browse files Browse the repository at this point in the history
  • Loading branch information
thapatechnical committed Nov 1, 2022
1 parent f62c71f commit f485c3a
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 9 deletions.
37 changes: 34 additions & 3 deletions src/components/FilterSection.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import styled from "styled-components";
import { useFilterContext } from "../context/filter_context";
import { FaCheck } from "react-icons/fa";
import FormatPrice from "../Helpers/FormatPrice";
import { Button } from "../styles/Button";

const FilterSection = () => {
const {
filters: { text, category, color },
filters: { text, category, color, price, maxPrice, minPrice },
updateFilterValue,
all_products,
clearFilters,
} = useFilterContext();

// get the unique values of each property
Expand Down Expand Up @@ -90,21 +93,49 @@ const FilterSection = () => {

<div className="filter-color-style">
{colorsData.map((curColor, index) => {
if (curColor === "all") {
return (
<button
key={index}
type="button"
value={curColor}
name="color"
className="color-all--style"
onClick={updateFilterValue}>
all
</button>
);
}
return (
<button
key={index}
type="button"
value={curColor}
name="color"
style={{ backgroundColor: curColor }}
className="btnStyle"
className={color === curColor ? "btnStyle active" : "btnStyle"}
onClick={updateFilterValue}>
{color === curColor ? "" : null}
{color === curColor ? <FaCheck className="checkStyle" /> : null}
</button>
);
})}
</div>
</div>

<div className="filter_price">
<h3>Price</h3>
<p>
<FormatPrice price={price} />
</p>
<input
type="range"
name="price"
min={minPrice}
max={maxPrice}
value={price}
onChange={updateFilterValue}
/>
</div>
</Wrapper>
);
};
Expand Down
9 changes: 9 additions & 0 deletions src/context/filter_context.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ const initialState = {
category: "all",
company: "all",
color: "all",
maxPrice: 0,
price: 0,
minPrice: 0,
},
};

Expand Down Expand Up @@ -46,6 +49,11 @@ export const FilterContextProvider = ({ children }) => {
return dispatch({ type: "UPDATE_FILTERS_VALUE", payload: { name, value } });
};

// to clear the filter
const clearFilters = () => {
dispatch({ type: "CLEAR_FILTERS" });
};

// to sort the product
useEffect(() => {
dispatch({ type: "FILTER_PRODUCTS" });
Expand All @@ -65,6 +73,7 @@ export const FilterContextProvider = ({ children }) => {
setListView,
sorting,
updateFilterValue,
clearFilters,
}}>
{children}
</FilterContext.Provider>
Expand Down
33 changes: 27 additions & 6 deletions src/reducer/filterReducer.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
const filterReducer = (state, action) => {
switch (action.type) {
case "LOAD_FILTER_PRODUCTS":
let priceArr = action.payload.map((curElem) => curElem.price);
console.log(
"🚀 ~ file: filterReducer.js ~ line 5 ~ filterReducer ~ priceArr",
priceArr
);

// 1way
// console.log(Math.max.apply(null, priceArr));

// let maxPrice = priceArr.reduce(
// (initialVal, curVal) => Math.max(initialVal, curVal),
// 0
// );
// console.log(
// "🚀 ~ file: filterReducer.js ~ line 16 ~ filterReducer ~ maxPrice",
// maxPrice
// );

let maxPrice = Math.max(...priceArr);
console.log(
"🚀 ~ file: filterReducer.js ~ line 23 ~ filterReducer ~ maxPrice",
maxPrice
);

return {
...state,
filter_products: [...action.payload],
all_products: [...action.payload],
filters: { ...state.filters, maxPrice, price: maxPrice },
};

case "SET_GRID_VIEW":
Expand Down Expand Up @@ -74,7 +99,7 @@ const filterReducer = (state, action) => {
let { all_products } = state;
let tempFilterProduct = [...all_products];

const { text, category, company, color } = state.filters;
const { text, category, company, color, price } = state.filters;

This comment has been minimized.

Copy link
@Shailenderdazonn

Shailenderdazonn Feb 22, 2023

test


if (text) {
tempFilterProduct = tempFilterProduct.filter((curElem) => {
Expand All @@ -94,15 +119,11 @@ const filterReducer = (state, action) => {
);
}

if (color) {
if (color !== "all") {
tempFilterProduct = tempFilterProduct.filter((curElem) =>
curElem.colors.includes(color)
);
}
return {
...state,
filter_products: tempFilterProduct,
};

default:
return state;
Expand Down

0 comments on commit f485c3a

Please sign in to comment.