@tiledev/sdk-shopify 0.9.1 → 0.10.1
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 +653 -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/useProductFeed.d.ts.map +1 -1
- package/dist/react/useProductFeed.js +33 -5
- package/dist/react/useProductFeed.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,292 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useCartStoreCredit = useCartStoreCredit;
|
|
4
|
+
/**
|
|
5
|
+
* Store credit on the cart: the balance, what is on the cart, and Apply / Remove, from the source the
|
|
6
|
+
* app chose on `ShopifyProvider` (`storeCredit.source`). One hook either way, so a cart screen needs no
|
|
7
|
+
* idea where the credit comes from.
|
|
8
|
+
*
|
|
9
|
+
* - **`tile`** (Tile Credit): Apply mints a gift card for the amount (`/public/me/redeem`) and adds it to
|
|
10
|
+
* the cart through the provider's cart queue (`useCart().addGiftCardCodes`). Remove takes the app's
|
|
11
|
+
* own card off and leaves every other gift card. Redeeming only reserves the credit: the wallet is
|
|
12
|
+
* charged for what the order uses, so the balance doesn't move on Apply.
|
|
13
|
+
* - **`shopify`** (Shopify's store credit): only Shopify's checkout can take it, so the hook reads the
|
|
14
|
+
* balance and says `atCheckout`; `apply` and `remove` do nothing.
|
|
15
|
+
*
|
|
16
|
+
* Rules (decided 2026-10-05, Head of Engineering):
|
|
17
|
+
* - **One Apply or Remove at a time.** A second call while one runs gets the running one's promise: two
|
|
18
|
+
* redeems at once with different keys would mint two cards (the service's idempotency isn't atomic).
|
|
19
|
+
* - **Every Apply mints a new card** with a new idempotency key. The key is reused only to retry the
|
|
20
|
+
* same attempt (same shopper, same amount, right after it failed), so a retry after a lost answer
|
|
21
|
+
* gets the card already minted instead of another. A new card disables the previous one, so any
|
|
22
|
+
* earlier card of the app's still on the cart is taken off once the new one is on.
|
|
23
|
+
* - **The app's card on the cart** is found by its last characters (`appliedGiftCards[].lastCharacters`):
|
|
24
|
+
* the last four of the card this hook minted, or after a restart the shopper's active card
|
|
25
|
+
* (`/public/me/gift-cards`). `applied` is what that card takes off this cart (`presentmentAmountUsed`).
|
|
26
|
+
* - **The balance is read** when the hook mounts, for each new shopper (nothing from the one before
|
|
27
|
+
* shows, not even for a render), when the app comes back to the foreground, after every Apply and
|
|
28
|
+
* Remove, and on `refresh()`.
|
|
29
|
+
* - Failures never reject: `apply` and `remove` resolve false and set `error`, a `TileCreditError`
|
|
30
|
+
* whose `code` the app words. A 401 renews the token once and never signs the shopper out.
|
|
31
|
+
*/
|
|
32
|
+
const react_1 = require("react");
|
|
33
|
+
const client_1 = require("../client");
|
|
34
|
+
const cart_1 = require("../cart");
|
|
35
|
+
const storeCredit_1 = require("../auth/storeCredit");
|
|
36
|
+
const tileCredit_1 = require("../tileCredit");
|
|
37
|
+
const types_1 = require("../types");
|
|
38
|
+
const appForeground_1 = require("./appForeground");
|
|
39
|
+
const ShopifyProvider_1 = require("./ShopifyProvider");
|
|
40
|
+
const useOrders_1 = require("./useOrders");
|
|
41
|
+
const tileCreditClient_1 = require("./tileCreditClient");
|
|
42
|
+
const nothingHeld = (owner) => ({
|
|
43
|
+
owner, balance: null, activeCardEnding: null, answered: false, readError: null,
|
|
44
|
+
});
|
|
45
|
+
/** Unique per attempt on this device; not a secret. */
|
|
46
|
+
function newIdempotencyKey() {
|
|
47
|
+
return `app-cart-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 10)}`;
|
|
48
|
+
}
|
|
49
|
+
/** Every failure as a `TileCreditError`, so the app words one kind of error. */
|
|
50
|
+
function asCreditError(error, fallback = 'internal') {
|
|
51
|
+
if (error instanceof types_1.TileCreditError)
|
|
52
|
+
return error;
|
|
53
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
54
|
+
if (error instanceof types_1.ShopifyError && error.errors.length)
|
|
55
|
+
return new types_1.TileCreditError('cart_refused', message);
|
|
56
|
+
if (/network|fetch|request failed|timed? ?out|offline/i.test(message))
|
|
57
|
+
return new types_1.TileCreditError('network', message);
|
|
58
|
+
return new types_1.TileCreditError(fallback, message);
|
|
59
|
+
}
|
|
60
|
+
/** The total the app's cards take off the cart, in its currency. */
|
|
61
|
+
function totalUsed(cards) {
|
|
62
|
+
var _a, _b, _c;
|
|
63
|
+
if (cards.length === 0)
|
|
64
|
+
return null;
|
|
65
|
+
const cents = cards.reduce((sum, card) => { var _a, _b; return sum + Math.round((Number.parseFloat((_b = (_a = card.presentmentAmountUsed) === null || _a === void 0 ? void 0 : _a.amount) !== null && _b !== void 0 ? _b : '0') || 0) * 100); }, 0);
|
|
66
|
+
const currencyCode = (_c = (_b = (_a = cards[0].presentmentAmountUsed) === null || _a === void 0 ? void 0 : _a.currencyCode) !== null && _b !== void 0 ? _b : (0, client_1.getCurrencyCode)()) !== null && _c !== void 0 ? _c : '';
|
|
67
|
+
return { amount: (0, tileCredit_1.centsToMoney)(cents), currencyCode };
|
|
68
|
+
}
|
|
69
|
+
function useCartStoreCredit() {
|
|
70
|
+
var _a, _b;
|
|
71
|
+
const { storeCredit } = (0, ShopifyProvider_1.useShopify)();
|
|
72
|
+
const customer = (0, ShopifyProvider_1.useCustomer)();
|
|
73
|
+
const cartState = (0, ShopifyProvider_1.useCart)();
|
|
74
|
+
const source = (_a = storeCredit === null || storeCredit === void 0 ? void 0 : storeCredit.source) !== null && _a !== void 0 ? _a : null;
|
|
75
|
+
const available = source !== null && customer.loggedIn && (source === 'tile' || customer.sessionKind === 'shopify');
|
|
76
|
+
const baseUrl = (storeCredit === null || storeCredit === void 0 ? void 0 : storeCredit.tileCreditBaseUrl) || tileCredit_1.DEFAULT_TILE_CREDIT_BASE_URL;
|
|
77
|
+
const shopper = (0, useOrders_1.useShopper)(customer);
|
|
78
|
+
// Whose credit is held, and from where: anything else starts from nothing.
|
|
79
|
+
const owner = available && shopper ? `${shopper}|${source}|${baseUrl}` : null;
|
|
80
|
+
const customerRef = (0, react_1.useRef)(customer);
|
|
81
|
+
customerRef.current = customer;
|
|
82
|
+
const cartRef = (0, react_1.useRef)(cartState);
|
|
83
|
+
cartRef.current = cartState;
|
|
84
|
+
const ownerRef = (0, react_1.useRef)(owner);
|
|
85
|
+
ownerRef.current = owner;
|
|
86
|
+
const [stored, setStored] = (0, react_1.useState)(() => nothingHeld(owner));
|
|
87
|
+
// Another shopper's balance never shows, not even for the render before the effect below clears it.
|
|
88
|
+
const held = stored.owner === owner ? stored : nothingHeld(owner);
|
|
89
|
+
const heldRef = (0, react_1.useRef)(held);
|
|
90
|
+
heldRef.current = held;
|
|
91
|
+
// The last characters of the card this hook minted, for this shopper.
|
|
92
|
+
const [minted, setMinted] = (0, react_1.useState)(null);
|
|
93
|
+
const mintedEnding = minted && minted.owner === owner ? minted.ending : null;
|
|
94
|
+
const mintedRef = (0, react_1.useRef)(mintedEnding);
|
|
95
|
+
mintedRef.current = mintedEnding;
|
|
96
|
+
const [running, setRunning] = (0, react_1.useState)(null);
|
|
97
|
+
const runningRef = (0, react_1.useRef)(null);
|
|
98
|
+
const [actionError, setActionError] = (0, react_1.useState)(null);
|
|
99
|
+
const attempt = (0, react_1.useRef)(null);
|
|
100
|
+
const reading = (0, react_1.useRef)(null);
|
|
101
|
+
const update = (0, react_1.useCallback)((forOwner, change) => {
|
|
102
|
+
setStored((state) => (state.owner === forOwner ? { ...state, ...change } : { ...nothingHeld(forOwner), ...change }));
|
|
103
|
+
}, []);
|
|
104
|
+
/** Reads the balance (and, for Tile Credit, the active card) for the shopper now. */
|
|
105
|
+
const read = (0, react_1.useCallback)((forOwner) => {
|
|
106
|
+
const flight = { owner: forOwner, done: Promise.resolve() };
|
|
107
|
+
flight.done = (async () => {
|
|
108
|
+
var _a, _b, _c, _d, _e;
|
|
109
|
+
try {
|
|
110
|
+
const now = customerRef.current;
|
|
111
|
+
const currency = (_a = (0, client_1.getCurrencyCode)()) !== null && _a !== void 0 ? _a : '';
|
|
112
|
+
if (source === 'shopify') {
|
|
113
|
+
const data = await now.request(storeCredit_1.STORE_CREDIT_QUERY);
|
|
114
|
+
if (ownerRef.current !== forOwner)
|
|
115
|
+
return;
|
|
116
|
+
update(forOwner, { balance: (0, storeCredit_1.sumStoreCredit)((_c = (_b = data.customer) === null || _b === void 0 ? void 0 : _b.storeCreditAccounts.nodes) !== null && _c !== void 0 ? _c : [], currency), answered: true, readError: null });
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
const client = (0, tileCreditClient_1.tileCreditClientFor)(now, baseUrl);
|
|
120
|
+
const [wallet, cards] = await Promise.allSettled([client.getWallet(), client.listGiftCards()]);
|
|
121
|
+
if (ownerRef.current !== forOwner)
|
|
122
|
+
return;
|
|
123
|
+
if (wallet.status === 'rejected')
|
|
124
|
+
throw wallet.reason;
|
|
125
|
+
const change = {
|
|
126
|
+
balance: { amount: (0, tileCredit_1.centsToMoney)(wallet.value.balanceCents), currencyCode: currency },
|
|
127
|
+
answered: true,
|
|
128
|
+
readError: null,
|
|
129
|
+
};
|
|
130
|
+
// The cards list is what finds the app's card after a restart; a failure keeps what was known.
|
|
131
|
+
if (cards.status === 'fulfilled') {
|
|
132
|
+
change.activeCardEnding = (_e = (_d = cards.value.giftCards.find((card) => card.status === 'active')) === null || _d === void 0 ? void 0 : _d.last4) !== null && _e !== void 0 ? _e : null;
|
|
133
|
+
}
|
|
134
|
+
update(forOwner, change);
|
|
135
|
+
}
|
|
136
|
+
catch (error) {
|
|
137
|
+
if (ownerRef.current === forOwner)
|
|
138
|
+
update(forOwner, { answered: true, readError: asCreditError(error, 'network') });
|
|
139
|
+
}
|
|
140
|
+
finally {
|
|
141
|
+
if (reading.current === flight)
|
|
142
|
+
reading.current = null;
|
|
143
|
+
}
|
|
144
|
+
})();
|
|
145
|
+
reading.current = flight;
|
|
146
|
+
return flight.done;
|
|
147
|
+
}, [source, baseUrl, update]);
|
|
148
|
+
const refresh = (0, react_1.useCallback)(() => {
|
|
149
|
+
var _a;
|
|
150
|
+
const forOwner = ownerRef.current;
|
|
151
|
+
if (!forOwner)
|
|
152
|
+
return Promise.resolve();
|
|
153
|
+
if (((_a = reading.current) === null || _a === void 0 ? void 0 : _a.owner) === forOwner)
|
|
154
|
+
return reading.current.done;
|
|
155
|
+
return read(forOwner);
|
|
156
|
+
}, [read]);
|
|
157
|
+
/** After a write: a new read, not one that started before the write. */
|
|
158
|
+
const readAfterWrite = (0, react_1.useCallback)((forOwner) => {
|
|
159
|
+
if (ownerRef.current === forOwner)
|
|
160
|
+
void read(forOwner);
|
|
161
|
+
}, [read]);
|
|
162
|
+
// Each shopper (or none) starts from nothing; signed out, nothing is read.
|
|
163
|
+
(0, react_1.useEffect)(() => {
|
|
164
|
+
setStored((state) => (state.owner === owner ? state : nothingHeld(owner)));
|
|
165
|
+
if (owner)
|
|
166
|
+
void refresh();
|
|
167
|
+
}, [owner, refresh]);
|
|
168
|
+
// Back in the foreground: the balance may have moved (an order, the web store's own redeem).
|
|
169
|
+
(0, react_1.useEffect)(() => {
|
|
170
|
+
if (!owner)
|
|
171
|
+
return undefined;
|
|
172
|
+
return (0, appForeground_1.onAppForeground)(() => void refresh());
|
|
173
|
+
}, [owner, refresh]);
|
|
174
|
+
/** Runs `work` as the one Apply or Remove, or hands back the one already running. */
|
|
175
|
+
const runOne = (0, react_1.useCallback)((kind, forOwner, work) => {
|
|
176
|
+
const now = runningRef.current;
|
|
177
|
+
if (now)
|
|
178
|
+
return now.done;
|
|
179
|
+
setActionError(null);
|
|
180
|
+
const entry = { owner: forOwner, kind, done: Promise.resolve(false) };
|
|
181
|
+
entry.done = (async () => {
|
|
182
|
+
try {
|
|
183
|
+
return await work();
|
|
184
|
+
}
|
|
185
|
+
catch (error) {
|
|
186
|
+
if (ownerRef.current === forOwner)
|
|
187
|
+
setActionError({ owner: forOwner, error: asCreditError(error) });
|
|
188
|
+
return false;
|
|
189
|
+
}
|
|
190
|
+
finally {
|
|
191
|
+
runningRef.current = null;
|
|
192
|
+
setRunning(null);
|
|
193
|
+
readAfterWrite(forOwner);
|
|
194
|
+
}
|
|
195
|
+
})();
|
|
196
|
+
runningRef.current = entry;
|
|
197
|
+
setRunning(entry);
|
|
198
|
+
return entry.done;
|
|
199
|
+
}, [readAfterWrite]);
|
|
200
|
+
const apply = (0, react_1.useCallback)((amountCents) => {
|
|
201
|
+
const forOwner = ownerRef.current;
|
|
202
|
+
if (!forOwner || source !== 'tile')
|
|
203
|
+
return Promise.resolve(false);
|
|
204
|
+
return runOne('applying', forOwner, async () => {
|
|
205
|
+
if (!Number.isSafeInteger(amountCents) || amountCents <= 0) {
|
|
206
|
+
throw new types_1.TileCreditError('validation', 'The amount must be more than zero');
|
|
207
|
+
}
|
|
208
|
+
// A retry of the attempt that just failed keeps its key; anything else is a new attempt.
|
|
209
|
+
const last = attempt.current;
|
|
210
|
+
const thisAttempt = last && last.owner === forOwner && last.amountCents === amountCents
|
|
211
|
+
? last
|
|
212
|
+
: { owner: forOwner, amountCents, idempotencyKey: newIdempotencyKey() };
|
|
213
|
+
attempt.current = thisAttempt;
|
|
214
|
+
const earlierEndings = [mintedRef.current, heldRef.current.activeCardEnding].filter((ending) => !!ending);
|
|
215
|
+
const redeemed = await (0, tileCreditClient_1.tileCreditClientFor)(customerRef.current, baseUrl).redeem({
|
|
216
|
+
amountCents,
|
|
217
|
+
idempotencyKey: thisAttempt.idempotencyKey,
|
|
218
|
+
reason: 'Applied to the cart in the app',
|
|
219
|
+
});
|
|
220
|
+
if (ownerRef.current !== forOwner)
|
|
221
|
+
return false;
|
|
222
|
+
setMinted({ owner: forOwner, ending: redeemed.last4 });
|
|
223
|
+
mintedRef.current = redeemed.last4;
|
|
224
|
+
let onCart;
|
|
225
|
+
try {
|
|
226
|
+
onCart = await cartRef.current.addGiftCardCodes([redeemed.code]);
|
|
227
|
+
}
|
|
228
|
+
catch (error) {
|
|
229
|
+
// A card handed back for this key that won't go on (disabled since) is never retried with it.
|
|
230
|
+
if (redeemed.duplicate)
|
|
231
|
+
attempt.current = null;
|
|
232
|
+
const failure = asCreditError(error, 'cart_refused');
|
|
233
|
+
throw failure.code === 'network' ? failure : new types_1.TileCreditError('cart_refused', failure.message);
|
|
234
|
+
}
|
|
235
|
+
if (!onCart)
|
|
236
|
+
throw new types_1.TileCreditError('cart_refused', 'There is no cart to put the credit on');
|
|
237
|
+
attempt.current = null;
|
|
238
|
+
// The new card disabled the app's earlier one; if that is still on the cart, it comes off.
|
|
239
|
+
const newCard = new Set((0, cart_1.giftCardsEndingIn)(onCart, [redeemed.last4]).map((card) => card.id));
|
|
240
|
+
const stale = (0, cart_1.giftCardsEndingIn)(onCart, earlierEndings).filter((card) => !newCard.has(card.id));
|
|
241
|
+
if (stale.length) {
|
|
242
|
+
await cartRef.current.removeGiftCards(stale.map((card) => card.id)).catch(() => undefined);
|
|
243
|
+
}
|
|
244
|
+
return true;
|
|
245
|
+
});
|
|
246
|
+
}, [source, baseUrl, runOne]);
|
|
247
|
+
const remove = (0, react_1.useCallback)(() => {
|
|
248
|
+
const forOwner = ownerRef.current;
|
|
249
|
+
if (!forOwner || source !== 'tile')
|
|
250
|
+
return Promise.resolve(false);
|
|
251
|
+
return runOne('removing', forOwner, async () => {
|
|
252
|
+
const endings = [mintedRef.current, heldRef.current.activeCardEnding].filter((ending) => !!ending);
|
|
253
|
+
const ours = (0, cart_1.giftCardsEndingIn)(cartRef.current.cart, endings);
|
|
254
|
+
if (ours.length === 0)
|
|
255
|
+
return true;
|
|
256
|
+
await cartRef.current.removeGiftCards(ours.map((card) => card.id));
|
|
257
|
+
return true;
|
|
258
|
+
});
|
|
259
|
+
}, [source, runOne]);
|
|
260
|
+
const endings = [mintedEnding, held.activeCardEnding].filter((ending) => !!ending);
|
|
261
|
+
const ourCards = source === 'tile' && owner ? (0, cart_1.giftCardsEndingIn)(cartState.cart, endings) : [];
|
|
262
|
+
const appliedKey = ourCards.map((card) => { var _a; return `${card.id}:${(_a = card.presentmentAmountUsed) === null || _a === void 0 ? void 0 : _a.amount}`; }).join('|');
|
|
263
|
+
const applied = (0, react_1.useMemo)(() => totalUsed(ourCards), [appliedKey]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
264
|
+
const runningNow = running && running.owner === owner ? running.kind : null;
|
|
265
|
+
const error = (_b = (actionError && actionError.owner === owner ? actionError.error : null)) !== null && _b !== void 0 ? _b : held.readError;
|
|
266
|
+
let status;
|
|
267
|
+
if (!owner)
|
|
268
|
+
status = 'hidden';
|
|
269
|
+
else if (source === 'shopify')
|
|
270
|
+
status = held.balance ? 'atCheckout' : held.readError ? 'error' : 'loading';
|
|
271
|
+
else if (runningNow)
|
|
272
|
+
status = runningNow;
|
|
273
|
+
else if (ourCards.length > 0)
|
|
274
|
+
status = 'applied';
|
|
275
|
+
else if (held.balance)
|
|
276
|
+
status = 'ready';
|
|
277
|
+
else if (held.readError)
|
|
278
|
+
status = 'error';
|
|
279
|
+
else
|
|
280
|
+
status = 'loading';
|
|
281
|
+
return (0, react_1.useMemo)(() => ({
|
|
282
|
+
source,
|
|
283
|
+
status,
|
|
284
|
+
balance: owner ? held.balance : null,
|
|
285
|
+
applied: status === 'hidden' ? null : applied,
|
|
286
|
+
error: owner ? error : null,
|
|
287
|
+
apply,
|
|
288
|
+
remove,
|
|
289
|
+
refresh,
|
|
290
|
+
}), [source, status, owner, held.balance, applied, error, apply, remove, refresh]);
|
|
291
|
+
}
|
|
292
|
+
//# sourceMappingURL=useCartStoreCredit.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useCartStoreCredit.js","sourceRoot":"","sources":["../../src/react/useCartStoreCredit.ts"],"names":[],"mappings":";;AAkHA,gDAqNC;AAvUD;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,iCAA0E;AAC1E,sCAA4C;AAC5C,kCAA4C;AAC5C,qDAAsG;AACtG,8CAA2E;AAC3E,oCAAyD;AAEzD,mDAAkD;AAClD,uDAAqE;AACrE,2CAAyC;AACzC,yDAAyD;AAoCzD,MAAM,WAAW,GAAG,CAAC,KAAoB,EAAQ,EAAE,CAAC,CAAC;IACnD,KAAK,EAAE,OAAO,E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,CAAC,OAAO;QAAE,MAAM,GAAG,OAAO,CAAC;SACnC,IAAI,IAAI,CAAC,SAAS;QAAE,MAAM,GAAG,OAAO,CAAC;;QACrC,MAAM,GAAG,SAAS,CAAC;IAExB,OAAO,IAAA,eAAO,EACZ,GAAG,EAAE,CAAC,CAAC;QACL,MAAM;QACN,MAAM;QACN,OAAO,EAAE,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI;QACpC,OAAO,EAAE,MAAM,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,OAAO;QAC7C,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI;QAC3B,KAAK;QACL,MAAM;QACN,OAAO;KACR,CAAC,EACF,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,CAAC,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,CAC9E,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { type CustomerState } from './ShopifyProvider';
|
|
2
|
+
import type { OrderDetails, OrderSummary } from '../types';
|
|
3
|
+
/** Orders per page, unless `useOrders({ pageSize })` says otherwise. */
|
|
4
|
+
export declare const ORDERS_PAGE_SIZE = 25;
|
|
5
|
+
export interface UseOrdersOptions {
|
|
6
|
+
/** Orders per page. Default `ORDERS_PAGE_SIZE` (25). */
|
|
7
|
+
pageSize?: number;
|
|
8
|
+
}
|
|
9
|
+
export interface OrdersState {
|
|
10
|
+
/** Newest first. Empty when signed out. */
|
|
11
|
+
orders: OrderSummary[];
|
|
12
|
+
/**
|
|
13
|
+
* True only before the first answer: while a stored session is read on app open, then until the
|
|
14
|
+
* first page arrives. A refresh never sets it; the list stays on screen.
|
|
15
|
+
*/
|
|
16
|
+
loading: boolean;
|
|
17
|
+
/** A next page is on its way (`loadMore`). */
|
|
18
|
+
loadingMore: boolean;
|
|
19
|
+
/** The last read failed. Whatever was shown stays. Cleared by the next read that works. */
|
|
20
|
+
error: Error | null;
|
|
21
|
+
/** More orders than are loaded. */
|
|
22
|
+
hasMore: boolean;
|
|
23
|
+
/** Reads the next page. Does nothing while another read is on its way, or when there is no more. */
|
|
24
|
+
loadMore: () => void;
|
|
25
|
+
/**
|
|
26
|
+
* Reads the first page again (pull to refresh, a screen coming back into view), keeping the list on
|
|
27
|
+
* screen and the pages loaded after the first. Joins a read already on its way. Never rejects.
|
|
28
|
+
*/
|
|
29
|
+
refresh: () => Promise<void>;
|
|
30
|
+
}
|
|
31
|
+
export interface BuyAgainResult {
|
|
32
|
+
/**
|
|
33
|
+
* Lines that went into the cart, some perhaps fewer than ordered: cut to the stock left, or by the
|
|
34
|
+
* cart's guard (Cart Hold takes only the units still free). A cut isn't reported.
|
|
35
|
+
*/
|
|
36
|
+
added: number;
|
|
37
|
+
/**
|
|
38
|
+
* Lines that didn't: no variant any more, not for sale, `skipVariant` said so, no stock left, or
|
|
39
|
+
* refused by the cart.
|
|
40
|
+
*/
|
|
41
|
+
skipped: number;
|
|
42
|
+
}
|
|
43
|
+
export interface OrderState {
|
|
44
|
+
order: OrderDetails | null;
|
|
45
|
+
/** True only before the first answer (and while a stored session is read on app open). */
|
|
46
|
+
loading: boolean;
|
|
47
|
+
/** The last read failed. An order already shown stays. */
|
|
48
|
+
error: Error | null;
|
|
49
|
+
/** Shopify answered, and this shopper has no order with that id. */
|
|
50
|
+
notFound: boolean;
|
|
51
|
+
/** Reads the order again, keeping it on screen. Joins a read already on its way. Never rejects. */
|
|
52
|
+
refresh: () => Promise<void>;
|
|
53
|
+
/**
|
|
54
|
+
* Puts the order's lines back in the cart through the cart's own add (`addLines`: its guard, its
|
|
55
|
+
* line limit, one `cart:add`). Stock is read fresh first: a line whose variant is gone or not for
|
|
56
|
+
* sale is skipped, and a quantity over what is left (counting what the cart holds) is cut to it.
|
|
57
|
+
* Never throws; anything that fails counts as skipped.
|
|
58
|
+
*
|
|
59
|
+
* `options.skipVariant`: lines whose variant it returns true for are skipped before anything is
|
|
60
|
+
* claimed (the app passes Cart Hold's `isHeldOut`). The add is quiet (`addLines(…, { quiet: true })`):
|
|
61
|
+
* the guard and the line-by-line retry say nothing per line, and the caller shows one summary.
|
|
62
|
+
*/
|
|
63
|
+
buyAgain: (options?: {
|
|
64
|
+
skipVariant?: (variantId: string) => boolean;
|
|
65
|
+
}) => Promise<BuyAgainResult>;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Whose orders these are: a new value for each session, and for a different customer within one.
|
|
69
|
+
* Null when signed out. The profile arriving after the session began (app open, sign-in) is the same
|
|
70
|
+
* shopper, so it doesn't count; keying on the profile would read everything twice. Also keys
|
|
71
|
+
* `useStoreCreditHistory`.
|
|
72
|
+
*/
|
|
73
|
+
export declare function useShopper(customer: CustomerState): string | null;
|
|
74
|
+
/** The signed-in shopper's orders, newest first, a page at a time. */
|
|
75
|
+
export declare function useOrders(options?: UseOrdersOptions): OrdersState;
|
|
76
|
+
/**
|
|
77
|
+
* Shopify can take a few seconds to list a new order: until it does, the newest order is read again
|
|
78
|
+
* after these many seconds.
|
|
79
|
+
*/
|
|
80
|
+
export declare const LATEST_ORDER_READ_AGAIN_SECONDS: readonly number[];
|
|
81
|
+
export interface UseLatestOrderSinceOptions {
|
|
82
|
+
/** Seconds after which the newest order is read again while it isn't the new one yet. Default 3 and 8. */
|
|
83
|
+
readAgainAfterSeconds?: readonly number[];
|
|
84
|
+
/** How far the phone's clock may be ahead of Shopify's, in ms. Default `ORDER_CLOCK_LEEWAY_MS` (2 minutes). */
|
|
85
|
+
clockLeewayMs?: number;
|
|
86
|
+
}
|
|
87
|
+
export interface LatestOrderSince {
|
|
88
|
+
/**
|
|
89
|
+
* The shopper's newest order, once it was placed at or after `since` (less the clock leeway). Null
|
|
90
|
+
* until Shopify lists it, when it never does, and when signed out: no number rather than a wrong one.
|
|
91
|
+
*/
|
|
92
|
+
order: OrderSummary | null;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* The order the shopper just placed, for an Order Confirmed page: the newest order, but only once it
|
|
96
|
+
* was placed at or after `since` (when checkout opened, on the phone's clock). Until then the newest
|
|
97
|
+
* is the order before, which production showed. While it isn't listed yet, the newest is read again
|
|
98
|
+
* after 3 and 8 seconds. Signed out, nothing is read and `order` is null (a guest's order isn't known
|
|
99
|
+
* here). `since` left out takes the newest order, whatever its date.
|
|
100
|
+
*/
|
|
101
|
+
export declare function useLatestOrderSince(since: number | null | undefined, options?: UseLatestOrderSinceOptions): LatestOrderSince;
|
|
102
|
+
/** One of the signed-in shopper's orders, by the `id` `useOrders` gave. Nothing is read without an id. */
|
|
103
|
+
export declare function useOrder(id: string | undefined): OrderState;
|
|
104
|
+
//# sourceMappingURL=useOrders.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useOrders.d.ts","sourceRoot":"","sources":["../../src/react/useOrders.ts"],"names":[],"mappings":"AA4BA,OAAO,EAAwB,KAAK,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAC7E,OAAO,KAAK,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAE3D,wEAAwE;AACxE,eAAO,MAAM,gBAAgB,KAAK,CAAC;AAEnC,MAAM,WAAW,gBAAgB;IAC/B,wDAAwD;IACxD,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,WAAW;IAC1B,2CAA2C;IAC3C,MAAM,EAAE,YAAY,EAAE,CAAC;IACvB;;;OAGG;IACH,OAAO,EAAE,OAAO,CAAC;IACjB,8CAA8C;IAC9C,WAAW,EAAE,OAAO,CAAC;IACrB,2FAA2F;IAC3F,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC;IACpB,mCAAmC;IACnC,OAAO,EAAE,OAAO,CAAC;IACjB,oGAAoG;IACpG,QAAQ,EAAE,MAAM,IAAI,CAAC;IACrB;;;OAGG;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