@eetech-commerce/cart-react 0.6.4 → 0.7.0
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 +12 -33
- package/dist/index.d.ts +11 -6
- package/dist/index.js +256 -100
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -67,8 +67,8 @@ function Shop() {
|
|
|
67
67
|
| `tenantSlug` | `string` | Yes | - | Your tenant identifier |
|
|
68
68
|
| `cartApiUrl` | `string` | Yes | - | Base URL of the cart API |
|
|
69
69
|
| `storageAdapter` | `StorageAdapter` | No | `localStorage` | Custom storage implementation |
|
|
70
|
-
| `autoInitialize` | `boolean` | No | `true` | Auto-create cart on mount |
|
|
71
70
|
| `queryClient` | `QueryClient` | No | - | Custom TanStack Query client instance |
|
|
71
|
+
| `getAuthToken` | `() => Promise<string \| null>` | No | - | Async function returning the current auth token (enables authenticated carts) |
|
|
72
72
|
|
|
73
73
|
## Hooks Reference
|
|
74
74
|
|
|
@@ -82,7 +82,7 @@ function Shop() {
|
|
|
82
82
|
| `useUpdateItemQty()` | Update item quantity |
|
|
83
83
|
| `useRemoveItem()` | Remove an item from cart |
|
|
84
84
|
| `useClearCart()` | Clear all items from cart |
|
|
85
|
-
| `
|
|
85
|
+
| `useResetCart()` | Reset cart (clears session, creates fresh cart) |
|
|
86
86
|
|
|
87
87
|
### Checkout Hooks
|
|
88
88
|
|
|
@@ -167,46 +167,25 @@ function App() {
|
|
|
167
167
|
}
|
|
168
168
|
```
|
|
169
169
|
|
|
170
|
-
##
|
|
170
|
+
## Resetting the Cart
|
|
171
171
|
|
|
172
|
-
|
|
172
|
+
After a successful payment or when you need a fresh cart, use `useResetCart()`:
|
|
173
173
|
|
|
174
174
|
```tsx
|
|
175
|
-
import {
|
|
176
|
-
CartProvider,
|
|
177
|
-
useInitializeCart,
|
|
178
|
-
useCartContext,
|
|
179
|
-
} from '@eetech-commerce/cart-react';
|
|
175
|
+
import { useResetCart } from '@eetech-commerce/cart-react';
|
|
180
176
|
|
|
181
|
-
function
|
|
182
|
-
|
|
183
|
-
<CartProvider
|
|
184
|
-
tenantSlug="my-store"
|
|
185
|
-
cartApiUrl="https://cart.api.com"
|
|
186
|
-
autoInitialize={false}
|
|
187
|
-
>
|
|
188
|
-
<CartInitializer />
|
|
189
|
-
<Shop />
|
|
190
|
-
</CartProvider>
|
|
191
|
-
);
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
function CartInitializer() {
|
|
195
|
-
const { isInitializing } = useInitializeCart();
|
|
196
|
-
const { cartSessionId } = useCartContext();
|
|
177
|
+
function PaymentSuccessPage() {
|
|
178
|
+
const { resetCart } = useResetCart();
|
|
197
179
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
}
|
|
180
|
+
useEffect(() => {
|
|
181
|
+
resetCart();
|
|
182
|
+
}, [resetCart]);
|
|
201
183
|
|
|
202
|
-
return
|
|
184
|
+
return <div>Payment successful!</div>;
|
|
203
185
|
}
|
|
204
186
|
```
|
|
205
187
|
|
|
206
|
-
|
|
207
|
-
- Delay cart creation until user interaction
|
|
208
|
-
- Initialize the cart conditionally based on route or user state
|
|
209
|
-
- Handle initialization loading states explicitly
|
|
188
|
+
`resetCart()` clears the current session, invalidates all cached queries, and creates a new cart.
|
|
210
189
|
|
|
211
190
|
## Using with Existing QueryClient
|
|
212
191
|
|
package/dist/index.d.ts
CHANGED
|
@@ -20,7 +20,8 @@ interface CartContextValue {
|
|
|
20
20
|
cartSessionId: string | null;
|
|
21
21
|
setCartSessionId: (id: string | null) => void;
|
|
22
22
|
isInitializing: boolean;
|
|
23
|
-
|
|
23
|
+
resetCart: () => Promise<void>;
|
|
24
|
+
getAuthToken?: () => Promise<string | null>;
|
|
24
25
|
}
|
|
25
26
|
declare function useCartContext(): CartContextValue;
|
|
26
27
|
|
|
@@ -261,6 +262,10 @@ type CartResponseDto = {
|
|
|
261
262
|
* Public cart session identifier (nanoid)
|
|
262
263
|
*/
|
|
263
264
|
sessionId: string;
|
|
265
|
+
/**
|
|
266
|
+
* Authenticated user ID (null for anonymous carts)
|
|
267
|
+
*/
|
|
268
|
+
userId?: string | null;
|
|
264
269
|
/**
|
|
265
270
|
* Three-letter currency code (ISO 4217)
|
|
266
271
|
*/
|
|
@@ -912,8 +917,8 @@ declare function useClearCart(): {
|
|
|
912
917
|
isPending: boolean;
|
|
913
918
|
error: Error | null;
|
|
914
919
|
};
|
|
915
|
-
declare function
|
|
916
|
-
|
|
920
|
+
declare function useResetCart(): {
|
|
921
|
+
resetCart: () => Promise<void>;
|
|
917
922
|
};
|
|
918
923
|
|
|
919
924
|
declare function useVerifyCart(): {
|
|
@@ -1317,10 +1322,10 @@ interface CartProviderProps {
|
|
|
1317
1322
|
tenantSlug: string;
|
|
1318
1323
|
cartApiUrl: string;
|
|
1319
1324
|
storageAdapter?: StorageAdapter;
|
|
1320
|
-
autoInitialize?: boolean;
|
|
1321
1325
|
/** Optional external QueryClient. If provided, CartProvider will not create its own. */
|
|
1322
1326
|
queryClient?: QueryClient;
|
|
1327
|
+
getAuthToken?: () => Promise<string | null>;
|
|
1323
1328
|
}
|
|
1324
|
-
declare function CartProvider({ children, tenantSlug, cartApiUrl, storageAdapter,
|
|
1329
|
+
declare function CartProvider({ children, tenantSlug, cartApiUrl, storageAdapter, queryClient, getAuthToken, }: CartProviderProps): react_jsx_runtime.JSX.Element;
|
|
1325
1330
|
|
|
1326
|
-
export { type CartContextValue, CartProvider, type CartProviderProps, type CartResponseDto, type CreateCheckoutDto, type CreateEmbeddedPaymentSessionDto, type CreatePaymentIntentDto, type EmbeddedPaymentSessionResponseDto, type LineItemResponseDto, type LoadStripeFn, type MutationCallbacks, type PaymentIntentResponseDto, type ShippingOptionDto, type ShippingRegionDto, type ShippingRegionsResponseDto, type ShippingSelectionDto, type StorageAdapter, type UpdateCheckoutDto, type VerificationResultDto, cartKeys, localStorageAdapter, paymentKeys, shippingKeys, useAddToCart, useCart, useCartContext, useClearCart, useCreateCart, useCreateCheckoutSession, useCreateEmbeddedCheckoutSession, useCreatePaymentIntent,
|
|
1331
|
+
export { type CartContextValue, CartProvider, type CartProviderProps, type CartResponseDto, type CreateCheckoutDto, type CreateEmbeddedPaymentSessionDto, type CreatePaymentIntentDto, type EmbeddedPaymentSessionResponseDto, type LineItemResponseDto, type LoadStripeFn, type MutationCallbacks, type PaymentIntentResponseDto, type ShippingOptionDto, type ShippingRegionDto, type ShippingRegionsResponseDto, type ShippingSelectionDto, type StorageAdapter, type UpdateCheckoutDto, type VerificationResultDto, cartKeys, localStorageAdapter, paymentKeys, shippingKeys, useAddToCart, useCart, useCartContext, useClearCart, useCreateCart, useCreateCheckoutSession, useCreateEmbeddedCheckoutSession, useCreatePaymentIntent, usePaymentSession, useRemoveItem, useResetCart, useShippingOptions, useShippingRegions, useStripePromise, useUpdateCheckoutSession, useUpdateItemQty, useUpdateShippingSelections, useVerifyCart };
|
package/dist/index.js
CHANGED
|
@@ -27,7 +27,7 @@ function useCartContext() {
|
|
|
27
27
|
|
|
28
28
|
// src/hooks/cart.ts
|
|
29
29
|
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
30
|
-
import { useCallback
|
|
30
|
+
import { useCallback } from "react";
|
|
31
31
|
|
|
32
32
|
// src/generated/@tanstack/react-query.gen.ts
|
|
33
33
|
import { queryOptions } from "@tanstack/react-query";
|
|
@@ -861,6 +861,12 @@ var client = createClient(createConfig());
|
|
|
861
861
|
// src/generated/sdk.gen.ts
|
|
862
862
|
var cartControllerCreateCartV1 = (options) => {
|
|
863
863
|
return (options.client ?? client).post({
|
|
864
|
+
security: [
|
|
865
|
+
{
|
|
866
|
+
scheme: "bearer",
|
|
867
|
+
type: "http"
|
|
868
|
+
}
|
|
869
|
+
],
|
|
864
870
|
url: "/v1/t/{tenantSlug}/carts",
|
|
865
871
|
...options,
|
|
866
872
|
headers: {
|
|
@@ -869,20 +875,50 @@ var cartControllerCreateCartV1 = (options) => {
|
|
|
869
875
|
}
|
|
870
876
|
});
|
|
871
877
|
};
|
|
878
|
+
var cartControllerGetActiveCartV1 = (options) => {
|
|
879
|
+
return (options.client ?? client).get({
|
|
880
|
+
security: [
|
|
881
|
+
{
|
|
882
|
+
scheme: "bearer",
|
|
883
|
+
type: "http"
|
|
884
|
+
}
|
|
885
|
+
],
|
|
886
|
+
url: "/v1/t/{tenantSlug}/carts/me",
|
|
887
|
+
...options
|
|
888
|
+
});
|
|
889
|
+
};
|
|
872
890
|
var cartControllerGetCartV1 = (options) => {
|
|
873
891
|
return (options.client ?? client).get({
|
|
892
|
+
security: [
|
|
893
|
+
{
|
|
894
|
+
scheme: "bearer",
|
|
895
|
+
type: "http"
|
|
896
|
+
}
|
|
897
|
+
],
|
|
874
898
|
url: "/v1/t/{tenantSlug}/carts/{cartSessionId}",
|
|
875
899
|
...options
|
|
876
900
|
});
|
|
877
901
|
};
|
|
878
902
|
var cartControllerClearCartV1 = (options) => {
|
|
879
903
|
return (options.client ?? client).delete({
|
|
904
|
+
security: [
|
|
905
|
+
{
|
|
906
|
+
scheme: "bearer",
|
|
907
|
+
type: "http"
|
|
908
|
+
}
|
|
909
|
+
],
|
|
880
910
|
url: "/v1/t/{tenantSlug}/carts/{cartSessionId}/items",
|
|
881
911
|
...options
|
|
882
912
|
});
|
|
883
913
|
};
|
|
884
914
|
var cartControllerAddItemV1 = (options) => {
|
|
885
915
|
return (options.client ?? client).post({
|
|
916
|
+
security: [
|
|
917
|
+
{
|
|
918
|
+
scheme: "bearer",
|
|
919
|
+
type: "http"
|
|
920
|
+
}
|
|
921
|
+
],
|
|
886
922
|
url: "/v1/t/{tenantSlug}/carts/{cartSessionId}/items",
|
|
887
923
|
...options,
|
|
888
924
|
headers: {
|
|
@@ -893,12 +929,24 @@ var cartControllerAddItemV1 = (options) => {
|
|
|
893
929
|
};
|
|
894
930
|
var cartControllerRemoveItemV1 = (options) => {
|
|
895
931
|
return (options.client ?? client).delete({
|
|
932
|
+
security: [
|
|
933
|
+
{
|
|
934
|
+
scheme: "bearer",
|
|
935
|
+
type: "http"
|
|
936
|
+
}
|
|
937
|
+
],
|
|
896
938
|
url: "/v1/t/{tenantSlug}/carts/{cartSessionId}/items/{offerId}",
|
|
897
939
|
...options
|
|
898
940
|
});
|
|
899
941
|
};
|
|
900
942
|
var cartControllerUpdateItemV1 = (options) => {
|
|
901
943
|
return (options.client ?? client).patch({
|
|
944
|
+
security: [
|
|
945
|
+
{
|
|
946
|
+
scheme: "bearer",
|
|
947
|
+
type: "http"
|
|
948
|
+
}
|
|
949
|
+
],
|
|
902
950
|
url: "/v1/t/{tenantSlug}/carts/{cartSessionId}/items/{offerId}",
|
|
903
951
|
...options,
|
|
904
952
|
headers: {
|
|
@@ -909,6 +957,12 @@ var cartControllerUpdateItemV1 = (options) => {
|
|
|
909
957
|
};
|
|
910
958
|
var checkoutControllerVerifyCartV1 = (options) => {
|
|
911
959
|
return (options.client ?? client).post({
|
|
960
|
+
security: [
|
|
961
|
+
{
|
|
962
|
+
scheme: "bearer",
|
|
963
|
+
type: "http"
|
|
964
|
+
}
|
|
965
|
+
],
|
|
912
966
|
url: "/v1/t/{tenantSlug}/checkout/verify",
|
|
913
967
|
...options,
|
|
914
968
|
headers: {
|
|
@@ -919,12 +973,24 @@ var checkoutControllerVerifyCartV1 = (options) => {
|
|
|
919
973
|
};
|
|
920
974
|
var checkoutControllerGetShippingRegionsV1 = (options) => {
|
|
921
975
|
return (options.client ?? client).get({
|
|
976
|
+
security: [
|
|
977
|
+
{
|
|
978
|
+
scheme: "bearer",
|
|
979
|
+
type: "http"
|
|
980
|
+
}
|
|
981
|
+
],
|
|
922
982
|
url: "/v1/t/{tenantSlug}/checkout/shipping-regions",
|
|
923
983
|
...options
|
|
924
984
|
});
|
|
925
985
|
};
|
|
926
986
|
var checkoutControllerUpdateCheckoutSessionV1 = (options) => {
|
|
927
987
|
return (options.client ?? client).patch({
|
|
988
|
+
security: [
|
|
989
|
+
{
|
|
990
|
+
scheme: "bearer",
|
|
991
|
+
type: "http"
|
|
992
|
+
}
|
|
993
|
+
],
|
|
928
994
|
url: "/v1/t/{tenantSlug}/checkout/{cartSessionId}",
|
|
929
995
|
...options,
|
|
930
996
|
headers: {
|
|
@@ -935,6 +1001,12 @@ var checkoutControllerUpdateCheckoutSessionV1 = (options) => {
|
|
|
935
1001
|
};
|
|
936
1002
|
var checkoutControllerCreateCheckoutSessionV1 = (options) => {
|
|
937
1003
|
return (options.client ?? client).post({
|
|
1004
|
+
security: [
|
|
1005
|
+
{
|
|
1006
|
+
scheme: "bearer",
|
|
1007
|
+
type: "http"
|
|
1008
|
+
}
|
|
1009
|
+
],
|
|
938
1010
|
url: "/v1/t/{tenantSlug}/checkout/{cartSessionId}",
|
|
939
1011
|
...options,
|
|
940
1012
|
headers: {
|
|
@@ -945,12 +1017,24 @@ var checkoutControllerCreateCheckoutSessionV1 = (options) => {
|
|
|
945
1017
|
};
|
|
946
1018
|
var checkoutControllerGetShippingOptionsV1 = (options) => {
|
|
947
1019
|
return (options.client ?? client).get({
|
|
1020
|
+
security: [
|
|
1021
|
+
{
|
|
1022
|
+
scheme: "bearer",
|
|
1023
|
+
type: "http"
|
|
1024
|
+
}
|
|
1025
|
+
],
|
|
948
1026
|
url: "/v1/t/{tenantSlug}/checkout/{cartSessionId}/shipping-options",
|
|
949
1027
|
...options
|
|
950
1028
|
});
|
|
951
1029
|
};
|
|
952
1030
|
var checkoutControllerUpdateShippingSelectionsV1 = (options) => {
|
|
953
1031
|
return (options.client ?? client).patch({
|
|
1032
|
+
security: [
|
|
1033
|
+
{
|
|
1034
|
+
scheme: "bearer",
|
|
1035
|
+
type: "http"
|
|
1036
|
+
}
|
|
1037
|
+
],
|
|
954
1038
|
url: "/v1/t/{tenantSlug}/checkout/{cartSessionId}/shipping-selections",
|
|
955
1039
|
...options,
|
|
956
1040
|
headers: {
|
|
@@ -967,6 +1051,12 @@ var tenantPublicControllerGetPublicKeyV1 = (options) => {
|
|
|
967
1051
|
};
|
|
968
1052
|
var paymentControllerCreateEmbeddedPaymentSessionV1 = (options) => {
|
|
969
1053
|
return (options.client ?? client).post({
|
|
1054
|
+
security: [
|
|
1055
|
+
{
|
|
1056
|
+
scheme: "bearer",
|
|
1057
|
+
type: "http"
|
|
1058
|
+
}
|
|
1059
|
+
],
|
|
970
1060
|
url: "/v1/t/{tenantSlug}/payments/sessions/embedded",
|
|
971
1061
|
...options,
|
|
972
1062
|
headers: {
|
|
@@ -977,6 +1067,12 @@ var paymentControllerCreateEmbeddedPaymentSessionV1 = (options) => {
|
|
|
977
1067
|
};
|
|
978
1068
|
var paymentControllerCreatePaymentIntentV1 = (options) => {
|
|
979
1069
|
return (options.client ?? client).post({
|
|
1070
|
+
security: [
|
|
1071
|
+
{
|
|
1072
|
+
scheme: "bearer",
|
|
1073
|
+
type: "http"
|
|
1074
|
+
}
|
|
1075
|
+
],
|
|
980
1076
|
url: "/v1/t/{tenantSlug}/payments/intents",
|
|
981
1077
|
...options,
|
|
982
1078
|
headers: {
|
|
@@ -987,6 +1083,12 @@ var paymentControllerCreatePaymentIntentV1 = (options) => {
|
|
|
987
1083
|
};
|
|
988
1084
|
var paymentControllerGetPaymentSessionV1 = (options) => {
|
|
989
1085
|
return (options.client ?? client).get({
|
|
1086
|
+
security: [
|
|
1087
|
+
{
|
|
1088
|
+
scheme: "bearer",
|
|
1089
|
+
type: "http"
|
|
1090
|
+
}
|
|
1091
|
+
],
|
|
990
1092
|
url: "/v1/t/{tenantSlug}/payments/sessions/{id}",
|
|
991
1093
|
...options
|
|
992
1094
|
});
|
|
@@ -1399,79 +1501,9 @@ function useClearCart() {
|
|
|
1399
1501
|
error: mutation.error
|
|
1400
1502
|
};
|
|
1401
1503
|
}
|
|
1402
|
-
function
|
|
1403
|
-
const {
|
|
1404
|
-
|
|
1405
|
-
const creatingCartRef = useRef(false);
|
|
1406
|
-
const cartQuery = useQuery({
|
|
1407
|
-
...cartControllerGetCartV1Options({
|
|
1408
|
-
path: {
|
|
1409
|
-
tenantSlug,
|
|
1410
|
-
cartSessionId
|
|
1411
|
-
},
|
|
1412
|
-
baseUrl: cartApiUrl
|
|
1413
|
-
}),
|
|
1414
|
-
enabled: !!cartSessionId,
|
|
1415
|
-
retry: false
|
|
1416
|
-
});
|
|
1417
|
-
const createCartMutation = useMutation({
|
|
1418
|
-
...cartControllerCreateCartV1Mutation(),
|
|
1419
|
-
onMutate: () => {
|
|
1420
|
-
creatingCartRef.current = true;
|
|
1421
|
-
},
|
|
1422
|
-
onSuccess: (data) => {
|
|
1423
|
-
setCartSessionId(data.sessionId);
|
|
1424
|
-
queryClient.invalidateQueries({ queryKey: cartKeys.all() });
|
|
1425
|
-
creatingCartRef.current = false;
|
|
1426
|
-
},
|
|
1427
|
-
onError: () => {
|
|
1428
|
-
creatingCartRef.current = false;
|
|
1429
|
-
}
|
|
1430
|
-
});
|
|
1431
|
-
useEffect(() => {
|
|
1432
|
-
if (creatingCartRef.current) return;
|
|
1433
|
-
if (cartSessionId) {
|
|
1434
|
-
if (cartQuery.isError) {
|
|
1435
|
-
setCartSessionId(null);
|
|
1436
|
-
createCartMutation.mutate({
|
|
1437
|
-
path: { tenantSlug },
|
|
1438
|
-
body: { currency: "USD", metadata: {} },
|
|
1439
|
-
baseUrl: cartApiUrl
|
|
1440
|
-
});
|
|
1441
|
-
return;
|
|
1442
|
-
}
|
|
1443
|
-
if (cartQuery.data?.completedAt) {
|
|
1444
|
-
setCartSessionId(null);
|
|
1445
|
-
createCartMutation.mutate({
|
|
1446
|
-
path: { tenantSlug },
|
|
1447
|
-
body: { currency: "USD", metadata: {} },
|
|
1448
|
-
baseUrl: cartApiUrl
|
|
1449
|
-
});
|
|
1450
|
-
return;
|
|
1451
|
-
}
|
|
1452
|
-
if (cartQuery.data) {
|
|
1453
|
-
setIsInitializing(false);
|
|
1454
|
-
}
|
|
1455
|
-
return;
|
|
1456
|
-
}
|
|
1457
|
-
createCartMutation.mutate({
|
|
1458
|
-
path: { tenantSlug },
|
|
1459
|
-
body: { currency: "USD", metadata: {} },
|
|
1460
|
-
baseUrl: cartApiUrl
|
|
1461
|
-
});
|
|
1462
|
-
}, [
|
|
1463
|
-
tenantSlug,
|
|
1464
|
-
cartApiUrl,
|
|
1465
|
-
cartSessionId,
|
|
1466
|
-
cartQuery.isError,
|
|
1467
|
-
cartQuery.data?.completedAt,
|
|
1468
|
-
cartQuery.data,
|
|
1469
|
-
setCartSessionId,
|
|
1470
|
-
setIsInitializing,
|
|
1471
|
-
createCartMutation
|
|
1472
|
-
]);
|
|
1473
|
-
const isInitializing = cartQuery.isLoading || createCartMutation.isPending || !cartSessionId && !createCartMutation.isSuccess;
|
|
1474
|
-
return { isInitializing };
|
|
1504
|
+
function useResetCart() {
|
|
1505
|
+
const { resetCart } = useCartContext();
|
|
1506
|
+
return { resetCart };
|
|
1475
1507
|
}
|
|
1476
1508
|
|
|
1477
1509
|
// src/hooks/checkout.ts
|
|
@@ -1706,21 +1738,32 @@ function usePaymentSession(sessionId) {
|
|
|
1706
1738
|
}
|
|
1707
1739
|
|
|
1708
1740
|
// src/providers/CartProvider.tsx
|
|
1709
|
-
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
|
1710
|
-
import { useCallback as useCallback4, useEffect
|
|
1711
|
-
import { jsx
|
|
1741
|
+
import { QueryClient, QueryClientProvider, useQueryClient as useQueryClient3 } from "@tanstack/react-query";
|
|
1742
|
+
import { useCallback as useCallback4, useEffect, useMemo as useMemo2, useRef, useState } from "react";
|
|
1743
|
+
import { jsx } from "react/jsx-runtime";
|
|
1712
1744
|
var STORAGE_KEY = "cartSessionId";
|
|
1713
1745
|
function CartProviderInner({
|
|
1714
1746
|
children,
|
|
1715
1747
|
tenantSlug,
|
|
1716
1748
|
cartApiUrl,
|
|
1717
1749
|
storage,
|
|
1718
|
-
|
|
1750
|
+
getAuthToken: getAuthToken2
|
|
1719
1751
|
}) {
|
|
1720
1752
|
const [cartSessionId, setCartSessionIdState] = useState(
|
|
1721
1753
|
() => storage.get(STORAGE_KEY)
|
|
1722
1754
|
);
|
|
1723
|
-
const [isInitializing, setIsInitializing] = useState(
|
|
1755
|
+
const [isInitializing, setIsInitializing] = useState(true);
|
|
1756
|
+
const queryClient = useQueryClient3();
|
|
1757
|
+
const seedCartCache = useCallback4(
|
|
1758
|
+
(cartData) => {
|
|
1759
|
+
const queryKey = cartControllerGetCartV1QueryKey({
|
|
1760
|
+
baseUrl: cartApiUrl,
|
|
1761
|
+
path: { tenantSlug, cartSessionId: cartData.sessionId }
|
|
1762
|
+
});
|
|
1763
|
+
queryClient.setQueryData(queryKey, cartData);
|
|
1764
|
+
},
|
|
1765
|
+
[cartApiUrl, tenantSlug, queryClient]
|
|
1766
|
+
);
|
|
1724
1767
|
const setCartSessionId = useCallback4(
|
|
1725
1768
|
(id) => {
|
|
1726
1769
|
setCartSessionIdState(id);
|
|
@@ -1732,6 +1775,114 @@ function CartProviderInner({
|
|
|
1732
1775
|
},
|
|
1733
1776
|
[storage]
|
|
1734
1777
|
);
|
|
1778
|
+
const [accessToken, setAccessToken] = useState(null);
|
|
1779
|
+
const authSettledRef = useRef(false);
|
|
1780
|
+
const prevAuthenticatedRef = useRef(false);
|
|
1781
|
+
useEffect(() => {
|
|
1782
|
+
let cancelled = false;
|
|
1783
|
+
(async () => {
|
|
1784
|
+
const token = getAuthToken2 ? await getAuthToken2() : null;
|
|
1785
|
+
if (cancelled) return;
|
|
1786
|
+
setAccessToken(token);
|
|
1787
|
+
if (!authSettledRef.current) {
|
|
1788
|
+
authSettledRef.current = true;
|
|
1789
|
+
prevAuthenticatedRef.current = !!token;
|
|
1790
|
+
}
|
|
1791
|
+
})();
|
|
1792
|
+
return () => {
|
|
1793
|
+
cancelled = true;
|
|
1794
|
+
};
|
|
1795
|
+
});
|
|
1796
|
+
const isAuthenticated = !!accessToken;
|
|
1797
|
+
const cancelInitRef = useRef(null);
|
|
1798
|
+
const resetInProgressRef = useRef(false);
|
|
1799
|
+
const initializeCart = useCallback4(
|
|
1800
|
+
async ({ forceCreate = false } = {}) => {
|
|
1801
|
+
cancelInitRef.current?.();
|
|
1802
|
+
let cancelled = false;
|
|
1803
|
+
cancelInitRef.current = () => {
|
|
1804
|
+
cancelled = true;
|
|
1805
|
+
};
|
|
1806
|
+
setIsInitializing(true);
|
|
1807
|
+
const token = getAuthToken2 ? await getAuthToken2() : null;
|
|
1808
|
+
if (cancelled) return;
|
|
1809
|
+
if (!forceCreate) {
|
|
1810
|
+
if (token) {
|
|
1811
|
+
try {
|
|
1812
|
+
const res = await cartControllerGetActiveCartV1({ path: { tenantSlug } });
|
|
1813
|
+
if (!cancelled && res.data) {
|
|
1814
|
+
seedCartCache(res.data);
|
|
1815
|
+
setCartSessionId(res.data.sessionId);
|
|
1816
|
+
setIsInitializing(false);
|
|
1817
|
+
return;
|
|
1818
|
+
}
|
|
1819
|
+
} catch {
|
|
1820
|
+
}
|
|
1821
|
+
} else {
|
|
1822
|
+
const storedId = storage.get(STORAGE_KEY);
|
|
1823
|
+
if (storedId) {
|
|
1824
|
+
try {
|
|
1825
|
+
const res = await cartControllerGetCartV1({
|
|
1826
|
+
path: { tenantSlug, cartSessionId: storedId }
|
|
1827
|
+
});
|
|
1828
|
+
if (!cancelled && res.data && !res.data.completedAt) {
|
|
1829
|
+
seedCartCache(res.data);
|
|
1830
|
+
setCartSessionId(storedId);
|
|
1831
|
+
setIsInitializing(false);
|
|
1832
|
+
return;
|
|
1833
|
+
}
|
|
1834
|
+
} catch {
|
|
1835
|
+
}
|
|
1836
|
+
if (!cancelled) storage.remove(STORAGE_KEY);
|
|
1837
|
+
}
|
|
1838
|
+
}
|
|
1839
|
+
}
|
|
1840
|
+
if (cancelled) return;
|
|
1841
|
+
try {
|
|
1842
|
+
const res = await cartControllerCreateCartV1({
|
|
1843
|
+
path: { tenantSlug },
|
|
1844
|
+
body: { currency: "USD", metadata: {} }
|
|
1845
|
+
});
|
|
1846
|
+
if (!cancelled && res.data) {
|
|
1847
|
+
seedCartCache(res.data);
|
|
1848
|
+
setCartSessionId(res.data.sessionId);
|
|
1849
|
+
}
|
|
1850
|
+
} catch {
|
|
1851
|
+
}
|
|
1852
|
+
if (!cancelled) setIsInitializing(false);
|
|
1853
|
+
},
|
|
1854
|
+
[getAuthToken2, tenantSlug, storage, setCartSessionId, seedCartCache]
|
|
1855
|
+
);
|
|
1856
|
+
const mountedRef = useRef(false);
|
|
1857
|
+
useEffect(() => {
|
|
1858
|
+
if (mountedRef.current) return;
|
|
1859
|
+
mountedRef.current = true;
|
|
1860
|
+
if (resetInProgressRef.current) return;
|
|
1861
|
+
initializeCart();
|
|
1862
|
+
}, [initializeCart]);
|
|
1863
|
+
const clearCartState = useCallback4(() => {
|
|
1864
|
+
setCartSessionIdState(null);
|
|
1865
|
+
storage.remove(STORAGE_KEY);
|
|
1866
|
+
queryClient.removeQueries({ queryKey: cartKeys.all() });
|
|
1867
|
+
queryClient.removeQueries({ queryKey: shippingKeys.all() });
|
|
1868
|
+
queryClient.removeQueries({ queryKey: paymentKeys.all() });
|
|
1869
|
+
}, [storage, queryClient]);
|
|
1870
|
+
useEffect(() => {
|
|
1871
|
+
if (!authSettledRef.current) return;
|
|
1872
|
+
if (prevAuthenticatedRef.current === isAuthenticated) return;
|
|
1873
|
+
prevAuthenticatedRef.current = isAuthenticated;
|
|
1874
|
+
clearCartState();
|
|
1875
|
+
initializeCart();
|
|
1876
|
+
}, [isAuthenticated, clearCartState, initializeCart]);
|
|
1877
|
+
const resetCart = useCallback4(async () => {
|
|
1878
|
+
resetInProgressRef.current = true;
|
|
1879
|
+
clearCartState();
|
|
1880
|
+
try {
|
|
1881
|
+
await initializeCart({ forceCreate: true });
|
|
1882
|
+
} finally {
|
|
1883
|
+
resetInProgressRef.current = false;
|
|
1884
|
+
}
|
|
1885
|
+
}, [clearCartState, initializeCart]);
|
|
1735
1886
|
const contextValue = useMemo2(
|
|
1736
1887
|
() => ({
|
|
1737
1888
|
tenantSlug,
|
|
@@ -1740,22 +1891,21 @@ function CartProviderInner({
|
|
|
1740
1891
|
cartSessionId,
|
|
1741
1892
|
setCartSessionId,
|
|
1742
1893
|
isInitializing,
|
|
1743
|
-
|
|
1894
|
+
resetCart,
|
|
1895
|
+
getAuthToken: getAuthToken2
|
|
1744
1896
|
}),
|
|
1745
|
-
[
|
|
1897
|
+
[
|
|
1898
|
+
tenantSlug,
|
|
1899
|
+
cartApiUrl,
|
|
1900
|
+
storage,
|
|
1901
|
+
cartSessionId,
|
|
1902
|
+
setCartSessionId,
|
|
1903
|
+
isInitializing,
|
|
1904
|
+
resetCart,
|
|
1905
|
+
getAuthToken2
|
|
1906
|
+
]
|
|
1746
1907
|
);
|
|
1747
|
-
return /* @__PURE__ */
|
|
1748
|
-
autoInitialize !== false && /* @__PURE__ */ jsx(AutoInitializer, {}),
|
|
1749
|
-
children
|
|
1750
|
-
] });
|
|
1751
|
-
}
|
|
1752
|
-
function AutoInitializer() {
|
|
1753
|
-
const { isInitializing } = useInitializeCart();
|
|
1754
|
-
const { setIsInitializing } = useCartContext();
|
|
1755
|
-
useEffect2(() => {
|
|
1756
|
-
setIsInitializing(isInitializing);
|
|
1757
|
-
}, [isInitializing, setIsInitializing]);
|
|
1758
|
-
return null;
|
|
1908
|
+
return /* @__PURE__ */ jsx(CartContext.Provider, { value: contextValue, children });
|
|
1759
1909
|
}
|
|
1760
1910
|
var internalQueryClient = null;
|
|
1761
1911
|
function getOrCreateQueryClient() {
|
|
@@ -1774,13 +1924,19 @@ function CartProvider({
|
|
|
1774
1924
|
tenantSlug,
|
|
1775
1925
|
cartApiUrl,
|
|
1776
1926
|
storageAdapter,
|
|
1777
|
-
|
|
1778
|
-
|
|
1927
|
+
queryClient,
|
|
1928
|
+
getAuthToken: getAuthToken2
|
|
1779
1929
|
}) {
|
|
1780
1930
|
const storage = storageAdapter ?? localStorageAdapter;
|
|
1781
|
-
|
|
1782
|
-
client.setConfig({
|
|
1783
|
-
|
|
1931
|
+
useMemo2(() => {
|
|
1932
|
+
client.setConfig({
|
|
1933
|
+
baseUrl: cartApiUrl,
|
|
1934
|
+
auth: getAuthToken2 ? async () => {
|
|
1935
|
+
const token = await getAuthToken2();
|
|
1936
|
+
return token ?? void 0;
|
|
1937
|
+
} : void 0
|
|
1938
|
+
});
|
|
1939
|
+
}, [cartApiUrl, getAuthToken2]);
|
|
1784
1940
|
const [effectiveQueryClient] = useState(() => queryClient ?? getOrCreateQueryClient());
|
|
1785
1941
|
const inner = /* @__PURE__ */ jsx(
|
|
1786
1942
|
CartProviderInner,
|
|
@@ -1788,7 +1944,7 @@ function CartProvider({
|
|
|
1788
1944
|
tenantSlug,
|
|
1789
1945
|
cartApiUrl,
|
|
1790
1946
|
storage,
|
|
1791
|
-
|
|
1947
|
+
getAuthToken: getAuthToken2,
|
|
1792
1948
|
children
|
|
1793
1949
|
}
|
|
1794
1950
|
);
|
|
@@ -1811,9 +1967,9 @@ export {
|
|
|
1811
1967
|
useCreateCheckoutSession,
|
|
1812
1968
|
useCreateEmbeddedCheckoutSession,
|
|
1813
1969
|
useCreatePaymentIntent,
|
|
1814
|
-
useInitializeCart,
|
|
1815
1970
|
usePaymentSession,
|
|
1816
1971
|
useRemoveItem,
|
|
1972
|
+
useResetCart,
|
|
1817
1973
|
useShippingOptions,
|
|
1818
1974
|
useShippingRegions,
|
|
1819
1975
|
useStripePromise,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eetech-commerce/cart-react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -42,12 +42,12 @@
|
|
|
42
42
|
"@hey-api/openapi-ts": "0.87.5",
|
|
43
43
|
"@stripe/stripe-js": ">=2.0.0",
|
|
44
44
|
"@testing-library/react": "^16.0.0",
|
|
45
|
-
"@types/react": "^
|
|
46
|
-
"@types/react-dom": "^
|
|
45
|
+
"@types/react": "^19.0.0",
|
|
46
|
+
"@types/react-dom": "^19.0.0",
|
|
47
47
|
"jsdom": "^25.0.0",
|
|
48
48
|
"msw": "^2.0.0",
|
|
49
|
-
"react": "^
|
|
50
|
-
"react-dom": "^
|
|
49
|
+
"react": "^19.0.0",
|
|
50
|
+
"react-dom": "^19.0.0",
|
|
51
51
|
"tsup": "^8.0.0",
|
|
52
52
|
"typescript": "^5.0.0",
|
|
53
53
|
"vitest": "^2.0.0"
|