@@ -99,12 +99,11 @@ <h1>Shopping cart</h1>
9999 shopping</ a >
100100 </ div >
101101 < div class ="pull-right ">
102- < a class ="btn btn-default " onclick ="alert('Stub. Todo.' ) "> < i
102+ < a class ="btn btn-default " onclick ="updateCart( ) "> < i
103103 class ="fa fa-refresh "> </ i > Update basket
104104 </ a >
105105 < 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 >
108107 </ button >
109108 </ div >
110109 </ div >
@@ -335,6 +334,27 @@ <h4>Coupon code</h4>
335334 } ) ;
336335 }
337336
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+
338358 $ ( document ) . ready ( function ( ) {
339359 $ . ajaxSetup ( {
340360 contentType : "application/json; charset=utf-8"
@@ -349,7 +369,8 @@ <h4>Coupon code</h4>
349369 if ( element != null && element . itemId != null && element . itemId != "null" ) {
350370 $ . getJSON ( '/catalogue/' + element . itemId , function ( data ) {
351371 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 + '">\
353374 <td>\
354375 <a href="#">\
355376 <img src="' + data . imageUrl [ 0 ] + '" alt="' + data . namex + '">\
@@ -359,7 +380,7 @@ <h4>Coupon code</h4>
359380 <a href="#">' + data . name + '</a>\
360381 </td>\
361382 <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">\
363384 </td>\
364385 <td>$' + data . price . toFixed ( 2 ) + '</td>\
365386 <td>$0.00</td>\
0 commit comments