@@ -99,12 +99,11 @@ <h1>Shopping cart</h1>
99
99
shopping</ a >
100
100
</ div >
101
101
< div class ="pull-right ">
102
- < a class ="btn btn-default " onclick ="alert('Stub. Todo.' ) "> < i
102
+ < a class ="btn btn-default " onclick ="updateCart( ) "> < i
103
103
class ="fa fa-refresh "> </ i > Update basket
104
104
</ a >
105
105
< button id ="orderButton " type ="submit " class ="btn btn-primary " disabled >
106
- Proceed to checkout < i
107
- class ="fa fa-chevron-right "> </ i >
106
+ Proceed to checkout < i class ="fa fa-chevron-right "> </ i >
108
107
</ button >
109
108
</ div >
110
109
</ div >
@@ -335,6 +334,27 @@ <h4>Coupon code</h4>
335
334
} ) ;
336
335
}
337
336
337
+ function updateCart ( ) {
338
+ console . log ( 'Updating cart.' ) ;
339
+ $ ( "tr.item" ) . each ( function ( ) {
340
+ var id = $ ( this ) . find ( "input.id" ) . val ( ) ,
341
+ quantity = $ ( this ) . find ( "input.form-control" ) . val ( ) ;
342
+ console . log ( "Item nr " + id + " has " + quantity ) ;
343
+ $ . ajax ( {
344
+ url : "cart/" + id ,
345
+ type : "PUT" ,
346
+ data : JSON . stringify ( { "quantity" : quantity } ) ,
347
+ success : function ( data , textStatus , jqXHR ) {
348
+ console . log ( 'Worked!' ) ;
349
+ location . reload ( ) ;
350
+ } ,
351
+ error : function ( jqXHR , textStatus , errorThrown ) {
352
+ console . error ( 'Could not update item: ' + id + ', due to: ' + textStatus + ' | ' + errorThrown ) ;
353
+ }
354
+ } ) ;
355
+ } ) ;
356
+ }
357
+
338
358
$ ( document ) . ready ( function ( ) {
339
359
$ . ajaxSetup ( {
340
360
contentType : "application/json; charset=utf-8"
@@ -349,7 +369,8 @@ <h4>Coupon code</h4>
349
369
if ( element != null && element . itemId != null && element . itemId != "null" ) {
350
370
$ . getJSON ( '/catalogue/' + element . itemId , function ( data ) {
351
371
console . log ( JSON . stringify ( data ) ) ;
352
- $ ( '#cart-list' ) . append ( ' <tr>\
372
+ $ ( '#cart-list' ) . append ( ' <tr class="item">\
373
+ <input class="id" type="hidden" value="' + data . id + '">\
353
374
<td>\
354
375
<a href="#">\
355
376
<img src="' + data . imageUrl [ 0 ] + '" alt="' + data . namex + '">\
@@ -359,7 +380,7 @@ <h4>Coupon code</h4>
359
380
<a href="#">' + data . name + '</a>\
360
381
</td>\
361
382
<td>\
362
- <input type="number" min="0 " value="' + element . quantity + '" class="form-control">\
383
+ <input type="number" min="1 " value="' + element . quantity + '" class="form-control">\
363
384
</td>\
364
385
<td>$' + data . price . toFixed ( 2 ) + '</td>\
365
386
<td>$0.00</td>\
0 commit comments