@withvlibe/base-sdk 1.0.1 → 1.1.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/dist/{VlibeBaseAuth-BxS9T0vB.d.mts → VlibeBaseEcommerce-B6l17uup.d.mts} +198 -8
- package/dist/{VlibeBaseAuth-BxS9T0vB.d.ts → VlibeBaseEcommerce-B6l17uup.d.ts} +198 -8
- package/dist/index.d.mts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +518 -0
- package/dist/index.mjs +517 -0
- package/dist/react.d.mts +107 -3
- package/dist/react.d.ts +107 -3
- package/dist/react.js +173 -2
- package/dist/react.mjs +169 -1
- package/package.json +1 -1
package/dist/react.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export {
|
|
1
|
+
import { k as BaseRecord, V as VlibeBaseDatabase, I as UseCollectionOptions, H as UseCollectionReturn, J as UseKVReturn, c as VlibeBaseAuth, K as UseAuthReturn, d as VlibeBaseEcommerce, q as Product, u as CreateProductInput, t as CartItem, s as Address, O as Order } from './VlibeBaseEcommerce-B6l17uup.js';
|
|
2
|
+
export { v as CreateOrderInput, L as UsePaymentsReturn, m as VlibeUser } from './VlibeBaseEcommerce-B6l17uup.js';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* React hook for working with a database collection
|
|
@@ -93,4 +93,108 @@ declare function useKV<T = unknown>(db: VlibeBaseDatabase, key: string): UseKVRe
|
|
|
93
93
|
*/
|
|
94
94
|
declare function useAuth(auth: VlibeBaseAuth, initialToken?: string | null): UseAuthReturn;
|
|
95
95
|
|
|
96
|
-
|
|
96
|
+
interface UseProductsOptions {
|
|
97
|
+
category?: string;
|
|
98
|
+
isActive?: boolean;
|
|
99
|
+
autoLoad?: boolean;
|
|
100
|
+
}
|
|
101
|
+
interface UseProductsReturn {
|
|
102
|
+
products: Product[];
|
|
103
|
+
loading: boolean;
|
|
104
|
+
error: Error | null;
|
|
105
|
+
refresh: () => Promise<void>;
|
|
106
|
+
createProduct: (input: CreateProductInput) => Promise<Product>;
|
|
107
|
+
updateProduct: (productId: string, updates: Partial<Product>) => Promise<Product>;
|
|
108
|
+
deleteProduct: (productId: string) => Promise<void>;
|
|
109
|
+
updateInventory: (productId: string, quantity: number, operation: 'set' | 'increment' | 'decrement') => Promise<Product>;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* React hook for managing products
|
|
113
|
+
*
|
|
114
|
+
* @param ecommerce - VlibeBaseEcommerce instance
|
|
115
|
+
* @param options - Optional filtering and configuration
|
|
116
|
+
* @returns Product list with CRUD operations
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* ```tsx
|
|
120
|
+
* const { products, loading, createProduct } = useProducts(ecommerce, { isActive: true });
|
|
121
|
+
*
|
|
122
|
+
* const handleCreate = async () => {
|
|
123
|
+
* await createProduct({
|
|
124
|
+
* name: 'T-Shirt',
|
|
125
|
+
* price: 2999,
|
|
126
|
+
* stock: 50,
|
|
127
|
+
* currency: 'usd'
|
|
128
|
+
* });
|
|
129
|
+
* };
|
|
130
|
+
* ```
|
|
131
|
+
*/
|
|
132
|
+
declare function useProducts(ecommerce: VlibeBaseEcommerce, options?: UseProductsOptions): UseProductsReturn;
|
|
133
|
+
|
|
134
|
+
interface UseCartReturn {
|
|
135
|
+
cart: CartItem[];
|
|
136
|
+
itemCount: number;
|
|
137
|
+
loading: boolean;
|
|
138
|
+
error: Error | null;
|
|
139
|
+
refresh: () => Promise<void>;
|
|
140
|
+
addItem: (item: CartItem) => Promise<CartItem[]>;
|
|
141
|
+
updateItem: (productId: string, quantity: number) => Promise<CartItem[]>;
|
|
142
|
+
removeItem: (productId: string) => Promise<CartItem[]>;
|
|
143
|
+
clear: () => Promise<void>;
|
|
144
|
+
checkout: (shippingAddress: Address, paymentMethodId?: string) => Promise<Order>;
|
|
145
|
+
calculateTotal: () => Promise<any>;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* React hook for managing shopping cart
|
|
149
|
+
*
|
|
150
|
+
* @param ecommerce - VlibeBaseEcommerce instance
|
|
151
|
+
* @param userId - User ID for cart ownership
|
|
152
|
+
* @returns Shopping cart with add/update/remove/checkout operations
|
|
153
|
+
*
|
|
154
|
+
* @example
|
|
155
|
+
* ```tsx
|
|
156
|
+
* const { cart, itemCount, addItem, checkout } = useCart(ecommerce, userId);
|
|
157
|
+
*
|
|
158
|
+
* const handleAddToCart = async (productId: string) => {
|
|
159
|
+
* await addItem({ productId, quantity: 1 });
|
|
160
|
+
* };
|
|
161
|
+
*
|
|
162
|
+
* const handleCheckout = async () => {
|
|
163
|
+
* const order = await checkout(shippingAddress, paymentMethodId);
|
|
164
|
+
* };
|
|
165
|
+
* ```
|
|
166
|
+
*/
|
|
167
|
+
declare function useCart(ecommerce: VlibeBaseEcommerce, userId: string): UseCartReturn;
|
|
168
|
+
|
|
169
|
+
interface UseOrdersOptions {
|
|
170
|
+
userId?: string;
|
|
171
|
+
status?: Order['status'];
|
|
172
|
+
autoLoad?: boolean;
|
|
173
|
+
}
|
|
174
|
+
interface UseOrdersReturn {
|
|
175
|
+
orders: Order[];
|
|
176
|
+
loading: boolean;
|
|
177
|
+
error: Error | null;
|
|
178
|
+
refresh: () => Promise<void>;
|
|
179
|
+
updateStatus: (orderId: string, status: Order['status']) => Promise<Order>;
|
|
180
|
+
cancelOrder: (orderId: string, restoreInventory?: boolean) => Promise<Order>;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* React hook for managing orders
|
|
184
|
+
*
|
|
185
|
+
* @param ecommerce - VlibeBaseEcommerce instance
|
|
186
|
+
* @param options - Optional filtering and configuration
|
|
187
|
+
* @returns Order list with status management operations
|
|
188
|
+
*
|
|
189
|
+
* @example
|
|
190
|
+
* ```tsx
|
|
191
|
+
* const { orders, loading, updateStatus } = useOrders(ecommerce, { userId: 'user123' });
|
|
192
|
+
*
|
|
193
|
+
* const handleShip = async (orderId: string) => {
|
|
194
|
+
* await updateStatus(orderId, 'shipped');
|
|
195
|
+
* };
|
|
196
|
+
* ```
|
|
197
|
+
*/
|
|
198
|
+
declare function useOrders(ecommerce: VlibeBaseEcommerce, options?: UseOrdersOptions): UseOrdersReturn;
|
|
199
|
+
|
|
200
|
+
export { BaseRecord, CartItem, CreateProductInput, Order, Product, UseAuthReturn, type UseCartReturn, UseCollectionOptions, UseCollectionReturn, UseKVReturn, type UseOrdersOptions, type UseOrdersReturn, type UseProductsOptions, type UseProductsReturn, useAuth, useCart, useCollection, useKV, useOrders, useProducts };
|
package/dist/react.js
CHANGED
|
@@ -22,8 +22,11 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
22
22
|
var react_exports = {};
|
|
23
23
|
__export(react_exports, {
|
|
24
24
|
useAuth: () => useAuth,
|
|
25
|
+
useCart: () => useCart,
|
|
25
26
|
useCollection: () => useCollection,
|
|
26
|
-
useKV: () => useKV
|
|
27
|
+
useKV: () => useKV,
|
|
28
|
+
useOrders: () => useOrders,
|
|
29
|
+
useProducts: () => useProducts
|
|
27
30
|
});
|
|
28
31
|
module.exports = __toCommonJS(react_exports);
|
|
29
32
|
|
|
@@ -265,9 +268,177 @@ function useAuth(auth, initialToken) {
|
|
|
265
268
|
hasSubscription
|
|
266
269
|
};
|
|
267
270
|
}
|
|
271
|
+
|
|
272
|
+
// src/hooks/useProducts.ts
|
|
273
|
+
var import_react4 = require("react");
|
|
274
|
+
function useProducts(ecommerce, options) {
|
|
275
|
+
const [products, setProducts] = (0, import_react4.useState)([]);
|
|
276
|
+
const [loading, setLoading] = (0, import_react4.useState)(true);
|
|
277
|
+
const [error, setError] = (0, import_react4.useState)(null);
|
|
278
|
+
const loadProducts = (0, import_react4.useCallback)(async () => {
|
|
279
|
+
try {
|
|
280
|
+
setLoading(true);
|
|
281
|
+
setError(null);
|
|
282
|
+
const result = await ecommerce.listProducts(options);
|
|
283
|
+
setProducts(result.products);
|
|
284
|
+
} catch (err) {
|
|
285
|
+
setError(err instanceof Error ? err : new Error("Failed to load products"));
|
|
286
|
+
} finally {
|
|
287
|
+
setLoading(false);
|
|
288
|
+
}
|
|
289
|
+
}, [ecommerce, options?.category, options?.isActive]);
|
|
290
|
+
(0, import_react4.useEffect)(() => {
|
|
291
|
+
if (options?.autoLoad !== false) {
|
|
292
|
+
loadProducts();
|
|
293
|
+
}
|
|
294
|
+
}, [loadProducts, options?.autoLoad]);
|
|
295
|
+
const createProduct = (0, import_react4.useCallback)(async (input) => {
|
|
296
|
+
const product = await ecommerce.createProduct(input);
|
|
297
|
+
setProducts((prev) => [product, ...prev]);
|
|
298
|
+
return product;
|
|
299
|
+
}, [ecommerce]);
|
|
300
|
+
const updateProduct = (0, import_react4.useCallback)(async (productId, updates) => {
|
|
301
|
+
const product = await ecommerce.updateProduct(productId, updates);
|
|
302
|
+
setProducts((prev) => prev.map((p) => p.id === productId ? product : p));
|
|
303
|
+
return product;
|
|
304
|
+
}, [ecommerce]);
|
|
305
|
+
const deleteProduct = (0, import_react4.useCallback)(async (productId) => {
|
|
306
|
+
await ecommerce.deleteProduct(productId);
|
|
307
|
+
setProducts((prev) => prev.filter((p) => p.id !== productId));
|
|
308
|
+
}, [ecommerce]);
|
|
309
|
+
const updateInventory = (0, import_react4.useCallback)(async (productId, quantity, operation) => {
|
|
310
|
+
const product = await ecommerce.updateInventory(productId, quantity, operation);
|
|
311
|
+
setProducts((prev) => prev.map((p) => p.id === productId ? product : p));
|
|
312
|
+
return product;
|
|
313
|
+
}, [ecommerce]);
|
|
314
|
+
return {
|
|
315
|
+
products,
|
|
316
|
+
loading,
|
|
317
|
+
error,
|
|
318
|
+
refresh: loadProducts,
|
|
319
|
+
createProduct,
|
|
320
|
+
updateProduct,
|
|
321
|
+
deleteProduct,
|
|
322
|
+
updateInventory
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// src/hooks/useCart.ts
|
|
327
|
+
var import_react5 = require("react");
|
|
328
|
+
function useCart(ecommerce, userId) {
|
|
329
|
+
const [cart, setCart] = (0, import_react5.useState)([]);
|
|
330
|
+
const [loading, setLoading] = (0, import_react5.useState)(true);
|
|
331
|
+
const [error, setError] = (0, import_react5.useState)(null);
|
|
332
|
+
const loadCart = (0, import_react5.useCallback)(async () => {
|
|
333
|
+
if (!userId) {
|
|
334
|
+
setLoading(false);
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
try {
|
|
338
|
+
setLoading(true);
|
|
339
|
+
setError(null);
|
|
340
|
+
const items = await ecommerce.getCart(userId);
|
|
341
|
+
setCart(items);
|
|
342
|
+
} catch (err) {
|
|
343
|
+
setError(err instanceof Error ? err : new Error("Failed to load cart"));
|
|
344
|
+
} finally {
|
|
345
|
+
setLoading(false);
|
|
346
|
+
}
|
|
347
|
+
}, [ecommerce, userId]);
|
|
348
|
+
(0, import_react5.useEffect)(() => {
|
|
349
|
+
if (userId) {
|
|
350
|
+
loadCart();
|
|
351
|
+
}
|
|
352
|
+
}, [loadCart, userId]);
|
|
353
|
+
const addItem = (0, import_react5.useCallback)(async (item) => {
|
|
354
|
+
const updated = await ecommerce.addToCart(userId, item);
|
|
355
|
+
setCart(updated);
|
|
356
|
+
return updated;
|
|
357
|
+
}, [ecommerce, userId]);
|
|
358
|
+
const updateItem = (0, import_react5.useCallback)(async (productId, quantity) => {
|
|
359
|
+
const updated = await ecommerce.updateCartItem(userId, productId, quantity);
|
|
360
|
+
setCart(updated);
|
|
361
|
+
return updated;
|
|
362
|
+
}, [ecommerce, userId]);
|
|
363
|
+
const removeItem = (0, import_react5.useCallback)(async (productId) => {
|
|
364
|
+
return updateItem(productId, 0);
|
|
365
|
+
}, [updateItem]);
|
|
366
|
+
const clear = (0, import_react5.useCallback)(async () => {
|
|
367
|
+
await ecommerce.clearCart(userId);
|
|
368
|
+
setCart([]);
|
|
369
|
+
}, [ecommerce, userId]);
|
|
370
|
+
const checkout = (0, import_react5.useCallback)(async (shippingAddress, paymentMethodId) => {
|
|
371
|
+
const order = await ecommerce.checkout(userId, shippingAddress, paymentMethodId);
|
|
372
|
+
setCart([]);
|
|
373
|
+
return order;
|
|
374
|
+
}, [ecommerce, userId]);
|
|
375
|
+
const calculateTotal = (0, import_react5.useCallback)(async () => {
|
|
376
|
+
return ecommerce.calculateOrderTotal(cart);
|
|
377
|
+
}, [ecommerce, cart]);
|
|
378
|
+
const itemCount = cart.reduce((sum, item) => sum + item.quantity, 0);
|
|
379
|
+
return {
|
|
380
|
+
cart,
|
|
381
|
+
itemCount,
|
|
382
|
+
loading,
|
|
383
|
+
error,
|
|
384
|
+
refresh: loadCart,
|
|
385
|
+
addItem,
|
|
386
|
+
updateItem,
|
|
387
|
+
removeItem,
|
|
388
|
+
clear,
|
|
389
|
+
checkout,
|
|
390
|
+
calculateTotal
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
// src/hooks/useOrders.ts
|
|
395
|
+
var import_react6 = require("react");
|
|
396
|
+
function useOrders(ecommerce, options) {
|
|
397
|
+
const [orders, setOrders] = (0, import_react6.useState)([]);
|
|
398
|
+
const [loading, setLoading] = (0, import_react6.useState)(true);
|
|
399
|
+
const [error, setError] = (0, import_react6.useState)(null);
|
|
400
|
+
const loadOrders = (0, import_react6.useCallback)(async () => {
|
|
401
|
+
try {
|
|
402
|
+
setLoading(true);
|
|
403
|
+
setError(null);
|
|
404
|
+
const result = await ecommerce.listOrders(options);
|
|
405
|
+
setOrders(result.orders);
|
|
406
|
+
} catch (err) {
|
|
407
|
+
setError(err instanceof Error ? err : new Error("Failed to load orders"));
|
|
408
|
+
} finally {
|
|
409
|
+
setLoading(false);
|
|
410
|
+
}
|
|
411
|
+
}, [ecommerce, options?.userId, options?.status]);
|
|
412
|
+
(0, import_react6.useEffect)(() => {
|
|
413
|
+
if (options?.autoLoad !== false) {
|
|
414
|
+
loadOrders();
|
|
415
|
+
}
|
|
416
|
+
}, [loadOrders, options?.autoLoad]);
|
|
417
|
+
const updateStatus = (0, import_react6.useCallback)(async (orderId, status) => {
|
|
418
|
+
const order = await ecommerce.updateOrderStatus(orderId, status);
|
|
419
|
+
setOrders((prev) => prev.map((o) => o.id === orderId ? order : o));
|
|
420
|
+
return order;
|
|
421
|
+
}, [ecommerce]);
|
|
422
|
+
const cancelOrder = (0, import_react6.useCallback)(async (orderId, restoreInventory = true) => {
|
|
423
|
+
const order = await ecommerce.cancelOrder(orderId, restoreInventory);
|
|
424
|
+
setOrders((prev) => prev.map((o) => o.id === orderId ? order : o));
|
|
425
|
+
return order;
|
|
426
|
+
}, [ecommerce]);
|
|
427
|
+
return {
|
|
428
|
+
orders,
|
|
429
|
+
loading,
|
|
430
|
+
error,
|
|
431
|
+
refresh: loadOrders,
|
|
432
|
+
updateStatus,
|
|
433
|
+
cancelOrder
|
|
434
|
+
};
|
|
435
|
+
}
|
|
268
436
|
// Annotate the CommonJS export names for ESM import in node:
|
|
269
437
|
0 && (module.exports = {
|
|
270
438
|
useAuth,
|
|
439
|
+
useCart,
|
|
271
440
|
useCollection,
|
|
272
|
-
useKV
|
|
441
|
+
useKV,
|
|
442
|
+
useOrders,
|
|
443
|
+
useProducts
|
|
273
444
|
});
|
package/dist/react.mjs
CHANGED
|
@@ -238,8 +238,176 @@ function useAuth(auth, initialToken) {
|
|
|
238
238
|
hasSubscription
|
|
239
239
|
};
|
|
240
240
|
}
|
|
241
|
+
|
|
242
|
+
// src/hooks/useProducts.ts
|
|
243
|
+
import { useState as useState4, useEffect as useEffect4, useCallback as useCallback4 } from "react";
|
|
244
|
+
function useProducts(ecommerce, options) {
|
|
245
|
+
const [products, setProducts] = useState4([]);
|
|
246
|
+
const [loading, setLoading] = useState4(true);
|
|
247
|
+
const [error, setError] = useState4(null);
|
|
248
|
+
const loadProducts = useCallback4(async () => {
|
|
249
|
+
try {
|
|
250
|
+
setLoading(true);
|
|
251
|
+
setError(null);
|
|
252
|
+
const result = await ecommerce.listProducts(options);
|
|
253
|
+
setProducts(result.products);
|
|
254
|
+
} catch (err) {
|
|
255
|
+
setError(err instanceof Error ? err : new Error("Failed to load products"));
|
|
256
|
+
} finally {
|
|
257
|
+
setLoading(false);
|
|
258
|
+
}
|
|
259
|
+
}, [ecommerce, options?.category, options?.isActive]);
|
|
260
|
+
useEffect4(() => {
|
|
261
|
+
if (options?.autoLoad !== false) {
|
|
262
|
+
loadProducts();
|
|
263
|
+
}
|
|
264
|
+
}, [loadProducts, options?.autoLoad]);
|
|
265
|
+
const createProduct = useCallback4(async (input) => {
|
|
266
|
+
const product = await ecommerce.createProduct(input);
|
|
267
|
+
setProducts((prev) => [product, ...prev]);
|
|
268
|
+
return product;
|
|
269
|
+
}, [ecommerce]);
|
|
270
|
+
const updateProduct = useCallback4(async (productId, updates) => {
|
|
271
|
+
const product = await ecommerce.updateProduct(productId, updates);
|
|
272
|
+
setProducts((prev) => prev.map((p) => p.id === productId ? product : p));
|
|
273
|
+
return product;
|
|
274
|
+
}, [ecommerce]);
|
|
275
|
+
const deleteProduct = useCallback4(async (productId) => {
|
|
276
|
+
await ecommerce.deleteProduct(productId);
|
|
277
|
+
setProducts((prev) => prev.filter((p) => p.id !== productId));
|
|
278
|
+
}, [ecommerce]);
|
|
279
|
+
const updateInventory = useCallback4(async (productId, quantity, operation) => {
|
|
280
|
+
const product = await ecommerce.updateInventory(productId, quantity, operation);
|
|
281
|
+
setProducts((prev) => prev.map((p) => p.id === productId ? product : p));
|
|
282
|
+
return product;
|
|
283
|
+
}, [ecommerce]);
|
|
284
|
+
return {
|
|
285
|
+
products,
|
|
286
|
+
loading,
|
|
287
|
+
error,
|
|
288
|
+
refresh: loadProducts,
|
|
289
|
+
createProduct,
|
|
290
|
+
updateProduct,
|
|
291
|
+
deleteProduct,
|
|
292
|
+
updateInventory
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
// src/hooks/useCart.ts
|
|
297
|
+
import { useState as useState5, useEffect as useEffect5, useCallback as useCallback5 } from "react";
|
|
298
|
+
function useCart(ecommerce, userId) {
|
|
299
|
+
const [cart, setCart] = useState5([]);
|
|
300
|
+
const [loading, setLoading] = useState5(true);
|
|
301
|
+
const [error, setError] = useState5(null);
|
|
302
|
+
const loadCart = useCallback5(async () => {
|
|
303
|
+
if (!userId) {
|
|
304
|
+
setLoading(false);
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
try {
|
|
308
|
+
setLoading(true);
|
|
309
|
+
setError(null);
|
|
310
|
+
const items = await ecommerce.getCart(userId);
|
|
311
|
+
setCart(items);
|
|
312
|
+
} catch (err) {
|
|
313
|
+
setError(err instanceof Error ? err : new Error("Failed to load cart"));
|
|
314
|
+
} finally {
|
|
315
|
+
setLoading(false);
|
|
316
|
+
}
|
|
317
|
+
}, [ecommerce, userId]);
|
|
318
|
+
useEffect5(() => {
|
|
319
|
+
if (userId) {
|
|
320
|
+
loadCart();
|
|
321
|
+
}
|
|
322
|
+
}, [loadCart, userId]);
|
|
323
|
+
const addItem = useCallback5(async (item) => {
|
|
324
|
+
const updated = await ecommerce.addToCart(userId, item);
|
|
325
|
+
setCart(updated);
|
|
326
|
+
return updated;
|
|
327
|
+
}, [ecommerce, userId]);
|
|
328
|
+
const updateItem = useCallback5(async (productId, quantity) => {
|
|
329
|
+
const updated = await ecommerce.updateCartItem(userId, productId, quantity);
|
|
330
|
+
setCart(updated);
|
|
331
|
+
return updated;
|
|
332
|
+
}, [ecommerce, userId]);
|
|
333
|
+
const removeItem = useCallback5(async (productId) => {
|
|
334
|
+
return updateItem(productId, 0);
|
|
335
|
+
}, [updateItem]);
|
|
336
|
+
const clear = useCallback5(async () => {
|
|
337
|
+
await ecommerce.clearCart(userId);
|
|
338
|
+
setCart([]);
|
|
339
|
+
}, [ecommerce, userId]);
|
|
340
|
+
const checkout = useCallback5(async (shippingAddress, paymentMethodId) => {
|
|
341
|
+
const order = await ecommerce.checkout(userId, shippingAddress, paymentMethodId);
|
|
342
|
+
setCart([]);
|
|
343
|
+
return order;
|
|
344
|
+
}, [ecommerce, userId]);
|
|
345
|
+
const calculateTotal = useCallback5(async () => {
|
|
346
|
+
return ecommerce.calculateOrderTotal(cart);
|
|
347
|
+
}, [ecommerce, cart]);
|
|
348
|
+
const itemCount = cart.reduce((sum, item) => sum + item.quantity, 0);
|
|
349
|
+
return {
|
|
350
|
+
cart,
|
|
351
|
+
itemCount,
|
|
352
|
+
loading,
|
|
353
|
+
error,
|
|
354
|
+
refresh: loadCart,
|
|
355
|
+
addItem,
|
|
356
|
+
updateItem,
|
|
357
|
+
removeItem,
|
|
358
|
+
clear,
|
|
359
|
+
checkout,
|
|
360
|
+
calculateTotal
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
// src/hooks/useOrders.ts
|
|
365
|
+
import { useState as useState6, useEffect as useEffect6, useCallback as useCallback6 } from "react";
|
|
366
|
+
function useOrders(ecommerce, options) {
|
|
367
|
+
const [orders, setOrders] = useState6([]);
|
|
368
|
+
const [loading, setLoading] = useState6(true);
|
|
369
|
+
const [error, setError] = useState6(null);
|
|
370
|
+
const loadOrders = useCallback6(async () => {
|
|
371
|
+
try {
|
|
372
|
+
setLoading(true);
|
|
373
|
+
setError(null);
|
|
374
|
+
const result = await ecommerce.listOrders(options);
|
|
375
|
+
setOrders(result.orders);
|
|
376
|
+
} catch (err) {
|
|
377
|
+
setError(err instanceof Error ? err : new Error("Failed to load orders"));
|
|
378
|
+
} finally {
|
|
379
|
+
setLoading(false);
|
|
380
|
+
}
|
|
381
|
+
}, [ecommerce, options?.userId, options?.status]);
|
|
382
|
+
useEffect6(() => {
|
|
383
|
+
if (options?.autoLoad !== false) {
|
|
384
|
+
loadOrders();
|
|
385
|
+
}
|
|
386
|
+
}, [loadOrders, options?.autoLoad]);
|
|
387
|
+
const updateStatus = useCallback6(async (orderId, status) => {
|
|
388
|
+
const order = await ecommerce.updateOrderStatus(orderId, status);
|
|
389
|
+
setOrders((prev) => prev.map((o) => o.id === orderId ? order : o));
|
|
390
|
+
return order;
|
|
391
|
+
}, [ecommerce]);
|
|
392
|
+
const cancelOrder = useCallback6(async (orderId, restoreInventory = true) => {
|
|
393
|
+
const order = await ecommerce.cancelOrder(orderId, restoreInventory);
|
|
394
|
+
setOrders((prev) => prev.map((o) => o.id === orderId ? order : o));
|
|
395
|
+
return order;
|
|
396
|
+
}, [ecommerce]);
|
|
397
|
+
return {
|
|
398
|
+
orders,
|
|
399
|
+
loading,
|
|
400
|
+
error,
|
|
401
|
+
refresh: loadOrders,
|
|
402
|
+
updateStatus,
|
|
403
|
+
cancelOrder
|
|
404
|
+
};
|
|
405
|
+
}
|
|
241
406
|
export {
|
|
242
407
|
useAuth,
|
|
408
|
+
useCart,
|
|
243
409
|
useCollection,
|
|
244
|
-
useKV
|
|
410
|
+
useKV,
|
|
411
|
+
useOrders,
|
|
412
|
+
useProducts
|
|
245
413
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@withvlibe/base-sdk",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "SDK for Vlibe Base Apps - includes authentication, database, payments with transaction fees, and category-specific helpers for websites, SaaS, and e-commerce",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|