@behio/storefront-sdk 0.1.4 → 0.1.6
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 +71 -6
- package/dist/{chunk-SNODOM7L.js → chunk-GGAO5T5P.js} +17 -1
- package/dist/{chunk-DBZPF3IR.mjs → chunk-S4DOL3OV.mjs} +17 -1
- package/dist/index.d.mts +21 -1
- package/dist/index.d.ts +21 -1
- package/dist/index.js +2 -2
- package/dist/index.mjs +1 -1
- package/dist/react.d.mts +136 -16
- package/dist/react.d.ts +136 -16
- package/dist/react.js +169 -59
- package/dist/react.mjs +215 -105
- package/package.json +1 -1
package/dist/react.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
BehioStorefront
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-S4DOL3OV.mjs";
|
|
4
4
|
|
|
5
5
|
// src/react/provider.tsx
|
|
6
6
|
import { useRef, useEffect, useMemo } from "react";
|
|
@@ -156,95 +156,159 @@ function BehioProvider({
|
|
|
156
156
|
}
|
|
157
157
|
|
|
158
158
|
// src/react/hooks/use-products.ts
|
|
159
|
-
import {
|
|
159
|
+
import { useCallback, useMemo as useMemo2, useState } from "react";
|
|
160
|
+
import { useInfiniteQuery } from "@tanstack/react-query";
|
|
160
161
|
function useProducts(query) {
|
|
161
162
|
const { client } = useBehio();
|
|
162
|
-
const { initialData, enabled, ...
|
|
163
|
-
const
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
163
|
+
const { initialData, enabled, page: initialPage, limit, ...filters } = query ?? {};
|
|
164
|
+
const [page, setPage] = useState(initialPage ?? 1);
|
|
165
|
+
const serializedFilters = useMemo2(() => JSON.stringify({ ...filters, limit }), [filters, limit]);
|
|
166
|
+
const infinite = useInfiniteQuery({
|
|
167
|
+
queryKey: ["behio", "products", serializedFilters, page],
|
|
168
|
+
queryFn: ({ pageParam }) => client.catalog.getProducts({ ...filters, limit, page: pageParam }),
|
|
169
|
+
initialPageParam: page,
|
|
170
|
+
getNextPageParam: (lastPage2) => lastPage2.page < lastPage2.totalPages ? lastPage2.page + 1 : void 0,
|
|
171
|
+
getPreviousPageParam: (firstPage2) => firstPage2.page > 1 ? firstPage2.page - 1 : void 0,
|
|
172
|
+
initialData: initialData ? { pages: [initialData], pageParams: [initialData.page] } : void 0,
|
|
173
|
+
enabled: enabled !== false
|
|
174
|
+
});
|
|
175
|
+
const items = useMemo2(
|
|
176
|
+
() => infinite.data?.pages.flatMap((p) => p.items) ?? [],
|
|
177
|
+
[infinite.data]
|
|
178
|
+
);
|
|
179
|
+
const lastPage = infinite.data?.pages[infinite.data.pages.length - 1];
|
|
180
|
+
const firstPage = infinite.data?.pages[0];
|
|
181
|
+
const loadMore = useCallback(() => {
|
|
182
|
+
if (infinite.hasNextPage && !infinite.isFetchingNextPage) {
|
|
183
|
+
return infinite.fetchNextPage();
|
|
184
|
+
}
|
|
185
|
+
return Promise.resolve();
|
|
186
|
+
}, [infinite]);
|
|
187
|
+
const goToPage = useCallback((newPage) => {
|
|
188
|
+
setPage(newPage);
|
|
189
|
+
}, []);
|
|
190
|
+
return {
|
|
191
|
+
// --- Data ---
|
|
192
|
+
/** All items from all loaded pages (flat array) — for infinite scroll */
|
|
193
|
+
items,
|
|
194
|
+
/** Raw React Query data with all pages */
|
|
195
|
+
data: infinite.data,
|
|
196
|
+
/** Last loaded page meta */
|
|
197
|
+
total: lastPage?.total ?? 0,
|
|
198
|
+
totalPages: lastPage?.totalPages ?? 0,
|
|
199
|
+
currentPage: lastPage?.page ?? page,
|
|
200
|
+
limit: lastPage?.limit ?? limit ?? 20,
|
|
201
|
+
// --- Pagination controls ---
|
|
202
|
+
/** Current page (manual pagination) */
|
|
203
|
+
page,
|
|
204
|
+
/** Change page (traditional pagination — replaces items) */
|
|
205
|
+
setPage: goToPage,
|
|
206
|
+
/** Load next page (append to items — for infinite scroll / load more button) */
|
|
207
|
+
loadMore,
|
|
208
|
+
/** Fetch previous page */
|
|
209
|
+
loadPrevious: infinite.fetchPreviousPage,
|
|
210
|
+
/** Can load more? */
|
|
211
|
+
hasMore: infinite.hasNextPage ?? false,
|
|
212
|
+
hasPrevious: infinite.hasPreviousPage ?? false,
|
|
213
|
+
// --- States ---
|
|
214
|
+
isLoading: infinite.isLoading,
|
|
215
|
+
isFetching: infinite.isFetching,
|
|
216
|
+
isLoadingMore: infinite.isFetchingNextPage,
|
|
217
|
+
error: infinite.error,
|
|
218
|
+
isError: infinite.isError,
|
|
219
|
+
// --- Manual control ---
|
|
220
|
+
/** Force refetch all loaded pages */
|
|
221
|
+
refetch: infinite.refetch
|
|
222
|
+
};
|
|
170
223
|
}
|
|
171
224
|
|
|
172
225
|
// src/react/hooks/use-product.ts
|
|
173
|
-
import { useQuery
|
|
226
|
+
import { useQuery } from "@tanstack/react-query";
|
|
174
227
|
function useProduct(slug, options) {
|
|
175
228
|
const { client } = useBehio();
|
|
176
|
-
return
|
|
229
|
+
return useQuery({
|
|
177
230
|
queryKey: ["behio", "product", slug],
|
|
178
231
|
queryFn: () => client.catalog.getProduct(slug, {
|
|
179
232
|
locale: options?.locale,
|
|
180
233
|
currency: options?.currency
|
|
181
234
|
}),
|
|
182
235
|
initialData: options?.initialData,
|
|
183
|
-
enabled: options?.enabled
|
|
236
|
+
enabled: options?.enabled !== false && !!slug
|
|
184
237
|
});
|
|
185
238
|
}
|
|
186
239
|
|
|
187
240
|
// src/react/hooks/use-categories.ts
|
|
188
|
-
import { useQuery as
|
|
189
|
-
function useCategories(locale) {
|
|
241
|
+
import { useQuery as useQuery2 } from "@tanstack/react-query";
|
|
242
|
+
function useCategories(locale, options) {
|
|
190
243
|
const { client } = useBehio();
|
|
191
|
-
return
|
|
244
|
+
return useQuery2({
|
|
192
245
|
queryKey: ["behio", "categories", locale],
|
|
193
246
|
queryFn: async () => {
|
|
194
247
|
const result = await client.catalog.getCategories(locale);
|
|
195
248
|
return result.categories;
|
|
196
|
-
}
|
|
249
|
+
},
|
|
250
|
+
enabled: options?.enabled !== false
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
function useCategory(slug, options) {
|
|
254
|
+
const { client } = useBehio();
|
|
255
|
+
return useQuery2({
|
|
256
|
+
queryKey: ["behio", "category", slug, options?.locale],
|
|
257
|
+
queryFn: () => client.catalog.getCategory(slug, options?.locale),
|
|
258
|
+
enabled: options?.enabled !== false && !!slug
|
|
197
259
|
});
|
|
198
260
|
}
|
|
199
261
|
|
|
200
262
|
// src/react/hooks/use-labels.ts
|
|
201
|
-
import { useQuery as
|
|
202
|
-
function useLabels(locale) {
|
|
263
|
+
import { useQuery as useQuery3 } from "@tanstack/react-query";
|
|
264
|
+
function useLabels(locale, options) {
|
|
203
265
|
const { client } = useBehio();
|
|
204
|
-
return
|
|
266
|
+
return useQuery3({
|
|
205
267
|
queryKey: ["behio", "labels", locale],
|
|
206
268
|
queryFn: async () => {
|
|
207
269
|
const result = await client.catalog.getLabels(locale);
|
|
208
270
|
return result.labels;
|
|
209
|
-
}
|
|
271
|
+
},
|
|
272
|
+
enabled: options?.enabled !== false
|
|
210
273
|
});
|
|
211
274
|
}
|
|
212
275
|
|
|
213
276
|
// src/react/hooks/use-featured.ts
|
|
214
|
-
import { useQuery as
|
|
277
|
+
import { useQuery as useQuery4 } from "@tanstack/react-query";
|
|
215
278
|
function useFeatured(options) {
|
|
216
279
|
const { client } = useBehio();
|
|
217
|
-
return
|
|
280
|
+
return useQuery4({
|
|
218
281
|
queryKey: ["behio", "featured", options?.locale, options?.currency],
|
|
219
282
|
queryFn: () => client.catalog.getFeatured({
|
|
220
283
|
locale: options?.locale,
|
|
221
284
|
currency: options?.currency
|
|
222
285
|
}),
|
|
223
286
|
initialData: options?.initialData,
|
|
224
|
-
enabled: options?.enabled
|
|
287
|
+
enabled: options?.enabled !== false
|
|
225
288
|
});
|
|
226
289
|
}
|
|
227
290
|
|
|
228
291
|
// src/react/hooks/use-filters.ts
|
|
229
|
-
import { useQuery as
|
|
230
|
-
function useFilters() {
|
|
292
|
+
import { useQuery as useQuery5 } from "@tanstack/react-query";
|
|
293
|
+
function useFilters(options) {
|
|
231
294
|
const { client } = useBehio();
|
|
232
|
-
return
|
|
295
|
+
return useQuery5({
|
|
233
296
|
queryKey: ["behio", "filters"],
|
|
234
297
|
queryFn: async () => {
|
|
235
298
|
const result = await client.catalog.getFilters();
|
|
236
299
|
return result.filters;
|
|
237
|
-
}
|
|
300
|
+
},
|
|
301
|
+
enabled: options?.enabled !== false
|
|
238
302
|
});
|
|
239
303
|
}
|
|
240
304
|
|
|
241
305
|
// src/react/hooks/use-search.ts
|
|
242
|
-
import { useState, useEffect as useEffect2 } from "react";
|
|
243
|
-
import { useQuery as
|
|
306
|
+
import { useState as useState2, useEffect as useEffect2 } from "react";
|
|
307
|
+
import { useQuery as useQuery6 } from "@tanstack/react-query";
|
|
244
308
|
function useSearch(query, options) {
|
|
245
309
|
const { client } = useBehio();
|
|
246
310
|
const debounceMs = options?.debounceMs ?? 300;
|
|
247
|
-
const [debouncedQuery, setDebouncedQuery] =
|
|
311
|
+
const [debouncedQuery, setDebouncedQuery] = useState2(query);
|
|
248
312
|
useEffect2(() => {
|
|
249
313
|
if (debounceMs <= 0) {
|
|
250
314
|
setDebouncedQuery(query);
|
|
@@ -253,32 +317,32 @@ function useSearch(query, options) {
|
|
|
253
317
|
const timer = setTimeout(() => setDebouncedQuery(query), debounceMs);
|
|
254
318
|
return () => clearTimeout(timer);
|
|
255
319
|
}, [query, debounceMs]);
|
|
256
|
-
return
|
|
320
|
+
return useQuery6({
|
|
257
321
|
queryKey: ["behio", "search", debouncedQuery, options?.page, options?.limit],
|
|
258
322
|
queryFn: () => client.catalog.search(debouncedQuery, {
|
|
259
323
|
page: options?.page,
|
|
260
324
|
limit: options?.limit
|
|
261
325
|
}),
|
|
262
|
-
enabled:
|
|
326
|
+
enabled: options?.enabled !== false && debouncedQuery.length > 0
|
|
263
327
|
});
|
|
264
328
|
}
|
|
265
329
|
|
|
266
330
|
// src/react/hooks/use-cart.ts
|
|
267
|
-
import { useCallback } from "react";
|
|
268
|
-
import { useQuery as
|
|
331
|
+
import { useCallback as useCallback2 } from "react";
|
|
332
|
+
import { useQuery as useQuery7, useMutation, useQueryClient } from "@tanstack/react-query";
|
|
269
333
|
var CART_KEY = ["behio", "cart"];
|
|
270
|
-
function useCart() {
|
|
334
|
+
function useCart(options) {
|
|
271
335
|
const { client, storage } = useBehio();
|
|
272
336
|
const queryClient = useQueryClient();
|
|
273
337
|
const {
|
|
274
338
|
data: cart,
|
|
275
339
|
isLoading,
|
|
276
340
|
error
|
|
277
|
-
} =
|
|
341
|
+
} = useQuery7({
|
|
278
342
|
queryKey: [...CART_KEY],
|
|
279
343
|
queryFn: () => client.cart.get(),
|
|
280
344
|
// Only fetch if we have a cart session or are logged in
|
|
281
|
-
enabled: !!client.getCartSession() || !!client.getAccessToken()
|
|
345
|
+
enabled: options?.enabled !== false && (!!client.getCartSession() || !!client.getAccessToken())
|
|
282
346
|
});
|
|
283
347
|
const addMutation = useMutation({
|
|
284
348
|
mutationFn: async ({ productId, quantity }) => {
|
|
@@ -373,31 +437,31 @@ function useCart() {
|
|
|
373
437
|
storage.remove(STORAGE_KEYS.CART_SESSION);
|
|
374
438
|
}
|
|
375
439
|
});
|
|
376
|
-
const addItem =
|
|
440
|
+
const addItem = useCallback2(
|
|
377
441
|
(productId, quantity) => addMutation.mutateAsync({ productId, quantity }),
|
|
378
442
|
[addMutation]
|
|
379
443
|
);
|
|
380
|
-
const updateQuantity =
|
|
444
|
+
const updateQuantity = useCallback2(
|
|
381
445
|
(itemId, quantity) => updateMutation.mutateAsync({ itemId, quantity }),
|
|
382
446
|
[updateMutation]
|
|
383
447
|
);
|
|
384
|
-
const removeItem =
|
|
448
|
+
const removeItem = useCallback2(
|
|
385
449
|
(itemId) => removeMutation.mutateAsync(itemId),
|
|
386
450
|
[removeMutation]
|
|
387
451
|
);
|
|
388
|
-
const clear =
|
|
452
|
+
const clear = useCallback2(
|
|
389
453
|
() => clearMutation.mutateAsync(),
|
|
390
454
|
[clearMutation]
|
|
391
455
|
);
|
|
392
|
-
const applyDiscount =
|
|
456
|
+
const applyDiscount = useCallback2(
|
|
393
457
|
(code) => applyDiscountMutation.mutateAsync(code),
|
|
394
458
|
[applyDiscountMutation]
|
|
395
459
|
);
|
|
396
|
-
const removeDiscount =
|
|
460
|
+
const removeDiscount = useCallback2(
|
|
397
461
|
() => removeDiscountMutation.mutateAsync(),
|
|
398
462
|
[removeDiscountMutation]
|
|
399
463
|
);
|
|
400
|
-
const merge =
|
|
464
|
+
const merge = useCallback2(
|
|
401
465
|
() => mergeMutation.mutateAsync(),
|
|
402
466
|
[mergeMutation]
|
|
403
467
|
);
|
|
@@ -424,24 +488,24 @@ function useCart() {
|
|
|
424
488
|
}
|
|
425
489
|
|
|
426
490
|
// src/react/hooks/use-cart-count.ts
|
|
427
|
-
import { useQuery as
|
|
491
|
+
import { useQuery as useQuery8, useQueryClient as useQueryClient2 } from "@tanstack/react-query";
|
|
428
492
|
var CART_KEY2 = ["behio", "cart"];
|
|
429
|
-
function useCartCount() {
|
|
493
|
+
function useCartCount(options) {
|
|
430
494
|
const { client } = useBehio();
|
|
431
495
|
const queryClient = useQueryClient2();
|
|
432
496
|
const cachedCart = queryClient.getQueryData([...CART_KEY2]);
|
|
433
|
-
const { data } =
|
|
497
|
+
const { data } = useQuery8({
|
|
434
498
|
queryKey: [...CART_KEY2],
|
|
435
499
|
queryFn: () => client.cart.get(),
|
|
436
|
-
enabled: !cachedCart && (!!client.getCartSession() || !!client.getAccessToken())
|
|
500
|
+
enabled: options?.enabled !== false && !cachedCart && (!!client.getCartSession() || !!client.getAccessToken())
|
|
437
501
|
});
|
|
438
502
|
const cart = cachedCart ?? data;
|
|
439
503
|
return cart?.itemCount ?? 0;
|
|
440
504
|
}
|
|
441
505
|
|
|
442
506
|
// src/react/hooks/use-auth.ts
|
|
443
|
-
import { useCallback as
|
|
444
|
-
import { useQuery as
|
|
507
|
+
import { useCallback as useCallback3 } from "react";
|
|
508
|
+
import { useQuery as useQuery9, useMutation as useMutation2, useQueryClient as useQueryClient3 } from "@tanstack/react-query";
|
|
445
509
|
var CUSTOMER_KEY = ["behio", "customer"];
|
|
446
510
|
var CART_KEY3 = ["behio", "cart"];
|
|
447
511
|
function useAuth() {
|
|
@@ -451,12 +515,12 @@ function useAuth() {
|
|
|
451
515
|
const {
|
|
452
516
|
data: customer,
|
|
453
517
|
isLoading
|
|
454
|
-
} =
|
|
518
|
+
} = useQuery9({
|
|
455
519
|
queryKey: [...CUSTOMER_KEY],
|
|
456
520
|
queryFn: () => client.customer.getProfile(),
|
|
457
521
|
enabled: isLoggedIn
|
|
458
522
|
});
|
|
459
|
-
const persistTokens =
|
|
523
|
+
const persistTokens = useCallback3(
|
|
460
524
|
(tokens) => {
|
|
461
525
|
client.setTokens(tokens);
|
|
462
526
|
storage.set(STORAGE_KEYS.ACCESS_TOKEN, tokens.accessToken);
|
|
@@ -464,7 +528,7 @@ function useAuth() {
|
|
|
464
528
|
},
|
|
465
529
|
[client, storage]
|
|
466
530
|
);
|
|
467
|
-
const clearAuth =
|
|
531
|
+
const clearAuth = useCallback3(() => {
|
|
468
532
|
client.clearTokens();
|
|
469
533
|
storage.remove(STORAGE_KEYS.ACCESS_TOKEN);
|
|
470
534
|
storage.remove(STORAGE_KEYS.REFRESH_TOKEN);
|
|
@@ -472,7 +536,7 @@ function useAuth() {
|
|
|
472
536
|
queryClient.invalidateQueries({ queryKey: ["behio", "orders"] });
|
|
473
537
|
queryClient.invalidateQueries({ queryKey: ["behio", "addresses"] });
|
|
474
538
|
}, [client, storage, queryClient]);
|
|
475
|
-
const postAuth =
|
|
539
|
+
const postAuth = useCallback3(async () => {
|
|
476
540
|
await queryClient.invalidateQueries({ queryKey: [...CUSTOMER_KEY] });
|
|
477
541
|
if (client.getCartSession()) {
|
|
478
542
|
try {
|
|
@@ -517,27 +581,27 @@ function useAuth() {
|
|
|
517
581
|
const verifyEmailMutation = useMutation2({
|
|
518
582
|
mutationFn: (token) => client.auth.verifyEmail(token)
|
|
519
583
|
});
|
|
520
|
-
const login =
|
|
584
|
+
const login = useCallback3(
|
|
521
585
|
(email, password) => loginMutation.mutateAsync({ email, password }).then(() => void 0),
|
|
522
586
|
[loginMutation]
|
|
523
587
|
);
|
|
524
|
-
const register =
|
|
588
|
+
const register = useCallback3(
|
|
525
589
|
(input) => registerMutation.mutateAsync(input).then(() => void 0),
|
|
526
590
|
[registerMutation]
|
|
527
591
|
);
|
|
528
|
-
const logout =
|
|
592
|
+
const logout = useCallback3(
|
|
529
593
|
() => logoutMutation.mutateAsync().then(() => void 0),
|
|
530
594
|
[logoutMutation]
|
|
531
595
|
);
|
|
532
|
-
const forgotPassword =
|
|
596
|
+
const forgotPassword = useCallback3(
|
|
533
597
|
(email) => forgotPasswordMutation.mutateAsync(email).then(() => void 0),
|
|
534
598
|
[forgotPasswordMutation]
|
|
535
599
|
);
|
|
536
|
-
const resetPassword =
|
|
600
|
+
const resetPassword = useCallback3(
|
|
537
601
|
(token, newPassword) => resetPasswordMutation.mutateAsync({ token, newPassword }).then(() => void 0),
|
|
538
602
|
[resetPasswordMutation]
|
|
539
603
|
);
|
|
540
|
-
const verifyEmail =
|
|
604
|
+
const verifyEmail = useCallback3(
|
|
541
605
|
(token) => verifyEmailMutation.mutateAsync(token).then(() => void 0),
|
|
542
606
|
[verifyEmailMutation]
|
|
543
607
|
);
|
|
@@ -561,20 +625,20 @@ function useAuth() {
|
|
|
561
625
|
}
|
|
562
626
|
|
|
563
627
|
// src/react/hooks/use-customer.ts
|
|
564
|
-
import { useCallback as
|
|
565
|
-
import { useQuery as
|
|
628
|
+
import { useCallback as useCallback4 } from "react";
|
|
629
|
+
import { useQuery as useQuery10, useMutation as useMutation3, useQueryClient as useQueryClient4 } from "@tanstack/react-query";
|
|
566
630
|
var CUSTOMER_KEY2 = ["behio", "customer"];
|
|
567
|
-
function useCustomer() {
|
|
631
|
+
function useCustomer(options) {
|
|
568
632
|
const { client } = useBehio();
|
|
569
633
|
const queryClient = useQueryClient4();
|
|
570
634
|
const {
|
|
571
635
|
data,
|
|
572
636
|
isLoading,
|
|
573
637
|
error
|
|
574
|
-
} =
|
|
638
|
+
} = useQuery10({
|
|
575
639
|
queryKey: [...CUSTOMER_KEY2],
|
|
576
640
|
queryFn: () => client.customer.getProfile(),
|
|
577
|
-
enabled: !!client.getAccessToken()
|
|
641
|
+
enabled: options?.enabled !== false && !!client.getAccessToken()
|
|
578
642
|
});
|
|
579
643
|
const updateMutation = useMutation3({
|
|
580
644
|
mutationFn: (data2) => client.customer.updateProfile(data2),
|
|
@@ -582,7 +646,7 @@ function useCustomer() {
|
|
|
582
646
|
queryClient.setQueryData([...CUSTOMER_KEY2], updated);
|
|
583
647
|
}
|
|
584
648
|
});
|
|
585
|
-
const updateProfile =
|
|
649
|
+
const updateProfile = useCallback4(
|
|
586
650
|
(profileData) => updateMutation.mutateAsync(profileData),
|
|
587
651
|
[updateMutation]
|
|
588
652
|
);
|
|
@@ -596,23 +660,23 @@ function useCustomer() {
|
|
|
596
660
|
}
|
|
597
661
|
|
|
598
662
|
// src/react/hooks/use-addresses.ts
|
|
599
|
-
import { useCallback as
|
|
600
|
-
import { useQuery as
|
|
663
|
+
import { useCallback as useCallback5 } from "react";
|
|
664
|
+
import { useQuery as useQuery11, useMutation as useMutation4, useQueryClient as useQueryClient5 } from "@tanstack/react-query";
|
|
601
665
|
var ADDRESSES_KEY = ["behio", "addresses"];
|
|
602
|
-
function useAddresses() {
|
|
666
|
+
function useAddresses(options) {
|
|
603
667
|
const { client } = useBehio();
|
|
604
668
|
const queryClient = useQueryClient5();
|
|
605
669
|
const {
|
|
606
670
|
data,
|
|
607
671
|
isLoading,
|
|
608
672
|
error
|
|
609
|
-
} =
|
|
673
|
+
} = useQuery11({
|
|
610
674
|
queryKey: [...ADDRESSES_KEY],
|
|
611
675
|
queryFn: async () => {
|
|
612
676
|
const result = await client.customer.getAddresses();
|
|
613
677
|
return result.items;
|
|
614
678
|
},
|
|
615
|
-
enabled: !!client.getAccessToken()
|
|
679
|
+
enabled: options?.enabled !== false && !!client.getAccessToken()
|
|
616
680
|
});
|
|
617
681
|
const createMutation = useMutation4({
|
|
618
682
|
mutationFn: (address) => client.customer.createAddress(address),
|
|
@@ -632,15 +696,15 @@ function useAddresses() {
|
|
|
632
696
|
queryClient.invalidateQueries({ queryKey: [...ADDRESSES_KEY] });
|
|
633
697
|
}
|
|
634
698
|
});
|
|
635
|
-
const createAddress =
|
|
699
|
+
const createAddress = useCallback5(
|
|
636
700
|
(address) => createMutation.mutateAsync(address),
|
|
637
701
|
[createMutation]
|
|
638
702
|
);
|
|
639
|
-
const updateAddress =
|
|
703
|
+
const updateAddress = useCallback5(
|
|
640
704
|
(addressId, addressData) => updateMutation.mutateAsync({ addressId, data: addressData }),
|
|
641
705
|
[updateMutation]
|
|
642
706
|
);
|
|
643
|
-
const deleteAddress =
|
|
707
|
+
const deleteAddress = useCallback5(
|
|
644
708
|
(addressId) => deleteMutation.mutateAsync(addressId),
|
|
645
709
|
[deleteMutation]
|
|
646
710
|
);
|
|
@@ -657,33 +721,70 @@ function useAddresses() {
|
|
|
657
721
|
}
|
|
658
722
|
|
|
659
723
|
// src/react/hooks/use-orders.ts
|
|
660
|
-
import {
|
|
724
|
+
import { useCallback as useCallback6, useMemo as useMemo3, useState as useState3 } from "react";
|
|
725
|
+
import { useInfiniteQuery as useInfiniteQuery2 } from "@tanstack/react-query";
|
|
661
726
|
function useOrders(options) {
|
|
662
727
|
const { client } = useBehio();
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
728
|
+
const { page: initialPage, limit, enabled } = options ?? {};
|
|
729
|
+
const [page, setPage] = useState3(initialPage ?? 1);
|
|
730
|
+
const infinite = useInfiniteQuery2({
|
|
731
|
+
queryKey: ["behio", "orders", limit, page],
|
|
732
|
+
queryFn: ({ pageParam }) => client.orders.list({ limit, page: pageParam }),
|
|
733
|
+
initialPageParam: page,
|
|
734
|
+
getNextPageParam: (lastPage2) => lastPage2.page < lastPage2.totalPages ? lastPage2.page + 1 : void 0,
|
|
735
|
+
getPreviousPageParam: (firstPage) => firstPage.page > 1 ? firstPage.page - 1 : void 0,
|
|
736
|
+
enabled: enabled !== false && !!client.getAccessToken()
|
|
737
|
+
});
|
|
738
|
+
const items = useMemo3(
|
|
739
|
+
() => infinite.data?.pages.flatMap((p) => p.items) ?? [],
|
|
740
|
+
[infinite.data]
|
|
741
|
+
);
|
|
742
|
+
const lastPage = infinite.data?.pages[infinite.data.pages.length - 1];
|
|
743
|
+
const loadMore = useCallback6(() => {
|
|
744
|
+
if (infinite.hasNextPage && !infinite.isFetchingNextPage) {
|
|
745
|
+
return infinite.fetchNextPage();
|
|
746
|
+
}
|
|
747
|
+
return Promise.resolve();
|
|
748
|
+
}, [infinite]);
|
|
749
|
+
const goToPage = useCallback6((newPage) => {
|
|
750
|
+
setPage(newPage);
|
|
751
|
+
}, []);
|
|
752
|
+
return {
|
|
753
|
+
items,
|
|
754
|
+
data: infinite.data,
|
|
755
|
+
total: lastPage?.total ?? 0,
|
|
756
|
+
totalPages: lastPage?.totalPages ?? 0,
|
|
757
|
+
currentPage: lastPage?.page ?? page,
|
|
758
|
+
limit: lastPage?.limit ?? limit ?? 20,
|
|
759
|
+
page,
|
|
760
|
+
setPage: goToPage,
|
|
761
|
+
loadMore,
|
|
762
|
+
loadPrevious: infinite.fetchPreviousPage,
|
|
763
|
+
hasMore: infinite.hasNextPage ?? false,
|
|
764
|
+
hasPrevious: infinite.hasPreviousPage ?? false,
|
|
765
|
+
isLoading: infinite.isLoading,
|
|
766
|
+
isFetching: infinite.isFetching,
|
|
767
|
+
isLoadingMore: infinite.isFetchingNextPage,
|
|
768
|
+
error: infinite.error,
|
|
769
|
+
isError: infinite.isError,
|
|
770
|
+
refetch: infinite.refetch
|
|
771
|
+
};
|
|
671
772
|
}
|
|
672
773
|
|
|
673
774
|
// src/react/hooks/use-order.ts
|
|
674
|
-
import { useCallback as
|
|
675
|
-
import { useQuery as
|
|
676
|
-
function useOrder(orderNumber) {
|
|
775
|
+
import { useCallback as useCallback7 } from "react";
|
|
776
|
+
import { useQuery as useQuery12, useMutation as useMutation5, useQueryClient as useQueryClient6 } from "@tanstack/react-query";
|
|
777
|
+
function useOrder(orderNumber, options) {
|
|
677
778
|
const { client } = useBehio();
|
|
678
779
|
const queryClient = useQueryClient6();
|
|
679
780
|
const {
|
|
680
781
|
data,
|
|
681
782
|
isLoading,
|
|
682
783
|
error
|
|
683
|
-
} =
|
|
784
|
+
} = useQuery12({
|
|
684
785
|
queryKey: ["behio", "order", orderNumber],
|
|
685
786
|
queryFn: () => client.orders.get(orderNumber),
|
|
686
|
-
enabled: !!orderNumber && !!client.getAccessToken()
|
|
787
|
+
enabled: options?.enabled !== false && !!orderNumber && !!client.getAccessToken()
|
|
687
788
|
});
|
|
688
789
|
const cancelMutation = useMutation5({
|
|
689
790
|
mutationFn: () => client.orders.cancel(orderNumber),
|
|
@@ -692,7 +793,7 @@ function useOrder(orderNumber) {
|
|
|
692
793
|
queryClient.invalidateQueries({ queryKey: ["behio", "orders"] });
|
|
693
794
|
}
|
|
694
795
|
});
|
|
695
|
-
const cancel =
|
|
796
|
+
const cancel = useCallback7(
|
|
696
797
|
() => cancelMutation.mutateAsync(),
|
|
697
798
|
[cancelMutation]
|
|
698
799
|
);
|
|
@@ -706,12 +807,12 @@ function useOrder(orderNumber) {
|
|
|
706
807
|
}
|
|
707
808
|
|
|
708
809
|
// src/react/hooks/use-checkout.ts
|
|
709
|
-
import { useState as
|
|
810
|
+
import { useState as useState4, useCallback as useCallback8 } from "react";
|
|
710
811
|
import { useMutation as useMutation6, useQueryClient as useQueryClient7 } from "@tanstack/react-query";
|
|
711
812
|
function useCheckout() {
|
|
712
813
|
const { client, storage } = useBehio();
|
|
713
814
|
const queryClient = useQueryClient7();
|
|
714
|
-
const [order, setOrder] =
|
|
815
|
+
const [order, setOrder] = useState4(null);
|
|
715
816
|
const mutation = useMutation6({
|
|
716
817
|
mutationFn: (input) => client.checkout.createOrder(input),
|
|
717
818
|
onSuccess: (result) => {
|
|
@@ -721,11 +822,11 @@ function useCheckout() {
|
|
|
721
822
|
queryClient.invalidateQueries({ queryKey: ["behio", "orders"] });
|
|
722
823
|
}
|
|
723
824
|
});
|
|
724
|
-
const createOrder =
|
|
825
|
+
const createOrder = useCallback8(
|
|
725
826
|
(input) => mutation.mutateAsync(input),
|
|
726
827
|
[mutation]
|
|
727
828
|
);
|
|
728
|
-
const reset =
|
|
829
|
+
const reset = useCallback8(() => {
|
|
729
830
|
setOrder(null);
|
|
730
831
|
mutation.reset();
|
|
731
832
|
}, [mutation]);
|
|
@@ -739,36 +840,43 @@ function useCheckout() {
|
|
|
739
840
|
}
|
|
740
841
|
|
|
741
842
|
// src/react/hooks/use-pages.ts
|
|
742
|
-
import { useQuery as
|
|
743
|
-
function usePages(locale) {
|
|
843
|
+
import { useQuery as useQuery13 } from "@tanstack/react-query";
|
|
844
|
+
function usePages(locale, options) {
|
|
744
845
|
const { client } = useBehio();
|
|
745
|
-
return
|
|
846
|
+
return useQuery13({
|
|
746
847
|
queryKey: ["behio", "pages", locale],
|
|
747
848
|
queryFn: async () => {
|
|
748
849
|
const result = await client.pages.list(locale);
|
|
749
850
|
return result.pages;
|
|
750
|
-
}
|
|
851
|
+
},
|
|
852
|
+
enabled: options?.enabled !== false
|
|
751
853
|
});
|
|
752
854
|
}
|
|
753
|
-
function usePage(slug, locale) {
|
|
855
|
+
function usePage(slug, locale, options) {
|
|
754
856
|
const { client } = useBehio();
|
|
755
|
-
return
|
|
857
|
+
return useQuery13({
|
|
756
858
|
queryKey: ["behio", "page", slug, locale],
|
|
757
859
|
queryFn: () => client.pages.get(slug, locale),
|
|
758
|
-
enabled: !!slug
|
|
860
|
+
enabled: options?.enabled !== false && !!slug
|
|
759
861
|
});
|
|
760
862
|
}
|
|
761
863
|
|
|
762
864
|
// src/react/hooks/use-shop-info.ts
|
|
763
|
-
import { useQuery as
|
|
764
|
-
function useShopInfo() {
|
|
865
|
+
import { useQuery as useQuery14 } from "@tanstack/react-query";
|
|
866
|
+
function useShopInfo(options) {
|
|
765
867
|
const { client } = useBehio();
|
|
766
|
-
return
|
|
868
|
+
return useQuery14({
|
|
767
869
|
queryKey: ["behio", "shop-info"],
|
|
768
|
-
queryFn: () => client.getShopInfo()
|
|
870
|
+
queryFn: () => client.getShopInfo(),
|
|
871
|
+
enabled: options?.enabled !== false
|
|
769
872
|
});
|
|
770
873
|
}
|
|
771
874
|
|
|
875
|
+
// src/react/hooks/use-behio-client.ts
|
|
876
|
+
function useBehioClient() {
|
|
877
|
+
return useBehio().client;
|
|
878
|
+
}
|
|
879
|
+
|
|
772
880
|
// src/react/utils/format-price.ts
|
|
773
881
|
function formatPrice(amount, currency, locale) {
|
|
774
882
|
const resolvedLocale = locale ?? "cs";
|
|
@@ -794,9 +902,11 @@ export {
|
|
|
794
902
|
useAddresses,
|
|
795
903
|
useAuth,
|
|
796
904
|
useBehio,
|
|
905
|
+
useBehioClient,
|
|
797
906
|
useCart,
|
|
798
907
|
useCartCount,
|
|
799
908
|
useCategories,
|
|
909
|
+
useCategory,
|
|
800
910
|
useCheckout,
|
|
801
911
|
useCustomer,
|
|
802
912
|
useFeatured,
|