@shopify/hydrogen 1.4.2 → 1.4.3
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/esnext/components/CartProvider/CartProvider.client.js +24 -2
- package/dist/esnext/components/CartProvider/CartProviderV2.client.js +2 -4
- package/dist/esnext/components/CartProvider/useCartAPIStateMachine.client.d.ts +1 -1
- package/dist/esnext/components/CartProvider/useCartAPIStateMachine.client.js +38 -32
- package/dist/esnext/version.d.ts +1 -1
- package/dist/esnext/version.js +1 -1
- package/package.json +1 -1
|
@@ -6,6 +6,28 @@ import { useCartFetch } from './hooks.client.js';
|
|
|
6
6
|
import { CartContext } from './context.js';
|
|
7
7
|
import { CART_ID_STORAGE_KEY } from './constants.js';
|
|
8
8
|
import { ClientAnalytics } from '../../foundation/Analytics/ClientAnalytics.js';
|
|
9
|
+
function getLocalStoragePolyfill() {
|
|
10
|
+
const storage = {};
|
|
11
|
+
return {
|
|
12
|
+
removeItem(key) {
|
|
13
|
+
delete storage[key];
|
|
14
|
+
},
|
|
15
|
+
setItem(key, value) {
|
|
16
|
+
storage[key] = value;
|
|
17
|
+
},
|
|
18
|
+
getItem(key) {
|
|
19
|
+
return storage[key];
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
const localStorage = (function () {
|
|
24
|
+
try {
|
|
25
|
+
return window.localStorage || getLocalStoragePolyfill();
|
|
26
|
+
}
|
|
27
|
+
catch (e) {
|
|
28
|
+
return getLocalStoragePolyfill();
|
|
29
|
+
}
|
|
30
|
+
})();
|
|
9
31
|
function cartReducer(state, action) {
|
|
10
32
|
switch (action.type) {
|
|
11
33
|
case 'cartFetch': {
|
|
@@ -179,7 +201,7 @@ export function CartProvider({ children, numCartLines, onCreate, onLineAdd, onLi
|
|
|
179
201
|
},
|
|
180
202
|
});
|
|
181
203
|
if (!data?.cart) {
|
|
182
|
-
|
|
204
|
+
localStorage.removeItem(CART_ID_STORAGE_KEY);
|
|
183
205
|
dispatch({ type: 'resetCart' });
|
|
184
206
|
return;
|
|
185
207
|
}
|
|
@@ -226,7 +248,7 @@ export function CartProvider({ children, numCartLines, onCreate, onLineAdd, onLi
|
|
|
226
248
|
type: 'resolve',
|
|
227
249
|
cart: cartFromGraphQL(data.cartCreate.cart),
|
|
228
250
|
});
|
|
229
|
-
|
|
251
|
+
localStorage.setItem(CART_ID_STORAGE_KEY, data.cartCreate.cart.id);
|
|
230
252
|
}
|
|
231
253
|
}, [
|
|
232
254
|
onCreate,
|
|
@@ -18,6 +18,7 @@ export function CartProviderV2({ children, numCartLines, onCreate, onLineAdd, on
|
|
|
18
18
|
}
|
|
19
19
|
const [cartState, cartSend] = useCartAPIStateMachine({
|
|
20
20
|
numCartLines,
|
|
21
|
+
data: cart,
|
|
21
22
|
cartFragment,
|
|
22
23
|
countryCode,
|
|
23
24
|
onCartActionEntry(context, event) {
|
|
@@ -131,10 +132,7 @@ export function CartProviderV2({ children, numCartLines, onCreate, onLineAdd, on
|
|
|
131
132
|
*/
|
|
132
133
|
useEffect(() => {
|
|
133
134
|
if (!cartReady.current) {
|
|
134
|
-
if (cart) {
|
|
135
|
-
cartSend({ type: 'CART_SET', payload: { cart } });
|
|
136
|
-
}
|
|
137
|
-
else if (storageAvailable('localStorage')) {
|
|
135
|
+
if (!cart && storageAvailable('localStorage')) {
|
|
138
136
|
try {
|
|
139
137
|
const cartId = window.localStorage.getItem(CART_ID_STORAGE_KEY);
|
|
140
138
|
if (cartId) {
|
|
@@ -2,7 +2,7 @@ import { StateMachine } from '@xstate/fsm';
|
|
|
2
2
|
import { CartFragmentFragment } from './graphql/CartFragment.js';
|
|
3
3
|
import { Cart, CartMachineActionEvent, CartMachineContext, CartMachineEvent, CartMachineFetchResultEvent, CartMachineTypeState } from './types.js';
|
|
4
4
|
import { CountryCode } from '../../storefront-api-types.js';
|
|
5
|
-
export declare function useCartAPIStateMachine({ numCartLines, onCartActionEntry, onCartActionOptimisticUI, onCartActionComplete, data, cartFragment, countryCode, }: {
|
|
5
|
+
export declare function useCartAPIStateMachine({ numCartLines, onCartActionEntry, onCartActionOptimisticUI, onCartActionComplete, data: cart, cartFragment, countryCode, }: {
|
|
6
6
|
/** Maximum number of cart lines to fetch. Defaults to 250 cart lines. */
|
|
7
7
|
numCartLines?: number;
|
|
8
8
|
/** A callback that is invoked just before a Cart API action executes. */
|
|
@@ -86,46 +86,52 @@ const UPDATING_CART_EVENTS = {
|
|
|
86
86
|
target: 'discountCodesUpdating',
|
|
87
87
|
},
|
|
88
88
|
};
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
89
|
+
function createCartMachine(initialCart) {
|
|
90
|
+
return createMachine({
|
|
91
|
+
id: 'Cart',
|
|
92
|
+
initial: initialCart ? 'idle' : 'uninitialized',
|
|
93
|
+
context: {
|
|
94
|
+
cart: initialCart && cartFromGraphQL(initialCart),
|
|
95
95
|
},
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
96
|
+
states: {
|
|
97
|
+
uninitialized: {
|
|
98
|
+
on: INITIALIZING_CART_EVENTS,
|
|
99
|
+
},
|
|
100
|
+
cartCompleted: {
|
|
101
|
+
on: INITIALIZING_CART_EVENTS,
|
|
102
|
+
},
|
|
103
|
+
initializationError: {
|
|
104
|
+
on: INITIALIZING_CART_EVENTS,
|
|
105
|
+
},
|
|
106
|
+
idle: {
|
|
107
|
+
on: { ...INITIALIZING_CART_EVENTS, ...UPDATING_CART_EVENTS },
|
|
108
|
+
},
|
|
109
|
+
error: {
|
|
110
|
+
on: { ...INITIALIZING_CART_EVENTS, ...UPDATING_CART_EVENTS },
|
|
111
|
+
},
|
|
112
|
+
cartFetching: invokeCart('cartFetchAction', {
|
|
113
|
+
errorTarget: 'initializationError',
|
|
114
|
+
}),
|
|
115
|
+
cartCreating: invokeCart('cartCreateAction', {
|
|
116
|
+
errorTarget: 'initializationError',
|
|
117
|
+
}),
|
|
118
|
+
cartLineRemoving: invokeCart('cartLineRemoveAction'),
|
|
119
|
+
cartLineUpdating: invokeCart('cartLineUpdateAction'),
|
|
120
|
+
cartLineAdding: invokeCart('cartLineAddAction'),
|
|
121
|
+
noteUpdating: invokeCart('noteUpdateAction'),
|
|
122
|
+
buyerIdentityUpdating: invokeCart('buyerIdentityUpdateAction'),
|
|
123
|
+
cartAttributesUpdating: invokeCart('cartAttributesUpdateAction'),
|
|
124
|
+
discountCodesUpdating: invokeCart('discountCodesUpdateAction'),
|
|
107
125
|
},
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
}),
|
|
111
|
-
cartCreating: invokeCart('cartCreateAction', {
|
|
112
|
-
errorTarget: 'initializationError',
|
|
113
|
-
}),
|
|
114
|
-
cartLineRemoving: invokeCart('cartLineRemoveAction'),
|
|
115
|
-
cartLineUpdating: invokeCart('cartLineUpdateAction'),
|
|
116
|
-
cartLineAdding: invokeCart('cartLineAddAction'),
|
|
117
|
-
noteUpdating: invokeCart('noteUpdateAction'),
|
|
118
|
-
buyerIdentityUpdating: invokeCart('buyerIdentityUpdateAction'),
|
|
119
|
-
cartAttributesUpdating: invokeCart('cartAttributesUpdateAction'),
|
|
120
|
-
discountCodesUpdating: invokeCart('discountCodesUpdateAction'),
|
|
121
|
-
},
|
|
122
|
-
});
|
|
126
|
+
});
|
|
127
|
+
}
|
|
123
128
|
export function useCartAPIStateMachine({ numCartLines, onCartActionEntry, onCartActionOptimisticUI, onCartActionComplete, data: cart, cartFragment, countryCode, }) {
|
|
124
129
|
const { cartFetch, cartCreate, cartLineAdd, cartLineUpdate, cartLineRemove, noteUpdate, buyerIdentityUpdate, cartAttributesUpdate, discountCodesUpdate, } = useCartActions({
|
|
125
130
|
numCartLines,
|
|
126
131
|
cartFragment,
|
|
127
132
|
countryCode,
|
|
128
133
|
});
|
|
134
|
+
const cartMachine = useMemo(() => createCartMachine(cart), [cart]);
|
|
129
135
|
const [state, send, service] = useMachine(cartMachine, {
|
|
130
136
|
actions: {
|
|
131
137
|
cartFetchAction: async (_, event) => {
|
package/dist/esnext/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const LIB_VERSION = "1.4.
|
|
1
|
+
export declare const LIB_VERSION = "1.4.3";
|
package/dist/esnext/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const LIB_VERSION = '1.4.
|
|
1
|
+
export const LIB_VERSION = '1.4.3';
|