Skip to content

Commit eb067df

Browse files
TuVanDevglo17720
authored andcommitted
Update formating for captcha.js in the SalesRule module to ensure code consistency
1 parent 91cb4d4 commit eb067df

File tree

1 file changed

+55
-54
lines changed
  • app/code/Magento/SalesRule/view/frontend/web/js/view/payment

1 file changed

+55
-54
lines changed

app/code/Magento/SalesRule/view/frontend/web/js/view/payment/captcha.js

Lines changed: 55 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,65 +4,66 @@
44
*/
55

66
define([
7-
'Magento_Captcha/js/view/checkout/defaultCaptcha',
8-
'Magento_Captcha/js/model/captchaList',
9-
'Magento_SalesRule/js/action/set-coupon-code',
10-
'Magento_SalesRule/js/action/cancel-coupon',
11-
'Magento_Checkout/js/model/quote',
12-
'ko'
13-
],
14-
function (defaultCaptcha, captchaList, setCouponCodeAction, cancelCouponAction, quote, ko) {
15-
'use strict';
7+
'Magento_Captcha/js/view/checkout/defaultCaptcha',
8+
'Magento_Captcha/js/model/captchaList',
9+
'Magento_SalesRule/js/action/set-coupon-code',
10+
'Magento_SalesRule/js/action/cancel-coupon',
11+
'Magento_Checkout/js/model/quote',
12+
'ko'
13+
], function (defaultCaptcha, captchaList, setCouponCodeAction, cancelCouponAction, quote, ko) {
14+
'use strict';
1615

17-
var totals = quote.getTotals(),
18-
couponCode = ko.observable(null),
19-
isApplied;
16+
var totals = quote.getTotals(),
17+
couponCode = ko.observable(null),
18+
isApplied;
2019

21-
if (totals()) {
22-
couponCode(totals()['coupon_code']);
23-
}
24-
//Captcha can only be required for adding a coupon so we need to know if one was added already.
25-
isApplied = ko.observable(couponCode() != null);
20+
if (totals()) {
21+
couponCode(totals()['coupon_code']);
22+
}
2623

27-
return defaultCaptcha.extend({
28-
/** @inheritdoc */
29-
initialize: function () {
30-
var self = this,
31-
currentCaptcha;
24+
//Captcha can only be required for adding a coupon so we need to know if one was added already.
25+
var couponCodeValue = couponCode();
26+
isApplied = ko.observable(typeof couponCodeValue === 'string' && couponCodeValue.length > 0);
3227

33-
this._super();
34-
//Getting coupon captcha model.
35-
currentCaptcha = captchaList.getCaptchaByFormId(this.formId);
28+
return defaultCaptcha.extend({
29+
/** @inheritdoc */
30+
initialize: function () {
31+
var self = this,
32+
currentCaptcha;
3633

37-
if (currentCaptcha != null) {
38-
if (!isApplied()) {
39-
//Show captcha if we don't have a coupon applied.
40-
currentCaptcha.setIsVisible(true);
41-
}
42-
this.setCurrentCaptcha(currentCaptcha);
43-
//Add captcha code to coupon-apply request.
44-
setCouponCodeAction.registerDataModifier(function (headers) {
45-
if (self.isRequired()) {
46-
headers['X-Captcha'] = self.captchaValue()();
47-
}
48-
});
49-
//Refresh captcha after failed request.
50-
setCouponCodeAction.registerFailCallback(function () {
51-
if (self.isRequired()) {
52-
self.refresh();
53-
}
54-
});
55-
//Hide captcha when a coupon has been applied.
56-
setCouponCodeAction.registerSuccessCallback(function () {
57-
self.setIsVisible(false);
58-
});
59-
//Show captcha again if it was canceled.
60-
cancelCouponAction.registerSuccessCallback(function () {
61-
if (self.isRequired()) {
62-
self.setIsVisible(true);
63-
}
64-
});
34+
this._super();
35+
//Getting coupon captcha model.
36+
currentCaptcha = captchaList.getCaptchaByFormId(this.formId);
37+
38+
if (currentCaptcha != null) {
39+
if (!isApplied()) {
40+
//Show captcha if we don't have a coupon applied.
41+
currentCaptcha.setIsVisible(true);
6542
}
43+
this.setCurrentCaptcha(currentCaptcha);
44+
//Add captcha code to coupon-apply request.
45+
setCouponCodeAction.registerDataModifier(function (headers) {
46+
if (self.isRequired()) {
47+
headers['X-Captcha'] = self.captchaValue()();
48+
}
49+
});
50+
//Refresh captcha after failed request.
51+
setCouponCodeAction.registerFailCallback(function () {
52+
if (self.isRequired()) {
53+
self.refresh();
54+
}
55+
});
56+
//Hide captcha when a coupon has been applied.
57+
setCouponCodeAction.registerSuccessCallback(function () {
58+
self.setIsVisible(false);
59+
});
60+
//Show captcha again if it was canceled.
61+
cancelCouponAction.registerSuccessCallback(function () {
62+
if (self.isRequired()) {
63+
self.setIsVisible(true);
64+
}
65+
});
6666
}
67-
});
67+
}
6868
});
69+
});

0 commit comments

Comments
 (0)