Skip to content

Commit 1f8e3ac

Browse files
author
Artem
committed
Revert "Rewrite sanitize function for more sophisticated error handling"
This reverts commit db638ff.
1 parent 6612d17 commit 1f8e3ac

File tree

5 files changed

+52
-47
lines changed

5 files changed

+52
-47
lines changed

demo/peacock/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
"main": "index.js",
66
"scripts": {
77
"lint": "eslint --ext .js --ext .jsx .",
8-
"fix-lint": "eslint --ext .js --ext .jsx . --fix",
98
"flow": "flow check",
109
"dev": "gulp dev --optimize_for_size --stack_size=4096",
1110
"test": "node_modules/mocha/bin/mocha",

demo/peacock/src/components/cart/cart.jsx

+30-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import * as tracking from 'lib/analytics';
1111
import localized from 'lib/i18n';
1212
import { emailIsSet } from 'paragons/auth';
1313
import sanitizeAll from 'sanitizers';
14-
import sanitizeLineItems from 'sanitizers/line-items';
1514

1615
// actions
1716
import * as actions from 'modules/cart';
@@ -50,7 +49,7 @@ type Props = {
5049
applePayAvailable: boolean,
5150
checkApplePay: () => void,
5251
location: Object,
53-
beginApplePay: (paymentRequest: Object) => Promise<*>,
52+
beginApplePay: (paymentRequest: Object, lineItems: Object) => Promise<*>,
5453
};
5554

5655
type State = {
@@ -147,9 +146,29 @@ class Cart extends Component {
147146

148147
@autobind
149148
sanitize(err) {
150-
const sanitizedLineItems = sanitizeLineItems(err, this.props.skus);
149+
if (/Following SKUs are out/.test(err)) {
150+
const skus = err.split('.')[0].split(':')[1].split(',');
151151

152-
return sanitizedLineItems ? sanitizedLineItems : sanitizeAll(err);
152+
const products = _.reduce(skus, (acc, outOfStock) => {
153+
const sku = _.find(this.props.skus, { sku: outOfStock.trim() });
154+
if (sku) {
155+
return [
156+
...acc,
157+
sku.name,
158+
];
159+
}
160+
161+
return acc;
162+
}, []);
163+
164+
return (
165+
<span>
166+
Products <strong>{products.join(', ')}</strong> are out of stock. Please remove them to complete the checkout.
167+
</span>
168+
);
169+
}
170+
171+
return sanitizeAll(err);
153172
}
154173

155174
get errorsLine() {
@@ -177,7 +196,7 @@ class Cart extends Component {
177196

178197
@autobind
179198
beginApplePay() {
180-
const { total } = this.props.totals;
199+
const { total, taxes, adjustments } = this.props.totals;
181200
const amount = (parseFloat(total) / 100).toFixed(2);
182201
const paymentRequest = {
183202
countryCode: 'US',
@@ -197,7 +216,12 @@ class Cart extends Component {
197216
],
198217
};
199218

200-
this.props.beginApplePay(paymentRequest).then(() => {
219+
const lineItems = {
220+
taxes,
221+
promotion: adjustments,
222+
};
223+
224+
this.props.beginApplePay(paymentRequest, lineItems).then(() => {
201225
this.setState({ errors: null });
202226
browserHistory.push('/checkout/done');
203227
})

demo/peacock/src/modules/checkout.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,10 @@ export const checkApplePay = _checkApplePay.perform;
122122

123123
const _beginApplePay = createAsyncActions(
124124
'beginApplePay',
125-
function(paymentRequest) {
125+
function(paymentRequest, lineItems) {
126126
const { dispatch } = this;
127127

128-
return foxApi.applePay.beginApplePay(paymentRequest)
128+
return foxApi.applePay.beginApplePay(paymentRequest, lineItems)
129129
.then((res) => {
130130
tracking.purchase({
131131
...res,

demo/peacock/src/pages/checkout/checkout.jsx

+20-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { browserHistory } from 'lib/history';
1010
import * as tracking from 'lib/analytics';
1111
import { emailIsSet, isGuest } from 'paragons/auth';
1212
import classNames from 'classnames';
13-
import sanitizeLineItems from 'sanitizers/line-items';
1413

1514
// actions
1615
import * as actions from 'modules/checkout';
@@ -118,9 +117,26 @@ class Checkout extends Component {
118117

119118
@autobind
120119
sanitizeError(error) {
121-
const sanitizedLineItems = sanitizeLineItems(error, this.props.cart.skus);
122-
if (sanitizedLineItems) {
123-
return sanitizedLineItems;
120+
if (/Following SKUs are out/.test(error)) {
121+
const skus = error.split('.')[0].split(':')[1].split(',');
122+
123+
const products = _.reduce(skus, (acc, outOfStock) => {
124+
const sku = _.find(this.props.cart.skus, { sku: outOfStock.trim() });
125+
if (sku) {
126+
return [
127+
...acc,
128+
sku.name,
129+
];
130+
}
131+
132+
return acc;
133+
}, []);
134+
135+
return (
136+
<span>
137+
Products <strong>{products.join(', ')}</strong> are out of stock. Please remove them to complete the checkout.
138+
</span>
139+
);
124140
} else if (/is blacklisted/.test(error)) {
125141
return 'Your account has been blocked from making purchases on this site';
126142
}

demo/peacock/src/sanitizers/line-items.js

-34
This file was deleted.

0 commit comments

Comments
 (0)