@konfiqo/storefront-react 0.7.0 → 0.9.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 CHANGED
@@ -11,7 +11,7 @@ the payment form and the checkout challenge.
11
11
  "build your own storefront" guide.
12
12
  - No styling: components render a plain `<div>` you can give a `className`.
13
13
  - No runtime dependencies. Peer dependencies: `@konfiqo/storefront-sdk`
14
- `^0.7.0` and `react` ≥ 18. Install the SDK yourself, so your code and this
14
+ `^0.9.0` and `react` ≥ 18. Install the SDK yourself, so your code and this
15
15
  package share one copy (`isStorefrontError` also recognises errors from a
16
16
  second copy, but one copy keeps the bundle small).
17
17
  - Server-rendering safe: nothing touches `window`, `document` or storage
@@ -329,6 +329,27 @@ const { order, error, isLoading, isPolling, refetch } = useOrder(id, token, {
329
329
  Also exported: `isOrderSettled(order)`, `orderPollDelay(attempt)`,
330
330
  `ORDER_POLL_MIN_MS`, `ORDER_POLL_MAX_MS`, `isDeadCartError(e)`.
331
331
 
332
+ ## `useReturnActions(orderId, token, { onDone })`
333
+
334
+ ```tsx
335
+ const { create, cancel, pending, error } = useReturnActions(id, token, {
336
+ onDone: refetch, // the `refetch` of useOrder
337
+ });
338
+ await create({
339
+ lines: [{ lineId, quantity: 1, reason: 'changedMind' }],
340
+ note: 'Optional note',
341
+ });
342
+ await cancel(returnId);
343
+ ```
344
+
345
+ - Both actions resolve with the `OrderReturn` and reject with the error,
346
+ which `error` also holds (a `StorefrontError`). `error` is cleared when the
347
+ next action starts. For `return_not_allowed`, `error.details.reason` says why.
348
+ - A call while another is in flight returns that call's promise, so a double
349
+ click sends one request. `pending` is true meanwhile.
350
+ - `onDone` runs after a success. Show the form from `order.returnable` and
351
+ `order.returns` (see the SDK README).
352
+
332
353
  ## Consent, analytics and ad pixels
333
354
 
334
355
  Logic only: you build the banner, the preferences panel and the footer
package/dist/index.cjs CHANGED
@@ -64,6 +64,7 @@ __export(index_exports, {
64
64
  useOrder: () => useOrder,
65
65
  useProduct: () => useProduct,
66
66
  useProducts: () => useProducts,
67
+ useReturnActions: () => useReturnActions,
67
68
  useStore: () => useStore,
68
69
  useStorefrontClient: () => useStorefrontClient
69
70
  });
@@ -845,9 +846,68 @@ function useOrder(orderId, token, options = {}) {
845
846
  };
846
847
  }
847
848
 
848
- // src/payment-form.tsx
849
+ // src/use-return-actions.ts
849
850
  var import_react7 = require("react");
850
851
  var import_storefront_sdk5 = require("@konfiqo/storefront-sdk");
852
+ var asStorefrontError = (e) => (0, import_storefront_sdk5.isStorefrontError)(e) ? e : new import_storefront_sdk5.StorefrontError(
853
+ "network_error",
854
+ 0,
855
+ e instanceof Error ? e.message : "Return request failed",
856
+ void 0,
857
+ { cause: e }
858
+ );
859
+ function useReturnActions(orderId, token, opts = {}) {
860
+ const { client } = useStorefrontContext();
861
+ const [pending, setPending] = (0, import_react7.useState)(false);
862
+ const [error, setError] = (0, import_react7.useState)(null);
863
+ const inFlight = (0, import_react7.useRef)(null);
864
+ const alive = (0, import_react7.useRef)(true);
865
+ const onDone = (0, import_react7.useRef)(opts.onDone);
866
+ onDone.current = opts.onDone;
867
+ (0, import_react7.useEffect)(() => {
868
+ alive.current = true;
869
+ return () => {
870
+ alive.current = false;
871
+ };
872
+ }, []);
873
+ const run = (0, import_react7.useCallback)((action) => {
874
+ if (inFlight.current) return inFlight.current;
875
+ setPending(true);
876
+ setError(null);
877
+ const p = action().then(
878
+ (result) => {
879
+ inFlight.current = null;
880
+ if (alive.current) setPending(false);
881
+ onDone.current?.();
882
+ return result;
883
+ },
884
+ (e) => {
885
+ inFlight.current = null;
886
+ const err = asStorefrontError(e);
887
+ if (alive.current) {
888
+ setPending(false);
889
+ setError(err);
890
+ }
891
+ throw err;
892
+ }
893
+ );
894
+ inFlight.current = p;
895
+ return p;
896
+ }, []);
897
+ const create = (0, import_react7.useCallback)(
898
+ (input) => run(() => client.orders.createReturn(orderId, token, input)),
899
+ [client, orderId, token, run]
900
+ );
901
+ const cancel = (0, import_react7.useCallback)(
902
+ (returnId) => run(() => client.orders.cancelReturn(orderId, returnId, token)),
903
+ [client, orderId, token, run]
904
+ );
905
+ return { create, cancel, pending, error };
906
+ }
907
+
908
+ // src/payment-form.tsx
909
+ var import_react8 = require("react");
910
+ var import_storefront_sdk6 = require("@konfiqo/storefront-sdk");
851
911
  var import_jsx_runtime2 = require("react/jsx-runtime");
852
912
  var embeddedContent = (p) => p.presentation === "embedded" && typeof p.embed.content === "string" ? p.embed.content : null;
853
913
  function PaymentForm({
@@ -857,14 +917,14 @@ function PaymentForm({
857
917
  onError,
858
918
  ...divProps
859
919
  }) {
860
- const ref = (0, import_react7.useRef)(null);
861
- const callbacks = (0, import_react7.useRef)({ onClosed, onUnsupported, onError });
862
- (0, import_react7.useEffect)(() => {
920
+ const ref = (0, import_react8.useRef)(null);
921
+ const callbacks = (0, import_react8.useRef)({ onClosed, onUnsupported, onError });
922
+ (0, import_react8.useEffect)(() => {
863
923
  callbacks.current = { onClosed, onUnsupported, onError };
864
924
  });
865
- const pending = (0, import_react7.useRef)(null);
866
- const navigated = (0, import_react7.useRef)(null);
867
- (0, import_react7.useEffect)(() => {
925
+ const pending = (0, import_react8.useRef)(null);
926
+ const navigated = (0, import_react8.useRef)(null);
927
+ (0, import_react8.useEffect)(() => {
868
928
  const element = ref.current;
869
929
  if (!element) return;
870
930
  const prev = pending.current;
@@ -880,7 +940,7 @@ function PaymentForm({
880
940
  }
881
941
  let unmount = null;
882
942
  try {
883
- const mount = (0, import_storefront_sdk5.mountPayment)(element, presentation, {
943
+ const mount = (0, import_storefront_sdk6.mountPayment)(element, presentation, {
884
944
  onClosed: (page) => callbacks.current.onClosed?.(page)
885
945
  });
886
946
  if (mount.kind === "embedded") unmount = mount.unmount;
@@ -909,22 +969,22 @@ function PaymentForm({
909
969
  }
910
970
 
911
971
  // src/checkout-challenge.tsx
912
- var import_react8 = require("react");
913
- var import_storefront_sdk6 = require("@konfiqo/storefront-sdk");
972
+ var import_react9 = require("react");
973
+ var import_storefront_sdk7 = require("@konfiqo/storefront-sdk");
914
974
  var import_jsx_runtime3 = require("react/jsx-runtime");
915
- var CheckoutChallenge = (0, import_react8.forwardRef)(function CheckoutChallenge2({ siteKey, onToken, onExpired, onError, ...divProps }, handle) {
916
- const ref = (0, import_react8.useRef)(null);
917
- const widget = (0, import_react8.useRef)(null);
918
- const callbacks = (0, import_react8.useRef)({ onToken, onExpired, onError });
919
- (0, import_react8.useEffect)(() => {
975
+ var CheckoutChallenge = (0, import_react9.forwardRef)(function CheckoutChallenge2({ siteKey, onToken, onExpired, onError, ...divProps }, handle) {
976
+ const ref = (0, import_react9.useRef)(null);
977
+ const widget = (0, import_react9.useRef)(null);
978
+ const callbacks = (0, import_react9.useRef)({ onToken, onExpired, onError });
979
+ (0, import_react9.useEffect)(() => {
920
980
  callbacks.current = { onToken, onExpired, onError };
921
981
  });
922
- (0, import_react8.useEffect)(() => {
982
+ (0, import_react9.useEffect)(() => {
923
983
  const element = ref.current;
924
984
  if (!element || !siteKey) return;
925
985
  let active = true;
926
986
  let rendered = null;
927
- (0, import_storefront_sdk6.renderTurnstile)(element, siteKey, {
987
+ (0, import_storefront_sdk7.renderTurnstile)(element, siteKey, {
928
988
  onToken: (t) => callbacks.current.onToken(t),
929
989
  onExpired: () => callbacks.current.onExpired?.(),
930
990
  onError: (code) => callbacks.current.onError?.(code)
@@ -957,10 +1017,10 @@ var CheckoutChallenge = (0, import_react8.forwardRef)(function CheckoutChallenge
957
1017
  callbacks.current.onExpired?.();
958
1018
  widget.current.reset();
959
1019
  };
960
- (0, import_react8.useImperativeHandle)(handle, () => ({ reset }), []);
1020
+ (0, import_react9.useImperativeHandle)(handle, () => ({ reset }), []);
961
1021
  const attempts = useCheckoutAttempts();
962
- const seen = (0, import_react8.useRef)(attempts);
963
- (0, import_react8.useEffect)(() => {
1022
+ const seen = (0, import_react9.useRef)(attempts);
1023
+ (0, import_react9.useEffect)(() => {
964
1024
  if (attempts === seen.current) return;
965
1025
  seen.current = attempts;
966
1026
  reset();
@@ -971,8 +1031,8 @@ var CheckoutChallenge = (0, import_react8.forwardRef)(function CheckoutChallenge
971
1031
  // src/content.tsx
972
1032
  var import_markdoc = __toESM(require("@markdoc/markdoc"), 1);
973
1033
  var React = __toESM(require("react"), 1);
974
- var import_react9 = require("react");
975
- var import_storefront_sdk7 = require("@konfiqo/storefront-sdk");
1034
+ var import_react10 = require("react");
1035
+ var import_storefront_sdk8 = require("@konfiqo/storefront-sdk");
976
1036
  var import_jsx_runtime4 = require("react/jsx-runtime");
977
1037
  var HTML_TAGS = new Set(
978
1038
  "article section div span p h1 h2 h3 h4 h5 h6 ul ol li strong em s del code pre blockquote hr br a table thead tbody tr th td figure figcaption small sub sup mark".split(" ")
@@ -1052,7 +1112,7 @@ function ProductCard({
1052
1112
  const variant = product.variants[0];
1053
1113
  let price = null;
1054
1114
  try {
1055
- price = variant ? (0, import_storefront_sdk7.formatMoney)(variant.priceMinor, variant.currency) : null;
1115
+ price = variant ? (0, import_storefront_sdk8.formatMoney)(variant.priceMinor, variant.currency) : null;
1056
1116
  } catch {
1057
1117
  price = null;
1058
1118
  }
@@ -1134,7 +1194,7 @@ function Content({ contentKey, components }) {
1134
1194
  client,
1135
1195
  (signal) => client.getContent(contentKey, { signal })
1136
1196
  );
1137
- (0, import_react9.useEffect)(() => {
1197
+ (0, import_react10.useEffect)(() => {
1138
1198
  if (error && typeof process !== "undefined" && process.env?.NODE_ENV !== "production") {
1139
1199
  console.warn(
1140
1200
  `<Content contentKey="${contentKey}">: ${error.message || "could not be read"}`
@@ -1146,7 +1206,7 @@ function Content({ contentKey, components }) {
1146
1206
  }
1147
1207
 
1148
1208
  // src/consent-provider.tsx
1149
- var import_react10 = require("react");
1209
+ var import_react11 = require("react");
1150
1210
 
1151
1211
  // src/consent.ts
1152
1212
  var COOKIE = "_consent";
@@ -1217,7 +1277,7 @@ function analyticsConsent() {
1217
1277
  // src/consent-provider.tsx
1218
1278
  var import_jsx_runtime5 = require("react/jsx-runtime");
1219
1279
  var noop = () => void 0;
1220
- var ConsentContext = (0, import_react10.createContext)({
1280
+ var ConsentContext = (0, import_react11.createContext)({
1221
1281
  needed: false,
1222
1282
  open: noop,
1223
1283
  cookies: [],
@@ -1237,31 +1297,31 @@ function ConsentProvider({
1237
1297
  children
1238
1298
  }) {
1239
1299
  configureConsent(version);
1240
- const raw = (0, import_react10.useSyncExternalStore)(
1300
+ const raw = (0, import_react11.useSyncExternalStore)(
1241
1301
  subscribeConsent,
1242
1302
  rawConsent,
1243
1303
  serverSnapshot
1244
1304
  );
1245
- const [panelOpen, setPanelOpen] = (0, import_react10.useState)(false);
1246
- const [decidedVersion, setDecidedVersion] = (0, import_react10.useState)(null);
1247
- const opener = (0, import_react10.useRef)(null);
1248
- const onDecisionRef = (0, import_react10.useRef)(onDecision);
1249
- (0, import_react10.useInsertionEffect)(() => {
1305
+ const [panelOpen, setPanelOpen] = (0, import_react11.useState)(false);
1306
+ const [decidedVersion, setDecidedVersion] = (0, import_react11.useState)(null);
1307
+ const opener = (0, import_react11.useRef)(null);
1308
+ const onDecisionRef = (0, import_react11.useRef)(onDecision);
1309
+ (0, import_react11.useInsertionEffect)(() => {
1250
1310
  onDecisionRef.current = onDecision;
1251
1311
  });
1252
- const categories = (0, import_react10.useMemo)(
1312
+ const categories = (0, import_react11.useMemo)(
1253
1313
  () => OPTIONAL_CATEGORIES.filter((c) => cookies.some((k) => k.category === c)),
1254
1314
  [cookies]
1255
1315
  );
1256
1316
  const needed = categories.length > 0;
1257
- const stored = (0, import_react10.useMemo)(
1317
+ const stored = (0, import_react11.useMemo)(
1258
1318
  () => raw === void 0 ? null : consentFor(raw, version),
1259
1319
  [raw, version]
1260
1320
  );
1261
1321
  const known = raw !== void 0;
1262
1322
  const showBar = needed && known && !stored && decidedVersion !== version;
1263
- const restorePending = (0, import_react10.useRef)(false);
1264
- (0, import_react10.useEffect)(() => {
1323
+ const restorePending = (0, import_react11.useRef)(false);
1324
+ (0, import_react11.useEffect)(() => {
1265
1325
  if (panelOpen || !restorePending.current) return;
1266
1326
  restorePending.current = false;
1267
1327
  const target = opener.current;
@@ -1269,16 +1329,16 @@ function ConsentProvider({
1269
1329
  if (target?.isConnected) target.focus();
1270
1330
  else document.getElementById("main")?.focus({ preventScroll: true });
1271
1331
  }, [panelOpen, showBar]);
1272
- const open = (0, import_react10.useCallback)((from) => {
1332
+ const open = (0, import_react11.useCallback)((from) => {
1273
1333
  const active = from ?? document.activeElement;
1274
1334
  opener.current = active instanceof HTMLElement && active !== document.body ? active : null;
1275
1335
  setPanelOpen(true);
1276
1336
  }, []);
1277
- const close = (0, import_react10.useCallback)(() => {
1337
+ const close = (0, import_react11.useCallback)(() => {
1278
1338
  if (panelOpen) restorePending.current = true;
1279
1339
  setPanelOpen(false);
1280
1340
  }, [panelOpen]);
1281
- const decide = (0, import_react10.useCallback)(
1341
+ const decide = (0, import_react11.useCallback)(
1282
1342
  (granted2) => {
1283
1343
  const allowed = categories.filter((c) => granted2.includes(c));
1284
1344
  const consentId = writeConsent(version, allowed);
@@ -1293,7 +1353,7 @@ function ConsentProvider({
1293
1353
  },
1294
1354
  [categories, version, panelOpen]
1295
1355
  );
1296
- const value = (0, import_react10.useMemo)(
1356
+ const value = (0, import_react11.useMemo)(
1297
1357
  () => ({
1298
1358
  needed,
1299
1359
  open,
@@ -1322,7 +1382,7 @@ function ConsentProvider({
1322
1382
  return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(ConsentContext.Provider, { value, children });
1323
1383
  }
1324
1384
  function useConsent() {
1325
- return (0, import_react10.useContext)(ConsentContext);
1385
+ return (0, import_react11.useContext)(ConsentContext);
1326
1386
  }
1327
1387
 
1328
1388
  // src/analytics.ts
@@ -1456,14 +1516,14 @@ function recordConsent(record) {
1456
1516
  }
1457
1517
 
1458
1518
  // src/analytics-tracker.tsx
1459
- var import_react11 = require("react");
1519
+ var import_react12 = require("react");
1460
1520
  function AnalyticsTracker({
1461
1521
  baseUrl,
1462
1522
  publishableKey,
1463
1523
  pathname
1464
1524
  }) {
1465
1525
  configureAnalytics({ baseUrl, publishableKey });
1466
- (0, import_react11.useEffect)(() => {
1526
+ (0, import_react12.useEffect)(() => {
1467
1527
  track("page_view");
1468
1528
  }, [pathname]);
1469
1529
  return null;
@@ -1940,7 +2000,7 @@ function google(rawGtag, g, e) {
1940
2000
  }
1941
2001
 
1942
2002
  // src/ad-pixels-loader.tsx
1943
- var import_react12 = require("react");
2003
+ var import_react13 = require("react");
1944
2004
  function AdPixelsLoader({
1945
2005
  pixels: pixels2,
1946
2006
  hostOnly = false,
@@ -1949,11 +2009,11 @@ function AdPixelsLoader({
1949
2009
  }) {
1950
2010
  const { stored, known } = useConsent();
1951
2011
  const granted2 = stored?.granted.includes("marketing") ?? false;
1952
- (0, import_react12.useEffect)(() => {
2012
+ (0, import_react13.useEffect)(() => {
1953
2013
  if (!known) return;
1954
2014
  configurePixels(pixels2, granted2, { hostOnly, currency: currency2 });
1955
2015
  }, [pixels2, granted2, known, hostOnly, currency2]);
1956
- (0, import_react12.useEffect)(() => {
2016
+ (0, import_react13.useEffect)(() => {
1957
2017
  if (granted2) firePixel({ name: "page_view" });
1958
2018
  }, [granted2, pathname]);
1959
2019
  return null;
@@ -1993,6 +2053,7 @@ function AdPixelsLoader({
1993
2053
  useOrder,
1994
2054
  useProduct,
1995
2055
  useProducts,
2056
+ useReturnActions,
1996
2057
  useStore,
1997
2058
  useStorefrontClient
1998
2059
  });