@@ -69,14 +69,75 @@ const enableScroll = () => {
69
69
70
70
71
71
72
- // ======================= Modal window ======================= //
72
+
73
+ // ======================= Product cart ======================= //
74
+
75
+ // Getting data from an array of objects
76
+ const getLocalStorage = ( ) => JSON ?. parse ( localStorage . getItem ( 'cart-lomoda' ) ) || [ ] ;
77
+
78
+ // Storing data in an array of objects in local storage
79
+ const setLocalStorage = data => localStorage . setItem ( 'cart-lomoda' , JSON . stringify ( data ) ) ;
80
+
81
+ // Writting on the variable the list of goods
82
+ const cartListGoods = document . querySelector ( '.cart__list-goods' ) ;
83
+
84
+ // Writting on the variable the total cost
85
+ const cartTotalCost = document . querySelector ( '.cart__total-cost' ) ;
86
+
87
+ // The action of cart rendering
88
+ const renderCart = ( ) => {
89
+ cartListGoods . textContent = '' ;
90
+
91
+ const cartItems = getLocalStorage ( ) ;
92
+
93
+ let totalPrice = 0 ;
94
+
95
+ cartItems . forEach ( ( item , index ) => {
96
+
97
+ const tr = document . createElement ( 'tr' ) ;
98
+ tr . innerHTML = `
99
+ <td>${ index + 1 } </td>
100
+ <td>${ item . brand } ${ item . name } </td>
101
+ ${ item . color ? `<td>${ item . color } </td>` : '<td>-</td>' }
102
+ ${ item . size ? `<td>${ item . size } </td>` : '<td>-</td>' }
103
+ <td>${ item . cost } ₽</td>
104
+ <td><button class="btn-delete" data-id="${ item . id } ">×</button></td>
105
+ ` ;
106
+
107
+ totalPrice += item . cost ;
108
+
109
+ cartListGoods . append ( tr ) ;
110
+ } ) ;
111
+
112
+ cartTotalCost . textContent = totalPrice + ' ₽' ;
113
+ }
114
+
115
+ const deleteItemCart = id => {
116
+ const cartItems = getLocalStorage ( ) ;
117
+ const newCartItems = cartItems . filter ( item => item . id != id ) ;
118
+ setLocalStorage ( newCartItems ) ;
119
+ }
120
+
121
+ cartListGoods . addEventListener ( 'click' , event => {
122
+ if ( event . target . matches ( '.btn-delete' ) ) {
123
+ deleteItemCart ( event . target . dataset . id ) ;
124
+ renderCart ( ) ;
125
+ }
126
+ } ) ;
127
+
128
+ // ===================== End Product cart ===================== //
129
+
130
+
131
+
132
+ // ================ Modal window (product cart) ================ //
73
133
74
134
const subheaderCart = document . querySelector ( '.subheader__cart' ) ;
75
135
const cartOverlay = document . querySelector ( '.cart-overlay' ) ;
76
136
77
137
const cartModalOpen = ( ) => {
78
138
cartOverlay . classList . add ( 'cart-overlay-open' ) ;
79
139
disableScroll ( ) ;
140
+ renderCart ( ) ;
80
141
} ;
81
142
82
143
const cartModalClose = ( ) => {
@@ -105,7 +166,7 @@ document.addEventListener('keydown', event => {
105
166
}
106
167
} ) ;
107
168
108
- // ==================== End of Modal window ======= ============= //
169
+ // ============= End of Modal window (product cart) ============= //
109
170
110
171
111
172
@@ -185,14 +246,12 @@ try {
185
246
<h3 class="good__title">${ brand } <span class="good__title__grey">/ ${ name } </span></h3>
186
247
187
248
<!-- Checking whether the data element has a "sizes" property? -->
188
- ${ sizes ?
189
- `<p class="good__sizes">Размеры (RUS): <span class="good__sizes-list">${ sizes . join ( ' ' ) } </span></p>` :
190
- '' }
249
+ ${ sizes ? `<p class="good__sizes">Размеры (RUS): <span class="good__sizes-list">${ sizes . join ( ' ' ) } </span></p>` : '' }
191
250
192
251
<a class="good__link" href="card-good.html#${ id } ">Подробнее</a>
193
252
</div>
194
253
</article>
195
- `
254
+ ` ;
196
255
197
256
return li ;
198
257
} ;
@@ -224,7 +283,7 @@ try {
224
283
getGoods ( renderGoodsList , 'category' , hash ) ;
225
284
226
285
} catch ( err ) {
227
- console . warn ( err )
286
+ console . warn ( err ) ;
228
287
}
229
288
230
289
// ========== End od Display data response on goods.html ========== //
@@ -246,16 +305,20 @@ try {
246
305
const cardGoodSelectWrapper = document . querySelectorAll ( '.card-good__select__wrapper' ) ;
247
306
const cardGoodColorList = document . querySelector ( '.card-good__color-list' ) ;
248
307
const cardGoodSizes = document . querySelector ( '.card-good__sizes' ) ;
249
- const cardGoodSelectList = document . querySelector ( '.card-good__select-list' ) ;
308
+ const cardGoodSizesList = document . querySelector ( '.card-good__sizes-list' ) ;
309
+ const cardGoodBuy = document . querySelector ( '.card-good__buy' ) ;
250
310
251
311
const generatedList = data => data . reduce ( ( html , item , index ) => html +
252
312
`<li class="card-good__select-item" data-id="${ index } ">${ item } </li>` , '' ) ;
253
313
254
- const renderCardGood = ( [ { brand, name, cost, color, sizes, photo } ] ) => {
314
+ const renderCardGood = ( [ { id, brand, name, cost, color, sizes, photo } ] ) => {
315
+
316
+ // Creation of an object for rendering a product cart
317
+ const data = { brand, name, cost, id } ;
255
318
256
319
cardGoodImage . src = `goods-image/${ photo } ` ;
257
320
cardGoodImage . alt = `${ brand } ${ name } ` ;
258
- cardGoodBrand . alt = brand ;
321
+ cardGoodBrand . textContent = brand ;
259
322
cardGoodTitle . textContent = name ;
260
323
cardGoodPrice . textContent = `${ cost } ₽` ;
261
324
if ( color ) {
@@ -268,11 +331,38 @@ try {
268
331
if ( sizes ) {
269
332
cardGoodSizes . textContent = sizes [ 0 ] ;
270
333
cardGoodSizes . dataset . id = 0 ;
271
- cardGoodSelectList . innerHTML = generatedList ( sizes ) ;
334
+ cardGoodSizesList . innerHTML = generatedList ( sizes ) ;
272
335
} else {
273
336
cardGoodSizes . style . display = 'none' ;
274
337
}
275
338
339
+ if ( getLocalStorage ( ) . some ( item => item . id === id ) ) {
340
+ cardGoodBuy . classList . add ( 'delete' ) ;
341
+ cardGoodBuy . textContent = 'Удалить из корзины' ;
342
+ }
343
+
344
+ // Sending values to the goods cart
345
+ cardGoodBuy . addEventListener ( 'click' , ( ) => {
346
+ if ( cardGoodBuy . classList . contains ( 'delete' ) ) {
347
+ deleteItemCart ( id ) ;
348
+ cardGoodBuy . classList . remove ( 'delete' ) ;
349
+ cardGoodBuy . textContent = 'Добавить в корзину' ;
350
+ return ;
351
+ }
352
+
353
+ if ( color ) data . color = cardGoodColor . textContent ;
354
+ if ( color ) data . size = cardGoodSizes . textContent ;
355
+
356
+ cardGoodBuy . classList . add ( 'delete' ) ;
357
+ cardGoodBuy . textContent = 'Удалить из корзины' ;
358
+
359
+ // Getting actual data from the local storage
360
+ const cardData = getLocalStorage ( ) ;
361
+ // Pushing into the gettings data new data.
362
+ cardData . push ( data ) ;
363
+ // Setting new data to the local storage
364
+ setLocalStorage ( cardData ) ;
365
+ } ) ;
276
366
} ;
277
367
278
368
cardGoodSelectWrapper . forEach ( item => {
@@ -287,7 +377,7 @@ try {
287
377
const cardGoodSelect = item . querySelector ( '.card-good__select' ) ;
288
378
cardGoodSelect . textContent = target . textContent ;
289
379
cardGoodSelect . dataset . id = target . dataset . id ;
290
- cardGoodSelect . classList . remove ( 'card-good__select-item ' ) ;
380
+ cardGoodSelect . classList . remove ( 'card-good__select__open ' ) ;
291
381
}
292
382
} ) ;
293
383
} ) ;
0 commit comments