@cohostvip/cohost-react 0.3.3 → 0.3.5
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/dist/context/CohostCheckoutContext.d.ts.map +1 -1
- package/dist/context/CohostCheckoutContext.js +19 -14
- package/dist/context/CohostContext.d.ts +1 -0
- package/dist/context/CohostContext.d.ts.map +1 -1
- package/dist/context/CohostContext.js +2 -1
- package/dist/context/PaymentElementContext.d.ts +2 -0
- package/dist/context/PaymentElementContext.d.ts.map +1 -1
- package/dist/context/PaymentElementContext.js +36 -6
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CohostCheckoutContext.d.ts","sourceRoot":"","sources":["../../src/context/CohostCheckoutContext.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAG/B,OAAO,KAAK,EAAE,WAAW,EAAE,QAAQ,EAAE,aAAa,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AAEzG,MAAM,MAAM,2BAA2B,GAAG;IACtC,aAAa,EAAE,MAAM,CAAC;IACtB,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;CAC7B,CAAC;AAEF,MAAM,MAAM,yBAAyB,GAAG;IACpC,aAAa,EAAE,MAAM,CAAC;IACtB,WAAW,EAAE,WAAW,GAAG,IAAI,CAAC;IAChC,SAAS,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,OAAO,CAAC;QAAE,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;QAAC,KAAK,CAAC,EAAE;YAAE,OAAO,EAAE,MAAM,CAAC;YAAC,MAAM,CAAC,EAAE,MAAM,CAAA;SAAE,CAAA;KAAE,CAAC,CAAC;IACjH,UAAU,EAAE,CAAC,UAAU,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,GAAG,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAEnF,aAAa,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Aa,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;CACzE,CAAC;AAGF,eAAO,MAAM,qBAAqB,iDAAwD,CAAC;AAE3F,eAAO,MAAM,sBAAsB,EAAE,KAAK,CAAC,EAAE,CAAC,2BAA2B,CAkPxE,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,iBAAiB,QAAO,yBAIpC,CAAC"}
|
|
@@ -4,11 +4,16 @@ import { createContext, useContext, useEffect } from 'react';
|
|
|
4
4
|
import { useCohostClient } from './CohostContext';
|
|
5
5
|
export const CohostCheckoutContext = createContext(null);
|
|
6
6
|
export const CohostCheckoutProvider = ({ cartSessionId, children, }) => {
|
|
7
|
-
const { client } = useCohostClient();
|
|
7
|
+
const { client, debug } = useCohostClient();
|
|
8
8
|
const [cartSession, setCartSession] = React.useState(null);
|
|
9
|
+
const logError = (message, error) => {
|
|
10
|
+
if (debug) {
|
|
11
|
+
console.error(message, error);
|
|
12
|
+
}
|
|
13
|
+
};
|
|
9
14
|
const assertCartSession = () => {
|
|
10
15
|
if (!cartSession) {
|
|
11
|
-
|
|
16
|
+
logError("CohostCheckoutProvider requires a cartSession");
|
|
12
17
|
throw new Error("CohostCheckoutProvider requires a cartSession");
|
|
13
18
|
}
|
|
14
19
|
};
|
|
@@ -19,7 +24,7 @@ export const CohostCheckoutProvider = ({ cartSessionId, children, }) => {
|
|
|
19
24
|
setCartSession(updatedCart);
|
|
20
25
|
}
|
|
21
26
|
catch (error) {
|
|
22
|
-
|
|
27
|
+
logError("Error applying coupon:", error);
|
|
23
28
|
throw error;
|
|
24
29
|
}
|
|
25
30
|
};
|
|
@@ -30,7 +35,7 @@ export const CohostCheckoutProvider = ({ cartSessionId, children, }) => {
|
|
|
30
35
|
setCartSession(updatedCart);
|
|
31
36
|
}
|
|
32
37
|
catch (error) {
|
|
33
|
-
|
|
38
|
+
logError("Error removing coupon:", error);
|
|
34
39
|
throw error;
|
|
35
40
|
}
|
|
36
41
|
};
|
|
@@ -45,7 +50,7 @@ export const CohostCheckoutProvider = ({ cartSessionId, children, }) => {
|
|
|
45
50
|
return { itemId };
|
|
46
51
|
}
|
|
47
52
|
catch (error) {
|
|
48
|
-
|
|
53
|
+
logError("Error joining group:", error);
|
|
49
54
|
return {
|
|
50
55
|
itemId: null,
|
|
51
56
|
error: {
|
|
@@ -62,7 +67,7 @@ export const CohostCheckoutProvider = ({ cartSessionId, children, }) => {
|
|
|
62
67
|
setCartSession(updatedCart);
|
|
63
68
|
}
|
|
64
69
|
catch (error) {
|
|
65
|
-
|
|
70
|
+
logError("Error updating cart item:", error);
|
|
66
71
|
}
|
|
67
72
|
};
|
|
68
73
|
const incrementItem = async (itemId, options) => {
|
|
@@ -81,7 +86,7 @@ export const CohostCheckoutProvider = ({ cartSessionId, children, }) => {
|
|
|
81
86
|
}
|
|
82
87
|
}
|
|
83
88
|
catch (error) {
|
|
84
|
-
|
|
89
|
+
logError("Error incrementing cart item:", error);
|
|
85
90
|
}
|
|
86
91
|
};
|
|
87
92
|
const decrementItem = async (itemId) => {
|
|
@@ -97,7 +102,7 @@ export const CohostCheckoutProvider = ({ cartSessionId, children, }) => {
|
|
|
97
102
|
}
|
|
98
103
|
}
|
|
99
104
|
catch (error) {
|
|
100
|
-
|
|
105
|
+
logError("Error decrementing cart item:", error);
|
|
101
106
|
}
|
|
102
107
|
};
|
|
103
108
|
const updateCartSession = async (data) => {
|
|
@@ -107,7 +112,7 @@ export const CohostCheckoutProvider = ({ cartSessionId, children, }) => {
|
|
|
107
112
|
setCartSession(updatedCart);
|
|
108
113
|
}
|
|
109
114
|
catch (error) {
|
|
110
|
-
|
|
115
|
+
logError("Error updating cart session:", error);
|
|
111
116
|
}
|
|
112
117
|
};
|
|
113
118
|
const setCustomer = async (customer) => {
|
|
@@ -122,7 +127,7 @@ export const CohostCheckoutProvider = ({ cartSessionId, children, }) => {
|
|
|
122
127
|
setCartSession(updatedCart);
|
|
123
128
|
}
|
|
124
129
|
catch (error) {
|
|
125
|
-
|
|
130
|
+
logError("Error setting customer:", error);
|
|
126
131
|
}
|
|
127
132
|
};
|
|
128
133
|
const setBillingAddress = async (address) => {
|
|
@@ -145,7 +150,7 @@ export const CohostCheckoutProvider = ({ cartSessionId, children, }) => {
|
|
|
145
150
|
return res;
|
|
146
151
|
}
|
|
147
152
|
catch (error) {
|
|
148
|
-
|
|
153
|
+
logError("Error placing order:", error);
|
|
149
154
|
}
|
|
150
155
|
};
|
|
151
156
|
const processPayment = async (data) => {
|
|
@@ -155,12 +160,12 @@ export const CohostCheckoutProvider = ({ cartSessionId, children, }) => {
|
|
|
155
160
|
return res;
|
|
156
161
|
}
|
|
157
162
|
catch (error) {
|
|
158
|
-
|
|
163
|
+
logError("Error processing payment:", error);
|
|
159
164
|
}
|
|
160
165
|
};
|
|
161
166
|
useEffect(() => {
|
|
162
167
|
if (!cartSessionId) {
|
|
163
|
-
|
|
168
|
+
logError("CohostCheckoutProvider requires a cartSessionId");
|
|
164
169
|
return;
|
|
165
170
|
}
|
|
166
171
|
const fetchCartSession = async () => {
|
|
@@ -169,7 +174,7 @@ export const CohostCheckoutProvider = ({ cartSessionId, children, }) => {
|
|
|
169
174
|
setCartSession(cart);
|
|
170
175
|
}
|
|
171
176
|
catch (error) {
|
|
172
|
-
|
|
177
|
+
logError("Error fetching cart session:", error);
|
|
173
178
|
// rethrow the error to be handled by the caller
|
|
174
179
|
throw error;
|
|
175
180
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CohostContext.d.ts","sourceRoot":"","sources":["../../src/context/CohostContext.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAE/B,OAAO,EAAE,oBAAoB,EAAsB,KAAK,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAGrG,MAAM,MAAM,mBAAmB,GAAG;IAC9B,QAAQ,CAAC,EAAE,oBAAoB,CAAC;IAChC,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;CAC7B,GAAG,CAAC;IACD,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,YAAY,CAAC;CACzB,GAAG;IACA,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,YAAY,CAAC;CACxB,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"CohostContext.d.ts","sourceRoot":"","sources":["../../src/context/CohostContext.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAE/B,OAAO,EAAE,oBAAoB,EAAsB,KAAK,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAGrG,MAAM,MAAM,mBAAmB,GAAG;IAC9B,QAAQ,CAAC,EAAE,oBAAoB,CAAC;IAChC,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;CAC7B,GAAG,CAAC;IACD,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,YAAY,CAAC;CACzB,GAAG;IACA,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,YAAY,CAAC;CACxB,CAAC,CAAC;AAOH,eAAO,MAAM,cAAc,EAAE,KAAK,CAAC,EAAE,CAAC,mBAAmB,CAcxD,CAAC;AAEF,eAAO,MAAM,eAAe,QAAO;IAAE,MAAM,EAAE,YAAY,CAAC;IAAC,KAAK,EAAE,OAAO,CAAA;CAIxE,CAAC"}
|
|
@@ -4,7 +4,8 @@ import { createCohostClient } from '@cohostvip/cohost-node';
|
|
|
4
4
|
const CohostContext = createContext(null);
|
|
5
5
|
export const CohostProvider = ({ client: providedClient, settings, token: providedToken, children, }) => {
|
|
6
6
|
const client = providedClient ?? createCohostClient({ token: providedToken, settings }); // assumes a factory fn in cohost-node
|
|
7
|
-
|
|
7
|
+
const debug = settings?.debug ?? false;
|
|
8
|
+
return (_jsx(CohostContext.Provider, { value: { client, debug }, children: children }));
|
|
8
9
|
};
|
|
9
10
|
export const useCohostClient = () => {
|
|
10
11
|
const ctx = useContext(CohostContext);
|
|
@@ -5,6 +5,8 @@ export type PaymentElementProviderProps = {
|
|
|
5
5
|
};
|
|
6
6
|
export type PaymentElementContextType = {
|
|
7
7
|
tokenizeCard: (cardInfo: CreditCardInformation) => Promise<any>;
|
|
8
|
+
paymentIntent: any | null;
|
|
9
|
+
isLoading: boolean;
|
|
8
10
|
};
|
|
9
11
|
export declare const PaymentElementProvider: React.FC<PaymentElementProviderProps>;
|
|
10
12
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PaymentElementContext.d.ts","sourceRoot":"","sources":["../../src/context/PaymentElementContext.tsx"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"PaymentElementContext.d.ts","sourceRoot":"","sources":["../../src/context/PaymentElementContext.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAiE,MAAM,OAAO,CAAC;AAGtF,OAAO,EAAE,qBAAqB,EAAa,MAAM,yBAAyB,CAAC;AAG3E,MAAM,MAAM,2BAA2B,GAAG;IACtC,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;CAC7B,CAAC;AAEF,MAAM,MAAM,yBAAyB,GAAG;IACpC,YAAY,EAAE,CAAC,QAAQ,EAAE,qBAAqB,KAAK,OAAO,CAAC,GAAG,CAAC,CAAC;IAChE,aAAa,EAAE,GAAG,GAAG,IAAI,CAAC;IAC1B,SAAS,EAAE,OAAO,CAAC;CACtB,CAAC;AAUF,eAAO,MAAM,sBAAsB,EAAE,KAAK,CAAC,EAAE,CAAC,2BAA2B,CA6ExE,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,iBAAiB,QAAO,yBAIpC,CAAC"}
|
|
@@ -1,20 +1,23 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import { createContext, useContext, useEffect, useState } from 'react';
|
|
2
|
+
import { createContext, useContext, useEffect, useState, useRef } from 'react';
|
|
3
3
|
import { useCohostCheckout } from './CohostCheckoutContext';
|
|
4
|
+
import { useCohostClient } from './CohostContext';
|
|
4
5
|
import { authnetTokenizer } from '../lib/tokenizers/authnet';
|
|
5
6
|
const PaymentElementContext = createContext(null);
|
|
6
7
|
const tokenizers = {
|
|
7
8
|
'authnet': authnetTokenizer
|
|
8
9
|
};
|
|
9
10
|
export const PaymentElementProvider = ({ children }) => {
|
|
10
|
-
const { cartSession } = useCohostCheckout();
|
|
11
|
+
const { cartSession, cartSessionId } = useCohostCheckout();
|
|
12
|
+
const { client, debug } = useCohostClient();
|
|
11
13
|
const [paymentIntent, setPaymentIntent] = useState(null);
|
|
12
14
|
const [tokenizer, setTokenizer] = useState(null);
|
|
15
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
16
|
+
const fetchAttemptedRef = useRef(false);
|
|
13
17
|
const tokenizeCard = async (cardInfo) => {
|
|
14
18
|
if (!tokenizer) {
|
|
15
19
|
throw new Error("Tokenizer not found");
|
|
16
20
|
}
|
|
17
|
-
const paymentIntent = cartSession?.meta?.paymentIntent;
|
|
18
21
|
if (!paymentIntent) {
|
|
19
22
|
throw new Error("Payment intent not found");
|
|
20
23
|
}
|
|
@@ -32,10 +35,37 @@ export const PaymentElementProvider = ({ children }) => {
|
|
|
32
35
|
}
|
|
33
36
|
}, [paymentIntent]);
|
|
34
37
|
useEffect(() => {
|
|
35
|
-
|
|
36
|
-
|
|
38
|
+
if (!cartSession) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
// If cart session has payment intent, use it
|
|
42
|
+
if (cartSession.meta?.paymentIntent) {
|
|
43
|
+
setPaymentIntent(cartSession.meta.paymentIntent);
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
// Only fetch once per mount
|
|
47
|
+
if (fetchAttemptedRef.current) {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
fetchAttemptedRef.current = true;
|
|
51
|
+
setIsLoading(true);
|
|
52
|
+
client.cart.getPaymentIntent(cartSessionId)
|
|
53
|
+
.then((result) => {
|
|
54
|
+
setPaymentIntent(result);
|
|
55
|
+
})
|
|
56
|
+
.catch((error) => {
|
|
57
|
+
if (debug) {
|
|
58
|
+
console.error("Error fetching payment intent:", error);
|
|
59
|
+
}
|
|
60
|
+
})
|
|
61
|
+
.finally(() => {
|
|
62
|
+
setIsLoading(false);
|
|
63
|
+
});
|
|
64
|
+
}, [cartSession, cartSessionId, client]);
|
|
37
65
|
return (_jsx(PaymentElementContext.Provider, { value: {
|
|
38
|
-
tokenizeCard
|
|
66
|
+
tokenizeCard,
|
|
67
|
+
paymentIntent,
|
|
68
|
+
isLoading,
|
|
39
69
|
}, children: children }));
|
|
40
70
|
};
|
|
41
71
|
/**
|