@tiledev/sdk-shopify 0.9.0 → 0.10.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/README.md +650 -50
- package/dist/alertSettings.d.ts.map +1 -1
- package/dist/alertSettings.js +2 -0
- package/dist/alertSettings.js.map +1 -1
- package/dist/attribution.d.ts +21 -0
- package/dist/attribution.d.ts.map +1 -1
- package/dist/attribution.js +17 -0
- package/dist/attribution.js.map +1 -1
- package/dist/auth/pkce.d.ts +30 -0
- package/dist/auth/pkce.d.ts.map +1 -0
- package/dist/auth/pkce.js +128 -0
- package/dist/auth/pkce.js.map +1 -0
- package/dist/auth/session.d.ts +118 -0
- package/dist/auth/session.d.ts.map +1 -0
- package/dist/auth/session.js +896 -0
- package/dist/auth/session.js.map +1 -0
- package/dist/auth/storeCredit.d.ts +64 -0
- package/dist/auth/storeCredit.d.ts.map +1 -0
- package/dist/auth/storeCredit.js +274 -0
- package/dist/auth/storeCredit.js.map +1 -0
- package/dist/cart.d.ts +25 -1
- package/dist/cart.d.ts.map +1 -1
- package/dist/cart.js +95 -2
- package/dist/cart.js.map +1 -1
- package/dist/cartPolicy.js +4 -4
- package/dist/checkout.d.ts +123 -0
- package/dist/checkout.d.ts.map +1 -0
- package/dist/checkout.js +205 -0
- package/dist/checkout.js.map +1 -0
- package/dist/customer.d.ts.map +1 -1
- package/dist/customer.js +8 -0
- package/dist/customer.js.map +1 -1
- package/dist/index.d.ts +12 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +74 -2
- package/dist/index.js.map +1 -1
- package/dist/messages.d.ts.map +1 -1
- package/dist/messages.js +2 -0
- package/dist/messages.js.map +1 -1
- package/dist/money.d.ts +16 -0
- package/dist/money.d.ts.map +1 -1
- package/dist/money.js +55 -0
- package/dist/money.js.map +1 -1
- package/dist/orders.d.ts +99 -0
- package/dist/orders.d.ts.map +1 -0
- package/dist/orders.js +439 -0
- package/dist/orders.js.map +1 -0
- package/dist/productPage.d.ts +100 -0
- package/dist/productPage.d.ts.map +1 -0
- package/dist/productPage.js +298 -0
- package/dist/productPage.js.map +1 -0
- package/dist/productStore.d.ts +1 -1
- package/dist/productStore.d.ts.map +1 -1
- package/dist/productStore.js +63 -5
- package/dist/productStore.js.map +1 -1
- package/dist/products.d.ts.map +1 -1
- package/dist/products.js +15 -3
- package/dist/products.js.map +1 -1
- package/dist/purchaseRules.d.ts +74 -0
- package/dist/purchaseRules.d.ts.map +1 -0
- package/dist/purchaseRules.js +73 -0
- package/dist/purchaseRules.js.map +1 -0
- package/dist/queries.d.ts +15 -13
- package/dist/queries.d.ts.map +1 -1
- package/dist/queries.js +43 -7
- package/dist/queries.js.map +1 -1
- package/dist/react/ShopifyProvider.d.ts +167 -25
- package/dist/react/ShopifyProvider.d.ts.map +1 -1
- package/dist/react/ShopifyProvider.js +352 -207
- package/dist/react/ShopifyProvider.js.map +1 -1
- package/dist/react/appForeground.d.ts +6 -0
- package/dist/react/appForeground.d.ts.map +1 -0
- package/dist/react/appForeground.js +19 -0
- package/dist/react/appForeground.js.map +1 -0
- package/dist/react/appForeground.native.d.ts +2 -0
- package/dist/react/appForeground.native.d.ts.map +1 -0
- package/dist/react/appForeground.native.js +23 -0
- package/dist/react/appForeground.native.js.map +1 -0
- package/dist/react/index.d.ts +7 -1
- package/dist/react/index.d.ts.map +1 -1
- package/dist/react/index.js +23 -1
- package/dist/react/index.js.map +1 -1
- package/dist/react/tileCreditClient.d.ts +4 -0
- package/dist/react/tileCreditClient.d.ts.map +1 -0
- package/dist/react/tileCreditClient.js +27 -0
- package/dist/react/tileCreditClient.js.map +1 -0
- package/dist/react/useAppDiscountCode.d.ts +19 -0
- package/dist/react/useAppDiscountCode.d.ts.map +1 -0
- package/dist/react/useAppDiscountCode.js +44 -0
- package/dist/react/useAppDiscountCode.js.map +1 -0
- package/dist/react/useCartStoreCredit.d.ts +25 -0
- package/dist/react/useCartStoreCredit.d.ts.map +1 -0
- package/dist/react/useCartStoreCredit.js +292 -0
- package/dist/react/useCartStoreCredit.js.map +1 -0
- package/dist/react/useOrders.d.ts +104 -0
- package/dist/react/useOrders.d.ts.map +1 -0
- package/dist/react/useOrders.js +287 -0
- package/dist/react/useOrders.js.map +1 -0
- package/dist/react/useProduct.d.ts.map +1 -1
- package/dist/react/useProduct.js +1 -9
- package/dist/react/useProduct.js.map +1 -1
- package/dist/react/useProductPage.d.ts +148 -0
- package/dist/react/useProductPage.d.ts.map +1 -0
- package/dist/react/useProductPage.js +178 -0
- package/dist/react/useProductPage.js.map +1 -0
- package/dist/react/useProductRecommendations.d.ts +27 -0
- package/dist/react/useProductRecommendations.d.ts.map +1 -0
- package/dist/react/useProductRecommendations.js +76 -0
- package/dist/react/useProductRecommendations.js.map +1 -0
- package/dist/react/useStoreCredit.d.ts +58 -0
- package/dist/react/useStoreCredit.d.ts.map +1 -0
- package/dist/react/useStoreCredit.js +209 -0
- package/dist/react/useStoreCredit.js.map +1 -0
- package/dist/react/useTileCredit.d.ts +4 -3
- package/dist/react/useTileCredit.d.ts.map +1 -1
- package/dist/react/useTileCredit.js +63 -55
- package/dist/react/useTileCredit.js.map +1 -1
- package/dist/shopify.d.ts.map +1 -1
- package/dist/shopify.js +2 -0
- package/dist/shopify.js.map +1 -1
- package/dist/storedList.d.ts +27 -0
- package/dist/storedList.d.ts.map +1 -0
- package/dist/storedList.js +152 -0
- package/dist/storedList.js.map +1 -0
- package/dist/tileCredit.d.ts +25 -10
- package/dist/tileCredit.d.ts.map +1 -1
- package/dist/tileCredit.js +97 -45
- package/dist/tileCredit.js.map +1 -1
- package/dist/types.d.ts +407 -34
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +1 -2
- package/dist/types.js.map +1 -1
- package/dist/variants.d.ts.map +1 -1
- package/dist/variants.js +4 -3
- package/dist/variants.js.map +1 -1
- package/dist/waitlist.d.ts +4 -0
- package/dist/waitlist.d.ts.map +1 -0
- package/dist/waitlist.js +213 -0
- package/dist/waitlist.js.map +1 -0
- package/dist/wishlist.d.ts.map +1 -1
- package/dist/wishlist.js +70 -47
- package/dist/wishlist.js.map +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useVariantSelection = useVariantSelection;
|
|
4
|
+
exports.useAddToCart = useAddToCart;
|
|
5
|
+
exports.useProductPage = useProductPage;
|
|
6
|
+
/**
|
|
7
|
+
* Product page hooks, from small to whole:
|
|
8
|
+
*
|
|
9
|
+
* - `useVariantSelection(product)`: the selection, every option value's state, the chosen variant,
|
|
10
|
+
* its price and stock. For any surface that sells a variant (a page, a buy sheet, a quick add).
|
|
11
|
+
* - `useAddToCart(variant)`: add with the stock ceiling checked first, the `adding` state, the result.
|
|
12
|
+
* - `useProductPage(handle)`: both, on `useProduct`'s cache-first product, plus the gallery's media,
|
|
13
|
+
* the parsed description, the favourite, the share link and, when asked, "You may also like"
|
|
14
|
+
* (`useProductRecommendations`).
|
|
15
|
+
*
|
|
16
|
+
* Everything is overridable: each rule takes an option, each returned section is plain data a
|
|
17
|
+
* screen can ignore, and the pure helpers (`../productPage`) are there for a page of its own.
|
|
18
|
+
*/
|
|
19
|
+
const react_1 = require("react");
|
|
20
|
+
const productPage_1 = require("../productPage");
|
|
21
|
+
const ShopifyProvider_1 = require("./ShopifyProvider");
|
|
22
|
+
const useProduct_1 = require("./useProduct");
|
|
23
|
+
const useProductRecommendations_1 = require("./useProductRecommendations");
|
|
24
|
+
function useVariantSelection(product, options = {}) {
|
|
25
|
+
var _a, _b, _c;
|
|
26
|
+
const variants = (0, react_1.useMemo)(() => { var _a; return (_a = product === null || product === void 0 ? void 0 : product.variants) !== null && _a !== void 0 ? _a : []; }, [product === null || product === void 0 ? void 0 : product.variants]);
|
|
27
|
+
const known = variants.length > 0;
|
|
28
|
+
const isUnavailable = (_a = options.isUnavailable) !== null && _a !== void 0 ? _a : productPage_1.isVariantUnavailable;
|
|
29
|
+
const orderKey = ((_b = options.optionOrder) !== null && _b !== void 0 ? _b : []).join('\u0001');
|
|
30
|
+
const includeRef = (0, react_1.useRef)(options.includeOption);
|
|
31
|
+
includeRef.current = options.includeOption;
|
|
32
|
+
const selectable = (0, react_1.useMemo)(() => (0, productPage_1.selectableOptions)(product === null || product === void 0 ? void 0 : product.options, { order: options.optionOrder, include: includeRef.current }),
|
|
33
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
34
|
+
[product === null || product === void 0 ? void 0 : product.options, orderKey]);
|
|
35
|
+
const startRef = (0, react_1.useRef)(options.initialSelection);
|
|
36
|
+
startRef.current = options.initialSelection;
|
|
37
|
+
const initial = (0, react_1.useMemo)(() => {
|
|
38
|
+
var _a;
|
|
39
|
+
const linked = options.initialVariantId ? variants.find((v) => v.id === options.initialVariantId) : null;
|
|
40
|
+
if (linked)
|
|
41
|
+
return (0, productPage_1.selectionForVariant)(linked);
|
|
42
|
+
return ((_a = startRef.current) !== null && _a !== void 0 ? _a : productPage_1.initialSelection)(variants);
|
|
43
|
+
}, [variants, options.initialVariantId]);
|
|
44
|
+
const [chosen, setChosen] = (0, react_1.useState)(null);
|
|
45
|
+
const resetKey = options.resetKey !== undefined ? options.resetKey : ((_c = product === null || product === void 0 ? void 0 : product.id) !== null && _c !== void 0 ? _c : null);
|
|
46
|
+
(0, react_1.useEffect)(() => setChosen(null), [resetKey]);
|
|
47
|
+
const selection = chosen !== null && chosen !== void 0 ? chosen : initial;
|
|
48
|
+
const variant = known ? (0, productPage_1.findVariant)(variants, selection) : null;
|
|
49
|
+
const setOption = (0, react_1.useCallback)((name, value) => setChosen((current) => ({ ...(current !== null && current !== void 0 ? current : initial), [name]: value })), [initial]);
|
|
50
|
+
const selectVariant = (0, react_1.useCallback)((variantId) => {
|
|
51
|
+
const found = variants.find((v) => v.id === variantId);
|
|
52
|
+
if (found)
|
|
53
|
+
setChosen((0, productPage_1.selectionForVariant)(found));
|
|
54
|
+
}, [variants]);
|
|
55
|
+
const reset = (0, react_1.useCallback)(() => setChosen(null), []);
|
|
56
|
+
const states = (0, react_1.useMemo)(() => (0, productPage_1.optionStates)(selectable, variants, selection, isUnavailable),
|
|
57
|
+
// isUnavailable is a rule, read each time; its identity isn't part of the key.
|
|
58
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
59
|
+
[selectable, variants, selection]);
|
|
60
|
+
const threshold = options.lowStockThreshold === undefined ? productPage_1.DEFAULT_LOW_STOCK_THRESHOLD : options.lowStockThreshold;
|
|
61
|
+
return {
|
|
62
|
+
options: selectable,
|
|
63
|
+
optionStates: states,
|
|
64
|
+
selection,
|
|
65
|
+
setOption,
|
|
66
|
+
selectVariant,
|
|
67
|
+
reset,
|
|
68
|
+
variant,
|
|
69
|
+
label: (0, productPage_1.selectionLabel)(selectable, selection, options.separator),
|
|
70
|
+
known,
|
|
71
|
+
price: (0, productPage_1.variantPrice)(variant, product),
|
|
72
|
+
unavailable: known && (!(product === null || product === void 0 ? void 0 : product.availableForSale) || isUnavailable(variant)),
|
|
73
|
+
lowStock: known && (0, productPage_1.isLowStock)(product === null || product === void 0 ? void 0 : product.totalInventory, product === null || product === void 0 ? void 0 : product.availableForSale, threshold),
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
function useAddToCart(variant, options = {}) {
|
|
77
|
+
const { addLine, cart } = (0, ShopifyProvider_1.useCart)();
|
|
78
|
+
const [adding, setAdding] = (0, react_1.useState)(false);
|
|
79
|
+
const optionsRef = (0, react_1.useRef)(options);
|
|
80
|
+
optionsRef.current = options;
|
|
81
|
+
const ceiling = variant ? (options.maxQuantity ? options.maxQuantity(variant) : (0, productPage_1.stockCeiling)(variant)) : 0;
|
|
82
|
+
const inCart = (0, productPage_1.quantityInCart)(cart, variant === null || variant === void 0 ? void 0 : variant.id);
|
|
83
|
+
const add = (0, react_1.useCallback)(async (input = {}) => {
|
|
84
|
+
var _a, _b, _c, _d, _e;
|
|
85
|
+
const o = optionsRef.current;
|
|
86
|
+
const done = (result) => {
|
|
87
|
+
var _a;
|
|
88
|
+
(_a = (result.ok ? o.onAdded : o.onRefused)) === null || _a === void 0 ? void 0 : _a(result);
|
|
89
|
+
return result;
|
|
90
|
+
};
|
|
91
|
+
if (!variant)
|
|
92
|
+
return done({ ok: false, reason: 'no-variant', cart });
|
|
93
|
+
const max = o.maxQuantity ? o.maxQuantity(variant) : (0, productPage_1.stockCeiling)(variant);
|
|
94
|
+
setAdding(true);
|
|
95
|
+
try {
|
|
96
|
+
const result = await addLine({
|
|
97
|
+
merchandiseId: variant.id,
|
|
98
|
+
quantity: (_b = (_a = input.quantity) !== null && _a !== void 0 ? _a : o.quantity) !== null && _b !== void 0 ? _b : 1,
|
|
99
|
+
attributes: (_c = input.attributes) !== null && _c !== void 0 ? _c : o.attributes,
|
|
100
|
+
sellingPlanId: (_d = input.sellingPlanId) !== null && _d !== void 0 ? _d : o.sellingPlanId,
|
|
101
|
+
maxQuantity: max,
|
|
102
|
+
source: (_e = input.source) !== null && _e !== void 0 ? _e : o.source,
|
|
103
|
+
});
|
|
104
|
+
return done(result);
|
|
105
|
+
}
|
|
106
|
+
catch (error) {
|
|
107
|
+
// Shopify refused the line: the provider has raised cart:outOfStock already.
|
|
108
|
+
return done({ ok: false, reason: 'outOfStock', error, cart });
|
|
109
|
+
}
|
|
110
|
+
finally {
|
|
111
|
+
setAdding(false);
|
|
112
|
+
}
|
|
113
|
+
}, [variant, addLine, cart]);
|
|
114
|
+
return { add, adding, inCart, canAddMore: !!variant && (0, productPage_1.withinCeiling)(ceiling, inCart, 1) };
|
|
115
|
+
}
|
|
116
|
+
function useProductPage(handle, options = {}) {
|
|
117
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
|
|
118
|
+
const result = (0, useProduct_1.useProduct)(handle, { imageTransform: options.imageTransform, onError: options.onError });
|
|
119
|
+
const { product, preview } = result;
|
|
120
|
+
const shown = (_a = product !== null && product !== void 0 ? product : preview) !== null && _a !== void 0 ? _a : null;
|
|
121
|
+
const selection = useVariantSelection(shown, { ...options, resetKey: options.resetKey !== undefined ? options.resetKey : (handle !== null && handle !== void 0 ? handle : null) });
|
|
122
|
+
const cart = useAddToCart(selection.variant, options);
|
|
123
|
+
const wishlist = (0, ShopifyProvider_1.useWishlist)();
|
|
124
|
+
// Once per product, when the full product lands.
|
|
125
|
+
const viewed = (0, react_1.useRef)(null);
|
|
126
|
+
const onViewRef = (0, react_1.useRef)(options.onView);
|
|
127
|
+
onViewRef.current = options.onView;
|
|
128
|
+
(0, react_1.useEffect)(() => {
|
|
129
|
+
var _a;
|
|
130
|
+
if (!product || viewed.current === product.id)
|
|
131
|
+
return;
|
|
132
|
+
viewed.current = product.id;
|
|
133
|
+
(_a = onViewRef.current) === null || _a === void 0 ? void 0 : _a.call(onViewRef, product);
|
|
134
|
+
}, [product]);
|
|
135
|
+
const html = (shown === null || shown === void 0 ? void 0 : shown.descriptionHtml) || (shown === null || shown === void 0 ? void 0 : shown.description) || '';
|
|
136
|
+
const parse = (_b = options.parseDescription) !== null && _b !== void 0 ? _b : true;
|
|
137
|
+
const blocks = (0, react_1.useMemo)(() => (parse === false ? [] : typeof parse === 'function' ? parse(html) : (0, productPage_1.parseDescriptionHtml)(html)), [parse, html]);
|
|
138
|
+
const blocked = !!shown && !!((_c = options.isBlocked) === null || _c === void 0 ? void 0 : _c.call(options, shown));
|
|
139
|
+
const status = !shown
|
|
140
|
+
? 'loading'
|
|
141
|
+
: blocked
|
|
142
|
+
? 'blocked'
|
|
143
|
+
: !selection.known
|
|
144
|
+
? 'unknown'
|
|
145
|
+
: selection.unavailable
|
|
146
|
+
? 'unavailable'
|
|
147
|
+
: 'available';
|
|
148
|
+
const toggle = (0, react_1.useCallback)(() => (shown ? wishlist.toggle(product !== null && product !== void 0 ? product : shown.id) : Promise.resolve(false)), [wishlist, product, shown]);
|
|
149
|
+
const asked = options.recommendations;
|
|
150
|
+
const recommendationOptions = typeof asked === 'object' ? asked : {};
|
|
151
|
+
const isBlocked = options.isBlocked;
|
|
152
|
+
const recommendations = (0, useProductRecommendations_1.useProductRecommendations)(shown === null || shown === void 0 ? void 0 : shown.id, {
|
|
153
|
+
...recommendationOptions,
|
|
154
|
+
enabled: !!asked && ((_d = recommendationOptions.enabled) !== null && _d !== void 0 ? _d : true),
|
|
155
|
+
exclude: (_e = recommendationOptions.exclude) !== null && _e !== void 0 ? _e : (isBlocked ? (p) => isBlocked(p) : undefined),
|
|
156
|
+
onError: (_f = recommendationOptions.onError) !== null && _f !== void 0 ? _f : (options.onError ? (error, ctx) => options.onError(error, { at: ctx.at, handle: handle !== null && handle !== void 0 ? handle : '' }) : undefined),
|
|
157
|
+
});
|
|
158
|
+
return {
|
|
159
|
+
...result,
|
|
160
|
+
product,
|
|
161
|
+
preview,
|
|
162
|
+
shown,
|
|
163
|
+
status,
|
|
164
|
+
selection,
|
|
165
|
+
cart,
|
|
166
|
+
media: {
|
|
167
|
+
items: (_g = product === null || product === void 0 ? void 0 : product.media) !== null && _g !== void 0 ? _g : [],
|
|
168
|
+
initialIndex: product ? (0, productPage_1.initialMediaIndex)(product.media, options.playVideo) : 0,
|
|
169
|
+
firstImageUrl: product ? (0, productPage_1.firstImageUrl)(product) : ((_j = (_h = preview === null || preview === void 0 ? void 0 : preview.featuredImage) === null || _h === void 0 ? void 0 : _h.url) !== null && _j !== void 0 ? _j : null),
|
|
170
|
+
previewImageUrl: (_l = (_k = preview === null || preview === void 0 ? void 0 : preview.featuredImage) === null || _k === void 0 ? void 0 : _k.url) !== null && _l !== void 0 ? _l : null,
|
|
171
|
+
},
|
|
172
|
+
description: { html, blocks },
|
|
173
|
+
favorite: { isFavorite: !!shown && wishlist.has(shown.id), toggle },
|
|
174
|
+
shareUrl: (0, productPage_1.productShareUrl)(product !== null && product !== void 0 ? product : shown, options.storeDomain),
|
|
175
|
+
recommendations,
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
//# sourceMappingURL=useProductPage.js.map
|
|
@@ -0,0 +1 @@
|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AAK,mCAAI,EAAE;YAC3B,YAAY,EAAE,OAAO,CAAC,CAAC,CAAC,IAAA,+BAAiB,EAAC,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC;YAC/E,aAAa,EAAE,OAAO,CAAC,CAAC,CAAC,IAAA,2BAAa,EAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,MAAA,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,aAAa,0CAAE,GAAG,mCAAI,IAAI,CAAC;YACvF,eAAe,EAAE,MAAA,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,aAAa,0CAAE,GAAG,mCAAI,IAAI;SACrD;QACD,WAAW,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE;QAC7B,QAAQ,EAAE,EAAE,UAAU,EAAE,CAAC,CAAC,KAAK,IAAI,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC,EAAE,MAAM,EAAE;QACnE,QAAQ,EAAE,IAAA,6BAAe,EAAC,OAAO,aAAP,OAAO,cAAP,OAAO,GAAI,KAAK,EAAE,OAAO,CAAC,WAAW,CAAC;QAChE,eAAe;KAChB,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { ImageTransform, Product } from '../types';
|
|
2
|
+
export interface UseProductRecommendationsOptions {
|
|
3
|
+
/** Read them. Below the fold on most pages: pass false until the page has settled. @default true */
|
|
4
|
+
enabled?: boolean;
|
|
5
|
+
/** At most this many. @default 10 */
|
|
6
|
+
limit?: number;
|
|
7
|
+
/** For the cards' images. A grid's card size keeps them in the same image cache as the grids. */
|
|
8
|
+
imageTransform?: ImageTransform;
|
|
9
|
+
/** Leave a product out (e.g. one that can't be bought). The product itself is always left out. */
|
|
10
|
+
exclude?: (product: Product) => boolean;
|
|
11
|
+
onError?: (error: unknown, context: {
|
|
12
|
+
at: string;
|
|
13
|
+
productId: string;
|
|
14
|
+
}) => void;
|
|
15
|
+
}
|
|
16
|
+
export interface ProductRecommendations {
|
|
17
|
+
products: Product[];
|
|
18
|
+
/** Reading, with nothing to show yet. */
|
|
19
|
+
loading: boolean;
|
|
20
|
+
/** The read failed; whatever was shown stays. */
|
|
21
|
+
error: boolean;
|
|
22
|
+
}
|
|
23
|
+
export declare const DEFAULT_RECOMMENDATION_LIMIT = 10;
|
|
24
|
+
export declare function useProductRecommendations(productId: string | null | undefined, options?: UseProductRecommendationsOptions): ProductRecommendations;
|
|
25
|
+
/** Tests only: forget this session's reads. */
|
|
26
|
+
export declare function clearRecommendationSession(): void;
|
|
27
|
+
//# sourceMappingURL=useProductRecommendations.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useProductRecommendations.d.ts","sourceRoot":"","sources":["../../src/react/useProductRecommendations.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAE,cAAc,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AAGxD,MAAM,WAAW,gCAAgC;IAC/C,oGAAoG;IACpG,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,qCAAqC;IACrC,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,iGAAiG;IACjG,cAAc,CAAC,EAAE,cAAc,CAAC;IAChC,kGAAkG;IAClG,OAAO,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,OAAO,CAAC;IACxC,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,MAAM,CAAA;KAAE,KAAK,IAAI,CAAC;CAChF;AAED,MAAM,WAAW,sBAAsB;IACrC,QAAQ,EAAE,OAAO,EAAE,CAAC;IACpB,yCAAyC;IACzC,OAAO,EAAE,OAAO,CAAC;IACjB,iDAAiD;IACjD,KAAK,EAAE,OAAO,CAAC;CAChB;AAED,eAAO,MAAM,4BAA4B,KAAK,CAAC;AAY/C,wBAAgB,yBAAyB,CACvC,SAAS,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,EACpC,OAAO,GAAE,gCAAqC,GAC7C,sBAAsB,CA4CxB;AAED,+CAA+C;AAC/C,wBAAgB,0BAA0B,IAAI,IAAI,CAEjD"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DEFAULT_RECOMMENDATION_LIMIT = void 0;
|
|
4
|
+
exports.useProductRecommendations = useProductRecommendations;
|
|
5
|
+
exports.clearRecommendationSession = clearRecommendationSession;
|
|
6
|
+
/**
|
|
7
|
+
* "You may also like": Shopify's related products for a product (Storefront `productRecommendations`,
|
|
8
|
+
* the same list the online store's product page shows).
|
|
9
|
+
*
|
|
10
|
+
* Read once per product and kept for the session, so reopening a product shows them at once. Every
|
|
11
|
+
* product read also lands in the product store (base keys, variants included), so a recommendation
|
|
12
|
+
* tapped opens with its picker and Add to Cart in the first frame, like a grid's card.
|
|
13
|
+
*/
|
|
14
|
+
const react_1 = require("react");
|
|
15
|
+
const client_1 = require("../client");
|
|
16
|
+
const shopify_1 = require("../shopify");
|
|
17
|
+
const ShopifyProvider_1 = require("./ShopifyProvider");
|
|
18
|
+
exports.DEFAULT_RECOMMENDATION_LIMIT = 10;
|
|
19
|
+
/** This session's reads, by product and image size: the first render of a reopened product has them. */
|
|
20
|
+
const session = new Map();
|
|
21
|
+
const keyOf = (productId, transform) => `${productId}|${transform ? JSON.stringify(transform) : ''}`;
|
|
22
|
+
function pick(list, productId, limit, exclude) {
|
|
23
|
+
return list.filter((product) => product.id !== productId && !(exclude === null || exclude === void 0 ? void 0 : exclude(product))).slice(0, limit);
|
|
24
|
+
}
|
|
25
|
+
function useProductRecommendations(productId, options = {}) {
|
|
26
|
+
var _a, _b;
|
|
27
|
+
const { ready } = (0, ShopifyProvider_1.useShopify)();
|
|
28
|
+
const configured = ready || (0, client_1.isConfigured)();
|
|
29
|
+
const enabled = (_a = options.enabled) !== null && _a !== void 0 ? _a : true;
|
|
30
|
+
const limit = Math.max(1, Math.round((_b = options.limit) !== null && _b !== void 0 ? _b : exports.DEFAULT_RECOMMENDATION_LIMIT));
|
|
31
|
+
const transformKey = options.imageTransform ? JSON.stringify(options.imageTransform) : '';
|
|
32
|
+
const latest = (0, react_1.useRef)(options);
|
|
33
|
+
latest.current = options;
|
|
34
|
+
const cached = productId ? session.get(keyOf(productId, options.imageTransform)) : undefined;
|
|
35
|
+
const [state, setState] = (0, react_1.useState)(() => ({
|
|
36
|
+
products: cached ? pick(cached, productId, limit, options.exclude) : [],
|
|
37
|
+
loading: false,
|
|
38
|
+
error: false,
|
|
39
|
+
}));
|
|
40
|
+
(0, react_1.useEffect)(() => {
|
|
41
|
+
if (!productId || !enabled || !configured)
|
|
42
|
+
return;
|
|
43
|
+
const { imageTransform, exclude } = latest.current;
|
|
44
|
+
const key = keyOf(productId, imageTransform);
|
|
45
|
+
const known = session.get(key);
|
|
46
|
+
if (known) {
|
|
47
|
+
setState({ products: pick(known, productId, limit, exclude), loading: false, error: false });
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
let live = true;
|
|
51
|
+
setState((current) => ({ ...current, loading: current.products.length === 0, error: false }));
|
|
52
|
+
shopify_1.shopify.products
|
|
53
|
+
.recommended(productId, { imageTransform })
|
|
54
|
+
.then((list) => {
|
|
55
|
+
session.set(key, list);
|
|
56
|
+
if (live)
|
|
57
|
+
setState({ products: pick(list, productId, limit, latest.current.exclude), loading: false, error: false });
|
|
58
|
+
})
|
|
59
|
+
.catch((error) => {
|
|
60
|
+
var _a, _b;
|
|
61
|
+
(_b = (_a = latest.current).onError) === null || _b === void 0 ? void 0 : _b.call(_a, error, { at: 'useProductRecommendations', productId });
|
|
62
|
+
if (live)
|
|
63
|
+
setState((current) => ({ ...current, loading: false, error: true }));
|
|
64
|
+
});
|
|
65
|
+
return () => {
|
|
66
|
+
live = false;
|
|
67
|
+
};
|
|
68
|
+
// The options object is new every render; what it changes is read through `latest`.
|
|
69
|
+
}, [productId, enabled, configured, limit, transformKey]);
|
|
70
|
+
return state;
|
|
71
|
+
}
|
|
72
|
+
/** Tests only: forget this session's reads. */
|
|
73
|
+
function clearRecommendationSession() {
|
|
74
|
+
session.clear();
|
|
75
|
+
}
|
|
76
|
+
//# sourceMappingURL=useProductRecommendations.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useProductRecommendations.js","sourceRoot":"","sources":["../../src/react/useProductRecommendations.ts"],"names":[],"mappings":";;;AA8CA,8DA+CC;AAGD,gEAEC;AAlGD;;;;;;;GAOG;AACH,iCAAoD;AACpD,sCAAyC;AACzC,wCAAqC;AAErC,uDAA+C;AAsBlC,QAAA,4BAA4B,GAAG,EAAE,CAAC;AAE/C,wGAAwG;AACxG,MAAM,OAAO,GAAG,IAAI,GAAG,EAAqB,CAAC;AAE7C,MAAM,KAAK,GAAG,CAAC,SAAiB,EAAE,SAAqC,EAAE,EAAE,CACzE,GAAG,SAAS,IAAI,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC;AAE/D,SAAS,IAAI,CAAC,IAAe,EAAE,SAAiB,EAAE,KAAa,EAAE,OAAuC;IACtG,OAAO,IAAI,CAAC,MAAM,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,EAAE,KAAK,SAAS,IAAI,CAAC,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAG,OAAO,CAAC,CAAA,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC;AACnG,CAAC;AAED,SAAgB,yBAAyB,CACvC,SAAoC,EACpC,UAA4C,EAAE;;IAE9C,MAAM,EAAE,KAAK,EAAE,GAAG,IAAA,4BAAU,GAAE,CAAC;IAC/B,MAAM,UAAU,GAAG,KAAK,IAAI,IAAA,qBAAY,GAAE,CA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AAI,GAAG,KAAK,CAAC;QACf,CAAC,CAAC;QACF,oFAAoF;IACtF,CAAC,EAAE,CAAC,SAAS,EAAE,OAAO,EAAE,UAAU,EAAE,KAAK,EAAE,YAAY,CAAC,CAAC,CAAC;IAE1D,OAAO,KAAK,CAAC;AACf,CAAC;AAED,+CAA+C;AAC/C,SAAgB,0BAA0B;IACxC,OAAO,CAAC,KAAK,EAAE,CAAC;AAClB,CAAC"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { Money, StoreCreditEntry } from '../types';
|
|
2
|
+
export interface StoreCreditState {
|
|
3
|
+
/** The app's choice; null when it shows no store credit. */
|
|
4
|
+
source: 'shopify' | 'tile' | null;
|
|
5
|
+
/**
|
|
6
|
+
* The balance can be read for this shopper: signed in, and for `shopify`, signed in with Shopify.
|
|
7
|
+
* Shopify store credit is only readable through the Customer Account API, so it is unavailable
|
|
8
|
+
* to a password session (App Store review mode).
|
|
9
|
+
*/
|
|
10
|
+
available: boolean;
|
|
11
|
+
balance: Money | null;
|
|
12
|
+
loading: boolean;
|
|
13
|
+
error: Error | null;
|
|
14
|
+
refresh: () => Promise<void>;
|
|
15
|
+
}
|
|
16
|
+
export declare function useStoreCredit(): StoreCreditState;
|
|
17
|
+
export interface UseStoreCreditHistoryOptions {
|
|
18
|
+
/** Lines per page. Default `STORE_CREDIT_HISTORY_PAGE_SIZE` (25). */
|
|
19
|
+
pageSize?: number;
|
|
20
|
+
}
|
|
21
|
+
export interface StoreCreditHistoryState {
|
|
22
|
+
/** The app's choice; null when it shows no store credit. */
|
|
23
|
+
source: 'shopify' | 'tile' | null;
|
|
24
|
+
/** The history can be read for this shopper: the same rule as the balance (`useStoreCredit`). */
|
|
25
|
+
available: boolean;
|
|
26
|
+
/** Newest first. Empty when it can't be read. */
|
|
27
|
+
entries: StoreCreditEntry[];
|
|
28
|
+
/**
|
|
29
|
+
* True only before the first answer: while a stored session is read on app open, then until the
|
|
30
|
+
* first page arrives. A refresh never sets it; the list stays on screen.
|
|
31
|
+
*/
|
|
32
|
+
loading: boolean;
|
|
33
|
+
/** A next page is on its way (`loadMore`). */
|
|
34
|
+
loadingMore: boolean;
|
|
35
|
+
/** More lines than are loaded. */
|
|
36
|
+
hasMore: boolean;
|
|
37
|
+
/** The last read failed. Whatever was shown stays. Cleared by the next read that works. */
|
|
38
|
+
error: Error | null;
|
|
39
|
+
/** Reads the next page. Does nothing while another read is on its way, or at the end. */
|
|
40
|
+
loadMore: () => void;
|
|
41
|
+
/**
|
|
42
|
+
* Reads the newest page again (pull to refresh, a screen coming back into view) and starts the
|
|
43
|
+
* list over from it. Joins a first read already on its way. Never rejects.
|
|
44
|
+
*/
|
|
45
|
+
refresh: () => Promise<void>;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* The history behind the balance, from the same source as `useStoreCredit`, newest first, a page at a
|
|
49
|
+
* time:
|
|
50
|
+
* - `tile`: Tile Credit's ledger (`/public/me/ledger`), with the same service, token and shop as the
|
|
51
|
+
* balance. Either sign-in.
|
|
52
|
+
* - `shopify`: the Customer Account API's store-credit transactions, for a Shopify sign-in only.
|
|
53
|
+
*
|
|
54
|
+
* Signed out, or with no source, it is empty with no error and reads nothing. Loads when it becomes
|
|
55
|
+
* readable, and starts over for another shopper.
|
|
56
|
+
*/
|
|
57
|
+
export declare function useStoreCreditHistory(options?: UseStoreCreditHistoryOptions): StoreCreditHistoryState;
|
|
58
|
+
//# sourceMappingURL=useStoreCredit.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useStoreCredit.d.ts","sourceRoot":"","sources":["../../src/react/useStoreCredit.ts"],"names":[],"mappings":"AAuBA,OAAO,KAAK,EAAE,KAAK,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAExD,MAAM,WAAW,gBAAgB;IAC/B,4DAA4D;IAC5D,MAAM,EAAE,SAAS,GAAG,MAAM,GAAG,IAAI,CAAC;IAClC;;;;OAIG;IACH,SAAS,EAAE,OAAO,CAAC;IACnB,OAAO,EAAE,KAAK,GAAG,IAAI,CAAC;IACtB,OAAO,EAAE,OAAO,CAAC;IACjB,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC;IACpB,OAAO,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CAC9B;AAED,wBAAgB,cAAc,IAAI,gBAAgB,CAqDjD;AAID,MAAM,WAAW,4BAA4B;IAC3C,qEAAqE;IACrE,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,uBAAuB;IACtC,4DAA4D;IAC5D,MAAM,EAAE,SAAS,GAAG,MAAM,GAAG,IAAI,CAAC;IAClC,iGAAiG;IACjG,SAAS,EAAE,OAAO,CAAC;IACnB,iDAAiD;IACjD,OAAO,EAAE,gBAAgB,EAAE,CAAC;IAC5B;;;OAGG;IACH,OAAO,EAAE,OAAO,CAAC;IACjB,8CAA8C;IAC9C,WAAW,EAAE,OAAO,CAAC;IACrB,kCAAkC;IAClC,OAAO,EAAE,OAAO,CAAC;IACjB,2FAA2F;IAC3F,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC;IACpB,yFAAyF;IACzF,QAAQ,EAAE,MAAM,IAAI,CAAC;IACrB;;;OAGG;IACH,OAAO,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CAC9B;AAyBD;;;;;;;;;GASG;AACH,wBAAgB,qBAAqB,CAAC,OAAO,GAAE,4BAAiC,GAAG,uBAAuB,CA8GzG"}
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useStoreCredit = useStoreCredit;
|
|
4
|
+
exports.useStoreCreditHistory = useStoreCreditHistory;
|
|
5
|
+
/**
|
|
6
|
+
* The signed-in shopper's store credit, from the source the app chose on `ShopifyProvider`
|
|
7
|
+
* (`storeCredit.source`): Shopify's own store credit, or the Tile Credit wallet. One hook either
|
|
8
|
+
* way, so an account screen shows a balance without knowing where it comes from.
|
|
9
|
+
*
|
|
10
|
+
* Putting credit on the cart is `useCartStoreCredit`; Shopify store credit is spent at Shopify's
|
|
11
|
+
* checkout. `useStoreCreditHistory`, below, reads the lines behind the balance.
|
|
12
|
+
*/
|
|
13
|
+
const react_1 = require("react");
|
|
14
|
+
const client_1 = require("../client");
|
|
15
|
+
const storeCredit_1 = require("../auth/storeCredit");
|
|
16
|
+
const tileCredit_1 = require("../tileCredit");
|
|
17
|
+
const ShopifyProvider_1 = require("./ShopifyProvider");
|
|
18
|
+
const tileCreditClient_1 = require("./tileCreditClient");
|
|
19
|
+
const useOrders_1 = require("./useOrders");
|
|
20
|
+
function useStoreCredit() {
|
|
21
|
+
var _a, _b, _c;
|
|
22
|
+
const { storeCredit } = (0, ShopifyProvider_1.useShopify)();
|
|
23
|
+
const customer = (0, ShopifyProvider_1.useCustomer)();
|
|
24
|
+
const source = (_a = storeCredit === null || storeCredit === void 0 ? void 0 : storeCredit.source) !== null && _a !== void 0 ? _a : null;
|
|
25
|
+
const available = source !== null && customer.loggedIn && (source === 'tile' || customer.sessionKind === 'shopify');
|
|
26
|
+
const [balance, setBalance] = (0, react_1.useState)(null);
|
|
27
|
+
const [loading, setLoading] = (0, react_1.useState)(false);
|
|
28
|
+
const [error, setError] = (0, react_1.useState)(null);
|
|
29
|
+
// Read through refs so `refresh` keeps one identity across renders.
|
|
30
|
+
const customerRef = (0, react_1.useRef)(customer);
|
|
31
|
+
customerRef.current = customer;
|
|
32
|
+
const baseUrl = (storeCredit === null || storeCredit === void 0 ? void 0 : storeCredit.tileCreditBaseUrl) || tileCredit_1.DEFAULT_TILE_CREDIT_BASE_URL;
|
|
33
|
+
const request = (0, react_1.useRef)(0);
|
|
34
|
+
const refresh = (0, react_1.useCallback)(async () => {
|
|
35
|
+
var _a, _b, _c, _d;
|
|
36
|
+
const id = ++request.current;
|
|
37
|
+
if (!available) {
|
|
38
|
+
setBalance(null);
|
|
39
|
+
setError(null);
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
setLoading(true);
|
|
43
|
+
setError(null);
|
|
44
|
+
try {
|
|
45
|
+
let next;
|
|
46
|
+
if (source === 'shopify') {
|
|
47
|
+
const data = await customerRef.current.request(storeCredit_1.STORE_CREDIT_QUERY);
|
|
48
|
+
next = (0, storeCredit_1.sumStoreCredit)((_b = (_a = data.customer) === null || _a === void 0 ? void 0 : _a.storeCreditAccounts.nodes) !== null && _b !== void 0 ? _b : [], (_c = (0, client_1.getCurrencyCode)()) !== null && _c !== void 0 ? _c : '');
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
const wallet = await (0, tileCreditClient_1.tileCreditClientFor)(customerRef.current, baseUrl).getWallet();
|
|
52
|
+
next = { amount: (0, tileCredit_1.centsToMoney)(wallet.balanceCents), currencyCode: (_d = (0, client_1.getCurrencyCode)()) !== null && _d !== void 0 ? _d : '' };
|
|
53
|
+
}
|
|
54
|
+
if (id === request.current)
|
|
55
|
+
setBalance(next);
|
|
56
|
+
}
|
|
57
|
+
catch (e) {
|
|
58
|
+
if (id === request.current)
|
|
59
|
+
setError(e instanceof Error ? e : new Error(String(e)));
|
|
60
|
+
}
|
|
61
|
+
finally {
|
|
62
|
+
if (id === request.current)
|
|
63
|
+
setLoading(false);
|
|
64
|
+
}
|
|
65
|
+
}, [available, source, baseUrl]);
|
|
66
|
+
// Loads when it becomes readable, and again for a different shopper.
|
|
67
|
+
const customerId = (_c = (_b = customer.customer) === null || _b === void 0 ? void 0 : _b.id) !== null && _c !== void 0 ? _c : null;
|
|
68
|
+
(0, react_1.useEffect)(() => {
|
|
69
|
+
void refresh();
|
|
70
|
+
}, [refresh, customerId]);
|
|
71
|
+
return (0, react_1.useMemo)(() => ({ source, available, balance: available ? balance : null, loading, error, refresh }), [source, available, balance, loading, error, refresh]);
|
|
72
|
+
}
|
|
73
|
+
const emptyHistory = (owner) => ({
|
|
74
|
+
owner, entries: [], cursor: null, answered: false, loadingMore: false, error: null,
|
|
75
|
+
});
|
|
76
|
+
/** The lines shown so far and a next page, in date order, each line once. */
|
|
77
|
+
function withPage(entries, page) {
|
|
78
|
+
const seen = new Set(entries.map((entry) => entry.id));
|
|
79
|
+
const merged = [...entries, ...page.filter((entry) => !seen.has(entry.id))];
|
|
80
|
+
// Already in order from one source; Shopify's accounts in several currencies can interleave.
|
|
81
|
+
return merged.sort((a, b) => (Date.parse(b.createdAt) || 0) - (Date.parse(a.createdAt) || 0));
|
|
82
|
+
}
|
|
83
|
+
const asError = (error) => (error instanceof Error ? error : new Error(String(error)));
|
|
84
|
+
/**
|
|
85
|
+
* The history behind the balance, from the same source as `useStoreCredit`, newest first, a page at a
|
|
86
|
+
* time:
|
|
87
|
+
* - `tile`: Tile Credit's ledger (`/public/me/ledger`), with the same service, token and shop as the
|
|
88
|
+
* balance. Either sign-in.
|
|
89
|
+
* - `shopify`: the Customer Account API's store-credit transactions, for a Shopify sign-in only.
|
|
90
|
+
*
|
|
91
|
+
* Signed out, or with no source, it is empty with no error and reads nothing. Loads when it becomes
|
|
92
|
+
* readable, and starts over for another shopper.
|
|
93
|
+
*/
|
|
94
|
+
function useStoreCreditHistory(options = {}) {
|
|
95
|
+
var _a, _b;
|
|
96
|
+
const { storeCredit } = (0, ShopifyProvider_1.useShopify)();
|
|
97
|
+
const customer = (0, ShopifyProvider_1.useCustomer)();
|
|
98
|
+
const source = (_a = storeCredit === null || storeCredit === void 0 ? void 0 : storeCredit.source) !== null && _a !== void 0 ? _a : null;
|
|
99
|
+
const available = source !== null && customer.loggedIn && (source === 'tile' || customer.sessionKind === 'shopify');
|
|
100
|
+
const baseUrl = (storeCredit === null || storeCredit === void 0 ? void 0 : storeCredit.tileCreditBaseUrl) || tileCredit_1.DEFAULT_TILE_CREDIT_BASE_URL;
|
|
101
|
+
const shopper = (0, useOrders_1.useShopper)(customer);
|
|
102
|
+
// Whose history is held, and from where: anything else starts from nothing.
|
|
103
|
+
const owner = available && shopper ? `${shopper}|${source}|${baseUrl}` : null;
|
|
104
|
+
const customerRef = (0, react_1.useRef)(customer);
|
|
105
|
+
customerRef.current = customer;
|
|
106
|
+
const ownerRef = (0, react_1.useRef)(owner);
|
|
107
|
+
ownerRef.current = owner;
|
|
108
|
+
const pageSizeRef = (0, react_1.useRef)(storeCredit_1.STORE_CREDIT_HISTORY_PAGE_SIZE);
|
|
109
|
+
pageSizeRef.current = Math.max(1, Math.floor((_b = options.pageSize) !== null && _b !== void 0 ? _b : storeCredit_1.STORE_CREDIT_HISTORY_PAGE_SIZE)) || storeCredit_1.STORE_CREDIT_HISTORY_PAGE_SIZE;
|
|
110
|
+
const [stored, setStored] = (0, react_1.useState)(() => emptyHistory(owner));
|
|
111
|
+
// Another shopper's lines never show, not even for the render before the effect below clears them.
|
|
112
|
+
const shown = stored.owner === owner ? stored : emptyHistory(owner);
|
|
113
|
+
const shownRef = (0, react_1.useRef)(shown);
|
|
114
|
+
shownRef.current = shown;
|
|
115
|
+
const firstPage = (0, react_1.useRef)(null);
|
|
116
|
+
const nextPage = (0, react_1.useRef)(null);
|
|
117
|
+
const update = (0, react_1.useCallback)((forOwner, change) => {
|
|
118
|
+
setStored((state) => (state.owner === forOwner ? { ...state, ...change(state) } : state));
|
|
119
|
+
}, []);
|
|
120
|
+
/** The source for this shopper now. Tile Credit's client reads a fresh token for each request. */
|
|
121
|
+
const historySource = (0, react_1.useCallback)(async () => {
|
|
122
|
+
var _a;
|
|
123
|
+
const now = customerRef.current;
|
|
124
|
+
const currency = (_a = (0, client_1.getCurrencyCode)()) !== null && _a !== void 0 ? _a : '';
|
|
125
|
+
if (source === 'shopify')
|
|
126
|
+
return (0, storeCredit_1.shopifyStoreCreditHistory)(now.request, currency);
|
|
127
|
+
return (0, storeCredit_1.tileCreditHistory)((0, tileCreditClient_1.tileCreditClientFor)(now, baseUrl), currency);
|
|
128
|
+
}, [source, baseUrl]);
|
|
129
|
+
const refresh = (0, react_1.useCallback)(() => {
|
|
130
|
+
const forOwner = ownerRef.current;
|
|
131
|
+
if (!forOwner)
|
|
132
|
+
return Promise.resolve();
|
|
133
|
+
const running = firstPage.current;
|
|
134
|
+
if ((running === null || running === void 0 ? void 0 : running.owner) === forOwner)
|
|
135
|
+
return running.done;
|
|
136
|
+
// The list starts over, so a next page still on its way would land on the wrong list.
|
|
137
|
+
nextPage.current = null;
|
|
138
|
+
const flight = { owner: forOwner, done: Promise.resolve() };
|
|
139
|
+
flight.done = (async () => {
|
|
140
|
+
try {
|
|
141
|
+
const page = await (await historySource()).page(pageSizeRef.current, null);
|
|
142
|
+
update(forOwner, () => ({
|
|
143
|
+
entries: withPage([], page.entries), cursor: page.nextCursor, answered: true, loadingMore: false, error: null,
|
|
144
|
+
}));
|
|
145
|
+
}
|
|
146
|
+
catch (error) {
|
|
147
|
+
update(forOwner, () => ({ answered: true, loadingMore: false, error: asError(error) }));
|
|
148
|
+
}
|
|
149
|
+
finally {
|
|
150
|
+
if (firstPage.current === flight)
|
|
151
|
+
firstPage.current = null;
|
|
152
|
+
}
|
|
153
|
+
})();
|
|
154
|
+
firstPage.current = flight;
|
|
155
|
+
return flight.done;
|
|
156
|
+
}, [historySource, update]);
|
|
157
|
+
const loadMore = (0, react_1.useCallback)(() => {
|
|
158
|
+
var _a, _b;
|
|
159
|
+
const forOwner = ownerRef.current;
|
|
160
|
+
const now = shownRef.current;
|
|
161
|
+
if (!forOwner || !now.answered || !now.cursor)
|
|
162
|
+
return;
|
|
163
|
+
if (((_a = firstPage.current) === null || _a === void 0 ? void 0 : _a.owner) === forOwner || ((_b = nextPage.current) === null || _b === void 0 ? void 0 : _b.owner) === forOwner)
|
|
164
|
+
return;
|
|
165
|
+
const flight = { owner: forOwner };
|
|
166
|
+
nextPage.current = flight;
|
|
167
|
+
const cursor = now.cursor;
|
|
168
|
+
update(forOwner, () => ({ loadingMore: true }));
|
|
169
|
+
void (async () => {
|
|
170
|
+
try {
|
|
171
|
+
const page = await (await historySource()).page(pageSizeRef.current, cursor);
|
|
172
|
+
if (nextPage.current !== flight)
|
|
173
|
+
return;
|
|
174
|
+
update(forOwner, (state) => ({
|
|
175
|
+
entries: withPage(state.entries, page.entries), cursor: page.nextCursor, loadingMore: false, error: null,
|
|
176
|
+
}));
|
|
177
|
+
}
|
|
178
|
+
catch (error) {
|
|
179
|
+
if (nextPage.current !== flight)
|
|
180
|
+
return;
|
|
181
|
+
update(forOwner, () => ({ loadingMore: false, error: asError(error) }));
|
|
182
|
+
}
|
|
183
|
+
finally {
|
|
184
|
+
if (nextPage.current === flight)
|
|
185
|
+
nextPage.current = null;
|
|
186
|
+
}
|
|
187
|
+
})();
|
|
188
|
+
}, [historySource, update]);
|
|
189
|
+
// A new shopper (or none) starts from nothing; signed out, nothing is read.
|
|
190
|
+
(0, react_1.useEffect)(() => {
|
|
191
|
+
setStored((state) => (state.owner === owner ? state : emptyHistory(owner)));
|
|
192
|
+
if (owner)
|
|
193
|
+
void refresh();
|
|
194
|
+
}, [owner, refresh]);
|
|
195
|
+
const restoring = customer.restoring && owner === null;
|
|
196
|
+
const loading = restoring || (owner !== null && !shown.answered);
|
|
197
|
+
return (0, react_1.useMemo)(() => ({
|
|
198
|
+
source,
|
|
199
|
+
available,
|
|
200
|
+
entries: shown.entries,
|
|
201
|
+
loading,
|
|
202
|
+
loadingMore: shown.loadingMore,
|
|
203
|
+
hasMore: !!shown.cursor,
|
|
204
|
+
error: shown.error,
|
|
205
|
+
loadMore,
|
|
206
|
+
refresh,
|
|
207
|
+
}), [source, available, shown.entries, loading, shown.loadingMore, shown.cursor, shown.error, loadMore, refresh]);
|
|
208
|
+
}
|
|
209
|
+
//# sourceMappingURL=useStoreCredit.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useStoreCredit.js","sourceRoot":"","sources":["../../src/react/useStoreCredit.ts"],"names":[],"mappings":";;AAwCA,wCAqDC;AAqED,sDA8GC;AAhRD;;;;;;;GAOG;AACH,iCAA0E;AAC1E,sCAA4C;AAC5C,qDAQ6B;AAC7B,8CAA2E;AAC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AAM,SAAS,GAAG,QAAQ,CAAC,SAAS,IAAI,KAAK,KAAK,IAAI,CAAC;IACvD,MAAM,OAAO,GAAG,SAAS,IAAI,CAAC,KAAK,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;IACjE,OAAO,IAAA,eAAO,EACZ,GAAG,EAAE,CAAC,CAAC;QACL,MAAM;QACN,SAAS;QACT,OAAO,EAAE,KAAK,CAAC,OAAO;QACtB,OAAO;QACP,WAAW,EAAE,KAAK,CAAC,WAAW;QAC9B,OAAO,EAAE,CAAC,CAAC,KAAK,CAAC,MAAM;QACvB,KAAK,EAAE,KAAK,CAAC,KAAK;QAClB,QAAQ;QACR,OAAO;KACR,CAAC,EACF,CAAC,MAAM,EAAE,SAAS,EAAE,KAAK,CAAC,OAAO,EAAE,OAAO,EAAE,KAAK,CAAC,WAAW,EAAE,KAAK,CAAC,MAAM,EAAE,KAAK,CAAC,KAAK,EAAE,QAAQ,EAAE,OAAO,CAAC,CAC7G,CAAC;AACJ,CAAC"}
|
|
@@ -17,17 +17,18 @@ export interface UseTileCreditState {
|
|
|
17
17
|
loading: boolean;
|
|
18
18
|
error: Error | null;
|
|
19
19
|
refresh: () => Promise<void>;
|
|
20
|
-
/** Redeem N cents and
|
|
21
|
-
* Throws if no cart is loaded yet. */
|
|
20
|
+
/** Redeem N cents and add the card to the current cart (from `useCart`).
|
|
21
|
+
* Throws if no cart is loaded yet, or `TileCreditError('cart_refused')` when the card didn't go on. */
|
|
22
22
|
redeemAndApply: (amountCents: number, opts?: {
|
|
23
23
|
idempotencyKey?: string;
|
|
24
24
|
reason?: string;
|
|
25
|
+
/** No longer used: the provider sets the cart's country (its `config.country`, else the shop's). */
|
|
25
26
|
countryFallback?: string;
|
|
26
27
|
}) => Promise<{
|
|
27
28
|
redeemed: TileCreditRedeemResult;
|
|
28
29
|
cart: Cart;
|
|
29
30
|
}>;
|
|
30
|
-
/** Remove one or more applied gift cards from the current cart. */
|
|
31
|
+
/** Remove one or more applied gift cards from the current cart, by `AppliedGiftCard.id`. */
|
|
31
32
|
removeAppliedGiftCards: (appliedGiftCardIds: string[]) => Promise<Cart>;
|
|
32
33
|
}
|
|
33
34
|
export declare function useTileCredit(opts: UseTileCreditOptions): UseTileCreditState;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useTileCredit.d.ts","sourceRoot":"","sources":["../../src/react/useTileCredit.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useTileCredit.d.ts","sourceRoot":"","sources":["../../src/react/useTileCredit.ts"],"names":[],"mappings":"AAoBA,OAAO,KAAK,EACV,IAAI,EACJ,sBAAsB,EACtB,sBAAsB,EACtB,gBAAgB,EACjB,MAAM,UAAU,CAAC;AAElB,MAAM,WAAW,oBAAoB;IACnC;6DACyD;IACzD,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,8EAA8E;IAC9E,mBAAmB,EAAE,MAAM,GAAG,IAAI,CAAC;IACnC,8BAA8B;IAC9B,UAAU,EAAE,MAAM,CAAC;IACnB,kFAAkF;IAClF,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB;AAED,MAAM,WAAW,kBAAkB;IACjC,KAAK,EAAE,OAAO,CAAC;IACf,MAAM,EAAE,gBAAgB,GAAG,IAAI,CAAC;IAChC,MAAM,EAAE,sBAAsB,GAAG,IAAI,CAAC;IACtC,OAAO,EAAE,OAAO,CAAC;IACjB,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC;IACpB,OAAO,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;IAC7B;4GACwG;IACxG,cAAc,EAAE,CAAC,WAAW,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE;QAC3C,cAAc,CAAC,EAAE,MAAM,CAAC;QACxB,MAAM,CAAC,EAAE,MAAM,CAAC;QAChB,oGAAoG;QACpG,eAAe,CAAC,EAAE,MAAM,CAAC;KAC1B,KAAK,OAAO,CAAC;QAAE,QAAQ,EAAE,sBAAsB,CAAC;QAAC,IAAI,EAAE,IAAI,CAAA;KAAE,CAAC,CAAC;IAChE,4FAA4F;IAC5F,sBAAsB,EAAE,CAAC,kBAAkB,EAAE,MAAM,EAAE,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;CACzE;AAUD,wBAAgB,aAAa,CAAC,IAAI,EAAE,oBAAoB,GAAG,kBAAkB,CAmF5E"}
|