@simpleapps-com/augur-hooks 0.1.8 → 0.2.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/index.js CHANGED
@@ -1,9 +1,4 @@
1
1
  import {
2
- CATEGORY_CACHE_OPTIONS,
3
- INV_MAST_CACHE_OPTIONS,
4
- INV_MAST_DOC_CACHE_OPTIONS,
5
- PRICE_CACHE_OPTIONS,
6
- SEARCH_SUGGESTIONS_CACHE_OPTIONS,
7
2
  getCartPricingQueryOptions,
8
3
  getCategoryItemsInfiniteKey,
9
4
  getCategoryItemsInfiniteOptions,
@@ -23,13 +18,17 @@ import {
23
18
  getItemPriceOptions,
24
19
  getItemSearchInfiniteKey,
25
20
  getItemSearchInfiniteOptions,
21
+ getJoomlaContentKey,
22
+ getJoomlaContentListKey,
23
+ getJoomlaContentListOptions,
24
+ getJoomlaContentOptions,
26
25
  getProductCategoryKey,
27
26
  getProductCategoryOptions,
28
27
  getProductSearchKey,
29
28
  getProductSearchOptions,
30
29
  getSearchSuggestionsKey,
31
30
  getSearchSuggestionsOptions
32
- } from "./chunk-IGWLRQ6Y.js";
31
+ } from "./chunk-D3APHDL4.js";
33
32
 
34
33
  // src/provider.tsx
35
34
  import { createContext, useContext } from "react";
