1
1
const catID = localStorage . getItem ( "catID" )
2
+ let currentProductsArray = [ ] ;
3
+ let minPrice = undefined ;
4
+ let maxPrice = undefined ;
5
+
2
6
3
7
fetch ( `https://japceibal.github.io/emercado-api/cats_products/${ catID } .json` )
4
8
. then ( ( res ) => res . json ( ) )
@@ -7,18 +11,27 @@ fetch(`https://japceibal.github.io/emercado-api/cats_products/${catID}.json`)
7
11
const categoryTitle = document . getElementById ( 'categoryTitle' )
8
12
categoryTitle . textContent = catName
9
13
10
- const productsContainer = document . getElementById ( 'productsContainer' )
11
-
12
- const fragment = document . createDocumentFragment ( )
14
+ currentProductsArray = products
13
15
14
16
if ( products . length === 0 ) {
15
17
showAlert ( "No hay productos disponibles para esta categoría" )
16
18
return
17
19
}
18
20
19
- products . forEach (
20
- ( { image, name, description, currency, cost, soldCount } ) => {
21
+ showProductsList ( )
22
+ } )
23
+
24
+ function showProductsList ( ) {
25
+ const productsContainer = document . getElementById ( 'productsContainer' )
26
+ productsContainer . innerHTML = ""
27
+
28
+ const fragment = document . createDocumentFragment ( )
21
29
30
+ currentProductsArray . forEach (
31
+ ( { image, name, description, currency, cost, soldCount } ) => {
32
+
33
+ if ( minPrice == undefined || ( minPrice != undefined && parseFloat ( cost ) >= minPrice ) &&
34
+ maxPrice == undefined || ( maxPrice != undefined && parseFloat ( cost ) >= maxPrice ) ) {
22
35
const column = document . createElement ( 'div' )
23
36
column . classList . add ( 'col-12' , 'col-md-6' , 'col-lg-4' , 'my-3' )
24
37
@@ -55,10 +68,35 @@ fetch(`https://japceibal.github.io/emercado-api/cats_products/${catID}.json`)
55
68
column . appendChild ( card )
56
69
fragment . appendChild ( column )
57
70
}
58
- )
59
-
60
- productsContainer . appendChild ( fragment )
61
- } )
71
+ }
72
+ )
73
+
74
+ productsContainer . appendChild ( fragment )
75
+ }
76
+
77
+ function sortProducts ( criteria ) {
78
+ if ( criteria === "ascPrice" ) {
79
+ currentProductsArray . sort ( ( a , b ) => parseFloat ( a . cost ) - parseFloat ( b . cost ) )
80
+ } else if ( criteria === "descPrice" ) {
81
+ currentProductsArray . sort ( ( a , b ) => parseFloat ( b . cost ) - parseFloat ( a . cost ) )
82
+ } else if ( criteria === "relevance" ) {
83
+ currentProductsArray . sort ( ( a , b ) => parseFloat ( b . soldCount ) - parseFloat ( a . soldCount ) )
84
+ }
85
+
86
+ showProductsList ( ) ;
87
+ }
88
+
89
+ document . getElementById ( "sortAscPrice" ) . addEventListener ( "click" , function ( ) {
90
+ sortProducts ( "ascPrice" ) ;
91
+ } )
92
+
93
+ document . getElementById ( "sortDescPrice" ) . addEventListener ( "click" , function ( ) {
94
+ sortProducts ( "descPrice" ) ;
95
+ } )
96
+
97
+ document . getElementById ( "sortByRelevance" ) . addEventListener ( "click" , function ( ) {
98
+ sortProducts ( "relevance" ) ;
99
+ } )
62
100
63
101
function showAlert ( message ) {
64
102
const alertContainer = document . createElement ( 'div' )
0 commit comments