@@ -2,9 +2,8 @@ import { redirect } from "react-router";
22
33import { Container } from "@/components/ui" ;
44import { isValidCategorySlug , type Category } from "@/models/category.model" ;
5- import type { Product } from "@/models/product.model" ;
65import { getCategoryBySlug } from "@/services/category.service" ;
7- import { getProductsByCategorySlug } from "@/services/product.service" ;
6+ import { filterByMinMaxPrice } from "@/services/product.service" ;
87
98import { PriceFilter } from "./components/price-filter" ;
109import { ProductCard } from "./components/product-card" ;
@@ -19,54 +18,25 @@ export async function loader({ params, request }: Route.LoaderArgs) {
1918 }
2019
2120 const url = new URL ( request . url ) ;
22- const minPrice = url . searchParams . get ( "minPrice" ) || "" ;
23- const maxPrice = url . searchParams . get ( "maxPrice" ) || "" ;
21+ const minPrice = url . searchParams . get ( "minPrice" ) ;
22+ const minValue = minPrice ? parseFloat ( minPrice ) : undefined ;
23+ const maxPrice = url . searchParams . get ( "maxPrice" ) ;
24+ const maxValue = maxPrice ? parseFloat ( maxPrice ) : undefined ;
25+
2426
2527 try {
26- const [ category , products ] = await Promise . all ( [
28+ const [ category ] = await Promise . all ( [
2729 getCategoryBySlug ( categorySlug ) ,
28- getProductsByCategorySlug ( categorySlug ) ,
2930 ] ) ;
30-
31- const filterProductsByPrice = (
32- products : Product [ ] ,
33- minPrice : string ,
34- maxPrice : string
35- ) => {
36- const min = minPrice ? parseFloat ( minPrice ) : 0 ;
37- const max = maxPrice ? parseFloat ( maxPrice ) : Infinity ;
38- return products . filter (
39- ( product ) => {
40- const minProductPrice = product . minPrice || 0
41- const maxProductPrice = product . maxPrice || 0
42- const productPrice = product . price || 0
43-
44- if ( min && max ) {
45- return ( ( productPrice || minProductPrice ) >= min ) && ( ( productPrice || maxProductPrice ) <= max )
46- }
47-
48- if ( min ) {
49- return ( productPrice || minProductPrice ) >= min
50- }
51-
52- if ( max ) {
53- return ( productPrice || maxProductPrice ) <= max
54- }
55- return true
56- } ) ;
57- } ;
58-
59- const filteredProducts = filterProductsByPrice (
60- products ,
61- minPrice ,
62- maxPrice
63- ) ;
31+ console . log ( { categorySlug, minValue, maxValue} )
32+ const finalProducts = await filterByMinMaxPrice ( categorySlug , minValue , maxValue ) ;
6433
6534 return {
6635 category,
67- products : filteredProducts ,
68- minPrice,
69- maxPrice,
36+ products : finalProducts ,
37+ minPrice : minValue ,
38+ maxPrice : maxValue ,
39+ finalProducts
7040 } ;
7141 } catch ( e ) {
7242 throw new Response ( "Error loading category: " + e , { status : 500 } ) ;
0 commit comments