@betterstore/react 0.1.2 → 0.1.4

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 CHANGED
@@ -1,5 +1,17 @@
1
1
  # @betterstore/sdk
2
2
 
3
+ ## 0.1.4
4
+
5
+ ### Patch Changes
6
+
7
+ - usecart tweaks
8
+
9
+ ## 0.1.3
10
+
11
+ ### Patch Changes
12
+
13
+ - usecart hook added
14
+
3
15
  ## 0.1.2
4
16
 
5
17
  ### Patch Changes
@@ -0,0 +1,39 @@
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 LineItemOptionalParams = {
12
+ quantity?: number;
13
+ productId: string;
14
+ variantOptions?: {
15
+ name: string;
16
+ value: string;
17
+ }[];
18
+ metadata?: string;
19
+ };
20
+ interface Cart {
21
+ lineItems: LineItem[];
22
+ addItem: (productId: string, item: LineItemOptionalParams) => void;
23
+ removeItem: (id: string) => void;
24
+ updateQuantity: (id: string, quantity: number) => void;
25
+ getProductQuantity: (productId: string) => number;
26
+ clearCart: () => void;
27
+ }
28
+ export declare const useCart: import("zustand").UseBoundStore<Omit<import("zustand").StoreApi<Cart>, "persist"> & {
29
+ persist: {
30
+ setOptions: (options: Partial<import("zustand/middleware").PersistOptions<Cart, Cart>>) => void;
31
+ clearStorage: () => void;
32
+ rehydrate: () => Promise<void> | void;
33
+ hasHydrated: () => boolean;
34
+ onHydrate: (fn: (state: Cart) => void) => () => void;
35
+ onFinishHydration: (fn: (state: Cart) => void) => () => void;
36
+ getOptions: () => Partial<import("zustand/middleware").PersistOptions<Cart, Cart>>;
37
+ };
38
+ }>;
39
+ export {};
@@ -1,3 +1,4 @@
1
+ export { useCart } from "./cart/useCart";
1
2
  export { default as CheckoutEmbed } from "./checkout-embed";
2
3
  export { default as PaymentElement } from "./payment-element";
3
4
  export { useCheckout } from "./payment-element/useCheckout";
