1
- const catID = localStorage . getItem ( "catID" )
1
+ const catID = localStorage . getItem ( "catID" ) ;
2
2
let currentProductsArray = [ ] ;
3
3
let minPrice = undefined ;
4
4
let maxPrice = undefined ;
5
5
6
-
7
6
fetch ( `https://japceibal.github.io/emercado-api/cats_products/${ catID } .json` )
8
7
. then ( ( res ) => res . json ( ) )
9
8
. then ( ( { products, catName } ) => {
9
+ const categoryTitle = document . getElementById ( "categoryTitle" ) ;
10
+ categoryTitle . textContent = catName ;
10
11
11
- const categoryTitle = document . getElementById ( 'categoryTitle' )
12
- categoryTitle . textContent = catName
13
-
14
- currentProductsArray = products
12
+ currentProductsArray = products ;
15
13
16
14
if ( products . length === 0 ) {
17
- showAlert ( "No hay productos disponibles para esta categoría" )
18
- return
15
+ showAlert ( "No hay productos disponibles para esta categoría" ) ;
16
+ document . getElementById ( "sortAndFilterContainer" ) . style . display = "none" ;
17
+ return ;
19
18
}
20
-
21
- showProductsList ( )
22
- } )
19
+ document . getElementById ( "sortAndFilterContainer" ) . style . display = "block" ;
20
+ showProductsList ( ) ;
21
+ } ) ;
23
22
24
23
function showProductsList ( ) {
25
- const productsContainer = document . getElementById ( ' productsContainer' )
26
- productsContainer . innerHTML = ""
24
+ const productsContainer = document . getElementById ( " productsContainer" ) ;
25
+ productsContainer . innerHTML = "" ; // Limpiar el contenedor antes de mostrar
27
26
28
- const fragment = document . createDocumentFragment ( )
27
+ const fragment = document . createDocumentFragment ( ) ;
29
28
30
29
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 ) ) {
35
- const column = document . createElement ( 'div' )
36
- column . classList . add ( 'col-12' , 'col-md-6' , 'col-lg-4' , 'my-3' )
37
-
38
- const card = document . createElement ( 'div' )
39
- card . classList . add ( 'card' , 'custom-card' , 'cursor-active' , 'h-100' , 'w-100' )
40
- card . style = 'width: 24rem;'
41
-
42
- const img = document . createElement ( 'img' )
43
- img . classList . add ( 'card-img-top' )
44
- img . src = image
45
- img . alt = `Imagen de ${ name } `
46
-
47
- const cardBody = document . createElement ( 'div' )
48
- cardBody . classList . add ( 'card-body' , 'd-flex' , 'flex-column' , 'justify-content-between' )
49
-
50
- const cardTitle = document . createElement ( 'h5' )
51
- cardTitle . classList . add ( 'card-title' , 'card-title-h5' )
52
- cardTitle . textContent = name
53
-
54
- const cardDescription = document . createElement ( 'p' )
55
- cardDescription . classList . add ( 'card-text' , 'card-description' , 'flex-grow-1' )
56
- cardDescription . textContent = description
57
-
58
- const cardPrice = document . createElement ( 'p' )
59
- cardPrice . classList . add ( 'card-text' , 'card-price' , 'mt-auto' )
60
- cardPrice . textContent = `${ currency } ${ cost } `
61
-
62
- const cardSoldCount = document . createElement ( 'p' )
63
- cardSoldCount . classList . add ( 'card-text' , 'card-sold-count' )
64
- cardSoldCount . textContent = `${ soldCount } vendidos`
65
-
66
- cardBody . append ( cardTitle , cardDescription , cardPrice , cardSoldCount )
67
- card . append ( img , cardBody )
68
- column . appendChild ( card )
69
- fragment . appendChild ( column )
30
+ ( { id, image, name, description, currency, cost, soldCount } ) => {
31
+ if (
32
+ ( minPrice == undefined ||
33
+ ( minPrice != undefined && parseFloat ( cost ) >= minPrice ) ) &&
34
+ ( maxPrice == undefined ||
35
+ ( maxPrice != undefined && parseFloat ( cost ) <= maxPrice ) )
36
+ ) {
37
+ const column = document . createElement ( "div" ) ;
38
+ column . classList . add ( "col-12" , "col-md-6" , "col-lg-4" , "my-3" ) ;
39
+
40
+ const card = document . createElement ( "div" ) ;
41
+ card . classList . add (
42
+ "card" ,
43
+ "custom-card" ,
44
+ "cursor-active" ,
45
+ "h-100" ,
46
+ "w-100"
47
+ ) ;
48
+ card . style = "width: 24rem;" ;
49
+
50
+ const img = document . createElement ( "img" ) ;
51
+ img . classList . add ( "card-img-top" ) ;
52
+ img . src = image ;
53
+ img . alt = `Imagen de ${ name } ` ;
54
+ img . loading = "lazy" ;
55
+
56
+ const cardBody = document . createElement ( "div" ) ;
57
+ cardBody . classList . add (
58
+ "card-body" ,
59
+ "d-flex" ,
60
+ "flex-column" ,
61
+ "justify-content-between"
62
+ ) ;
63
+
64
+ const cardTitle = document . createElement ( "h5" ) ;
65
+ cardTitle . classList . add ( "card-title" , "card-title-h5" ) ;
66
+ cardTitle . textContent = name ;
67
+
68
+ const cardDescription = document . createElement ( "p" ) ;
69
+ cardDescription . classList . add (
70
+ "card-text" ,
71
+ "card-description" ,
72
+ "flex-grow-1"
73
+ ) ;
74
+ cardDescription . textContent = description ;
75
+
76
+ const cardPrice = document . createElement ( "p" ) ;
77
+ cardPrice . classList . add ( "card-text" , "card-price" , "mt-auto" ) ;
78
+ cardPrice . textContent = `${ currency } ${ cost } ` ;
79
+
80
+ const cardSoldCount = document . createElement ( "p" ) ;
81
+ cardSoldCount . classList . add ( "card-text" , "card-sold-count" ) ;
82
+ cardSoldCount . textContent = `${ soldCount } vendidos` ;
83
+
84
+ cardBody . append ( cardTitle , cardDescription , cardPrice , cardSoldCount ) ;
85
+ card . append ( img , cardBody ) ;
86
+ column . appendChild ( card ) ;
87
+ fragment . appendChild ( column ) ;
70
88
}
71
89
}
72
- )
90
+ ) ;
73
91
74
- productsContainer . appendChild ( fragment )
92
+ productsContainer . appendChild ( fragment ) ;
75
93
}
76
94
77
95
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 ) )
96
+ if ( criteria === "PriceAsc" ) {
97
+ currentProductsArray . sort (
98
+ ( a , b ) => parseFloat ( a . cost ) - parseFloat ( b . cost )
99
+ ) ;
100
+ } else if ( criteria === "PriceDesc" ) {
101
+ currentProductsArray . sort (
102
+ ( a , b ) => parseFloat ( b . cost ) - parseFloat ( a . cost )
103
+ ) ;
104
+ } else if ( criteria === "Relevance" ) {
105
+ currentProductsArray . sort (
106
+ ( a , b ) => parseInt ( b . soldCount ) - parseInt ( a . soldCount )
107
+ ) ;
84
108
}
85
109
86
110
showProductsList ( ) ;
87
111
}
88
112
89
- document . getElementById ( "sortAscPrice" ) . addEventListener ( "click" , function ( ) {
90
- sortProducts ( "ascPrice" ) ;
91
- } )
113
+ document . getElementById ( "sortAscPrice" ) . addEventListener ( "click" , function ( ) {
114
+ sortProducts ( "PriceAsc" ) ;
115
+ } ) ;
116
+
117
+ document . getElementById ( "sortDescPrice" ) . addEventListener ( "click" , function ( ) {
118
+ sortProducts ( "PriceDesc" ) ;
119
+ } ) ;
120
+
121
+ document
122
+ . getElementById ( "sortByRelevance" )
123
+ . addEventListener ( "click" , function ( ) {
124
+ sortProducts ( "Relevance" ) ;
125
+ } ) ;
126
+
127
+ document
128
+ . getElementById ( "clearRangeFilter" )
129
+ . addEventListener ( "click" , function ( ) {
130
+ document . getElementById ( "rangeFilterPriceMin" ) . value = "" ;
131
+ document . getElementById ( "rangeFilterPriceMax" ) . value = "" ;
132
+
133
+ minPrice = undefined ;
134
+ maxPrice = undefined ;
135
+
136
+ showProductsList ( ) ;
137
+ } ) ;
138
+
139
+ document
140
+ . getElementById ( "rangeFilterPrice" )
141
+ . addEventListener ( "click" , function ( ) {
142
+ minPrice = document . getElementById ( "rangeFilterPriceMin" ) . value ;
143
+ maxPrice = document . getElementById ( "rangeFilterPriceMax" ) . value ;
144
+
145
+ if ( minPrice != undefined && minPrice != "" && parseFloat ( minPrice ) >= 0 ) {
146
+ minPrice = parseFloat ( minPrice ) ;
147
+ } else {
148
+ minPrice = undefined ;
149
+ }
92
150
93
- document . getElementById ( "sortDescPrice" ) . addEventListener ( "click" , function ( ) {
94
- sortProducts ( "descPrice" ) ;
95
- } )
151
+ if ( maxPrice != undefined && maxPrice != "" && parseFloat ( maxPrice ) >= 0 ) {
152
+ maxPrice = parseFloat ( maxPrice ) ;
153
+ } else {
154
+ maxPrice = undefined ;
155
+ }
96
156
97
- document . getElementById ( "sortByRelevance" ) . addEventListener ( "click" , function ( ) {
98
- sortProducts ( "relevance" ) ;
99
- } )
157
+ showProductsList ( ) ;
158
+ } ) ;
100
159
101
160
function showAlert ( message ) {
102
- const alertContainer = document . createElement ( ' div' )
103
- alertContainer . className = ' alert alert-warning alert-dismissible fade show'
104
- alertContainer . role = ' alert'
161
+ const alertContainer = document . createElement ( " div" ) ;
162
+ alertContainer . className = " alert alert-warning alert-dismissible fade show" ;
163
+ alertContainer . role = " alert" ;
105
164
alertContainer . innerHTML = `
106
- <strong>Advertencia:</strong> ${ message }
107
- <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
108
- `
109
-
110
- // Insertar la alerta justo después del título de la categoría
111
- const categoryTitle = document . getElementById ( 'categoryTitle' )
112
- categoryTitle . insertAdjacentElement ( 'afterend' , alertContainer )
113
- }
165
+ <strong>Advertencia:</strong> ${ message }
166
+ <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
167
+ ` ;
168
+
169
+ const categoryTitle = document . getElementById ( "categoryTitle" ) ;
170
+ categoryTitle . insertAdjacentElement ( "afterend" , alertContainer ) ;
171
+ }
0 commit comments