Skip to content

Commit db638ff

Browse files
author
Artem
committed
Rewrite sanitize function for more sophisticated error handling
1 parent 4c4238e commit db638ff

File tree

5 files changed

+47
-52
lines changed

5 files changed

+47
-52
lines changed

demo/peacock/package.json

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

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

+6-30
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ 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';
1415

1516
// actions
1617
import * as actions from 'modules/cart';
@@ -49,7 +50,7 @@ type Props = {
4950
applePayAvailable: boolean,
5051
checkApplePay: () => void,
5152
location: Object,
52-
beginApplePay: (paymentRequest: Object, lineItems: Object) => Promise<*>,
53+
beginApplePay: (paymentRequest: Object) => Promise<*>,
5354
};
5455

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

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

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);
152+
return sanitizedLineItems ? sanitizedLineItems : sanitizeAll(err);
172153
}
173154

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

197178
@autobind
198179
beginApplePay() {
199-
const { total, taxes, adjustments } = this.props.totals;
180+
const { total } = this.props.totals;
200181
const amount = (parseFloat(total) / 100).toFixed(2);
201182
const paymentRequest = {
202183
countryCode: 'US',
@@ -216,12 +197,7 @@ class Cart extends Component {
216197
],
217198
};
218199

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

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, lineItems) {
125+
function(paymentRequest) {
126126
const { dispatch } = this;
127127

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

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

+4-20
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ 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';
1314

1415
// actions
1516
import * as actions from 'modules/checkout';
@@ -117,26 +118,9 @@ class Checkout extends Component {
117118

118119
@autobind
119120
sanitizeError(error) {
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-
);
121+
const sanitizedLineItems = sanitizeLineItems(error, this.props.cart.skus);
122+
if (sanitizedLineItems) {
123+
return sanitizedLineItems;
140124
} else if (/is blacklisted/.test(error)) {
141125
return 'Your account has been blocked from making purchases on this site';
142126
}
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* @flow */
2+
import React from 'react';
3+
import _ from 'lodash';
4+
5+
export default function sanitizeLineItems(error: string, lineItems: Array<mixed>) {
6+
if (/Following SKUs are out/.test(error)) {
7+
const skus = error.split('.')[0].split(':')[1].split(',');
8+
9+
const products = _.reduce(skus, (acc, outOfStock) => {
10+
const sku = _.find(lineItems, { sku: outOfStock.trim() });
11+
if (sku) {
12+
return [
13+
...acc,
14+
sku.name,
15+
];
16+
}
17+
18+
return acc;
19+
}, []);
20+
21+
const singleProduct = products.length === 1;
22+
const title = singleProduct ? 'Product' : 'Products';
23+
const verb = singleProduct ? 'is' : 'are';
24+
const pronoun = singleProduct ? 'it' : 'them';
25+
return (
26+
<span>
27+
{title} <strong>{products.join(', ')}</strong> {verb} out of stock.
28+
Please remove {pronoun} to complete the checkout.
29+
</span>
30+
);
31+
}
32+
33+
return null;
34+
}

0 commit comments

Comments
 (0)