@konfiqo/storefront-react 0.8.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 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.8.0` and `react` ≥ 18. Install the SDK yourself, so your code and this
14
+ `^0.10.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
  });
@@ -292,6 +293,8 @@ var CartStore = class {
292
293
  setCustomer = (input) => this.write((id) => this.client.carts.setCustomer(id, input));
293
294
  setAddress = (input) => this.write((id) => this.client.carts.setAddress(id, input));
294
295
  setShippingOption = (optionId) => this.write((id) => this.client.carts.setShippingOption(id, optionId));
296
+ applyCode = (code) => this.write((id) => this.client.carts.applyCode(id, code));
297
+ removeCode = () => this.write((id) => this.client.carts.removeCode(id));
295
298
  /** The shipping options of the cart (empty when there is no cart yet). */
296
299
  shippingOptions = async (options) => {
297
300
  const id = this.currentId();
@@ -656,6 +659,8 @@ function useCart() {
656
659
  setCustomer: store.setCustomer,
657
660
  setAddress: store.setAddress,
658
661
  setShippingOption: store.setShippingOption,
662
+ applyCode: store.applyCode,
663
+ removeCode: store.removeCode,
659
664
  shippingOptions: store.shippingOptions,
660
665
  refresh: store.refresh,
661
666
  reset: store.reset
@@ -845,9 +850,68 @@ function useOrder(orderId, token, options = {}) {
845
850
  };
846
851
  }
847
852
 
848
- // src/payment-form.tsx
853
+ // src/use-return-actions.ts
849
854
  var import_react7 = require("react");
850
855
  var import_storefront_sdk5 = require("@konfiqo/storefront-sdk");
856
+ var asStorefrontError = (e) => (0, import_storefront_sdk5.isStorefrontError)(e) ? e : new import_storefront_sdk5.StorefrontError(
857
+ "network_error",
858
+ 0,
859
+ e instanceof Error ? e.message : "Return request failed",
860
+ void 0,
861
+ { cause: e }
862
+ );
863
+ function useReturnActions(orderId, token, opts = {}) {
864
+ const { client } = useStorefrontContext();
865
+ const [pending, setPending] = (0, import_react7.useState)(false);
866
+ const [error, setError] = (0, import_react7.useState)(null);
867
+ const inFlight = (0, import_react7.useRef)(null);
868
+ const alive = (0, import_react7.useRef)(true);
869
+ const onDone = (0, import_react7.useRef)(opts.onDone);
870
+ onDone.current = opts.onDone;
871
+ (0, import_react7.useEffect)(() => {
872
+ alive.current = true;
873
+ return () => {
874
+ alive.current = false;
875
+ };
876
+ }, []);
877
+ const run = (0, import_react7.useCallback)((action) => {
878
+ if (inFlight.current) return inFlight.current;
879
+ setPending(true);
880
+ setError(null);
881
+ const p = action().then(
882
+ (result) => {
883
+ inFlight.current = null;
884
+ if (alive.current) setPending(false);
885
+ onDone.current?.();
886
+ return result;
887
+ },
888
+ (e) => {
889
+ inFlight.current = null;
890
+ const err = asStorefrontError(e);
891
+ if (alive.current) {
892
+ setPending(false);
893
+ setError(err);
894
+ }
895
+ throw err;
896
+ }
897
+ );
898
+ inFlight.current = p;
899
+ return p;
900
+ }, []);
901
+ const create = (0, import_react7.useCallback)(
902
+ (input) => run(() => client.orders.createReturn(orderId, token, input)),
903
+ [client, orderId, token, run]
904
+ );
905
+ const cancel = (0, import_react7.useCallback)(
906
+ (returnId) => run(() => client.orders.cancelReturn(orderId, returnId, token)),
907
+ [client, orderId, token, run]
908
+ );
909
+ return { create, cancel, pending, error };
910
+ }
911
+
912
+ // src/payment-form.tsx
913
+ var import_react8 = require("react");
914
+ var import_storefront_sdk6 = require("@konfiqo/storefront-sdk");
851
915
  var import_jsx_runtime2 = require("react/jsx-runtime");
852
916
  var embeddedContent = (p) => p.presentation === "embedded" && typeof p.embed.content === "string" ? p.embed.content : null;
853
917
  function PaymentForm({
@@ -857,14 +921,14 @@ function PaymentForm({
857
921
  onError,
858
922
  ...divProps
859
923
  }) {
860
- const ref = (0, import_react7.useRef)(null);
861
- const callbacks = (0, import_react7.useRef)({ onClosed, onUnsupported, onError });
862
- (0, import_react7.useEffect)(() => {
924
+ const ref = (0, import_react8.useRef)(null);
925
+ const callbacks = (0, import_react8.useRef)({ onClosed, onUnsupported, onError });
926
+ (0, import_react8.useEffect)(() => {
863
927
  callbacks.current = { onClosed, onUnsupported, onError };
864
928
  });
865
- const pending = (0, import_react7.useRef)(null);
866
- const navigated = (0, import_react7.useRef)(null);
867
- (0, import_react7.useEffect)(() => {
929
+ const pending = (0, import_react8.useRef)(null);
930
+ const navigated = (0, import_react8.useRef)(null);
931
+ (0, import_react8.useEffect)(() => {
868
932
  const element = ref.current;
869
933
  if (!element) return;
870
934
  const prev = pending.current;
@@ -880,7 +944,7 @@ function PaymentForm({
880
944
  }
881
945
  let unmount = null;
882
946
  try {
883
- const mount = (0, import_storefront_sdk5.mountPayment)(element, presentation, {
947
+ const mount = (0, import_storefront_sdk6.mountPayment)(element, presentation, {
884
948
  onClosed: (page) => callbacks.current.onClosed?.(page)
885
949
  });
886
950
  if (mount.kind === "embedded") unmount = mount.unmount;
@@ -909,22 +973,22 @@ function PaymentForm({
909
973
  }
910
974
 
911
975
  // src/checkout-challenge.tsx
912
- var import_react8 = require("react");
913
- var import_storefront_sdk6 = require("@konfiqo/storefront-sdk");
976
+ var import_react9 = require("react");
977
+ var import_storefront_sdk7 = require("@konfiqo/storefront-sdk");
914
978
  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)(() => {
979
+ var CheckoutChallenge = (0, import_react9.forwardRef)(function CheckoutChallenge2({ siteKey, onToken, onExpired, onError, ...divProps }, handle) {
980
+ const ref = (0, import_react9.useRef)(null);
981
+ const widget = (0, import_react9.useRef)(null);
982
+ const callbacks = (0, import_react9.useRef)({ onToken, onExpired, onError });
983
+ (0, import_react9.useEffect)(() => {
920
984
  callbacks.current = { onToken, onExpired, onError };
921
985
  });
922
- (0, import_react8.useEffect)(() => {
986
+ (0, import_react9.useEffect)(() => {
923
987
  const element = ref.current;
924
988
  if (!element || !siteKey) return;
925
989
  let active = true;
926
990
  let rendered = null;
927
- (0, import_storefront_sdk6.renderTurnstile)(element, siteKey, {
991
+ (0, import_storefront_sdk7.renderTurnstile)(element, siteKey, {
928
992
  onToken: (t) => callbacks.current.onToken(t),
929
993
  onExpired: () => callbacks.current.onExpired?.(),
930
994
  onError: (code) => callbacks.current.onError?.(code)
@@ -957,10 +1021,10 @@ var CheckoutChallenge = (0, import_react8.forwardRef)(function CheckoutChallenge
957
1021
  callbacks.current.onExpired?.();
958
1022
  widget.current.reset();
959
1023
  };
960
- (0, import_react8.useImperativeHandle)(handle, () => ({ reset }), []);
1024
+ (0, import_react9.useImperativeHandle)(handle, () => ({ reset }), []);
961
1025
  const attempts = useCheckoutAttempts();
962
- const seen = (0, import_react8.useRef)(attempts);
963
- (0, import_react8.useEffect)(() => {
1026
+ const seen = (0, import_react9.useRef)(attempts);
1027
+ (0, import_react9.useEffect)(() => {
964
1028
  if (attempts === seen.current) return;
965
1029
  seen.current = attempts;
966
1030
  reset();
@@ -971,8 +1035,8 @@ var CheckoutChallenge = (0, import_react8.forwardRef)(function CheckoutChallenge
971
1035
  // src/content.tsx
972
1036
  var import_markdoc = __toESM(require("@markdoc/markdoc"), 1);
973
1037
  var React = __toESM(require("react"), 1);
974
- var import_react9 = require("react");
975
- var import_storefront_sdk7 = require("@konfiqo/storefront-sdk");
1038
+ var import_react10 = require("react");
1039
+ var import_storefront_sdk8 = require("@konfiqo/storefront-sdk");
976
1040
  var import_jsx_runtime4 = require("react/jsx-runtime");
977
1041
  var HTML_TAGS = new Set(
978
1042
  "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 +1116,7 @@ function ProductCard({
1052
1116
  const variant = product.variants[0];
1053
1117
  let price = null;
1054
1118
  try {
1055
- price = variant ? (0, import_storefront_sdk7.formatMoney)(variant.priceMinor, variant.currency) : null;
1119
+ price = variant ? (0, import_storefront_sdk8.formatMoney)(variant.priceMinor, variant.currency) : null;
1056
1120
  } catch {
1057
1121
  price = null;
1058
1122
  }
@@ -1134,7 +1198,7 @@ function Content({ contentKey, components }) {
1134
1198
  client,
1135
1199
  (signal) => client.getContent(contentKey, { signal })
1136
1200
  );
1137
- (0, import_react9.useEffect)(() => {
1201
+ (0, import_react10.useEffect)(() => {
1138
1202
  if (error && typeof process !== "undefined" && process.env?.NODE_ENV !== "production") {
1139
1203
  console.warn(
1140
1204
  `<Content contentKey="${contentKey}">: ${error.message || "could not be read"}`
@@ -1146,7 +1210,7 @@ function Content({ contentKey, components }) {
1146
1210
  }
1147
1211
 
1148
1212
  // src/consent-provider.tsx
1149
- var import_react10 = require("react");
1213
+ var import_react11 = require("react");
1150
1214
 
1151
1215
  // src/consent.ts
1152
1216
  var COOKIE = "_consent";
@@ -1217,7 +1281,7 @@ function analyticsConsent() {
1217
1281
  // src/consent-provider.tsx
1218
1282
  var import_jsx_runtime5 = require("react/jsx-runtime");
1219
1283
  var noop = () => void 0;
1220
- var ConsentContext = (0, import_react10.createContext)({
1284
+ var ConsentContext = (0, import_react11.createContext)({
1221
1285
  needed: false,
1222
1286
  open: noop,
1223
1287
  cookies: [],
@@ -1237,31 +1301,31 @@ function ConsentProvider({
1237
1301
  children
1238
1302
  }) {
1239
1303
  configureConsent(version);
1240
- const raw = (0, import_react10.useSyncExternalStore)(
1304
+ const raw = (0, import_react11.useSyncExternalStore)(
1241
1305
  subscribeConsent,
1242
1306
  rawConsent,
1243
1307
  serverSnapshot
1244
1308
  );
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)(() => {
1309
+ const [panelOpen, setPanelOpen] = (0, import_react11.useState)(false);
1310
+ const [decidedVersion, setDecidedVersion] = (0, import_react11.useState)(null);
1311
+ const opener = (0, import_react11.useRef)(null);
1312
+ const onDecisionRef = (0, import_react11.useRef)(onDecision);
1313
+ (0, import_react11.useInsertionEffect)(() => {
1250
1314
  onDecisionRef.current = onDecision;
1251
1315
  });
1252
- const categories = (0, import_react10.useMemo)(
1316
+ const categories = (0, import_react11.useMemo)(
1253
1317
  () => OPTIONAL_CATEGORIES.filter((c) => cookies.some((k) => k.category === c)),
1254
1318
  [cookies]
1255
1319
  );
1256
1320
  const needed = categories.length > 0;
1257
- const stored = (0, import_react10.useMemo)(
1321
+ const stored = (0, import_react11.useMemo)(
1258
1322
  () => raw === void 0 ? null : consentFor(raw, version),
1259
1323
  [raw, version]
1260
1324
  );
1261
1325
  const known = raw !== void 0;
1262
1326
  const showBar = needed && known && !stored && decidedVersion !== version;
1263
- const restorePending = (0, import_react10.useRef)(false);
1264
- (0, import_react10.useEffect)(() => {
1327
+ const restorePending = (0, import_react11.useRef)(false);
1328
+ (0, import_react11.useEffect)(() => {
1265
1329
  if (panelOpen || !restorePending.current) return;
1266
1330
  restorePending.current = false;
1267
1331
  const target = opener.current;
@@ -1269,16 +1333,16 @@ function ConsentProvider({
1269
1333
  if (target?.isConnected) target.focus();
1270
1334
  else document.getElementById("main")?.focus({ preventScroll: true });
1271
1335
  }, [panelOpen, showBar]);
1272
- const open = (0, import_react10.useCallback)((from) => {
1336
+ const open = (0, import_react11.useCallback)((from) => {
1273
1337
  const active = from ?? document.activeElement;
1274
1338
  opener.current = active instanceof HTMLElement && active !== document.body ? active : null;
1275
1339
  setPanelOpen(true);
1276
1340
  }, []);
1277
- const close = (0, import_react10.useCallback)(() => {
1341
+ const close = (0, import_react11.useCallback)(() => {
1278
1342
  if (panelOpen) restorePending.current = true;
1279
1343
  setPanelOpen(false);
1280
1344
  }, [panelOpen]);
1281
- const decide = (0, import_react10.useCallback)(
1345
+ const decide = (0, import_react11.useCallback)(
1282
1346
  (granted2) => {
1283
1347
  const allowed = categories.filter((c) => granted2.includes(c));
1284
1348
  const consentId = writeConsent(version, allowed);
@@ -1293,7 +1357,7 @@ function ConsentProvider({
1293
1357
  },
1294
1358
  [categories, version, panelOpen]
1295
1359
  );
1296
- const value = (0, import_react10.useMemo)(
1360
+ const value = (0, import_react11.useMemo)(
1297
1361
  () => ({
1298
1362
  needed,
1299
1363
  open,
@@ -1322,7 +1386,7 @@ function ConsentProvider({
1322
1386
  return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(ConsentContext.Provider, { value, children });
1323
1387
  }
1324
1388
  function useConsent() {
1325
- return (0, import_react10.useContext)(ConsentContext);
1389
+ return (0, import_react11.useContext)(ConsentContext);
1326
1390
  }
1327
1391
 
1328
1392
  // src/analytics.ts
@@ -1456,14 +1520,14 @@ function recordConsent(record) {
1456
1520
  }
1457
1521
 
1458
1522
  // src/analytics-tracker.tsx
1459
- var import_react11 = require("react");
1523
+ var import_react12 = require("react");
1460
1524
  function AnalyticsTracker({
1461
1525
  baseUrl,
1462
1526
  publishableKey,
1463
1527
  pathname
1464
1528
  }) {
1465
1529
  configureAnalytics({ baseUrl, publishableKey });
1466
- (0, import_react11.useEffect)(() => {
1530
+ (0, import_react12.useEffect)(() => {
1467
1531
  track("page_view");
1468
1532
  }, [pathname]);
1469
1533
  return null;
@@ -1940,7 +2004,7 @@ function google(rawGtag, g, e) {
1940
2004
  }
1941
2005
 
1942
2006
  // src/ad-pixels-loader.tsx
1943
- var import_react12 = require("react");
2007
+ var import_react13 = require("react");
1944
2008
  function AdPixelsLoader({
1945
2009
  pixels: pixels2,
1946
2010
  hostOnly = false,
@@ -1949,11 +2013,11 @@ function AdPixelsLoader({
1949
2013
  }) {
1950
2014
  const { stored, known } = useConsent();
1951
2015
  const granted2 = stored?.granted.includes("marketing") ?? false;
1952
- (0, import_react12.useEffect)(() => {
2016
+ (0, import_react13.useEffect)(() => {
1953
2017
  if (!known) return;
1954
2018
  configurePixels(pixels2, granted2, { hostOnly, currency: currency2 });
1955
2019
  }, [pixels2, granted2, known, hostOnly, currency2]);
1956
- (0, import_react12.useEffect)(() => {
2020
+ (0, import_react13.useEffect)(() => {
1957
2021
  if (granted2) firePixel({ name: "page_view" });
1958
2022
  }, [granted2, pathname]);
1959
2023
  return null;
@@ -1993,6 +2057,7 @@ function AdPixelsLoader({
1993
2057
  useOrder,
1994
2058
  useProduct,
1995
2059
  useProducts,
2060
+ useReturnActions,
1996
2061
  useStore,
1997
2062
  useStorefrontClient
1998
2063
  });