@@ -41,13 +40,15 @@ var AugurCallbacksContext = createContext(
41
40
  var AugurAuthReactContext = createContext(
42
41
  void 0
43
42
  );
43
+ var AugurCacheContext = createContext(void 0);
44
44
  function AugurHooksProvider({
45
45
  api,
46
46
  callbacks,
47
47
  auth,
48
+ cache,
48
49
  children
49
50
  }) {
50
- return /* @__PURE__ */ jsx(AugurApiContext.Provider, { value: api, children: /* @__PURE__ */ jsx(AugurCallbacksContext.Provider, { value: callbacks, children: /* @__PURE__ */ jsx(AugurAuthReactContext.Provider, { value: auth, children }) }) });
51
+ return /* @__PURE__ */ jsx(AugurApiContext.Provider, { value: api, children: /* @__PURE__ */ jsx(AugurCallbacksContext.Provider, { value: callbacks, children: /* @__PURE__ */ jsx(AugurAuthReactContext.Provider, { value: auth, children: /* @__PURE__ */ jsx(AugurCacheContext.Provider, { value: cache, children }) }) }) });
51
52
  }
52
53
  function useAugurApi() {
53
54
  const api = useContext(AugurApiContext);
@@ -64,6 +65,9 @@ function useAugurCallbacks() {
64
65
  function useAugurAuth() {
65
66
  return useContext(AugurAuthReactContext);
66
67
  }
68
+ function useAugurCache() {
69
+ return useContext(AugurCacheContext);
70
+ }
67
71
 
68
72
  // src/stores/cart-store.ts
69
73
  import { create } from "zustand";
@@ -156,8 +160,9 @@ function useItemPrice(itemId, customerId, quantity = 1, options = {}) {
156
160
  const api = useAugurApi();
157
161
  const callbacks = useAugurCallbacks();
158
162
  const auth = useAugurAuth();
163
+ const cache = useAugurCache();
159
164
  const effectiveCustomerId = customerId ?? auth?.customerId;
160
- const defaultOptions = getItemPriceOptions(api, itemId, effectiveCustomerId, quantity);
165
+ const defaultOptions = getItemPriceOptions(api, itemId, effectiveCustomerId, quantity, cache);
161
166
  const resolvedQueryFn = options.queryFn ?? (callbacks?.getItemPrice ? () => callbacks.getItemPrice(itemId, effectiveCustomerId, quantity) : void 0);
162
167
  return useQuery({
163
168
  ...defaultOptions,
@@ -172,11 +177,13 @@ import { useQuery as useQuery2 } from "@tanstack/react-query";
172
177
  function useInvMastDoc(invMastUid, itemId, options = {}) {
173
178
  const api = useAugurApi();
174
179
  const callbacks = useAugurCallbacks();
180
+ const cache = useAugurCache();
175
181
  const queryOpts = getInvMastDocOptions(
176
182
  api,
177
183
  invMastUid,
178
184
  itemId,
179
- options.includePricing ?? "N"
185
+ options.includePricing ?? "N",
186
+ cache
180
187
  );
181
188
  const { data, isLoading, error } = useQuery2({
182
189
  ...queryOpts,
@@ -192,10 +199,12 @@ import { useQuery as useQuery3 } from "@tanstack/react-query";
192
199
  function useItemCategory(itemCategoryUid, options = {}) {
193
200
  const api = useAugurApi();
194
201
  const callbacks = useAugurCallbacks();
202
+ const cache = useAugurCache();
195
203
  const queryOpts = getItemCategoryOptions(
196
204
  api,
197
205
  itemCategoryUid,
198
- options.apiOptions
206
+ options.apiOptions,
207
+ cache
199
208
  );
200
209
  const { data, isLoading, error } = useQuery3({
201
210
  ...queryOpts,
@@ -210,7 +219,8 @@ import { useQuery as useQuery4 } from "@tanstack/react-query";
210
219
  function useInvMast(invMastUid, itemId, options = {}) {
211
220
  const api = useAugurApi();
212
221
  const callbacks = useAugurCallbacks();
213
- const queryOpts = getInvMastOptions(api, invMastUid, itemId);
222
+ const cache = useAugurCache();
223
+ const queryOpts = getInvMastOptions(api, invMastUid, itemId, cache);
214
224
  const { data, isLoading, error } = useQuery4({
215
225
  ...queryOpts,
216
226
  ...options.queryFn ?? callbacks?.getInvMast ? { queryFn: options.queryFn ?? (() => callbacks.getInvMast(invMastUid, itemId)) } : {},
@@ -224,7 +234,8 @@ import { useQuery as useQuery5 } from "@tanstack/react-query";
224
234
  function useInvMastStock(invMastUid, options = {}) {
225
235
  const api = useAugurApi();
226
236
  const callbacks = useAugurCallbacks();
227
- const queryOpts = getInvMastStockOptions(api, invMastUid);
237
+ const cache = useAugurCache();
238
+ const queryOpts = getInvMastStockOptions(api, invMastUid, cache);
228
239
  const { data, isLoading, error } = useQuery5({
229
240
  ...queryOpts,
230
241
  ...options.queryFn ?? callbacks?.getInvMastStock ? { queryFn: options.queryFn ?? (() => callbacks.getInvMastStock(invMastUid)) } : {},
@@ -254,7 +265,8 @@ function resolveData(data) {
254
265
  function useProductCategory(itemCategoryUid, options = {}) {
255
266
  const api = useAugurApi();
256
267
  const callbacks = useAugurCallbacks();
257
- const baseOptions = itemCategoryUid ? getProductCategoryOptions(api, itemCategoryUid) : {
268
+ const cache = useAugurCache();
269
+ const baseOptions = itemCategoryUid ? getProductCategoryOptions(api, itemCategoryUid, cache) : {
258
270
  queryKey: getProductCategoryKey(null),
259
271
  /* v8 ignore next */
260
272
  queryFn: () => Promise.reject()
@@ -281,8 +293,9 @@ import { useQuery as useQuery7 } from "@tanstack/react-query";
281
293
  function useItemDetails(itemId, options = {}) {
282
294
  const api = useAugurApi();
283
295
  const callbacks = useAugurCallbacks();
296
+ const cache = useAugurCache();
284
297
  const { data, isLoading, error } = useQuery7({
285
- ...itemId ? getItemDetailsOptions(api, itemId) : {
298
+ ...itemId ? getItemDetailsOptions(api, itemId, cache) : {
286
299
  queryKey: getItemDetailsKey(""),
287
300
  /* v8 ignore next */
288
301
  queryFn: () => Promise.reject()
@@ -308,8 +321,9 @@ import { useQuery as useQuery8 } from "@tanstack/react-query";
308
321
  function useItemAttributes(itemCategoryUid, options = {}) {
309
322
  const api = useAugurApi();
310
323
  const callbacks = useAugurCallbacks();
324
+ const cache = useAugurCache();
311
325
  const { data, isLoading, error } = useQuery8({
312
- ...itemCategoryUid ? getItemAttributesOptions(api, itemCategoryUid) : {
326
+ ...itemCategoryUid ? getItemAttributesOptions(api, itemCategoryUid, cache) : {
313
327
  queryKey: getItemAttributesKey(null),
314
328
  /* v8 ignore next */
315
329
  queryFn: () => Promise.reject()
@@ -333,7 +347,8 @@ import { useQuery as useQuery9 } from "@tanstack/react-query";
333
347
  function useProductSearch(pageData, options = {}) {
334
348
  const api = useAugurApi();
335
349
  const callbacks = useAugurCallbacks();
336
- const defaultOptions = getProductSearchOptions(api, pageData);
350
+ const cache = useAugurCache();
351
+ const defaultOptions = getProductSearchOptions(api, pageData, cache);
337
352
  const { data, isLoading, error } = useQuery9({
338
353
  ...defaultOptions,
339
354
  ...options.queryFn ?? callbacks?.getProductSearch ? { queryFn: options.queryFn ?? (() => callbacks.getProductSearch(pageData)) } : {},
@@ -355,10 +370,11 @@ import { useQuery as useQuery10 } from "@tanstack/react-query";
355
370
  function useSearchSuggestions(query, options = {}) {
356
371
  const api = useAugurApi();
357
372
  const callbacks = useAugurCallbacks();
373
+ const cache = useAugurCache();
358
374
  const limit = options.limit ?? 10;
359
375
  const offset = options.offset ?? 0;
360
376
  const enabled = (options.enabled ?? true) && query.trim().length > 0;
361
- const defaultOptions = getSearchSuggestionsOptions(api, query, limit, offset);
377
+ const defaultOptions = getSearchSuggestionsOptions(api, query, limit, offset, cache);
362
378
  const { data, isLoading, error } = useQuery10({
363
379
  ...defaultOptions,
364
380
  ...options.queryFn ?? callbacks?.getSearchSuggestions ? { queryFn: options.queryFn ?? (() => callbacks.getSearchSuggestions(query, limit, offset)) } : {},
@@ -558,7 +574,6 @@ function useCartActions(callbacks) {
558
574
  // src/hooks/use-cart-initialization.ts
559
575
  import { useEffect as useEffect2, useMemo, useRef } from "react";
560
576
  import { useQuery as useQuery11, useQueryClient as useQueryClient2 } from "@tanstack/react-query";
561
- import { CACHE_CONFIG } from "@simpleapps-com/augur-utils";
562
577
  var MAX_RETRY_ATTEMPTS = 7;
563
578
  var RETRY_DELAY_MS = 1e3;
564
579
  function useCartInitialization(session, callbacks) {
@@ -642,8 +657,7 @@ function useCartInitialization(session, callbacks) {
642
657
  const { data: cartLinesData } = useQuery11({
643
658
  queryKey: ["cartLines", cartHdrUid],
644
659
  queryFn: async () => await callbacks.getCartLines(Number(cartHdrUid)) ?? [],
645
- enabled: !!cartHdrUid,
646
- ...CACHE_CONFIG.CART
660
+ enabled: !!cartHdrUid
647
661
  });
648
662
  useEffect2(() => {
649
663
  if (cartLinesData) {
@@ -664,6 +678,7 @@ function useCartPricing(options = {}) {
664
678
  const api = useAugurApi();
665
679
  const callbacks = useAugurCallbacks();
666
680
  const auth = useAugurAuth();
681
+ const cache = useAugurCache();
667
682
  const cartLines = useCartLines();
668
683
  const customerId = options.customerId ?? auth?.customerId;
669
684
  const priceFn = options.queryFn ?? callbacks?.getItemPrice;
@@ -672,8 +687,10 @@ function useCartPricing(options = {}) {
672
687
  const base = priceFn ? {
673
688
  queryKey: getItemPriceKey(line.itemId, customerId, line.quantity),
674
689
  queryFn: () => priceFn(line.itemId, customerId, line.quantity),
675
- ...PRICE_CACHE_OPTIONS
676
- } : getItemPriceOptions(api, line.itemId, customerId, line.quantity);
690
+ refetchOnReconnect: true,
691
+ refetchOnWindowFocus: false,
692
+ meta: { persist: true }
693
+ } : getItemPriceOptions(api, line.itemId, customerId, line.quantity, cache);
677
694
  return {
678
695
  ...base,
679
696
  enabled: !!customerId && !!line.itemId
@@ -771,11 +788,13 @@ import { useInfiniteQuery } from "@tanstack/react-query";
771
788
  function useCategoryItemsInfinite(itemCategoryUid, itemsFilters, options = {}) {
772
789
  const api = useAugurApi();
773
790
  const callbacks = useAugurCallbacks();
791
+ const cache = useAugurCache();
774
792
  const { enabled = true } = options;
775
793
  const defaultOptions = getCategoryItemsInfiniteOptions(
776
794
  api,
777
795
  itemCategoryUid,
778
- itemsFilters
796
+ itemsFilters,
797
+ cache
779
798
  );
780
799
  const cb = callbacks?.getCategoryItemsInfinite;
781
800
  const resolvedQueryFn = options.queryFn ?? (cb ? ({ pageParam }) => cb({
@@ -796,11 +815,13 @@ import { useInfiniteQuery as useInfiniteQuery2 } from "@tanstack/react-query";
796
815
  function useItemSearchInfinite(itemsFilters, itemCategoryUid, options = {}) {
797
816
  const api = useAugurApi();
798
817
  const callbacks = useAugurCallbacks();
818
+ const cache = useAugurCache();
799
819
  const { enabled = true } = options;
800
820
  const defaultOptions = getItemSearchInfiniteOptions(
801
821
  api,
802
822
  itemsFilters,
803
- itemCategoryUid
823
+ itemCategoryUid,
824
+ cache
804
825
  );
805
826
  const cb = callbacks?.getItemSearchInfinite;
806
827
  const resolvedQueryFn = options.queryFn ?? (cb ? ({ pageParam }) => cb({
@@ -815,13 +836,108 @@ function useItemSearchInfinite(itemsFilters, itemCategoryUid, options = {}) {
815
836
  enabled: enabled && !!itemsFilters.q
816
837
  });
817
838
  }
839
+
840
+ // src/hooks/use-joomla-content.ts
841
+ import { useQuery as useQuery12 } from "@tanstack/react-query";
842
+ function useJoomlaContent(articleId, options = {}) {
843
+ const api = useAugurApi();
844
+ const callbacks = useAugurCallbacks();
845
+ const cache = useAugurCache();
846
+ const { data, isLoading, error } = useQuery12({
847
+ ...articleId ? getJoomlaContentOptions(api, articleId, cache) : {
848
+ queryKey: getJoomlaContentKey(""),
849
+ /* v8 ignore next */
850
+ queryFn: () => Promise.reject()
851
+ },
852
+ ...options.queryFn ?? callbacks?.getJoomlaContent ? { queryFn: options.queryFn ?? (() => callbacks.getJoomlaContent(articleId)) } : {},
853
+ enabled: (options.enabled ?? true) && !!articleId
854
+ });
855
+ return {
856
+ article: data ?? null,
857
+ isLoading,
858
+ error
859
+ };
860
+ }
861
+
862
+ // src/hooks/use-joomla-content-list.ts
863
+ import { useQuery as useQuery13 } from "@tanstack/react-query";
864
+ function useJoomlaContentList(categoryId, options = {}) {
865
+ const api = useAugurApi();
866
+ const callbacks = useAugurCallbacks();
867
+ const cache = useAugurCache();
868
+ const { data, isLoading, error } = useQuery13({
869
+ ...categoryId ? getJoomlaContentListOptions(api, categoryId, options.filters, cache) : {
870
+ queryKey: getJoomlaContentListKey(""),
871
+ /* v8 ignore next */
872
+ queryFn: () => Promise.reject()
873
+ },
874
+ ...options.queryFn ?? callbacks?.getJoomlaContentList ? {
875
+ queryFn: options.queryFn ?? (() => callbacks.getJoomlaContentList(categoryId, options.filters))
876
+ } : {},
877
+ enabled: (options.enabled ?? true) && !!categoryId
878
+ });
879
+ return {
880
+ articles: data ?? [],
881
+ isLoading,
882
+ error
883
+ };
884
+ }
885
+
886
+ // src/create-site-hooks.ts
887
+ import { createElement } from "react";
888
+ function createSiteHooks(config = {}) {
889
+ const { callbacks, defaultCustomerId, cache } = config;
890
+ function Provider({ api, auth, children }) {
891
+ const mergedAuth = {
892
+ status: auth?.status ?? "unauthenticated",
893
+ customerId: auth?.customerId ?? defaultCustomerId,
894
+ userId: auth?.userId,
895
+ cartHdrUid: auth?.cartHdrUid
896
+ };
897
+ return createElement(AugurHooksProvider, {
898
+ api,
899
+ callbacks,
900
+ auth: mergedAuth,
901
+ cache,
902
+ children
903
+ });
904
+ }
905
+ Provider.displayName = "AugurSiteProvider";
906
+ return {
907
+ Provider,
908
+ // Hooks -- pricing
909
+ useItemPrice,
910
+ useCartPricing,
911
+ // Hooks -- inventory
912
+ useInvMast,
913
+ useInvMastDoc,
914
+ useInvMastStock,
915
+ // Hooks -- items & categories
916
+ useItemCategory,
917
+ useProductCategory,
918
+ useItemDetails,
919
+ useItemAttributes,
920
+ // Hooks -- search
921
+ useProductSearch,
922
+ useSearchSuggestions,
923
+ // Hooks -- cart
924
+ useCartActions,
925
+ useCartInitialization,
926
+ // Hooks -- pagination & infinite scroll
927
+ usePaginationPrefetch,
928
+ useCategoryItemsInfinite,
929
+ useItemSearchInfinite,
930
+ // Hooks -- Joomla
931
+ useJoomlaContent,
932
+ useJoomlaContentList,
933
+ // Hooks -- utilities
934
+ useDebounce,
935
+ useFormatPrice
936
+ };
937
+ }
818
938
  export {
819
939
  AugurHooksProvider,
820
- CATEGORY_CACHE_OPTIONS,
821
- INV_MAST_CACHE_OPTIONS,
822
- INV_MAST_DOC_CACHE_OPTIONS,
823
- PRICE_CACHE_OPTIONS,
824
- SEARCH_SUGGESTIONS_CACHE_OPTIONS,
940
+ createSiteHooks,
825
941
  getCartPricingQueryOptions,
826
942
  getCategoryItemsInfiniteKey,
827
943
  getCategoryItemsInfiniteOptions,
@@ -841,6 +957,10 @@ export {
841
957
  getItemPriceOptions,
842
958
  getItemSearchInfiniteKey,
843
959
  getItemSearchInfiniteOptions,
960
+ getJoomlaContentKey,
961
+ getJoomlaContentListKey,
962
+ getJoomlaContentListOptions,
963
+ getJoomlaContentOptions,
844
964
  getProductCategoryKey,
845
965
  getProductCategoryOptions,
846
966
  getProductSearchKey,
@@ -849,6 +969,7 @@ export {
849
969
  getSearchSuggestionsOptions,
850
970
  useAugurApi,
851
971
  useAugurAuth,
972
+ useAugurCache,
852
973
  useCartActions,
853
974
  useCartHdrUid,
854
975
  useCartInitialization,
@@ -869,6 +990,8 @@ export {
869
990
  useItemFiltersStore,
870
991
  useItemPrice,
871
992
  useItemSearchInfinite,
993
+ useJoomlaContent,
994
+ useJoomlaContentList,
872
995
  usePaginationPrefetch,
873
996
  useProductCategory,
874
997
  useProductSearch,