@@ -14,14 +14,17 @@ document.addEventListener('DOMContentLoaded', function () {
1414} ) ;
1515
1616function addToCart ( id ) {
17- console . log ( `Tu as ajouté le produit ${ id } ` ) ; // Console log the product id
17+ console . log ( `Tu as ajouté le produit ${ id } ` ) ; // Console log the product i
1818
1919 // Get the product
20- for ( var i = 0 ; i < COURSES . length ; i ++ ) { // Loop through all courses
21- if ( COURSES [ i ] . id == id ) { // If the id matches the id of the product
22- var product = COURSES [ i ] ; // Set the product to the current course
23- break ; // Stop the loop
24- }
20+ let product = getProductDetails ( Number ( id ) ) ;
21+
22+ if ( product . stock <= 0 ) {
23+ alert ( `Ìl n'y a plus de stock dispo` ) ;
24+ return ;
25+ } else {
26+ product . stock -- ;
27+ document . querySelector ( `.stock-${ product . id } ` ) . innerHTML = product . stock ;
2528 }
2629
2730 // Check if product is already in cart
@@ -34,7 +37,7 @@ function addToCart(id) {
3437 localStorage . setItem ( productCartName , JSON . stringify ( cart ) ) ; // Set the product cart
3538
3639 // Update the cart
37- // console.log(product.slug + '-inCart');
40+ console . log ( product . slug + '-inCart' ) ;
3841 document . querySelector ( `.${ product . slug } -inCart` ) . innerHTML = cart . productsInCart ; // Update the product in cart
3942
4043 } else { // If the product cart doesn't exist
@@ -44,27 +47,8 @@ function addToCart(id) {
4447
4548 productItemsInCart ( product , cart ) ;
4649 }
47-
48- // Show product on cart page
49-
5050 // Show notification
5151 notification ( `Vous avez ajouté ${ product . title } au panier` ) ; // Run notification function
52-
53- // ---
54-
55- if ( e . target . classList . contains ( 'add-to-cart' ) ) {
56-
57- }
58-
59-
60- // ---
61-
62- // Update stock product
63- COURSES [ product . id ] . stock -- ;
64- let stock = parent . getElementsByClassName ( 'stock' ) ;
65- stock = stock - 1 ;
66-
67-
6852}
6953//Pop up notification
7054
@@ -98,7 +82,7 @@ function productItems(querySelector, product) {
9882 <span class="discount">${ product . price } €</span>
9983 </p>
10084 <p>
101- Disponible: <span class="stock">${ product . stock } </span>
85+ Disponible: <span class="stock stock- ${ product . id } ">${ product . stock } </span>
10286 </p>
10387 <a href="#" class="add-to-cart" data-id="${ product . id } "><i class="fa
10488 fa-cart-plus"></i>Ajouter au panier</a>
@@ -125,19 +109,53 @@ function productItemsInCart(product, cart) {
125109 ` ;
126110}
127111
112+ function getProductDetails ( id ) {
113+ for ( var i = 0 ; i < COURSES . length ; i ++ ) {
114+ if ( COURSES [ i ] . id === id ) {
115+ return COURSES [ i ] ;
116+ }
117+ }
118+ }
119+
128120//vider le panier
129121
130122// On button click add product to cart
131123document . addEventListener ( 'click' , function ( e ) {
132- if ( e . target . classList . contains ( 'add-to-cart' ) ) {
124+ if ( e . target . classList . contains ( 'add-to-cart' ) ) {
133125 addToCart ( e . target . dataset . id ) ; // Add to cart
134- } else if ( e . target . classList . contains ( 'empty-cart' ) ) {
126+ } else if ( e . target . classList . contains ( 'empty-cart' ) ) {
127+ for ( var i = 0 ; i < COURSES . length ; i ++ ) {
128+ const getItem = JSON . parse ( localStorage . getItem ( `inCart-${ COURSES [ i ] . id } ` ) ) ;
129+
130+ if ( getItem !== null ) {
131+ COURSES [ i ] . stock += getItem . productsInCart ;
132+ console . log ( "Stock " + COURSES [ i ] . stock ) ;
133+
134+ document . querySelector ( `.stock-${ COURSES [ i ] . id } ` ) . innerHTML = COURSES [ i ] . stock ;
135+ }
136+ }
137+
135138 localStorage . clear ( ) ;
136139 document . querySelector ( '#cart-table tbody' ) . innerHTML = "" ;
137- } else if ( e . target . classList . contains ( 'fa-trash' ) ) {
140+
141+ } else if ( e . target . classList . contains ( 'fa-trash' ) ) {
138142 let idTrash = e . target . parentNode . parentNode . dataset . id ;
143+ let product = getProductDetails ( Number ( idTrash ) ) ;
144+
145+ product . stock ++ ;
146+ document . querySelector ( `.stock-${ idTrash } ` ) . innerHTML = product . stock ;
147+
148+ let getItem = JSON . parse ( localStorage . getItem ( `inCart-${ idTrash } ` ) ) ;
149+
150+ if ( getItem . productsInCart <= 1 ) {
151+ localStorage . removeItem ( `inCart-${ idTrash } ` ) ;
152+ document . querySelector ( `.${ e . target . parentNode . parentNode . classList [ 0 ] } ` ) . remove ( ) ;
153+ } else {
154+ getItem . productsInCart -- ;
155+ localStorage . setItem ( `inCart-${ idTrash } ` , JSON . stringify ( getItem ) ) ;
156+ document . querySelector ( `.${ product . slug } -inCart` ) . innerHTML = getItem . productsInCart ;
157+ }
158+
139159
140- localStorage . removeItem ( `inCart-${ idTrash } ` ) ;
141- document . querySelector ( `.${ e . target . parentNode . parentNode . classList [ 0 ] } ` ) . remove ( ) ;
142160 }
143161} ) ;
0 commit comments