-
-
RWF
-
2350
+ const [categories, setCategories] = useState
([]);
+ const [products, setProducts] = useState([]);
+ const [minPrice, setMinPrice] = useState('');
+ const [maxPrice, setMaxPrice] = useState('');
+ const [selectedCategory, setSelectedCategory] = useState('');
+ const [selectedProduct, setSelectedProduct] = useState('');
+
+ const router = useRouter();
+
+ useEffect(() => {
+ async function fetchCategories() {
+ try {
+ const allCategories: any = await request.get('/categories');
+ setCategories(allCategories.categories || []);
+ } catch (error) {
+ console.error('Error fetching categories:', error);
+ }
+ }
+
+ async function fetchProducts() {
+ try {
+ const allProducts: any = await request.get(`/products`);
+ setProducts(allProducts.products || []);
+ } catch (error) {
+ console.error('Error fetching products:', error);
+ }
+ }
+
+ fetchCategories();
+ fetchProducts();
+ }, []);
+
+ useEffect(() => {
+ handleSearchParams();
+ }, [minPrice, maxPrice, selectedCategory, selectedProduct]);
+
+ const handleSearchParams = () => {
+ if (minPrice || maxPrice || selectedCategory || selectedProduct) {
+ const queryParams: { [key: string]: string } = {};
+ if (minPrice) queryParams.minPrice = minPrice;
+ if (maxPrice) queryParams.maxPrice = maxPrice;
+ if (selectedCategory) queryParams.category = selectedCategory;
+ if (selectedProduct) queryParams.name = selectedProduct;
+
+ const queryString = new URLSearchParams(queryParams).toString();
+ console.log('Query Params:', queryString); // Debugging log
+ router.push(`/products?${queryString}`);
+ }
+ };
+
+ const handleCategoryClick = (category: string) => {
+ setSelectedCategory(category);
+ setSelectedProduct('');
+ };
+
+ const handleProductClick = (product: string) => {
+ setSelectedProduct(product);
+ setSelectedCategory('');
+ };
+
+ const handleMinPriceChange = (e: React.ChangeEvent) => {
+ setMinPrice(e.target.value);
+ };
+
+ const handleMaxPriceChange = (e: React.ChangeEvent) => {
+ setMaxPrice(e.target.value);
+ };
+
+ return (
+
+
+
Filter Product
+
+
Categories
+ {categories.length > 0 ? (
+ categories.map((category: any) => (
+
handleCategoryClick(category.id)}
+ >
+ {category.categoryName}
+
+ ))
+ ) : (
+
No categories available
+ )}
+
Price
-
-
-
+
+ {/*
Size
-
small
-
large
+
Popular
+
Recent
-
x-large
-
xx-large
+
Exprore
+
More iked
-
+
*/}
-
+
- >
-)
+ );
}
-export default Side
\ No newline at end of file
+export default Side;
diff --git a/src/components/Skeleton.tsx b/src/components/Skeleton.tsx
index d1752ba..9533a40 100644
--- a/src/components/Skeleton.tsx
+++ b/src/components/Skeleton.tsx
@@ -66,29 +66,14 @@ export const SideSkeleton = () => {
return (
<>
diff --git a/src/components/allProducts.tsx b/src/components/allProducts.tsx
index ba7eceb..8a43d96 100644
--- a/src/components/allProducts.tsx
+++ b/src/components/allProducts.tsx
@@ -1,10 +1,15 @@
'use client';
import React, { useState, useEffect, useRef } from 'react';
import ProductList from './ProductList';
+import {AiOutlineSearch} from 'react-icons/ai'
+import { useRouter } from 'next/navigation';
+import request from '@/utils/axios';
export const ProductWithFilter = () => {
const [Value, setValue] = useState('');
const [locarstorage, SetLocal] = useState(null);
const [activeButton, setActiveButton] = useState('All');
+ const [searchResults, setSearchResults] = useState([]);
+ const router = useRouter();
const Options = [
{ laber: 'All', value: 1 },
{ laber: 'Rating', value: 2 },
@@ -27,6 +32,50 @@ export const ProductWithFilter = () => {
`px-4 py-2 ${
activeButton === buttonName ? 'bg-black text-white' : ''
} focus:outline-none `;
+
+ const handleSearch = async (e: { preventDefault: () => void; }) => {
+ e.preventDefault();
+ console.log("Search query:", Value);
+
+ try {
+ let queryParams: any = {};
+ const trimmedValue = Value.trim();
+ const numericValue = parseFloat(trimmedValue);
+
+ if (!isNaN(numericValue)) {
+ queryParams.minPrice = numericValue;
+ }
+ else if (!isNaN(numericValue)) {
+ queryParams.maxPrice = numericValue; }
+ else if (!isNaN(numericValue)) {
+ queryParams.minPrice = numericValue;
+ queryParams.maxPrice = numericValue;
+ } else if(trimmedValue.length > 0 ) {
+
+ queryParams.name = Value;
+
+ }else if(trimmedValue.length > 0 ) {
+
+ } else if(trimmedValue.length > 0 ) {
+
+ queryParams.name = Value;
+ queryParams.category = Value;
+ }
+
+ const queryString = new URLSearchParams(queryParams).toString();
+ console.log(queryString)
+ // router.push(`/products?${queryString}`);
+ const url = `/search?${new URLSearchParams(queryParams).toString()}`;
+
+ const response:any = await request.get(url);
+
+ setSearchResults(response);
+
+ }
+ catch (error) {
+ console.error('Error fetching search results:', error);
+ }
+ };
return (
@@ -89,20 +138,25 @@ export const ProductWithFilter = () => {
>
)}
{/* Search */}
-
-
-
-
+
+
{/* Product Section */}
-
diff --git a/src/types/Product.ts b/src/types/Product.ts
index 7d4e6a6..785322f 100644
--- a/src/types/Product.ts
+++ b/src/types/Product.ts
@@ -6,14 +6,14 @@ export interface Seller {
phone: string;
}
export interface ReviewType {
- buyerId:string;
+ buyerId: string;
rating: number;
feedback: string;
- userProfile:{
- firstName:string;
- lastName:string;
- profileImage:string;
- }
+ userProfile: {
+ firstName: string;
+ lastName: string;
+ profileImage: string;
+ };
}
export interface imageType {
imgId: string;
@@ -33,35 +33,46 @@ export interface ProductType {
productDescription: string;
productDiscount?: number;
productName: string;
- productPictures: imageType[]
+ productPictures: imageType[];
productPrice: number;
productThumbnail: string;
reviews: ReviewType[];
related: any;
seller?: Seller;
- sellerId?: string;
+ sellerId?: string;
stockLevel?: number;
updatedAt?: number;
- }
-
- export interface ProductObj {
- currentPage: number;
- products: ProductType[];
- totalItems: number;
- toatalPages: number;
- }
+}
+
+export interface ProductObj {
+ currentPage: number;
+ products: ProductType[];
+ totalItems: number;
+ toatalPages: number;
+}
+
+export interface Cards {
+ key: string;
+ id: string;
+ productPrice: number;
+ productThumbnail: string;
+ productDescription: string;
+ reviews: ReviewType[];
+ productName: string;
+}
+export interface TableType {
+ product: string;
+ index: number;
+ id: string;
+}
+export interface CategoryType {
+ id: string;
+ categoryName: string;
+ createdAt: string;
+ updatedAt: string;
+}
- export interface Cards {
- key: string;
- id: string;
- productPrice: number;
- productThumbnail: string;
- productDescription: string;
- reviews:ReviewType[];
- productName:string;
- }
- export interface TableType {
- product: string;
- index: number;
- id:string;
- }
+export interface CategoryResponse {
+ message: string;
+ categories: CategoryType[];
+}