@metron-labs/quote-react 0.1.19 → 0.1.21

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.
@@ -1,6 +1,6 @@
1
1
  "use client";
2
2
  import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
3
- import { useState } from "react";
3
+ import { useEffect, useRef, useState } from "react";
4
4
  import { assetUrl } from "../lib/assets";
5
5
  import { getQuoteConfig } from "../lib/config";
6
6
  import { ESTIMATOR_TOOLS } from "../components/estimators/registry";
@@ -21,6 +21,15 @@ function EstimatingToolsInner({ fullPage = false, hideHeader = false, internal =
21
21
  const activeEstimator = ESTIMATOR_TOOLS.find((tool) => tool.id === activeEstimatorId);
22
22
  const { cart, updateCartQty, updateCartSellUnits, updateCartConversions, removeFromCart, clearCart } = useCart();
23
23
  const { adding, basketError, addToBasket } = useFixedBasket(activeEstimator?.title ?? "calculator");
24
+ // Every screen change, forward or back, lands at the top.
25
+ const view = showBasket ? "basket" : adding ? "adding" : review ? "review" : activeEstimatorId ?? "list";
26
+ const scrolledView = useRef(view);
27
+ useEffect(() => {
28
+ if (scrolledView.current === view)
29
+ return;
30
+ scrolledView.current = view;
31
+ window.scrollTo(0, 0);
32
+ }, [view]);
24
33
  const reviewOrBasket = (requirements) => {
25
34
  if (!activeEstimator)
26
35
  return;
@@ -99,7 +99,6 @@ function QuickQuoteInner({ fullPage = false, hideHeader = false, internal = fals
99
99
  const goToBasket = useCallback(() => {
100
100
  writeQuoteHistory("basket");
101
101
  setStage("basket");
102
- window.scrollTo(0, 0);
103
102
  }, [writeQuoteHistory]);
104
103
  const backToResults = useCallback(() => {
105
104
  const quoteStage = window.history.state?.boqQuoteStage;
@@ -108,6 +107,15 @@ function QuickQuoteInner({ fullPage = false, hideHeader = false, internal = fals
108
107
  else
109
108
  setStage(scheduleResult ? "results" : "entry");
110
109
  }, [scheduleResult]);
110
+ // Every screen change, forward or back, lands at the top. Runs after the new screen has
111
+ // rendered, so it also wins over the browser restoring the old scroll on history.back().
112
+ const scrolledStage = useRef(stage);
113
+ useEffect(() => {
114
+ if (scrolledStage.current === stage)
115
+ return;
116
+ scrolledStage.current = stage;
117
+ window.scrollTo(0, 0);
118
+ }, [stage]);
111
119
  // Reset back to the method-selection screen. The uploaded catalogue stays in
112
120
  // place — it's a session-level prerequisite, not part of an individual quote.
113
121
  const resetToEntry = () => {
@@ -11,6 +11,7 @@ import { clientHeaders, getBoqClientId, readJson } from "../../lib/api/boqClient
11
11
  // ponytail: flat UK VAT rate, matching fmtCurrency's GBP-only assumption elsewhere in this app.
12
12
  const VAT_RATE = 0.2;
13
13
  const PAGE_SIZE = 10;
14
+ const INVALID_EMAIL_MESSAGE = "Enter a valid email address so we can send your quote.";
14
15
  const orderSummary = (item, pricing) => {
15
16
  const requested = `${pricing.requestedAmount} ${unitDisplayText(pricing.requestedUnit, pricing.requestedAmount)}`;
16
17
  const order = `${pricing.requiredSellUnits} ${unitDisplayText(pricing.sellUnit, pricing.requiredSellUnits ?? 0)}`;
@@ -145,7 +146,7 @@ export default function BasketStage({ cart, onUpdateQty, onUpdateSellUnits, onRe
145
146
  async function submitQuoteRequest() {
146
147
  const email = reference.trim();
147
148
  if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
148
- setQuoteContactError("Enter a valid email address so we can send your better price.");
149
+ setQuoteContactError(INVALID_EMAIL_MESSAGE);
149
150
  return;
150
151
  }
151
152
  setQuoteContactError("");
@@ -175,7 +176,10 @@ export default function BasketStage({ cart, onUpdateQty, onUpdateSellUnits, onRe
175
176
  });
176
177
  if (!res.ok) {
177
178
  const payload = await readJson(res);
178
- setQuoteContactError(res.status === 429 ? "You've sent a few requests already. Please try again in a few minutes." : (payload?.message ?? "Your request couldn't be sent. Please try again."));
179
+ // The API's own message is written for developers (e.g. "items.0: Unrecognized key(s)"),
180
+ // so the customer gets a plain one and the console keeps the detail.
181
+ console.error("Quote request failed", res.status, payload?.message);
182
+ setQuoteContactError(res.status === 429 ? "You've sent a few requests already. Please try again in a few minutes." : "Your request couldn't be sent. Please try again, or contact us if it keeps happening.");
179
183
  return;
180
184
  }
181
185
  setQuoteRequestSent(true);
@@ -232,7 +236,7 @@ export default function BasketStage({ cart, onUpdateQty, onUpdateSellUnits, onRe
232
236
  setReference(e.target.value);
233
237
  if (qzaBasketLayout)
234
238
  setQuoteContactError("");
235
- }, placeholder: qzaBasketLayout ? "you@example.com" : "PO number or job reference" }), qzaBasketLayout && quoteContactError && (_jsx("p", { className: "basket-quote-request-error", role: "alert", children: quoteContactError })), qzaBasketLayout ? (_jsx("button", { type: "button", className: "cta-btn", onClick: () => void submitQuoteRequest(), disabled: quoteRequestSending, "aria-busy": quoteRequestSending, children: _jsx("span", { children: quoteRequestSending ? "Sending…" : "Request our best quote" }) })) : (_jsx("button", { className: "cta-btn", onClick: () => void exportBasketExcel(items), "aria-label": internal ? "Download selection" : "Download basket", children: _jsx("span", { children: internal ? "Download Quote" : "Buy Now" }) })), _jsxs("p", { className: "basket-sidebar-disclaimer", children: [_jsx("span", { "aria-hidden": "true", children: "\u26A0\uFE0F" }), qzaBasketLayout
239
+ }, placeholder: qzaBasketLayout ? "you@example.com" : "PO number or job reference", "aria-invalid": qzaBasketLayout && quoteContactError === INVALID_EMAIL_MESSAGE, "aria-describedby": qzaBasketLayout && quoteContactError ? `${uid}-basket-reference-error` : undefined }), qzaBasketLayout && quoteContactError && (_jsx("p", { id: `${uid}-basket-reference-error`, className: "basket-quote-request-error", role: "alert", children: quoteContactError })), qzaBasketLayout ? (_jsx("button", { type: "button", className: "cta-btn", onClick: () => void submitQuoteRequest(), disabled: quoteRequestSending, "aria-busy": quoteRequestSending, children: _jsx("span", { children: quoteRequestSending ? "Sending…" : "Request our best quote" }) })) : (_jsx("button", { className: "cta-btn", onClick: () => void exportBasketExcel(items), "aria-label": internal ? "Download selection" : "Download basket", children: _jsx("span", { children: internal ? "Download Quote" : "Buy Now" }) })), _jsxs("p", { className: "basket-sidebar-disclaimer", children: [_jsx("span", { "aria-hidden": "true", children: "\u26A0\uFE0F" }), qzaBasketLayout
236
240
  ? "This is a quote request, not an order. We'll confirm our best price, availability and delivery before you approve."
237
241
  : "Prices and availability are estimates and should be confirmed before procurement."] })] })] }), _jsx("div", { className: "btn-row results-actions", children: _jsxs("button", { className: "cta-btn cta-btn-secondary", style: { flex: "0 1 30%", gap: 10, justifyContent: "center" }, onClick: onBack, "aria-label": "Back to results", children: [_jsx(BackArrow, {}), _jsx("span", { children: "Back to results" })] }) })] })) })] }) }));
238
242
  }