package/dist/index.cjs.js CHANGED
@@ -2,6 +2,281 @@
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: (productId, newItem) => set((state) => {
246
+ var _a, _b;
247
+ const formattedNewItem = {
248
+ productId: productId,
249
+ quantity: (_a = newItem.quantity) !== null && _a !== void 0 ? _a : 1,
250
+ variantOptions: (_b = newItem.variantOptions) !== null && _b !== void 0 ? _b : [],
251
+ metadata: newItem.metadata,
252
+ };
253
+ const id = generateLineItemId(formattedNewItem);
254
+ const existingItemIndex = state.lineItems.findIndex((item) => item.id === id);
255
+ if (existingItemIndex !== -1) {
256
+ const updatedItems = [...state.lineItems];
257
+ updatedItems[existingItemIndex] = Object.assign(Object.assign({}, updatedItems[existingItemIndex]), { quantity: updatedItems[existingItemIndex].quantity +
258
+ formattedNewItem.quantity });
259
+ return { lineItems: updatedItems };
260
+ }
261
+ return {
262
+ lineItems: [...state.lineItems, Object.assign(Object.assign({}, formattedNewItem), { id })],
263
+ };
264
+ }),
265
+ removeItem: (id) => set((state) => ({
266
+ lineItems: state.lineItems.filter((i) => i.id !== id),
267
+ })),
268
+ updateQuantity: (id, quantity) => set((state) => ({
269
+ lineItems: state.lineItems.map((i) => i.id === id ? Object.assign(Object.assign({}, i), { quantity }) : i),
270
+ })),
271
+ getProductQuantity: (productId) => {
272
+ const items = get().lineItems.filter((item) => item.productId === productId);
273
+ return items.reduce((acc, item) => acc + item.quantity, 0);
274
+ },
275
+ clearCart: () => set({ lineItems: [] }),
276
+ }), {
277
+ name: "cart",
278
+ }));
279
+
5
280
  /******************************************************************************
6
281
  Copyright (c) Microsoft Corporation.
7
282
 
@@ -2216,47 +2491,6 @@ var loadStripe = function loadStripe() {
2216
2491
  });
2217
2492
  };
2218
2493
 
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
2494
  const useCheckout = create((set) => ({
2261
2495
  isSubmitting: false,
2262
2496
  setIsSubmitting: (isSubmitting) => set({ isSubmitting }),
@@ -2309,4 +2543,5 @@ var index = React.memo(PaymentElement);
2309
2543
 
2310
2544
  exports.CheckoutEmbed = index$1;
2311
2545
  exports.PaymentElement = index;
2546
+ exports.useCart = useCart;
2312
2547
  exports.useCheckout = useCheckout;
package/dist/index.esm.js CHANGED
@@ -1,5 +1,280 @@
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: (productId, newItem) => set((state) => {
244
+ var _a, _b;
245
+ const formattedNewItem = {
246
+ productId: productId,
247
+ quantity: (_a = newItem.quantity) !== null && _a !== void 0 ? _a : 1,
248
+ variantOptions: (_b = newItem.variantOptions) !== null && _b !== void 0 ? _b : [],
249
+ metadata: newItem.metadata,
250
+ };
251
+ const id = generateLineItemId(formattedNewItem);
252
+ const existingItemIndex = state.lineItems.findIndex((item) => item.id === id);
253
+ if (existingItemIndex !== -1) {
254
+ const updatedItems = [...state.lineItems];
255
+ updatedItems[existingItemIndex] = Object.assign(Object.assign({}, updatedItems[existingItemIndex]), { quantity: updatedItems[existingItemIndex].quantity +
256
+ formattedNewItem.quantity });
257
+ return { lineItems: updatedItems };
258
+ }
259
+ return {
260
+ lineItems: [...state.lineItems, Object.assign(Object.assign({}, formattedNewItem), { id })],
261
+ };
262
+ }),
263
+ removeItem: (id) => set((state) => ({
264
+ lineItems: state.lineItems.filter((i) => i.id !== id),
265
+ })),
266
+ updateQuantity: (id, quantity) => set((state) => ({
267
+ lineItems: state.lineItems.map((i) => i.id === id ? Object.assign(Object.assign({}, i), { quantity }) : i),
268
+ })),
269
+ getProductQuantity: (productId) => {
270
+ const items = get().lineItems.filter((item) => item.productId === productId);
271
+ return items.reduce((acc, item) => acc + item.quantity, 0);
272
+ },
273
+ clearCart: () => set({ lineItems: [] }),
274
+ }), {
275
+ name: "cart",
276
+ }));
277
+
3
278
  /******************************************************************************
4
279
  Copyright (c) Microsoft Corporation.
5
280
 
@@ -2214,47 +2489,6 @@ var loadStripe = function loadStripe() {
2214
2489
  });
2215
2490
  };
2216
2491
 
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
2492
  const useCheckout = create((set) => ({
2259
2493
  isSubmitting: false,
2260
2494
  setIsSubmitting: (isSubmitting) => set({ isSubmitting }),
@@ -2305,4 +2539,4 @@ function PaymentElement({ paymentSecret, checkoutAppearance, onSuccess, onError,
2305
2539
  }
2306
2540
  var index = memo(PaymentElement);
2307
2541
 
2308
- export { index$1 as CheckoutEmbed, index as PaymentElement, useCheckout };
2542
+ export { index$1 as CheckoutEmbed, index as PaymentElement, useCart, useCheckout };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@betterstore/react",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "E-commerce for Developers",
5
5
  "private": false,
6
6
  "publishConfig": {