|
1 |
| -// interface Props { |
2 |
| -// search?: string |
3 |
| -// categories?: string |
4 |
| -// brands?: string |
5 |
| -// discount?: number |
6 |
| -// price?: number |
7 |
| -// } |
| 1 | +interface FilterProps { |
| 2 | + search?: string |
| 3 | + categories: string |
| 4 | + brands: string |
| 5 | + discount: number |
| 6 | + price: number |
| 7 | +} |
8 | 8 |
|
9 | 9 | import type { Product } from '@/env'
|
10 | 10 |
|
11 | 11 | import { useStoreProducts } from "@/stores/storeProducts"
|
| 12 | +import { parsePriceNumber } from '@utils/parsePriceNumber' |
| 13 | +import { useEffect } from 'react' |
| 14 | + |
| 15 | +export const useFilterAllProducts = () => { |
| 16 | + const { cacheProducts, setProducts, filter, setFilter } = useStoreProducts() |
12 | 17 |
|
13 |
| -const debounce = (func: Function, delay: number) => { |
14 |
| - let timeoutId: ReturnType<typeof setTimeout> | undefined |
15 |
| - return (...args: any[]) => { |
16 |
| - if (timeoutId) clearTimeout(timeoutId) |
17 |
| - timeoutId = setTimeout(() => { |
18 |
| - func.apply(null, args) |
19 |
| - }, delay) |
| 18 | + const handleSearch = ({ search }: { search: string }) => { |
| 19 | + const newFilter = { |
| 20 | + ...filter, |
| 21 | + search |
| 22 | + } |
| 23 | + |
| 24 | + setFilter(newFilter) |
20 | 25 | }
|
21 |
| -} |
22 | 26 |
|
23 |
| -export const useFilterAllProducts = () => { |
24 |
| - const { cacheProducts, setSearchProducts } = useStoreProducts() |
25 |
| - |
26 |
| - const handleSearch = debounce((item: string) => { |
27 |
| - const value = item.toLowerCase().trim() |
28 |
| - const newValue = value.split(' ') |
29 |
| - |
30 |
| - const filterLetters = newValue.length > 3 |
31 |
| - |
32 |
| - const products = cacheProducts.filter((product: Product) => { |
33 |
| - if (filterLetters) { |
34 |
| - return ( |
35 |
| - product?.title?.toLowerCase().includes(item) || |
36 |
| - product?.category?.toLowerCase().includes(item) || |
37 |
| - product?.brandProduct?.toLowerCase().includes(item) || |
38 |
| - product?.collection?.toLowerCase().includes(item) || |
39 |
| - product?.origin?.toLowerCase().includes(item) |
40 |
| - ) |
41 |
| - } |
42 |
| - |
43 |
| - return newValue.some( |
44 |
| - (word) => |
45 |
| - product?.title?.toLowerCase().includes(word) || |
46 |
| - product?.category?.toLowerCase().includes(word) || |
47 |
| - product?.brandProduct?.toLowerCase().includes(word) || |
48 |
| - product?.collection?.toLowerCase().includes(word) || |
49 |
| - product?.origin?.toLowerCase().includes(word) |
50 |
| - ) |
51 |
| - }) |
52 |
| - |
53 |
| - setSearchProducts(products) |
54 |
| - }, 300) |
55 |
| - |
56 |
| - const filterProducts = ({ search = '', categories = 'all', brands = 'all', discount = 0, price = 0 }) => { |
57 |
| - |
58 |
| - handleSearch(search) |
| 27 | + const handlePrice = ({ price }: { price: number }) => { |
| 28 | + const newFilter = { |
| 29 | + ...filter, |
| 30 | + price |
| 31 | + } |
| 32 | + |
| 33 | + setFilter(newFilter) |
59 | 34 | }
|
60 | 35 |
|
61 |
| - return { filterProducts }; |
| 36 | + const filterProducts = () => { |
| 37 | + const newProducts = cacheProducts.filter((product: Product) => (product.title?.toLocaleUpperCase().includes(filter?.search?.toLocaleUpperCase() ?? '') && parsePriceNumber(product?.price?.toString() ? product?.price?.toString() : product?.descuento?.toString()) >= filter.price)) |
| 38 | + |
| 39 | + console.log(filter) |
| 40 | + setProducts(newProducts) |
| 41 | + } |
| 42 | + |
| 43 | + useEffect(() => { |
| 44 | + filterProducts() |
| 45 | + }, [filter]) |
| 46 | + |
| 47 | + return { filter, handleSearch, handlePrice }; |
62 | 48 | };
|
| 49 | + |
| 50 | + |
| 51 | +// const debounce = (func: Function, delay: number) => { |
| 52 | +// let timeoutId: ReturnType<typeof setTimeout> | undefined |
| 53 | +// return (...args: any[]) => { |
| 54 | +// if (timeoutId) clearTimeout(timeoutId) |
| 55 | +// timeoutId = setTimeout(() => { |
| 56 | +// func.apply(null, args) |
| 57 | +// }, delay) |
| 58 | +// } |
| 59 | +// } |
| 60 | + |
| 61 | +// const handleSearch = debounce((item: string) => { |
| 62 | +// const value = item.toLowerCase().trim() |
| 63 | +// const newValue = value.split(' ') |
| 64 | +// |
| 65 | +// const filterLetters = newValue.length > 3 |
| 66 | +// |
| 67 | +// const products = cacheProducts.filter((product: Product) => { |
| 68 | +// if (filterLetters) { |
| 69 | +// return ( |
| 70 | +// product?.title?.toLowerCase().includes(item) || |
| 71 | +// product?.category?.toLowerCase().includes(item) || |
| 72 | +// product?.brandProduct?.toLowerCase().includes(item) || |
| 73 | +// product?.collection?.toLowerCase().includes(item) || |
| 74 | +// product?.origin?.toLowerCase().includes(item) |
| 75 | +// ) |
| 76 | +// } |
| 77 | +// |
| 78 | +// |
| 79 | +// return newValue.some( |
| 80 | +// (word) => |
| 81 | +// product?.title?.toLowerCase().includes(word) || |
| 82 | +// product?.category?.toLowerCase().includes(word) || |
| 83 | +// product?.brandProduct?.toLowerCase().includes(word) || |
| 84 | +// product?.collection?.toLowerCase().includes(word) || |
| 85 | +// product?.origin?.toLowerCase().includes(word) |
| 86 | +// ) |
| 87 | +// }) |
| 88 | +// |
| 89 | +// setSearchProducts(products) |
| 90 | +// }, 300) |
0 commit comments