@@ -69,7 +69,7 @@ var flavours = [
6969 console . log ( '### remove ###' ) ;
7070 window . localStorage . removeItem ( key ) ;
7171 }
72- } ;
72+ } , start_button_producing_text = 'Produce' , start_button_loading_text = 'Please wait...' ;
7373
7474// conversation variables
7575var conversation_result , is_wating = false , controls = {
@@ -145,34 +145,36 @@ var conversation_result, is_wating = false, controls = {
145145 var contextToCommand = function ( conversation_result ) {
146146
147147 var context = conversation_result . context ;
148- if ( context . flavour ) {
149- order . flavour = context . flavour ;
150- controls . $flavour . find ( 'option[data-name="' + order . flavour + '"]' ) . prop ( 'selected' , true ) . trigger ( 'change' ) ;
151- }
148+ if ( context ) {
149+ if ( context . flavour ) {
150+ order . flavour = context . flavour ;
151+ controls . $flavour . find ( 'option[data-name="' + order . flavour + '"]' ) . prop ( 'selected' , true ) . trigger ( 'change' ) ;
152+ }
152153
153- if ( context . amount ) {
154- order . amount = context . amount ;
155- controls . $qty . val ( order . amount ) . trigger ( 'change' ) ;
156- }
154+ if ( context . amount ) {
155+ order . amount = context . amount ;
156+ controls . $qty . val ( order . amount ) . trigger ( 'change' ) ;
157+ }
157158
158- if ( context . location ) {
159- order . location = context . location ;
160- controls . $address . val ( order . location ) . trigger ( 'change' ) ;
161- }
159+ if ( context . location ) {
160+ order . location = context . location ;
161+ controls . $address . val ( order . location ) . trigger ( 'change' ) ;
162+ }
162163
163- if ( context . consignee ) {
164- order . consignee = context . consignee ;
165- controls . $consignee . val ( order . consignee ) . trigger ( 'change' ) ;
166- }
164+ if ( context . consignee ) {
165+ order . consignee = context . consignee ;
166+ controls . $consignee . val ( order . consignee ) . trigger ( 'change' ) ;
167+ }
167168
168- if ( context . reset ) {
169- conversation_result . context . flavour = order . flavour = null ;
170- conversation_result . context . amount = order . amount = 0 ;
171- conversation_result . context . location = order . location = null ;
172- }
169+ if ( context . reset ) {
170+ conversation_result . context . flavour = order . flavour = null ;
171+ conversation_result . context . amount = order . amount = 0 ;
172+ conversation_result . context . location = order . location = null ;
173+ }
173174
174- if ( context . placeorder ) {
175- controls . $start . trigger ( 'click' ) ;
175+ if ( context . placeorder ) {
176+ controls . $start . trigger ( 'click' ) ;
177+ }
176178 }
177179 } ;
178180
@@ -438,6 +440,8 @@ var conversation_result, is_wating = false, controls = {
438440 var total = ( qty * price ) . toFixed ( 2 ) ;
439441 var address = controls . $address . val ( ) ;
440442 var consignee = controls . $consignee . val ( ) ;
443+
444+
441445
442446 var sn = storage . get ( 'SN' ) ;
443447
@@ -454,6 +458,7 @@ var conversation_result, is_wating = false, controls = {
454458 consignee_address : address ,
455459 consignee_phone : '+86 8888888888' ,
456460 timestamp : new Date ( ) . getTime ( ) ,
461+ // typo: should be `environment_limit`
457462 enviorment_limit : {
458463 temperature_low : - 30 ,
459464 temperature_high : 0 ,
@@ -463,6 +468,7 @@ var conversation_result, is_wating = false, controls = {
463468 }
464469 } ;
465470
471+ controls . $start . val ( start_button_producing_text ) ;
466472 controls . $start . on ( 'click' , function ( evt ) {
467473 console . log ( '### submit ###' ) ;
468474 var iceCream = getIceCream ( ) ;
@@ -478,20 +484,27 @@ var conversation_result, is_wating = false, controls = {
478484 controls . $consignee . focus ( ) ;
479485 return null ;
480486 }
487+
488+ if ( controls . $start . val ( ) === start_button_producing_text ) {
489+ controls . $start . val ( start_button_loading_text ) ;
490+
491+ sendRequest ( 'icecream' , JSON . stringify ( iceCream ) ) . then ( function ( xhr , status , code ) {
492+ console . log ( '### service response ###' ) ;
493+ console . log ( xhr ) ;
494+ var json = JSON . parse ( xhr ) ;
495+ console . log ( '### /service response ###' ) ;
496+ var obj = $ ( 'a[href="#vr-panel"]' ) ;
497+ obj . trigger ( 'click' ) ;
498+ controls . $start . val ( start_button_producing_text ) ;
499+
500+ } , function ( error ) {
501+ console . log ( '### error ###' ) ;
502+ console . log ( error ) ;
503+ controls . $start . val ( startText ) ;
504+ console . log ( '### /error ###' ) ;
505+ } ) ;
506+ }
481507
482- sendRequest ( 'icecream' , JSON . stringify ( iceCream ) ) . then ( function ( xhr , status , code ) {
483- console . log ( '### service response ###' ) ;
484- console . log ( xhr ) ;
485- var json = JSON . parse ( xhr ) ;
486- console . log ( '### /service response ###' ) ;
487- var obj = $ ( 'a[href="#vr-panel"]' ) ;
488- obj . trigger ( 'click' ) ;
489-
490- } , function ( error ) {
491- console . log ( '### error ###' ) ;
492- console . log ( error ) ;
493- console . log ( '### /error ###' ) ;
494- } ) ;
495508 } ) ;
496509
497510 controls . $qty . on ( 'change' , function ( evt ) {
@@ -583,14 +596,14 @@ var conversation_result, is_wating = false, controls = {
583596 } ,
584597 init : function ( ) {
585598 var sn = storage . get ( 'SN' ) ;
586- var name = storage . get ( 'NAME' ) ;
587- var phone = storage . get ( 'PHONE' ) ;
599+ consigner_name = storage . get ( 'NAME' ) ;
600+ consigner_phone = storage . get ( 'PHONE' ) ;
588601
589- if ( sn && name && phone ) {
602+ if ( sn && consigner_name && consigner_phone ) {
590603 methods . start ( ) ;
591604 controls . $sn . val ( sn ) ;
592- controls . $name . val ( name ) ;
593- controls . $phone . val ( phone ) ;
605+ controls . $name . val ( consigner_name ) ;
606+ controls . $phone . val ( consigner_phone ) ;
594607 }
595608 else {
596609 methods . end ( ) ;
0 commit comments