@nuskin/ns-shop 5.14.1 → 5.14.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/cart/cart.js +22 -11
- package/src/cart/cartService.js +1 -1
- package/src/cart/persistentCartService.js +207 -114
- package/src/checkout/checkoutApi.js +3 -3
package/package.json
CHANGED
package/src/cart/cart.js
CHANGED
|
@@ -58,6 +58,7 @@ export default function Cart(cartData) {
|
|
|
58
58
|
|
|
59
59
|
let cntryCd = '';
|
|
60
60
|
let userId = '';
|
|
61
|
+
let type = 'market';
|
|
61
62
|
let scanCards = [];
|
|
62
63
|
let signupInfo = {
|
|
63
64
|
checkForSignup: true,
|
|
@@ -94,6 +95,7 @@ export default function Cart(cartData) {
|
|
|
94
95
|
"version": version,
|
|
95
96
|
"userId": userId,
|
|
96
97
|
"cntryCd": cntryCd,
|
|
98
|
+
"type": type,
|
|
97
99
|
"summerPromo": summerPromo,
|
|
98
100
|
"payForShippingWithPoints": payForShippingWithPoints,
|
|
99
101
|
"isGift": isGift,
|
|
@@ -496,7 +498,7 @@ export default function Cart(cartData) {
|
|
|
496
498
|
*/
|
|
497
499
|
this.removeItems = function(options) {
|
|
498
500
|
if (options.all) {
|
|
499
|
-
removeAllItems();
|
|
501
|
+
removeAllItems(options);
|
|
500
502
|
} else {
|
|
501
503
|
if (options.cartItems || options.cartOrderItems || options.cartAdrItems) {
|
|
502
504
|
removeCartItems(options);
|
|
@@ -902,11 +904,11 @@ export default function Cart(cartData) {
|
|
|
902
904
|
}
|
|
903
905
|
|
|
904
906
|
// Clears the whole cart
|
|
905
|
-
function removeAllItems() {
|
|
907
|
+
function removeAllItems(options) {
|
|
906
908
|
itemTable.clear();
|
|
907
909
|
trimScanCards(0);
|
|
908
910
|
updateCartTotals();
|
|
909
|
-
publish(events.shop.CART_UPDATED, {removeAll: true});
|
|
911
|
+
publish(events.shop.CART_UPDATED, {removeAll: true, removeCart: options.removeCart === true});
|
|
910
912
|
}
|
|
911
913
|
|
|
912
914
|
/**
|
|
@@ -914,16 +916,12 @@ export default function Cart(cartData) {
|
|
|
914
916
|
* @param options
|
|
915
917
|
*/
|
|
916
918
|
function removeCartItems(options) {
|
|
917
|
-
const updateInfo = {};
|
|
919
|
+
const updateInfo = {removeCart: options.removeCart === true};
|
|
918
920
|
const items = getCartItems(options);
|
|
919
|
-
const allCartItems = getAllCartItems();
|
|
920
921
|
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
} else {
|
|
925
|
-
updateInfo.removeAll = true;
|
|
926
|
-
}
|
|
922
|
+
updateInfo.removeItems = {
|
|
923
|
+
items: items.map((item) => ({id: item.id, qty: item.qty}))
|
|
924
|
+
};
|
|
927
925
|
|
|
928
926
|
removeItemsFromItemTable(items);
|
|
929
927
|
if (options.cartItems || options.cartAdrItems) {
|
|
@@ -1302,6 +1300,7 @@ export default function Cart(cartData) {
|
|
|
1302
1300
|
}
|
|
1303
1301
|
json.pcId = id;
|
|
1304
1302
|
json.pcVersion = version;
|
|
1303
|
+
json.pcType = type;
|
|
1305
1304
|
}
|
|
1306
1305
|
events.publish(event, json);
|
|
1307
1306
|
}
|
|
@@ -1368,6 +1367,7 @@ export default function Cart(cartData) {
|
|
|
1368
1367
|
return {
|
|
1369
1368
|
id: id,
|
|
1370
1369
|
version: version,
|
|
1370
|
+
type: type,
|
|
1371
1371
|
qty: scope.getItemCnt(options),
|
|
1372
1372
|
totalOrderPrice: totalOrderPrice,
|
|
1373
1373
|
totalAdrPrice: totalAdrPrice,
|
|
@@ -1489,6 +1489,9 @@ export default function Cart(cartData) {
|
|
|
1489
1489
|
cntryCd = cartData.cntryCd;
|
|
1490
1490
|
currencyCode = CurrencyService.getCurrency(cntryCd).currencyCode;
|
|
1491
1491
|
}
|
|
1492
|
+
if (cartData.type) {
|
|
1493
|
+
type = cartData.type;
|
|
1494
|
+
}
|
|
1492
1495
|
if (cartData.userId) {
|
|
1493
1496
|
userId = cartData.userId;
|
|
1494
1497
|
}
|
|
@@ -1582,6 +1585,14 @@ export default function Cart(cartData) {
|
|
|
1582
1585
|
version = _version;
|
|
1583
1586
|
}
|
|
1584
1587
|
},
|
|
1588
|
+
type: {
|
|
1589
|
+
get: function() {
|
|
1590
|
+
return type;
|
|
1591
|
+
},
|
|
1592
|
+
set: function(_type) {
|
|
1593
|
+
type = _type;
|
|
1594
|
+
}
|
|
1595
|
+
},
|
|
1585
1596
|
payForShippingWithPoints: {
|
|
1586
1597
|
get: function() {
|
|
1587
1598
|
return payForShippingWithPoints;
|
package/src/cart/cartService.js
CHANGED
|
@@ -144,7 +144,7 @@ function setCart(cart, updateTimestamp = true) {
|
|
|
144
144
|
cart.timestamp = updateTimestamp ? Date.now() : cart.timestamp;
|
|
145
145
|
storage.setItem({...CART_STORAGE_KEY, key: _getCartStorageName(cart.cntryCd)}, cart);
|
|
146
146
|
} else {
|
|
147
|
-
cart = new Cart({cntryCd: RunConfigService.getRunConfig().country, userId: ''});
|
|
147
|
+
cart = new Cart({cntryCd: RunConfigService.getRunConfig().country, userId: '', type: _cart_.type});
|
|
148
148
|
storage.setItem({...CART_STORAGE_KEY, key: _getCartStorageName(cart.cntryCd)}, cart);
|
|
149
149
|
}
|
|
150
150
|
}
|
|
@@ -6,8 +6,6 @@ import {get, forEach} from 'lodash';
|
|
|
6
6
|
import axios from 'axios';
|
|
7
7
|
|
|
8
8
|
const ADD = 'add';
|
|
9
|
-
const REMOVE = 'remove';
|
|
10
|
-
const REMOVE_CART = 'remove-cart';
|
|
11
9
|
const MERGE_PERSISTED_CART= 'merge persisted cart';
|
|
12
10
|
|
|
13
11
|
let timerId = null;
|
|
@@ -24,28 +22,42 @@ class PcActions {
|
|
|
24
22
|
|
|
25
23
|
this.pcId = this.q.length > 0 ? this.q[0].pcId : 0;
|
|
26
24
|
this.pcVersion = this.q.length > 0 ? this.q[0].pcVersion : 0;
|
|
25
|
+
this.pcType = this.q.length > 0 ? this.q[0].pcType : 0;
|
|
27
26
|
}
|
|
28
27
|
|
|
29
28
|
hasItems() {
|
|
30
29
|
return this.q.length > 0;
|
|
31
30
|
}
|
|
32
31
|
|
|
33
|
-
|
|
32
|
+
hasRemoveCartItem() {
|
|
34
33
|
let retVal = false;
|
|
35
34
|
|
|
36
35
|
this.q.forEach((action) => {
|
|
37
|
-
retVal = retVal || action.
|
|
36
|
+
retVal = retVal || action.removeCart;
|
|
38
37
|
});
|
|
39
38
|
|
|
40
39
|
return retVal;
|
|
41
40
|
}
|
|
42
41
|
|
|
42
|
+
hasLoadCartItem() {
|
|
43
|
+
let type = '';
|
|
44
|
+
|
|
45
|
+
this.q.forEach((action) => {
|
|
46
|
+
if (!type && action.loadCart) {
|
|
47
|
+
type = action.loadCart.type;
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
return type;
|
|
52
|
+
}
|
|
53
|
+
|
|
43
54
|
setInStorage() {
|
|
44
55
|
if (this.q.length > 0) {
|
|
45
56
|
storage.setItem(this.PC_ACTIONS, this.q);
|
|
46
57
|
} else {
|
|
47
58
|
this.pcId = null;
|
|
48
59
|
this.pcVersion = null;
|
|
60
|
+
this.pcType = null;
|
|
49
61
|
storage.removeItem(this.PC_ACTIONS);
|
|
50
62
|
}
|
|
51
63
|
}
|
|
@@ -156,9 +168,10 @@ class PcActions {
|
|
|
156
168
|
this.setInStorage();
|
|
157
169
|
}
|
|
158
170
|
|
|
159
|
-
setPcInfo(pcId, pcVersion) {
|
|
171
|
+
setPcInfo(pcId, pcVersion, pcType) {
|
|
160
172
|
this.pcId = pcId;
|
|
161
173
|
this.pcVersion = pcVersion;
|
|
174
|
+
this.pcType = pcType;
|
|
162
175
|
}
|
|
163
176
|
|
|
164
177
|
/**
|
|
@@ -170,7 +183,7 @@ class PcActions {
|
|
|
170
183
|
push(cartAction) {
|
|
171
184
|
let merged = false;
|
|
172
185
|
|
|
173
|
-
if (cartAction.
|
|
186
|
+
if (cartAction.removeCart || cartAction.loadCart) {
|
|
174
187
|
this.clear();
|
|
175
188
|
}
|
|
176
189
|
|
|
@@ -186,6 +199,7 @@ class PcActions {
|
|
|
186
199
|
}
|
|
187
200
|
this.pcId = cartAction.pcId;
|
|
188
201
|
this.pcVersion = cartAction.pcVersion;
|
|
202
|
+
this.pcType = cartAction.pcType;
|
|
189
203
|
this.setInStorage();
|
|
190
204
|
}
|
|
191
205
|
}
|
|
@@ -224,7 +238,7 @@ const getUrl = (path, pathParam, queryParams = {}) => {
|
|
|
224
238
|
const queryStr = UrlService.toQueryString(queryParams);
|
|
225
239
|
const queryString = queryStr.length > 0 ? `?${queryStr}` : '';
|
|
226
240
|
|
|
227
|
-
return `${ConfigService.getMarketConfig().awsUrl}/cart/
|
|
241
|
+
return `${ConfigService.getMarketConfig().awsUrl}/cart/v2/${path}/${pathParam}${queryString}`;
|
|
228
242
|
};
|
|
229
243
|
|
|
230
244
|
/**
|
|
@@ -233,14 +247,29 @@ const getUrl = (path, pathParam, queryParams = {}) => {
|
|
|
233
247
|
* @param user
|
|
234
248
|
* @return {Promise<void>}
|
|
235
249
|
*/
|
|
236
|
-
const getPersistedCart = async (user) => {
|
|
250
|
+
const getPersistedCart = async (user, type) => {
|
|
237
251
|
const runConfig = RunConfigService.getRunConfig();
|
|
252
|
+
const lsCartType = CartService.getCartProperty('type');
|
|
253
|
+
const lsCartId = CartService.getCartProperty('id');
|
|
254
|
+
|
|
238
255
|
const response = await axios({
|
|
239
|
-
method: '
|
|
240
|
-
|
|
256
|
+
method: 'GET',
|
|
257
|
+
type: type,
|
|
258
|
+
url: getUrl('getCartByAccountId', user.id, {country: runConfig.country, type: type}),
|
|
241
259
|
headers: getHeaders(user.eid)
|
|
242
260
|
});
|
|
243
|
-
|
|
261
|
+
const pCart = response.data.cart;
|
|
262
|
+
|
|
263
|
+
if ((lsCartType != type) || (pCart && lsCartId && pCart.id != lsCartId) || (!pCart && lsCartId)) {
|
|
264
|
+
CartService.clearCart();
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (pCart) {
|
|
268
|
+
CartService.setCartProperties({id: pCart.id, version: response.data.version, type: type});
|
|
269
|
+
} else {
|
|
270
|
+
CartService.setCartProperties({type: type});
|
|
271
|
+
}
|
|
272
|
+
return pCart;
|
|
244
273
|
};
|
|
245
274
|
|
|
246
275
|
/**
|
|
@@ -260,9 +289,32 @@ const updatePersistedCart = async (user, cartId, payload) => {
|
|
|
260
289
|
});
|
|
261
290
|
|
|
262
291
|
const pCart = response.data.cart || null;
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
292
|
+
let cartOutdated = response.data.cartOutdated;
|
|
293
|
+
if (cartId && pCart.id !== cartId) {
|
|
294
|
+
CartService.clearCart();
|
|
295
|
+
cartOutdated = pCart.lineItems.length > 0;
|
|
296
|
+
}
|
|
297
|
+
CartService.setCartProperties({id: pCart.id, version: pCart ? response.data.version : -1});
|
|
298
|
+
return {pCart, cartOutdated: cartOutdated, version: response.data.version, type: response.data.type};
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Removes a persisted cart
|
|
303
|
+
*
|
|
304
|
+
* @param {object} user
|
|
305
|
+
* @param {string} cartId
|
|
306
|
+
*/
|
|
307
|
+
const removePersistedCart = async (user, cartId) => {
|
|
308
|
+
try {
|
|
309
|
+
await axios({
|
|
310
|
+
method: 'DELETE',
|
|
311
|
+
url: getUrl('deleteCart', cartId, {accountId: user.id}),
|
|
312
|
+
headers: getHeaders(user.eid)
|
|
313
|
+
});
|
|
314
|
+
CartService.setCartProperties({id: '', version: -1});
|
|
315
|
+
} catch (err) {
|
|
316
|
+
console.error('Unable to delete persistent cart', err);
|
|
317
|
+
}
|
|
266
318
|
};
|
|
267
319
|
|
|
268
320
|
/**
|
|
@@ -283,7 +335,7 @@ const addPersistedItemToLocal = (pItem, userId, country, language) => {
|
|
|
283
335
|
return CartService.addSkuToCart({
|
|
284
336
|
id: pItem.id,
|
|
285
337
|
agelocme: pItem.agelocme,
|
|
286
|
-
sku:
|
|
338
|
+
sku: pItem.sku,
|
|
287
339
|
userId: userId,
|
|
288
340
|
cntryCd: country,
|
|
289
341
|
language: language,
|
|
@@ -388,12 +440,10 @@ const mergeIntoLocalCart = async (userId, pCart) => {
|
|
|
388
440
|
//Todo review (seems overly complicated)
|
|
389
441
|
mergingIntoLocalCart = true;
|
|
390
442
|
if (pCart) {
|
|
391
|
-
CartService.setCartProperties({id: pCart.id, version: pCart.version});
|
|
392
|
-
|
|
393
443
|
const {country, language} = RunConfigService.getRunConfig();
|
|
394
444
|
const lineItems = pCart.lineItems || [];
|
|
395
445
|
const pItemsBySku = lineItems.reduce((skuArrs, pItem) => {
|
|
396
|
-
let skuArray = skuArrs.find((arr) =>
|
|
446
|
+
let skuArray = skuArrs.find((arr) => arr[0].sku === pItem.sku);
|
|
397
447
|
|
|
398
448
|
if (!skuArray) {
|
|
399
449
|
skuArray = [pItem];
|
|
@@ -411,7 +461,7 @@ const mergeIntoLocalCart = async (userId, pCart) => {
|
|
|
411
461
|
if (pSkuArray.length === 1) {
|
|
412
462
|
const pLineItem = pSkuArray[0];
|
|
413
463
|
// For agelocme the sku will be pLineItem.sku pLineItem.price.custom.fields.sku will be the content sku for agelocme
|
|
414
|
-
const matchedCartItems = cartItems.filter((cItem) => cItem.sku === pLineItem.sku || cItem.sku ===
|
|
464
|
+
const matchedCartItems = cartItems.filter((cItem) => cItem.sku === pLineItem.sku || cItem.sku === pLineItem.sku);
|
|
415
465
|
|
|
416
466
|
if (matchedCartItems.length > 0) {
|
|
417
467
|
promises.push(mergeOneToOnePlus(pLineItem, matchedCartItems));
|
|
@@ -419,7 +469,7 @@ const mergeIntoLocalCart = async (userId, pCart) => {
|
|
|
419
469
|
promises.push(addPersistedItemToLocal(pLineItem, userId, country, language));
|
|
420
470
|
}
|
|
421
471
|
} else {
|
|
422
|
-
const pSku =
|
|
472
|
+
const pSku = pSkuArray[0].sku;
|
|
423
473
|
const cartSkuItems = cartItems.filter((cItem) => cItem.sku === pSkuArray[0].sku || cItem.sku === pSku);
|
|
424
474
|
|
|
425
475
|
if (cartSkuItems.length === 0) {
|
|
@@ -435,7 +485,7 @@ const mergeIntoLocalCart = async (userId, pCart) => {
|
|
|
435
485
|
// Remove local items that do not exist in persisted cart
|
|
436
486
|
cartItems.forEach((cItem) => {
|
|
437
487
|
if (cItem.id) {
|
|
438
|
-
if (!lineItems.some((pItem) => (pItem.sku === cItem.sku ||
|
|
488
|
+
if (!lineItems.some((pItem) => (pItem.sku === cItem.sku || pItem.sku === cItem.sku) && !!pItem.oneTime === !cItem.isAdr)) {
|
|
439
489
|
CartService.removeItemByKey(cItem.key);
|
|
440
490
|
}
|
|
441
491
|
}
|
|
@@ -462,19 +512,9 @@ const getAction = (action, data) => {
|
|
|
462
512
|
gpid: data.globalProductID,
|
|
463
513
|
sku: data.sku,
|
|
464
514
|
quantity: data.quantity || data.qty,
|
|
465
|
-
channel: data.isAdr ? 'subscription' : 'order',
|
|
466
515
|
oneTime: !data.isAdr
|
|
467
516
|
};
|
|
468
517
|
break;
|
|
469
|
-
case REMOVE:
|
|
470
|
-
retVal.product = {
|
|
471
|
-
lineItemId: data.id,
|
|
472
|
-
quantity: data.quantity || data.qty
|
|
473
|
-
};
|
|
474
|
-
break;
|
|
475
|
-
case REMOVE_CART:
|
|
476
|
-
retVal.createNew = UserService.isLoggedIn();
|
|
477
|
-
break;
|
|
478
518
|
default:
|
|
479
519
|
console.error(`Bad update action: ${action}`);
|
|
480
520
|
break;
|
|
@@ -497,6 +537,7 @@ const persistLocalItems = async (cartItems, user) => {
|
|
|
497
537
|
const payload = {
|
|
498
538
|
accountId: user.id,
|
|
499
539
|
country: runConfig.country,
|
|
540
|
+
type: cartInfo.type,
|
|
500
541
|
version: cartInfo.version,
|
|
501
542
|
actions: []
|
|
502
543
|
};
|
|
@@ -517,19 +558,28 @@ const persistLocalItems = async (cartItems, user) => {
|
|
|
517
558
|
*
|
|
518
559
|
* @return {Promise<void>}
|
|
519
560
|
*/
|
|
520
|
-
const syncCart = async () => {
|
|
521
|
-
|
|
561
|
+
const syncCart = async (type) => {
|
|
562
|
+
let user = UserService.getUser();
|
|
563
|
+
let pCart = null;
|
|
522
564
|
|
|
523
|
-
if (user) {
|
|
565
|
+
if (user && type) {
|
|
524
566
|
try {
|
|
525
|
-
|
|
526
|
-
const unPersistedItems = CartService.getItemData().filter(i => !i.id && !i.isBusinessPortfolio);
|
|
567
|
+
pCart = await getPersistedCart(user, type);
|
|
527
568
|
|
|
528
|
-
|
|
529
|
-
|
|
569
|
+
// there is a chance the user got logged out
|
|
570
|
+
user = UserService.getUser();
|
|
571
|
+
if (user) {
|
|
572
|
+
await mergeIntoLocalCart(user.id, pCart);
|
|
573
|
+
}
|
|
530
574
|
} catch (err) {
|
|
531
575
|
console.error('Failed to load the persisted cart!', err && err.response && err.response.data ? err.response.data : err);
|
|
532
576
|
}
|
|
577
|
+
if (user) {
|
|
578
|
+
const unPersistedItems = CartService.getItemData().filter(i => !i.id && !i.isBusinessPortfolio);
|
|
579
|
+
await persistLocalItems(unPersistedItems, user);
|
|
580
|
+
} else {
|
|
581
|
+
CartService.clearCart();
|
|
582
|
+
}
|
|
533
583
|
}
|
|
534
584
|
events.setValue(events.shop.CART_SYNCED_ON_LOAD, [true]);
|
|
535
585
|
};
|
|
@@ -543,7 +593,7 @@ const syncCartItems = (pCart) => {
|
|
|
543
593
|
const syncItems = [];
|
|
544
594
|
const cartItems = CartService.getItemData();
|
|
545
595
|
|
|
546
|
-
|
|
596
|
+
pCart.lineItems.forEach((pItem) => {
|
|
547
597
|
const pGpid = pItem.gpid;
|
|
548
598
|
const pSku = pItem.sku;
|
|
549
599
|
const pIsAdr = !pItem.oneTime;
|
|
@@ -564,18 +614,19 @@ const syncCartItems = (pCart) => {
|
|
|
564
614
|
CartService.syncPersistedInfo(syncItems);
|
|
565
615
|
};
|
|
566
616
|
|
|
567
|
-
const doPersistentUpdates = async (actions, pcId, pcVersion) => {
|
|
617
|
+
const doPersistentUpdates = async (actions, pcId, pcVersion, pcType) => {
|
|
568
618
|
try {
|
|
569
619
|
const user = UserService.getUser();
|
|
570
620
|
const payload = {
|
|
571
621
|
accountId: user.id,
|
|
572
622
|
country: RunConfigService.getRunConfig().country,
|
|
623
|
+
type: pcType,
|
|
573
624
|
actions: actions,
|
|
574
625
|
version: pcVersion
|
|
575
626
|
};
|
|
576
627
|
|
|
577
|
-
const {pCart, cartOutdated} = await updatePersistedCart(user, pcId, payload);
|
|
578
|
-
pcActions.setPcInfo(pCart.id,
|
|
628
|
+
const {pCart, cartOutdated, version, type} = await updatePersistedCart(user, pcId, payload);
|
|
629
|
+
pcActions.setPcInfo(pCart.id, version, type);
|
|
579
630
|
if (cartOutdated) {
|
|
580
631
|
await mergeIntoLocalCart(user.id, pCart);
|
|
581
632
|
}
|
|
@@ -585,6 +636,43 @@ const doPersistentUpdates = async (actions, pcId, pcVersion) => {
|
|
|
585
636
|
}
|
|
586
637
|
};
|
|
587
638
|
|
|
639
|
+
const handleAddAction = (addAction, actions) => {
|
|
640
|
+
const item = addAction.item;
|
|
641
|
+
const agelocMe = /[a-zA-Z]/g; // block agelocMe items until they can be persisted properly
|
|
642
|
+
|
|
643
|
+
if (!item.isSapLineItem && !agelocMe.test(item.sku)) {
|
|
644
|
+
actions.push(getAction(ADD, item));
|
|
645
|
+
}
|
|
646
|
+
};
|
|
647
|
+
|
|
648
|
+
const handleModifyAction = (modifyAction, actions) => {
|
|
649
|
+
const {type, item, qtyDiff} = modifyAction;
|
|
650
|
+
|
|
651
|
+
if (type === 'quantity-changed') {
|
|
652
|
+
if (qtyDiff != 0) {
|
|
653
|
+
actions.push(getAction(ADD, Object.assign({}, item, {quantity: qtyDiff})));
|
|
654
|
+
}
|
|
655
|
+
} else if (type === 'adr-status-changed' || type === 'product-changed') {
|
|
656
|
+
if (modifyAction.removeId) {
|
|
657
|
+
actions.push(getAction(ADD, Object.assign({}, item, {quantity: -9999, isAdr: !item.isAdr})));
|
|
658
|
+
}
|
|
659
|
+
actions.push(getAction(ADD, Object.assign({}, item, {quantity: qtyDiff || item.qty})));
|
|
660
|
+
}
|
|
661
|
+
};
|
|
662
|
+
|
|
663
|
+
const handleRemoveAction = (removeAction, actions) => {
|
|
664
|
+
const item = removeAction.item;
|
|
665
|
+
|
|
666
|
+
if (item.id) {
|
|
667
|
+
actions.push(getAction(ADD, Object.assign({}, item, {quantity: -item.qty})));
|
|
668
|
+
}
|
|
669
|
+
};
|
|
670
|
+
|
|
671
|
+
const handleRemoveItemsAction = (removeItemsAction, actions) => {
|
|
672
|
+
const removeItems = removeItemsAction.items || [];
|
|
673
|
+
removeItems.forEach((item) => actions.push(getAction(ADD, Object.assign({}, item, {quantity: - item.qty}))));
|
|
674
|
+
};
|
|
675
|
+
|
|
588
676
|
/**
|
|
589
677
|
* Updates the persisted cart based on the CART_UPDATE events that are in
|
|
590
678
|
* the action queue
|
|
@@ -593,63 +681,40 @@ const doPersistentUpdates = async (actions, pcId, pcVersion) => {
|
|
|
593
681
|
*/
|
|
594
682
|
const persistCartUpdates = async () => {
|
|
595
683
|
persistingCart = true;
|
|
596
|
-
let pcId = pcActions
|
|
597
|
-
let pcVersion = pcActions.pcVersion;
|
|
684
|
+
let {pcId, pcVersion, pcType} = pcActions;
|
|
598
685
|
let actions = [];
|
|
599
|
-
let
|
|
686
|
+
let removeCart = false;
|
|
687
|
+
let loadCartType = null;
|
|
600
688
|
|
|
601
689
|
if (pcActions.hasItems()) {
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
690
|
+
removeCart = pcActions.hasRemoveCartItem();
|
|
691
|
+
loadCartType = pcActions.hasLoadCartItem();
|
|
692
|
+
if (!(removeCart || loadCartType)) {
|
|
693
|
+
let info = pcActions.peek();
|
|
694
|
+
while (info) {
|
|
695
|
+
if (info.add && info.add.item.qty > 0) {
|
|
696
|
+
handleAddAction(info.add, actions);
|
|
697
|
+
} else if (info.modify) {
|
|
698
|
+
handleModifyAction(info.modify, actions);
|
|
699
|
+
} else if (info.remove) {
|
|
700
|
+
handleRemoveAction(info.remove, actions);
|
|
701
|
+
} else if (info.removeItems) {
|
|
702
|
+
handleRemoveItemsAction(info.removeItems, actions);
|
|
610
703
|
}
|
|
611
|
-
|
|
612
|
-
const {type, item, qtyDiff} = info.modify;
|
|
613
|
-
|
|
614
|
-
if (type === 'quantity-changed') {
|
|
615
|
-
if (qtyDiff > 0) {
|
|
616
|
-
actions.push(getAction(ADD, Object.assign({}, item, {quantity: qtyDiff})));
|
|
617
|
-
} else {
|
|
618
|
-
if (item.id) {
|
|
619
|
-
actions.push(getAction(REMOVE, {id: item.id, quantity: -qtyDiff}));
|
|
620
|
-
}
|
|
621
|
-
}
|
|
622
|
-
} else if (type === 'adr-status-changed' || type === 'product-changed') {
|
|
623
|
-
if (info.modify.removeId) {
|
|
624
|
-
actions.push(getAction(REMOVE, {id: info.modify.removeId, quantity: item.qty}));
|
|
625
|
-
}
|
|
626
|
-
actions.push(getAction(ADD, Object.assign({}, item, {quantity: qtyDiff || item.qty})));
|
|
627
|
-
}
|
|
628
|
-
} else if (info.remove) {
|
|
629
|
-
const item = info.remove.item;
|
|
630
|
-
|
|
631
|
-
if (item.id) {
|
|
632
|
-
actions.push(getAction(REMOVE, item));
|
|
633
|
-
}
|
|
634
|
-
} else if (info.removeItems) {
|
|
635
|
-
const removeItems = info.removeItemInfo || [];
|
|
636
|
-
removeItems.forEach((item) => actions.push(getAction(REMOVE, item)));
|
|
637
|
-
} else if (info.removeAll) {
|
|
638
|
-
if (actions.length > 0) {
|
|
639
|
-
pcActions.clear(actions.length)
|
|
640
|
-
}
|
|
641
|
-
await doPersistentUpdates([getAction(REMOVE_CART)], pcId, pcVersion);
|
|
642
|
-
pcId = pcActions.pcId;
|
|
643
|
-
pcVersion = pcActions.pcVersion;
|
|
644
|
-
actions = [];
|
|
704
|
+
info = pcActions.peekNext();
|
|
645
705
|
}
|
|
646
|
-
info = pcActions.peekNext();
|
|
647
706
|
}
|
|
648
707
|
}
|
|
649
|
-
pcActions.clear(
|
|
708
|
+
pcActions.clear();
|
|
650
709
|
|
|
651
|
-
if (
|
|
652
|
-
await
|
|
710
|
+
if (removeCart) {
|
|
711
|
+
await removePersistedCart(UserService.getUser(), pcId);
|
|
712
|
+
}
|
|
713
|
+
if (loadCartType) {
|
|
714
|
+
await syncCart(loadCartType);
|
|
715
|
+
}
|
|
716
|
+
if (!(removeCart || loadCartType) && actions.length > 0) {
|
|
717
|
+
await doPersistentUpdates(actions, pcId, pcVersion, pcType);
|
|
653
718
|
}
|
|
654
719
|
if (pcActions.hasItems()) {
|
|
655
720
|
clearTimeout(timerId);
|
|
@@ -663,6 +728,45 @@ const timerCallback = () => {
|
|
|
663
728
|
persistCartUpdates();
|
|
664
729
|
};
|
|
665
730
|
|
|
731
|
+
const addAction = (info) => {
|
|
732
|
+
const user = UserService.getUser();
|
|
733
|
+
|
|
734
|
+
// If the code is currently merging the persistent cart into the local cart, cart update
|
|
735
|
+
// actions are being generated and those need to be ignored. A loadCart action is not is
|
|
736
|
+
// generated elsewhere and should be let through.
|
|
737
|
+
if (user && (info.loadCart || !mergingIntoLocalCart)) {
|
|
738
|
+
if (info.add && info.add.item && !info.add.item.isBusinessPortfolio ||
|
|
739
|
+
info.modify || info.remove || info.removeItems || info.removeCart || info.loadCart) {
|
|
740
|
+
pcActions.push(info);
|
|
741
|
+
clearTimeout(timerId);
|
|
742
|
+
timerId = 0;
|
|
743
|
+
if (!persistingCart) {
|
|
744
|
+
if (info.removeCart) {
|
|
745
|
+
// Likely a page refresh coming, wait for it to happen.
|
|
746
|
+
// The cart actions will be picked up there.
|
|
747
|
+
timerId = setTimeout(timerCallback, 1000);
|
|
748
|
+
} else if (info.loadCart) {
|
|
749
|
+
timerId = setTimeout(timerCallback, 0);
|
|
750
|
+
} else {
|
|
751
|
+
timerId = setTimeout(timerCallback, 200);
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
};
|
|
757
|
+
|
|
758
|
+
const loadPersistedCart = (type) => {
|
|
759
|
+
const lsType = CartService.getCartProperty('type');
|
|
760
|
+
if (type != lsType) {
|
|
761
|
+
if (!documentReady) {
|
|
762
|
+
CartService.clearCart();
|
|
763
|
+
CartService.setCartProperties({type: type});
|
|
764
|
+
} else {
|
|
765
|
+
addAction({loadCart: {type: type}});
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
};
|
|
769
|
+
|
|
666
770
|
/**
|
|
667
771
|
* Initializes the CART_UPDATED listener and the LOGIN listener.
|
|
668
772
|
*/
|
|
@@ -671,30 +775,13 @@ const initListeners = () => {
|
|
|
671
775
|
// listener for CART_UPDATED events. As they come in they will get added
|
|
672
776
|
// to a queue with a deloy of 200 ms before processing so if multiple actions
|
|
673
777
|
// come in quickly they can be merged or just collected for processing.
|
|
674
|
-
events.subscribe(events.shop.CART_UPDATED,
|
|
675
|
-
const user = UserService.getUser();
|
|
676
|
-
|
|
677
|
-
if (!mergingIntoLocalCart && user) {
|
|
678
|
-
if (info.add && info.add.item && !info.add.item.isBusinessPortfolio || info.modify || info.remove || info.removeItems || info.removeAll) {
|
|
679
|
-
pcActions.push(info);
|
|
680
|
-
clearTimeout(timerId);
|
|
681
|
-
timerId = 0;
|
|
682
|
-
if (!persistingCart) {
|
|
683
|
-
if (pcActions.hasRemoveAllItem()) {
|
|
684
|
-
// Likely a page refresh coming, wait for it to happen.
|
|
685
|
-
// The cart actions will be picked up there.
|
|
686
|
-
timerId = setTimeout(timerCallback, 1000);
|
|
687
|
-
} else {
|
|
688
|
-
timerId = setTimeout(timerCallback, 200);
|
|
689
|
-
}
|
|
690
|
-
}
|
|
691
|
-
}
|
|
692
|
-
}
|
|
693
|
-
});
|
|
778
|
+
events.subscribe(events.shop.CART_UPDATED, addAction);
|
|
694
779
|
|
|
695
780
|
events.subscribe(events.authentication.LOGIN, function() {
|
|
696
781
|
syncCart();
|
|
697
782
|
});
|
|
783
|
+
|
|
784
|
+
events.subscribe('get-persisted-cart-type', loadPersistedCart)
|
|
698
785
|
};
|
|
699
786
|
|
|
700
787
|
/**
|
|
@@ -708,29 +795,35 @@ const initPersistentCartService = async () => {
|
|
|
708
795
|
!window.location.pathname.startsWith("/content/login/corporate")) {
|
|
709
796
|
initListeners();
|
|
710
797
|
if (window.location.pathname != '/static/checkout/checkout.html') {
|
|
711
|
-
//
|
|
798
|
+
// Persist changed that may not have gotten persisted before page refress
|
|
712
799
|
// like ADR changes that redirect before persisted cart gets updated.
|
|
713
800
|
await persistCartUpdates();
|
|
714
801
|
|
|
715
802
|
// now sync persisted cart into local.
|
|
716
|
-
await syncCart();
|
|
803
|
+
await syncCart(CartService.getCartProperty('type'));
|
|
717
804
|
}
|
|
718
805
|
} else {
|
|
719
806
|
events.setValue(events.shop.CART_SYNCED_ON_LOAD, [true]);
|
|
720
807
|
}
|
|
721
808
|
};
|
|
722
809
|
|
|
723
|
-
|
|
724
810
|
// This check is so persistent cart does not get initialized on a page
|
|
725
811
|
// more than once. This currently happens in profile. The side affect here
|
|
726
812
|
// is when an item gets added to the cart from the favorites page the separate
|
|
727
813
|
// instances of persistent cart take turns persisting the item going from a
|
|
728
814
|
// quantity of one to 900+ fairly quickly.
|
|
729
815
|
// TODO: this check can be removed once profile is a static app.
|
|
816
|
+
let documentReady = false;
|
|
730
817
|
if (window.nsPersistentCartInitialized !== true) {
|
|
731
818
|
window.nsPersistentCartInitialized = true;
|
|
732
819
|
// Wait for page to load and them initialize the service
|
|
733
820
|
$(document).ready(async function() {
|
|
821
|
+
documentReady = true;
|
|
734
822
|
await initPersistentCartService();
|
|
735
823
|
});
|
|
736
824
|
}
|
|
825
|
+
|
|
826
|
+
|
|
827
|
+
export default {
|
|
828
|
+
loadPersistedCart
|
|
829
|
+
}
|
|
@@ -34,11 +34,11 @@ function cleanupOrder() {
|
|
|
34
34
|
}
|
|
35
35
|
_cleanupPersonalOfferCheckout();
|
|
36
36
|
if (order.adr && adr.shipImmediate) {
|
|
37
|
-
CartService.removeItems({all: true});
|
|
37
|
+
CartService.removeItems({all: true, removeCart: !nextOrder});
|
|
38
38
|
} else if (order.adr) {
|
|
39
|
-
CartService.removeItems({cartAdrItems: true, sapAdrItems: true});
|
|
39
|
+
CartService.removeItems({cartAdrItems: true, sapAdrItems: true, removeCart: !nextOrder});
|
|
40
40
|
} else {
|
|
41
|
-
CartService.removeItems({cartOrderItems: true, sapOrderItems: true});
|
|
41
|
+
CartService.removeItems({cartOrderItems: true, sapOrderItems: true, removeCart: !nextOrder});
|
|
42
42
|
}
|
|
43
43
|
// First order for this user has been placed now cleanup the cart
|
|
44
44
|
CartService.setCartProperties({summerPromo: false, inSignup: false});
|