3
3
* See COPYING.txt for license details.
4
4
*/
5
5
/*jshint browser:true*/
6
- /*global define,alert */
6
+ /*global define*/
7
7
define (
8
8
[
9
9
"jquery" ,
10
10
'Magento_Ui/js/form/form' ,
11
11
'ko' ,
12
12
'Magento_Customer/js/model/customer' ,
13
13
'../action/select-billing-address' ,
14
- 'Magento_Checkout/js /model/step-navigator' ,
14
+ '.. /model/step-navigator' ,
15
15
'../model/quote' ,
16
16
'../model/addresslist' ,
17
- '../action/check-email-availability' ,
18
- 'mage/validation'
17
+ 'mage/translate'
19
18
] ,
20
- function ( $ , Component , ko , customer , selectBillingAddress , navigator , quote , addressList , checkEmailAvailability ) {
19
+ function ( $ , Component , ko , customer , selectBillingAddress , navigator , quote , addressList , $t ) {
21
20
"use strict" ;
22
21
var stepName = 'billingAddress' ;
23
22
var newAddressSelected = ko . observable ( false ) ;
@@ -35,12 +34,23 @@ define(
35
34
return navigator . getStepClassAttributes ( stepName ) ;
36
35
} ,
37
36
stepNumber : navigator . getStepNumber ( stepName ) ,
38
- billingAddresses : customer . getBillingAddressList ( ) ,
39
- selectedBillingAddressId : addressList . getAddresses ( ) [ 0 ] . customerAddressId ,
37
+ billingAddresses : function ( ) {
38
+ var newAddress = {
39
+ getAddressInline : function ( ) {
40
+ return $t ( 'New address' ) ;
41
+ } ,
42
+ customerAddressId : null
43
+ } ,
44
+ addresses = addressList . getAddresses ( ) ;
45
+ addresses . push ( newAddress ) ;
46
+ return addresses ;
47
+ } ,
48
+ selectedBillingAddressId : ko . observable (
49
+ addressList . getAddresses ( ) . length ? addressList . getAddresses ( ) [ 0 ] . customerAddressId : null
50
+ ) ,
40
51
isVisible : navigator . isStepVisible ( stepName ) ,
41
52
useForShipping : "1" ,
42
53
quoteIsVirtual : quote . isVirtual ( ) ,
43
- isEmailCheckComplete : $ . Deferred ( ) ,
44
54
billingAddressesOptionsText : function ( item ) {
45
55
return item . getAddressInline ( ) ;
46
56
} ,
@@ -53,42 +63,32 @@ define(
53
63
return additionalData ;
54
64
} ,
55
65
submitBillingAddress : function ( ) {
56
- if ( this . selectedBillingAddressId ) {
57
- var additionalData = this . checkUseForShipping ( this . useForShipping ) ;
66
+ var additionalData = this . checkUseForShipping ( this . useForShipping ) ;
67
+ if ( this . selectedBillingAddressId ( ) ) {
58
68
selectBillingAddress (
59
- addressList . getAddressById ( this . selectedBillingAddressId ) ,
69
+ addressList . getAddressById ( this . selectedBillingAddressId ( ) ) ,
60
70
this . useForShipping ,
61
71
additionalData
62
72
) ;
63
73
} else {
64
- var that = this ;
65
74
this . validate ( ) ;
66
- $ . when ( this . isEmailCheckComplete ) . done ( function ( ) {
67
- if ( ! that . source . get ( 'params.invalid' ) ) {
68
- var addressData = that . source . get ( 'billingAddress' ) ;
69
- var additionalData = that . checkUseForShipping ( that . useForShipping ) ;
70
- /**
71
- * All the the input fields that are not a part of the address but need to be submitted
72
- * in the same request must have data-scope attribute set
73
- */
74
- var additionalFields = $ ( 'input[data-scope="additionalAddressData"]' ) . serializeArray ( ) ;
75
- additionalFields . forEach ( function ( field ) {
76
- additionalData [ field . name ] = field . value ;
77
- } ) ;
78
- if ( quote . getCheckoutMethod ( ) ( ) && ! customer . isLoggedIn ( ) ( ) ) {
79
- addressData . email = that . source . get ( 'customerDetails.email' ) ;
80
- }
81
- if ( $ ( billingFormSelector ) . validation ( ) && $ ( billingFormSelector ) . validation ( 'isValid' ) ) {
82
- selectBillingAddress ( addressData , that . useForShipping , additionalData ) ;
83
- }
75
+ if ( ! this . source . get ( 'params.invalid' ) ) {
76
+ var addressData = this . source . get ( 'billingAddress' ) ;
77
+ /**
78
+ * All the the input fields that are not a part of the address (e. g. CAPTCHA) but need to be
79
+ * submitted in the same request must have data-scope attribute set
80
+ */
81
+ var additionalFields = $ ( 'input[data-scope="additionalAddressData"]' ) . serializeArray ( ) ;
82
+ additionalFields . forEach ( function ( field ) {
83
+ additionalData [ field . name ] = field . value ;
84
+ } ) ;
85
+ if ( quote . getCheckoutMethod ( ) ( ) && ! customer . isLoggedIn ( ) ( ) ) {
86
+ addressData . email = this . source . get ( 'customerDetails.email' ) ;
87
+ }
88
+ if ( $ ( billingFormSelector ) . validation ( ) && $ ( billingFormSelector ) . validation ( 'isValid' ) ) {
89
+ selectBillingAddress ( addressData , this . useForShipping , additionalData ) ;
84
90
}
85
- } ) . fail ( function ( ) {
86
- alert (
87
- "There is already a registered customer using this email address. " +
88
- "Please log in using this email address or enter a different email address " +
89
- "to register your account."
90
- ) ;
91
- } ) ;
91
+ }
92
92
}
93
93
} ,
94
94
navigateToCurrentStep : function ( ) {
@@ -103,11 +103,7 @@ define(
103
103
return newAddressSelected ( ) ;
104
104
} ,
105
105
onAddressChange : function ( value ) {
106
- if ( value === null ) {
107
- newAddressSelected ( true ) ;
108
- } else {
109
- newAddressSelected ( false ) ;
110
- }
106
+ value ( ) === null ? newAddressSelected ( true ) : newAddressSelected ( false ) ;
111
107
} ,
112
108
validate : function ( ) {
113
109
var fields = $ ( billingFormSelector ) . find ( 'input, select' ) ;
@@ -116,7 +112,6 @@ define(
116
112
fields . trigger ( 'change' ) ;
117
113
this . source . trigger ( 'billingAddress.data.validate' ) ;
118
114
this . validateAdditionalAddressFields ( ) ;
119
- this . isEmailCheckComplete . resolve ( ) ;
120
115
} ,
121
116
validateAdditionalAddressFields : function ( ) {
122
117
$ ( billingFormSelector ) . validation ( ) ;
0 commit comments