1
+ 'use strict' ;
1
2
2
3
const url = 'https://raw.githubusercontent.com/alexsimkovich/patronage/main/api/data.json' ;
3
4
4
- const productList = document . querySelector ( '.product-list__items' ) ;
5
+ const main = document . querySelector ( 'main' ) ;
6
+
7
+ const productListItems = document . querySelector ( '.product-list__items' ) ;
8
+ const filterProducts = document . querySelector ( '.product-list__filter--input' ) ;
9
+ const sortProducts = document . querySelector ( '#product-list__sort--select' ) ;
10
+
11
+ const cartList = document . querySelector ( '.cart-list' )
12
+ const shoppingCartGoBack = document . querySelector ( '.cart-list__go-back' )
13
+ const clearCart = document . querySelector ( '.cart-list__remove-items' )
5
14
const cartProducts = document . querySelector ( '.cart-list__products' ) ;
6
15
const hungryInfo = document . querySelector ( '.cart-list__empty' ) ;
7
16
const checkout = document . querySelector ( '.cart-list__checkout' ) ;
8
17
const total = document . querySelector ( '.cart-list__total' ) ;
9
- const main = document . querySelector ( 'main' ) ;
18
+
19
+ const shoppingCartGoTo = document . querySelector ( '.shopping-cart' )
20
+ const shoppingCartButtonTotal = document . querySelector ( '.shopping-cart__price' ) ;
21
+
10
22
const spinner = document . querySelector ( '.spinner' ) ;
11
23
12
24
13
25
const products = [ ] ;
14
- const cart = [ ] ;
26
+ const cart = JSON . parse ( localStorage . getItem ( 'cartStorage' ) ) || [ ] ;
27
+ updateCart ( ) ;
15
28
16
29
fetch ( url )
17
30
. then ( response => response . json ( ) )
18
- . then ( data => data . forEach ( item => products . push ( item ) ) )
19
- . then ( ( ) => renderPizza ( ) )
31
+ . then ( data => Array . prototype . push . apply ( products , data ) )
32
+ . then ( ( ) => renderPizza ( products ) )
20
33
21
34
22
- function renderPizza ( ) {
35
+ function renderPizza ( products ) {
23
36
24
- products . forEach ( ( productItem ) => {
37
+ sortPizza ( products ) ;
38
+ let result = '' ;
25
39
26
- const { id, title, image, ingredients, price } = productItem
40
+ if ( products . length ) {
41
+ products . forEach ( ( { id, title, image, ingredients, price } ) => {
27
42
28
- productList . innerHTML += `
29
- <div class="product-item">
30
- <div class="product-item__content">
31
-
32
- <div class="product-item__image">
33
- <img src="${ image } " alt="${ title } ">
34
- </div>
35
-
36
- <div class="product-item__info">
37
-
38
- <div class="product-item__title">
39
- <h3>${ title } </h3>
43
+ result += `
44
+ <div class="product-item">
45
+ <div class="product-item__content">
46
+
47
+ <div class="product-item__image">
48
+ <img src="${ image } " alt="${ title } ">
40
49
</div>
41
- <div class="product-item__description">
42
- ${ ingredients . join ( ', ' ) }
50
+
51
+ <div class="product-item__info">
52
+
53
+ <div class="product-item__title">
54
+ <h3 class="product-item__title-text">${ title } </h3>
55
+ </div>
56
+ <div class="product-item__description">
57
+ ${ ingredients . join ( ', ' ) }
58
+ </div>
59
+
43
60
</div>
44
-
61
+
62
+ <div class="product-item__checkout">
63
+
64
+ <div class="product-item__price-bar">
65
+ <div>${ price . toFixed ( 2 ) } zł</div>
66
+ </div>
67
+
68
+ <div class="product-item__cart-bar">
69
+ <button class="product-item__button button button__main" data-product-id="${ id } ">Zamów</button>
70
+ </div>
71
+
72
+ </div>
73
+
45
74
</div>
75
+ </div>
76
+ ` ;
46
77
47
- <div class="product-item__checkout">
78
+ productListItems . innerHTML = result ;
48
79
49
- <div class="product-item__price-bar">
50
- <div>${ price . toFixed ( 2 ) } zł</div>
51
- </div>
80
+ // handler for buttons to add products to cart
81
+ document . querySelectorAll ( '.product-item__button' ) . forEach ( item => {
82
+ item . addEventListener ( 'click' , ( ) => {
83
+ const productId = parseInt ( item . dataset . productId )
84
+ addToCart ( productId )
85
+ } )
52
86
53
- <div class="product-item__cart-bar">
54
- <button class="product-item__button button button__main" data-product-id="${ id } ">Zamów</button>
55
- </div>
56
-
57
- </div>
87
+ } )
58
88
59
- </div>
89
+ } ) ;
90
+
91
+ } else {
92
+ result += `
93
+ <div class="product-list__not-found">
94
+ brak wyników wyszukiwania dla: ${ filterProducts . value }
60
95
</div>
61
96
` ;
97
+ productListItems . innerHTML = result ;
98
+ }
62
99
63
- // handler for buttons to add products to cart
64
- document . querySelectorAll ( '.product-item__button' ) . forEach ( item => {
65
- item . addEventListener ( 'click' , ( ) => {
66
- const productId = parseInt ( item . dataset . productId )
67
- addToCart ( productId )
68
- } )
100
+ }
69
101
70
- } )
102
+ function sortPizza ( products ) {
103
+ const sortOption = sortProducts . value ;
104
+ switch ( sortOption ) {
105
+ case 'nameAscending' :
106
+ products . sort ( ( a , b ) => sortAtoZ ( a , b ) ) ;
107
+ break ;
108
+ case 'nameDescending' :
109
+ products . sort ( ( a , b ) => sortZtoA ( a , b ) ) ;
110
+ break ;
111
+ case 'priceAscending' :
112
+ products . sort ( ( a , b ) => priceAscending ( a , b ) ) ;
113
+ break ;
114
+ case 'priceDescending' :
115
+ products . sort ( ( a , b ) => priceDescending ( a , b ) ) ;
116
+ break ;
117
+ default :
118
+ products . sort ( ( a , b ) => sortAtoZ ( a , b ) ) ;
119
+ break ;
120
+ }
121
+ return products ;
122
+ }
71
123
72
- } ) ;
73
124
125
+ // filter products by providing ingredients to search bar
126
+ filterProducts . addEventListener ( 'input' , ( ) => filterPizza ( ) )
127
+ // sort products by selecting sorting option and considering products filtration
128
+ sortProducts . addEventListener ( 'change' , ( ) => filterPizza ( ) )
129
+
130
+
131
+ function filterPizza ( ) {
132
+ const filterIngredients = filterProducts . value . toUpperCase ( ) . replace ( / \s / g, '' ) . split ( ',' )
133
+ const filteredProducts = products . filter ( product =>
134
+ filterIngredients . every ( searchProduct => product . ingredients . some ( ingredient => ingredient . toUpperCase ( ) . includes ( searchProduct ) ) )
135
+ )
136
+ renderPizza ( filteredProducts )
137
+ }
138
+
139
+ // sort functions
140
+ function sortAtoZ ( a , b ) {
141
+ return a . title . localeCompare ( b . title )
74
142
}
75
143
144
+ function sortZtoA ( a , b ) {
145
+ return b . title . localeCompare ( a . title )
146
+ }
147
+
148
+ function priceAscending ( a , b ) {
149
+ return ( a . price > b . price ) ? 1 : ( a . price === b . price ) ? ( a . title . localeCompare ( b . title ) ) : - 1
150
+ }
151
+
152
+ function priceDescending ( a , b ) {
153
+ return ( a . price < b . price ) ? 1 : ( a . price === b . price ) ? ( b . title . localeCompare ( a . title ) ) : - 1
154
+ }
155
+
156
+ // add selected product to cart, if present increase quantity of selected product +1
76
157
function addToCart ( id ) {
77
158
const productInCart = cart . find ( item => item . id === id )
78
159
@@ -89,6 +170,7 @@ function addToCart(id) {
89
170
updateCart ( ) ;
90
171
}
91
172
173
+ // decrease quantity of selected product -1, if quantity===1 -remove one item from cart
92
174
function removeItemFromCart ( id ) {
93
175
const productInCart = cart . find ( item => item . id === id )
94
176
const index = cart . findIndex ( item => item . id === id )
@@ -102,9 +184,17 @@ function removeItemFromCart(id) {
102
184
updateCart ( ) ;
103
185
}
104
186
187
+ // clear cart and remove all items
188
+ clearCart . addEventListener ( 'click' , ( ) => {
189
+ cart . length = 0 ;
190
+ updateCart ( )
191
+ } )
192
+
105
193
function updateCart ( ) {
106
194
renderCartProducts ( ) ;
107
195
renderTotal ( ) ;
196
+
197
+ localStorage . setItem ( 'cartStorage' , JSON . stringify ( cart ) )
108
198
}
109
199
110
200
function renderCartProducts ( ) {
@@ -171,19 +261,33 @@ function renderTotal() {
171
261
} ) ;
172
262
173
263
total . innerHTML = `${ totalPrice . toFixed ( 2 ) } zł` ;
264
+ shoppingCartButtonTotal . innerHTML = `${ totalPrice . toFixed ( 2 ) } zł`
174
265
shoppingCartListener ( totalItems ) ;
175
266
}
176
267
177
268
269
+ // onclick go to shopping cart
270
+ shoppingCartGoTo . addEventListener ( 'click' , ( ) => {
271
+ cartList . classList . add ( 'active' )
272
+ document . body . classList . add ( 'scroll-lock' )
273
+ } )
274
+
275
+ // onclick return to products list from shopping cart
276
+ shoppingCartGoBack . addEventListener ( 'click' , ( ) => {
277
+ cartList . classList . remove ( 'active' )
278
+ document . body . classList . remove ( 'scroll-lock' )
279
+ } )
280
+
178
281
// if shoppingCart is empty - display hungry information
179
282
function shoppingCartListener ( items ) {
180
283
if ( items > 0 ) {
181
284
hungryInfo . classList . add ( 'hide' )
182
285
checkout . classList . remove ( 'hide' )
183
-
286
+ clearCart . classList . remove ( 'hide' )
184
287
} else {
185
288
hungryInfo . classList . remove ( 'hide' )
186
289
checkout . classList . add ( 'hide' )
290
+ clearCart . classList . add ( 'hide' )
187
291
}
188
292
}
189
293
0 commit comments