@betterstore/react 0.1.2 → 0.1.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/CHANGELOG.md +6 -0
- package/dist/components/cart/useCart.d.ts +31 -0
- package/dist/components/index.d.ts +1 -0
- package/dist/index.cjs.js +268 -41
- package/dist/index.esm.js +268 -42
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
interface LineItem {
|
|
2
|
+
id: string;
|
|
3
|
+
quantity: number;
|
|
4
|
+
productId: string;
|
|
5
|
+
variantOptions: {
|
|
6
|
+
name: string;
|
|
7
|
+
value: string;
|
|
8
|
+
}[];
|
|
9
|
+
metadata?: string;
|
|
10
|
+
}
|
|
11
|
+
type LineItemWithoutId = Omit<LineItem, "id">;
|
|
12
|
+
interface Cart {
|
|
13
|
+
lineItems: LineItem[];
|
|
14
|
+
addItem: (item: LineItemWithoutId) => void;
|
|
15
|
+
removeItem: (id: string) => void;
|
|
16
|
+
updateQuantity: (id: string, quantity: number) => void;
|
|
17
|
+
getProductQuantity: (productId: string) => number;
|
|
18
|
+
clearCart: () => void;
|
|
19
|
+
}
|
|
20
|
+
export declare const useCart: import("zustand").UseBoundStore<Omit<import("zustand").StoreApi<Cart>, "persist"> & {
|
|
21
|
+
persist: {
|
|
22
|
+
setOptions: (options: Partial<import("zustand/middleware").PersistOptions<Cart, Cart>>) => void;
|
|
23
|
+
clearStorage: () => void;
|
|
24
|
+
rehydrate: () => Promise<void> | void;
|
|
25
|
+
hasHydrated: () => boolean;
|
|
26
|
+
onHydrate: (fn: (state: Cart) => void) => () => void;
|
|
27
|
+
onFinishHydration: (fn: (state: Cart) => void) => () => void;
|
|
28
|
+
getOptions: () => Partial<import("zustand/middleware").PersistOptions<Cart, Cart>>;
|
|
29
|
+
};
|
|
30
|
+
}>;
|
|
31
|
+
export {};
|
package/dist/index.cjs.js
CHANGED
|
@@ -2,6 +2,273 @@
|
|
|
2
2
|
|
|
3
3
|
var React = require('react');
|
|
4
4
|
|
|
5
|
+
const createStoreImpl = (createState) => {
|
|
6
|
+
let state;
|
|
7
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
8
|
+
const setState = (partial, replace) => {
|
|
9
|
+
const nextState = typeof partial === "function" ? partial(state) : partial;
|
|
10
|
+
if (!Object.is(nextState, state)) {
|
|
11
|
+
const previousState = state;
|
|
12
|
+
state = (replace != null ? replace : typeof nextState !== "object" || nextState === null) ? nextState : Object.assign({}, state, nextState);
|
|
13
|
+
listeners.forEach((listener) => listener(state, previousState));
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
const getState = () => state;
|
|
17
|
+
const getInitialState = () => initialState;
|
|
18
|
+
const subscribe = (listener) => {
|
|
19
|
+
listeners.add(listener);
|
|
20
|
+
return () => listeners.delete(listener);
|
|
21
|
+
};
|
|
22
|
+
const api = { setState, getState, getInitialState, subscribe };
|
|
23
|
+
const initialState = state = createState(setState, getState, api);
|
|
24
|
+
return api;
|
|
25
|
+
};
|
|
26
|
+
const createStore = (createState) => createState ? createStoreImpl(createState) : createStoreImpl;
|
|
27
|
+
|
|
28
|
+
const identity = (arg) => arg;
|
|
29
|
+
function useStore(api, selector = identity) {
|
|
30
|
+
const slice = React.useSyncExternalStore(
|
|
31
|
+
api.subscribe,
|
|
32
|
+
() => selector(api.getState()),
|
|
33
|
+
() => selector(api.getInitialState())
|
|
34
|
+
);
|
|
35
|
+
React.useDebugValue(slice);
|
|
36
|
+
return slice;
|
|
37
|
+
}
|
|
38
|
+
const createImpl = (createState) => {
|
|
39
|
+
const api = createStore(createState);
|
|
40
|
+
const useBoundStore = (selector) => useStore(api, selector);
|
|
41
|
+
Object.assign(useBoundStore, api);
|
|
42
|
+
return useBoundStore;
|
|
43
|
+
};
|
|
44
|
+
const create = (createState) => createState ? createImpl(createState) : createImpl;
|
|
45
|
+
|
|
46
|
+
function createJSONStorage(getStorage, options) {
|
|
47
|
+
let storage;
|
|
48
|
+
try {
|
|
49
|
+
storage = getStorage();
|
|
50
|
+
} catch (e) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
const persistStorage = {
|
|
54
|
+
getItem: (name) => {
|
|
55
|
+
var _a;
|
|
56
|
+
const parse = (str2) => {
|
|
57
|
+
if (str2 === null) {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
return JSON.parse(str2, undefined );
|
|
61
|
+
};
|
|
62
|
+
const str = (_a = storage.getItem(name)) != null ? _a : null;
|
|
63
|
+
if (str instanceof Promise) {
|
|
64
|
+
return str.then(parse);
|
|
65
|
+
}
|
|
66
|
+
return parse(str);
|
|
67
|
+
},
|
|
68
|
+
setItem: (name, newValue) => storage.setItem(
|
|
69
|
+
name,
|
|
70
|
+
JSON.stringify(newValue, undefined )
|
|
71
|
+
),
|
|
72
|
+
removeItem: (name) => storage.removeItem(name)
|
|
73
|
+
};
|
|
74
|
+
return persistStorage;
|
|
75
|
+
}
|
|
76
|
+
const toThenable = (fn) => (input) => {
|
|
77
|
+
try {
|
|
78
|
+
const result = fn(input);
|
|
79
|
+
if (result instanceof Promise) {
|
|
80
|
+
return result;
|
|
81
|
+
}
|
|
82
|
+
return {
|
|
83
|
+
then(onFulfilled) {
|
|
84
|
+
return toThenable(onFulfilled)(result);
|
|
85
|
+
},
|
|
86
|
+
catch(_onRejected) {
|
|
87
|
+
return this;
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
} catch (e) {
|
|
91
|
+
return {
|
|
92
|
+
then(_onFulfilled) {
|
|
93
|
+
return this;
|
|
94
|
+
},
|
|
95
|
+
catch(onRejected) {
|
|
96
|
+
return toThenable(onRejected)(e);
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
const persistImpl = (config, baseOptions) => (set, get, api) => {
|
|
102
|
+
let options = {
|
|
103
|
+
storage: createJSONStorage(() => localStorage),
|
|
104
|
+
partialize: (state) => state,
|
|
105
|
+
version: 0,
|
|
106
|
+
merge: (persistedState, currentState) => ({
|
|
107
|
+
...currentState,
|
|
108
|
+
...persistedState
|
|
109
|
+
}),
|
|
110
|
+
...baseOptions
|
|
111
|
+
};
|
|
112
|
+
let hasHydrated = false;
|
|
113
|
+
const hydrationListeners = /* @__PURE__ */ new Set();
|
|
114
|
+
const finishHydrationListeners = /* @__PURE__ */ new Set();
|
|
115
|
+
let storage = options.storage;
|
|
116
|
+
if (!storage) {
|
|
117
|
+
return config(
|
|
118
|
+
(...args) => {
|
|
119
|
+
console.warn(
|
|
120
|
+
`[zustand persist middleware] Unable to update item '${options.name}', the given storage is currently unavailable.`
|
|
121
|
+
);
|
|
122
|
+
set(...args);
|
|
123
|
+
},
|
|
124
|
+
get,
|
|
125
|
+
api
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
const setItem = () => {
|
|
129
|
+
const state = options.partialize({ ...get() });
|
|
130
|
+
return storage.setItem(options.name, {
|
|
131
|
+
state,
|
|
132
|
+
version: options.version
|
|
133
|
+
});
|
|
134
|
+
};
|
|
135
|
+
const savedSetState = api.setState;
|
|
136
|
+
api.setState = (state, replace) => {
|
|
137
|
+
savedSetState(state, replace);
|
|
138
|
+
void setItem();
|
|
139
|
+
};
|
|
140
|
+
const configResult = config(
|
|
141
|
+
(...args) => {
|
|
142
|
+
set(...args);
|
|
143
|
+
void setItem();
|
|
144
|
+
},
|
|
145
|
+
get,
|
|
146
|
+
api
|
|
147
|
+
);
|
|
148
|
+
api.getInitialState = () => configResult;
|
|
149
|
+
let stateFromStorage;
|
|
150
|
+
const hydrate = () => {
|
|
151
|
+
var _a, _b;
|
|
152
|
+
if (!storage) return;
|
|
153
|
+
hasHydrated = false;
|
|
154
|
+
hydrationListeners.forEach((cb) => {
|
|
155
|
+
var _a2;
|
|
156
|
+
return cb((_a2 = get()) != null ? _a2 : configResult);
|
|
157
|
+
});
|
|
158
|
+
const postRehydrationCallback = ((_b = options.onRehydrateStorage) == null ? undefined : _b.call(options, (_a = get()) != null ? _a : configResult)) || undefined;
|
|
159
|
+
return toThenable(storage.getItem.bind(storage))(options.name).then((deserializedStorageValue) => {
|
|
160
|
+
if (deserializedStorageValue) {
|
|
161
|
+
if (typeof deserializedStorageValue.version === "number" && deserializedStorageValue.version !== options.version) {
|
|
162
|
+
if (options.migrate) {
|
|
163
|
+
const migration = options.migrate(
|
|
164
|
+
deserializedStorageValue.state,
|
|
165
|
+
deserializedStorageValue.version
|
|
166
|
+
);
|
|
167
|
+
if (migration instanceof Promise) {
|
|
168
|
+
return migration.then((result) => [true, result]);
|
|
169
|
+
}
|
|
170
|
+
return [true, migration];
|
|
171
|
+
}
|
|
172
|
+
console.error(
|
|
173
|
+
`State loaded from storage couldn't be migrated since no migrate function was provided`
|
|
174
|
+
);
|
|
175
|
+
} else {
|
|
176
|
+
return [false, deserializedStorageValue.state];
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
return [false, undefined];
|
|
180
|
+
}).then((migrationResult) => {
|
|
181
|
+
var _a2;
|
|
182
|
+
const [migrated, migratedState] = migrationResult;
|
|
183
|
+
stateFromStorage = options.merge(
|
|
184
|
+
migratedState,
|
|
185
|
+
(_a2 = get()) != null ? _a2 : configResult
|
|
186
|
+
);
|
|
187
|
+
set(stateFromStorage, true);
|
|
188
|
+
if (migrated) {
|
|
189
|
+
return setItem();
|
|
190
|
+
}
|
|
191
|
+
}).then(() => {
|
|
192
|
+
postRehydrationCallback == null ? undefined : postRehydrationCallback(stateFromStorage, undefined);
|
|
193
|
+
stateFromStorage = get();
|
|
194
|
+
hasHydrated = true;
|
|
195
|
+
finishHydrationListeners.forEach((cb) => cb(stateFromStorage));
|
|
196
|
+
}).catch((e) => {
|
|
197
|
+
postRehydrationCallback == null ? undefined : postRehydrationCallback(undefined, e);
|
|
198
|
+
});
|
|
199
|
+
};
|
|
200
|
+
api.persist = {
|
|
201
|
+
setOptions: (newOptions) => {
|
|
202
|
+
options = {
|
|
203
|
+
...options,
|
|
204
|
+
...newOptions
|
|
205
|
+
};
|
|
206
|
+
if (newOptions.storage) {
|
|
207
|
+
storage = newOptions.storage;
|
|
208
|
+
}
|
|
209
|
+
},
|
|
210
|
+
clearStorage: () => {
|
|
211
|
+
storage == null ? undefined : storage.removeItem(options.name);
|
|
212
|
+
},
|
|
213
|
+
getOptions: () => options,
|
|
214
|
+
rehydrate: () => hydrate(),
|
|
215
|
+
hasHydrated: () => hasHydrated,
|
|
216
|
+
onHydrate: (cb) => {
|
|
217
|
+
hydrationListeners.add(cb);
|
|
218
|
+
return () => {
|
|
219
|
+
hydrationListeners.delete(cb);
|
|
220
|
+
};
|
|
221
|
+
},
|
|
222
|
+
onFinishHydration: (cb) => {
|
|
223
|
+
finishHydrationListeners.add(cb);
|
|
224
|
+
return () => {
|
|
225
|
+
finishHydrationListeners.delete(cb);
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
if (!options.skipHydration) {
|
|
230
|
+
hydrate();
|
|
231
|
+
}
|
|
232
|
+
return stateFromStorage || configResult;
|
|
233
|
+
};
|
|
234
|
+
const persist = persistImpl;
|
|
235
|
+
|
|
236
|
+
const generateLineItemId = (item) => {
|
|
237
|
+
return btoa(JSON.stringify({
|
|
238
|
+
productId: item.productId,
|
|
239
|
+
variantOptions: item.variantOptions,
|
|
240
|
+
metadata: item.metadata,
|
|
241
|
+
}));
|
|
242
|
+
};
|
|
243
|
+
const useCart = create()(persist((set, get) => ({
|
|
244
|
+
lineItems: [],
|
|
245
|
+
addItem: (newItem) => set((state) => {
|
|
246
|
+
const id = generateLineItemId(newItem);
|
|
247
|
+
const existingItemIndex = state.lineItems.findIndex((item) => item.id === id);
|
|
248
|
+
if (existingItemIndex !== -1) {
|
|
249
|
+
const updatedItems = [...state.lineItems];
|
|
250
|
+
updatedItems[existingItemIndex] = Object.assign(Object.assign({}, updatedItems[existingItemIndex]), { quantity: updatedItems[existingItemIndex].quantity + newItem.quantity });
|
|
251
|
+
return { lineItems: updatedItems };
|
|
252
|
+
}
|
|
253
|
+
return {
|
|
254
|
+
lineItems: [...state.lineItems, Object.assign(Object.assign({}, newItem), { id })],
|
|
255
|
+
};
|
|
256
|
+
}),
|
|
257
|
+
removeItem: (id) => set((state) => ({
|
|
258
|
+
lineItems: state.lineItems.filter((i) => i.id !== id),
|
|
259
|
+
})),
|
|
260
|
+
updateQuantity: (id, quantity) => set((state) => ({
|
|
261
|
+
lineItems: state.lineItems.map((i) => i.id === id ? Object.assign(Object.assign({}, i), { quantity }) : i),
|
|
262
|
+
})),
|
|
263
|
+
getProductQuantity: (productId) => {
|
|
264
|
+
const items = get().lineItems.filter((item) => item.productId === productId);
|
|
265
|
+
return items.reduce((acc, item) => acc + item.quantity, 0);
|
|
266
|
+
},
|
|
267
|
+
clearCart: () => set({ lineItems: [] }),
|
|
268
|
+
}), {
|
|
269
|
+
name: "cart",
|
|
270
|
+
}));
|
|
271
|
+
|
|
5
272
|
/******************************************************************************
|
|
6
273
|
Copyright (c) Microsoft Corporation.
|
|
7
274
|
|
|
@@ -2216,47 +2483,6 @@ var loadStripe = function loadStripe() {
|
|
|
2216
2483
|
});
|
|
2217
2484
|
};
|
|
2218
2485
|
|
|
2219
|
-
const createStoreImpl = (createState) => {
|
|
2220
|
-
let state;
|
|
2221
|
-
const listeners = /* @__PURE__ */ new Set();
|
|
2222
|
-
const setState = (partial, replace) => {
|
|
2223
|
-
const nextState = typeof partial === "function" ? partial(state) : partial;
|
|
2224
|
-
if (!Object.is(nextState, state)) {
|
|
2225
|
-
const previousState = state;
|
|
2226
|
-
state = (replace != null ? replace : typeof nextState !== "object" || nextState === null) ? nextState : Object.assign({}, state, nextState);
|
|
2227
|
-
listeners.forEach((listener) => listener(state, previousState));
|
|
2228
|
-
}
|
|
2229
|
-
};
|
|
2230
|
-
const getState = () => state;
|
|
2231
|
-
const getInitialState = () => initialState;
|
|
2232
|
-
const subscribe = (listener) => {
|
|
2233
|
-
listeners.add(listener);
|
|
2234
|
-
return () => listeners.delete(listener);
|
|
2235
|
-
};
|
|
2236
|
-
const api = { setState, getState, getInitialState, subscribe };
|
|
2237
|
-
const initialState = state = createState(setState, getState, api);
|
|
2238
|
-
return api;
|
|
2239
|
-
};
|
|
2240
|
-
const createStore = (createState) => createState ? createStoreImpl(createState) : createStoreImpl;
|
|
2241
|
-
|
|
2242
|
-
const identity = (arg) => arg;
|
|
2243
|
-
function useStore(api, selector = identity) {
|
|
2244
|
-
const slice = React.useSyncExternalStore(
|
|
2245
|
-
api.subscribe,
|
|
2246
|
-
() => selector(api.getState()),
|
|
2247
|
-
() => selector(api.getInitialState())
|
|
2248
|
-
);
|
|
2249
|
-
React.useDebugValue(slice);
|
|
2250
|
-
return slice;
|
|
2251
|
-
}
|
|
2252
|
-
const createImpl = (createState) => {
|
|
2253
|
-
const api = createStore(createState);
|
|
2254
|
-
const useBoundStore = (selector) => useStore(api, selector);
|
|
2255
|
-
Object.assign(useBoundStore, api);
|
|
2256
|
-
return useBoundStore;
|
|
2257
|
-
};
|
|
2258
|
-
const create = (createState) => createState ? createImpl(createState) : createImpl;
|
|
2259
|
-
|
|
2260
2486
|
const useCheckout = create((set) => ({
|
|
2261
2487
|
isSubmitting: false,
|
|
2262
2488
|
setIsSubmitting: (isSubmitting) => set({ isSubmitting }),
|
|
@@ -2309,4 +2535,5 @@ var index = React.memo(PaymentElement);
|
|
|
2309
2535
|
|
|
2310
2536
|
exports.CheckoutEmbed = index$1;
|
|
2311
2537
|
exports.PaymentElement = index;
|
|
2538
|
+
exports.useCart = useCart;
|
|
2312
2539
|
exports.useCheckout = useCheckout;
|
package/dist/index.esm.js
CHANGED
|
@@ -1,5 +1,272 @@
|
|
|
1
1
|
import React, { memo, useState, useEffect } from 'react';
|
|
2
2
|
|
|
3
|
+
const createStoreImpl = (createState) => {
|
|
4
|
+
let state;
|
|
5
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
6
|
+
const setState = (partial, replace) => {
|
|
7
|
+
const nextState = typeof partial === "function" ? partial(state) : partial;
|
|
8
|
+
if (!Object.is(nextState, state)) {
|
|
9
|
+
const previousState = state;
|
|
10
|
+
state = (replace != null ? replace : typeof nextState !== "object" || nextState === null) ? nextState : Object.assign({}, state, nextState);
|
|
11
|
+
listeners.forEach((listener) => listener(state, previousState));
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
const getState = () => state;
|
|
15
|
+
const getInitialState = () => initialState;
|
|
16
|
+
const subscribe = (listener) => {
|
|
17
|
+
listeners.add(listener);
|
|
18
|
+
return () => listeners.delete(listener);
|
|
19
|
+
};
|
|
20
|
+
const api = { setState, getState, getInitialState, subscribe };
|
|
21
|
+
const initialState = state = createState(setState, getState, api);
|
|
22
|
+
return api;
|
|
23
|
+
};
|
|
24
|
+
const createStore = (createState) => createState ? createStoreImpl(createState) : createStoreImpl;
|
|
25
|
+
|
|
26
|
+
const identity = (arg) => arg;
|
|
27
|
+
function useStore(api, selector = identity) {
|
|
28
|
+
const slice = React.useSyncExternalStore(
|
|
29
|
+
api.subscribe,
|
|
30
|
+
() => selector(api.getState()),
|
|
31
|
+
() => selector(api.getInitialState())
|
|
32
|
+
);
|
|
33
|
+
React.useDebugValue(slice);
|
|
34
|
+
return slice;
|
|
35
|
+
}
|
|
36
|
+
const createImpl = (createState) => {
|
|
37
|
+
const api = createStore(createState);
|
|
38
|
+
const useBoundStore = (selector) => useStore(api, selector);
|
|
39
|
+
Object.assign(useBoundStore, api);
|
|
40
|
+
return useBoundStore;
|
|
41
|
+
};
|
|
42
|
+
const create = (createState) => createState ? createImpl(createState) : createImpl;
|
|
43
|
+
|
|
44
|
+
function createJSONStorage(getStorage, options) {
|
|
45
|
+
let storage;
|
|
46
|
+
try {
|
|
47
|
+
storage = getStorage();
|
|
48
|
+
} catch (e) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
const persistStorage = {
|
|
52
|
+
getItem: (name) => {
|
|
53
|
+
var _a;
|
|
54
|
+
const parse = (str2) => {
|
|
55
|
+
if (str2 === null) {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
return JSON.parse(str2, undefined );
|
|
59
|
+
};
|
|
60
|
+
const str = (_a = storage.getItem(name)) != null ? _a : null;
|
|
61
|
+
if (str instanceof Promise) {
|
|
62
|
+
return str.then(parse);
|
|
63
|
+
}
|
|
64
|
+
return parse(str);
|
|
65
|
+
},
|
|
66
|
+
setItem: (name, newValue) => storage.setItem(
|
|
67
|
+
name,
|
|
68
|
+
JSON.stringify(newValue, undefined )
|
|
69
|
+
),
|
|
70
|
+
removeItem: (name) => storage.removeItem(name)
|
|
71
|
+
};
|
|
72
|
+
return persistStorage;
|
|
73
|
+
}
|
|
74
|
+
const toThenable = (fn) => (input) => {
|
|
75
|
+
try {
|
|
76
|
+
const result = fn(input);
|
|
77
|
+
if (result instanceof Promise) {
|
|
78
|
+
return result;
|
|
79
|
+
}
|
|
80
|
+
return {
|
|
81
|
+
then(onFulfilled) {
|
|
82
|
+
return toThenable(onFulfilled)(result);
|
|
83
|
+
},
|
|
84
|
+
catch(_onRejected) {
|
|
85
|
+
return this;
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
} catch (e) {
|
|
89
|
+
return {
|
|
90
|
+
then(_onFulfilled) {
|
|
91
|
+
return this;
|
|
92
|
+
},
|
|
93
|
+
catch(onRejected) {
|
|
94
|
+
return toThenable(onRejected)(e);
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
const persistImpl = (config, baseOptions) => (set, get, api) => {
|
|
100
|
+
let options = {
|
|
101
|
+
storage: createJSONStorage(() => localStorage),
|
|
102
|
+
partialize: (state) => state,
|
|
103
|
+
version: 0,
|
|
104
|
+
merge: (persistedState, currentState) => ({
|
|
105
|
+
...currentState,
|
|
106
|
+
...persistedState
|
|
107
|
+
}),
|
|
108
|
+
...baseOptions
|
|
109
|
+
};
|
|
110
|
+
let hasHydrated = false;
|
|
111
|
+
const hydrationListeners = /* @__PURE__ */ new Set();
|
|
112
|
+
const finishHydrationListeners = /* @__PURE__ */ new Set();
|
|
113
|
+
let storage = options.storage;
|
|
114
|
+
if (!storage) {
|
|
115
|
+
return config(
|
|
116
|
+
(...args) => {
|
|
117
|
+
console.warn(
|
|
118
|
+
`[zustand persist middleware] Unable to update item '${options.name}', the given storage is currently unavailable.`
|
|
119
|
+
);
|
|
120
|
+
set(...args);
|
|
121
|
+
},
|
|
122
|
+
get,
|
|
123
|
+
api
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
const setItem = () => {
|
|
127
|
+
const state = options.partialize({ ...get() });
|
|
128
|
+
return storage.setItem(options.name, {
|
|
129
|
+
state,
|
|
130
|
+
version: options.version
|
|
131
|
+
});
|
|
132
|
+
};
|
|
133
|
+
const savedSetState = api.setState;
|
|
134
|
+
api.setState = (state, replace) => {
|
|
135
|
+
savedSetState(state, replace);
|
|
136
|
+
void setItem();
|
|
137
|
+
};
|
|
138
|
+
const configResult = config(
|
|
139
|
+
(...args) => {
|
|
140
|
+
set(...args);
|
|
141
|
+
void setItem();
|
|
142
|
+
},
|
|
143
|
+
get,
|
|
144
|
+
api
|
|
145
|
+
);
|
|
146
|
+
api.getInitialState = () => configResult;
|
|
147
|
+
let stateFromStorage;
|
|
148
|
+
const hydrate = () => {
|
|
149
|
+
var _a, _b;
|
|
150
|
+
if (!storage) return;
|
|
151
|
+
hasHydrated = false;
|
|
152
|
+
hydrationListeners.forEach((cb) => {
|
|
153
|
+
var _a2;
|
|
154
|
+
return cb((_a2 = get()) != null ? _a2 : configResult);
|
|
155
|
+
});
|
|
156
|
+
const postRehydrationCallback = ((_b = options.onRehydrateStorage) == null ? undefined : _b.call(options, (_a = get()) != null ? _a : configResult)) || undefined;
|
|
157
|
+
return toThenable(storage.getItem.bind(storage))(options.name).then((deserializedStorageValue) => {
|
|
158
|
+
if (deserializedStorageValue) {
|
|
159
|
+
if (typeof deserializedStorageValue.version === "number" && deserializedStorageValue.version !== options.version) {
|
|
160
|
+
if (options.migrate) {
|
|
161
|
+
const migration = options.migrate(
|
|
162
|
+
deserializedStorageValue.state,
|
|
163
|
+
deserializedStorageValue.version
|
|
164
|
+
);
|
|
165
|
+
if (migration instanceof Promise) {
|
|
166
|
+
return migration.then((result) => [true, result]);
|
|
167
|
+
}
|
|
168
|
+
return [true, migration];
|
|
169
|
+
}
|
|
170
|
+
console.error(
|
|
171
|
+
`State loaded from storage couldn't be migrated since no migrate function was provided`
|
|
172
|
+
);
|
|
173
|
+
} else {
|
|
174
|
+
return [false, deserializedStorageValue.state];
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return [false, undefined];
|
|
178
|
+
}).then((migrationResult) => {
|
|
179
|
+
var _a2;
|
|
180
|
+
const [migrated, migratedState] = migrationResult;
|
|
181
|
+
stateFromStorage = options.merge(
|
|
182
|
+
migratedState,
|
|
183
|
+
(_a2 = get()) != null ? _a2 : configResult
|
|
184
|
+
);
|
|
185
|
+
set(stateFromStorage, true);
|
|
186
|
+
if (migrated) {
|
|
187
|
+
return setItem();
|
|
188
|
+
}
|
|
189
|
+
}).then(() => {
|
|
190
|
+
postRehydrationCallback == null ? undefined : postRehydrationCallback(stateFromStorage, undefined);
|
|
191
|
+
stateFromStorage = get();
|
|
192
|
+
hasHydrated = true;
|
|
193
|
+
finishHydrationListeners.forEach((cb) => cb(stateFromStorage));
|
|
194
|
+
}).catch((e) => {
|
|
195
|
+
postRehydrationCallback == null ? undefined : postRehydrationCallback(undefined, e);
|
|
196
|
+
});
|
|
197
|
+
};
|
|
198
|
+
api.persist = {
|
|
199
|
+
setOptions: (newOptions) => {
|
|
200
|
+
options = {
|
|
201
|
+
...options,
|
|
202
|
+
...newOptions
|
|
203
|
+
};
|
|
204
|
+
if (newOptions.storage) {
|
|
205
|
+
storage = newOptions.storage;
|
|
206
|
+
}
|
|
207
|
+
},
|
|
208
|
+
clearStorage: () => {
|
|
209
|
+
storage == null ? undefined : storage.removeItem(options.name);
|
|
210
|
+
},
|
|
211
|
+
getOptions: () => options,
|
|
212
|
+
rehydrate: () => hydrate(),
|
|
213
|
+
hasHydrated: () => hasHydrated,
|
|
214
|
+
onHydrate: (cb) => {
|
|
215
|
+
hydrationListeners.add(cb);
|
|
216
|
+
return () => {
|
|
217
|
+
hydrationListeners.delete(cb);
|
|
218
|
+
};
|
|
219
|
+
},
|
|
220
|
+
onFinishHydration: (cb) => {
|
|
221
|
+
finishHydrationListeners.add(cb);
|
|
222
|
+
return () => {
|
|
223
|
+
finishHydrationListeners.delete(cb);
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
if (!options.skipHydration) {
|
|
228
|
+
hydrate();
|
|
229
|
+
}
|
|
230
|
+
return stateFromStorage || configResult;
|
|
231
|
+
};
|
|
232
|
+
const persist = persistImpl;
|
|
233
|
+
|
|
234
|
+
const generateLineItemId = (item) => {
|
|
235
|
+
return btoa(JSON.stringify({
|
|
236
|
+
productId: item.productId,
|
|
237
|
+
variantOptions: item.variantOptions,
|
|
238
|
+
metadata: item.metadata,
|
|
239
|
+
}));
|
|
240
|
+
};
|
|
241
|
+
const useCart = create()(persist((set, get) => ({
|
|
242
|
+
lineItems: [],
|
|
243
|
+
addItem: (newItem) => set((state) => {
|
|
244
|
+
const id = generateLineItemId(newItem);
|
|
245
|
+
const existingItemIndex = state.lineItems.findIndex((item) => item.id === id);
|
|
246
|
+
if (existingItemIndex !== -1) {
|
|
247
|
+
const updatedItems = [...state.lineItems];
|
|
248
|
+
updatedItems[existingItemIndex] = Object.assign(Object.assign({}, updatedItems[existingItemIndex]), { quantity: updatedItems[existingItemIndex].quantity + newItem.quantity });
|
|
249
|
+
return { lineItems: updatedItems };
|
|
250
|
+
}
|
|
251
|
+
return {
|
|
252
|
+
lineItems: [...state.lineItems, Object.assign(Object.assign({}, newItem), { id })],
|
|
253
|
+
};
|
|
254
|
+
}),
|
|
255
|
+
removeItem: (id) => set((state) => ({
|
|
256
|
+
lineItems: state.lineItems.filter((i) => i.id !== id),
|
|
257
|
+
})),
|
|
258
|
+
updateQuantity: (id, quantity) => set((state) => ({
|
|
259
|
+
lineItems: state.lineItems.map((i) => i.id === id ? Object.assign(Object.assign({}, i), { quantity }) : i),
|
|
260
|
+
})),
|
|
261
|
+
getProductQuantity: (productId) => {
|
|
262
|
+
const items = get().lineItems.filter((item) => item.productId === productId);
|
|
263
|
+
return items.reduce((acc, item) => acc + item.quantity, 0);
|
|
264
|
+
},
|
|
265
|
+
clearCart: () => set({ lineItems: [] }),
|
|
266
|
+
}), {
|
|
267
|
+
name: "cart",
|
|
268
|
+
}));
|
|
269
|
+
|
|
3
270
|
/******************************************************************************
|
|
4
271
|
Copyright (c) Microsoft Corporation.
|
|
5
272
|
|
|
@@ -2214,47 +2481,6 @@ var loadStripe = function loadStripe() {
|
|
|
2214
2481
|
});
|
|
2215
2482
|
};
|
|
2216
2483
|
|
|
2217
|
-
const createStoreImpl = (createState) => {
|
|
2218
|
-
let state;
|
|
2219
|
-
const listeners = /* @__PURE__ */ new Set();
|
|
2220
|
-
const setState = (partial, replace) => {
|
|
2221
|
-
const nextState = typeof partial === "function" ? partial(state) : partial;
|
|
2222
|
-
if (!Object.is(nextState, state)) {
|
|
2223
|
-
const previousState = state;
|
|
2224
|
-
state = (replace != null ? replace : typeof nextState !== "object" || nextState === null) ? nextState : Object.assign({}, state, nextState);
|
|
2225
|
-
listeners.forEach((listener) => listener(state, previousState));
|
|
2226
|
-
}
|
|
2227
|
-
};
|
|
2228
|
-
const getState = () => state;
|
|
2229
|
-
const getInitialState = () => initialState;
|
|
2230
|
-
const subscribe = (listener) => {
|
|
2231
|
-
listeners.add(listener);
|
|
2232
|
-
return () => listeners.delete(listener);
|
|
2233
|
-
};
|
|
2234
|
-
const api = { setState, getState, getInitialState, subscribe };
|
|
2235
|
-
const initialState = state = createState(setState, getState, api);
|
|
2236
|
-
return api;
|
|
2237
|
-
};
|
|
2238
|
-
const createStore = (createState) => createState ? createStoreImpl(createState) : createStoreImpl;
|
|
2239
|
-
|
|
2240
|
-
const identity = (arg) => arg;
|
|
2241
|
-
function useStore(api, selector = identity) {
|
|
2242
|
-
const slice = React.useSyncExternalStore(
|
|
2243
|
-
api.subscribe,
|
|
2244
|
-
() => selector(api.getState()),
|
|
2245
|
-
() => selector(api.getInitialState())
|
|
2246
|
-
);
|
|
2247
|
-
React.useDebugValue(slice);
|
|
2248
|
-
return slice;
|
|
2249
|
-
}
|
|
2250
|
-
const createImpl = (createState) => {
|
|
2251
|
-
const api = createStore(createState);
|
|
2252
|
-
const useBoundStore = (selector) => useStore(api, selector);
|
|
2253
|
-
Object.assign(useBoundStore, api);
|
|
2254
|
-
return useBoundStore;
|
|
2255
|
-
};
|
|
2256
|
-
const create = (createState) => createState ? createImpl(createState) : createImpl;
|
|
2257
|
-
|
|
2258
2484
|
const useCheckout = create((set) => ({
|
|
2259
2485
|
isSubmitting: false,
|
|
2260
2486
|
setIsSubmitting: (isSubmitting) => set({ isSubmitting }),
|
|
@@ -2305,4 +2531,4 @@ function PaymentElement({ paymentSecret, checkoutAppearance, onSuccess, onError,
|
|
|
2305
2531
|
}
|
|
2306
2532
|
var index = memo(PaymentElement);
|
|
2307
2533
|
|
|
2308
|
-
export { index$1 as CheckoutEmbed, index as PaymentElement, useCheckout };
|
|
2534
|
+
export { index$1 as CheckoutEmbed, index as PaymentElement, useCart, useCheckout };
|