merchi_cart 1.0.0-test-e → 1.0.0-test-f
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/README.md +1 -0
- package/package.json +1 -1
- package/src/CartProvider.tsx +32 -0
package/README.md
CHANGED
package/package.json
CHANGED
package/src/CartProvider.tsx
CHANGED
|
@@ -5,6 +5,7 @@ import { Alert, Tab } from '../types';
|
|
|
5
5
|
import {
|
|
6
6
|
getCartShipmentGroupsAndQuotes,
|
|
7
7
|
makeCart,
|
|
8
|
+
makeCartItem,
|
|
8
9
|
stripeIsValidAndActive,
|
|
9
10
|
} from './utilities/cart';
|
|
10
11
|
import { cartEmbed } from './utilities/helpers';
|
|
@@ -13,6 +14,8 @@ import { Merchi } from 'merchi_sdk_ts';
|
|
|
13
14
|
import { getCartCookie, getCartCookieToken, setCartCookie } from './utilities/cookie';
|
|
14
15
|
import { makeAddress } from './utilities/address';
|
|
15
16
|
import { appendStyleSheetText } from './utilities/helpers';
|
|
17
|
+
import { makeJob } from './utilities/job';
|
|
18
|
+
import { makeProduct } from './utilities/product';
|
|
16
19
|
|
|
17
20
|
export interface PropsCart {
|
|
18
21
|
cart: any;
|
|
@@ -678,8 +681,37 @@ const CartProvider = ({
|
|
|
678
681
|
|
|
679
682
|
const [loadingTotals, setLoadingTotals] = useState(false);
|
|
680
683
|
|
|
684
|
+
async function addCartItem(
|
|
685
|
+
jobJson: any,
|
|
686
|
+
onSuccess: (cartItem: any) => void,
|
|
687
|
+
onError: (e: any) => void,
|
|
688
|
+
) {
|
|
689
|
+
setFetchingCart(true);
|
|
690
|
+
const jobEnt = makeJob(jobJson, true);
|
|
691
|
+
const cartToken = await getCartCookieToken((domainId as number));
|
|
692
|
+
const cartEnt = makeCart(cart, false, cartToken);
|
|
693
|
+
const cartItemEnt = makeCartItem({}, true);
|
|
694
|
+
const cartItemProduct = makeProduct({ id: (jobEnt.product as any).id });
|
|
695
|
+
cartItemEnt.cart = cartEnt;
|
|
696
|
+
cartItemEnt.quantity = jobEnt.quantity;
|
|
697
|
+
cartItemEnt.product = cartItemProduct;
|
|
698
|
+
cartItemEnt.variations = jobEnt.variations;
|
|
699
|
+
cartItemEnt.variationsGroups = jobEnt.variationsGroups;
|
|
700
|
+
cartItemEnt.taxType = jobEnt.taxType;
|
|
701
|
+
try {
|
|
702
|
+
const item = await cartItemEnt.create();
|
|
703
|
+
const itemJson = await item.toJson();
|
|
704
|
+
onSuccess(itemJson);
|
|
705
|
+
} catch(e: any) {
|
|
706
|
+
onError(e);
|
|
707
|
+
} finally {
|
|
708
|
+
setFetchingCart(false);
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
|
|
681
712
|
// Set a global toggle function for cart wrapper
|
|
682
713
|
if (window && typeof window !== 'undefined') {
|
|
714
|
+
(window as any).addCartItem = addCartItem;
|
|
683
715
|
(window as any).toggleCartOpen = toggleCartModal;
|
|
684
716
|
(window as any).getCart = () => actionGetMerchiCart();
|
|
685
717
|
(window as any).getMerchiCartValues = getMerchiCartValues;
|