From e39c44cb4b985da41c072d22becb1e6dcedcd027 Mon Sep 17 00:00:00 2001 From: ShilpaPandey26 Date: Tue, 27 Aug 2024 16:48:40 +0530 Subject: [PATCH] adding sorting functionality --- src/Pages/ShopCategory.jsx | 92 +++++++++++++++++++++++++++++++------- 1 file changed, 77 insertions(+), 15 deletions(-) diff --git a/src/Pages/ShopCategory.jsx b/src/Pages/ShopCategory.jsx index 038b10b..2f8f1b1 100644 --- a/src/Pages/ShopCategory.jsx +++ b/src/Pages/ShopCategory.jsx @@ -1,28 +1,65 @@ import React, { useContext, useState } from "react"; import { ShopContext } from "../Context/ShopContext"; import Dropdown from "../assets/dropdown_icon.png"; -import Product from "./Product"; import Item from "../Components/Item"; import { Link } from "react-router-dom"; + const ShopCategory = (props) => { const { all_products } = useContext(ShopContext); const [displayCount, setDisplayCount] = useState(12); + const [sortCriteria, setSortCriteria] = useState("default"); + + + const [isDropdownOpen, setIsDropdownOpen] = useState(false); + + + // Filter products based on category + const filteredProducts = all_products.filter( + (item) => item.category === props.category + ); + - const filteredProducts = all_products.filter((item) => { - if (item.category === props.category) { - return item; + // Sort products based on selected criteria + const sortProducts = (products) => { + if (sortCriteria === "priceLowToHigh") { + return products.slice().sort((a, b) => parseFloat(a.new_price) - parseFloat(b.new_price)); + } else if (sortCriteria === "priceHighToLow") { + return products.slice().sort((a, b) => parseFloat(b.new_price) - parseFloat(a.new_price)); + } else if (sortCriteria === "popularity") { + // Ensure the 'popularity' attribute is available in your data + return products.slice().sort((a, b) => b.popularity - a.popularity); + } else if (sortCriteria === "discount") { + return products.slice().sort((a, b) => { + const discountA = a.old_price ? ((parseFloat(a.old_price) - parseFloat(a.new_price)) / parseFloat(a.old_price)) * 100 : 0; + const discountB = b.old_price ? ((parseFloat(b.old_price) - parseFloat(b.new_price)) / parseFloat(b.old_price)) * 100 : 0; + return discountB - discountA; // Sort in descending order + }); + } else { + return products; // Default sort } - }); + }; + + + + // Apply sorting to the filtered products + const sortedProducts = sortProducts(filteredProducts); + console.log(filteredProducts) + + // Handle the load more functionality const showMore = () => { setDisplayCount(displayCount + 12); }; - const handleClicked = () => { - alert("clicked"); + + // Handle sorting criteria change + const handleSortChange = (criteria) => { + setSortCriteria(criteria); + setIsDropdownOpen(false); // Close the dropdown after selecting a sort option }; + return (
@@ -31,29 +68,53 @@ const ShopCategory = (props) => {

-

Showing 1-{displayCount}

+

Showing 1-{displayCount}

out of {filteredProducts.length} results

-
+
+ {isDropdownOpen && ( +
+
    +
  • handleSortChange("priceLowToHigh")} + > + Price-low to high +
  • +
  • handleSortChange("priceHighToLow")} + > + Price- high to low +
  • +
  • handleSortChange("discount")} + > + Discount +
  • +
+
+ )}
- {filteredProducts.slice(0, displayCount).map((item) => { - return ; - })} + {sortedProducts.slice(0, displayCount).map((item) => ( + + ))}
{filteredProducts.length >= displayCount ? (