@quantaroute/checkout 1.8.2 → 1.8.4
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/CHANGELOG.md +16 -0
- package/dist/embed/quantaroute-checkout.css +1 -1
- package/dist/embed/quantaroute-checkout.js +1 -1
- package/dist/lib/index.d.ts +11 -1
- package/dist/lib/quantaroute-checkout.es.js +88 -80
- package/dist/lib/quantaroute-checkout.umd.js +59 -51
- package/dist/style.css +1 -1
- package/package.json +1 -1
- package/src/components/SavedAddressStep.tsx +106 -18
- package/src/core/types.ts +11 -1
- package/src/styles/checkout.css +54 -0
|
@@ -50,9 +50,32 @@ interface AddressCardProps {
|
|
|
50
50
|
selecting: boolean;
|
|
51
51
|
/** Another card is being selected: block every action. */
|
|
52
52
|
locked: boolean;
|
|
53
|
+
/** Set when the merchant doesn't deliver to this address (the card shows one note, no button). */
|
|
54
|
+
unavailableReason?: string;
|
|
55
|
+
/** A retryable error from the last "Deliver here" on this card. */
|
|
56
|
+
error?: string;
|
|
53
57
|
}
|
|
54
58
|
|
|
55
|
-
|
|
59
|
+
const AlertIcon = () => (
|
|
60
|
+
<svg aria-hidden="true" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
|
|
61
|
+
<circle cx="12" cy="12" r="9" />
|
|
62
|
+
<path d="M12 7.5v5.5M12 16.5h.01" />
|
|
63
|
+
</svg>
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
function AddressCard({
|
|
67
|
+
address,
|
|
68
|
+
onUse,
|
|
69
|
+
onDelete,
|
|
70
|
+
onSetDefault,
|
|
71
|
+
deleting,
|
|
72
|
+
selecting,
|
|
73
|
+
locked,
|
|
74
|
+
unavailableReason,
|
|
75
|
+
error,
|
|
76
|
+
}: AddressCardProps) {
|
|
77
|
+
const errorId = `qr-addr-error-${address.id}`;
|
|
78
|
+
const unavailable = Boolean(unavailableReason);
|
|
56
79
|
const parts = [
|
|
57
80
|
address.flat_number,
|
|
58
81
|
address.building_name,
|
|
@@ -68,7 +91,9 @@ function AddressCard({ address, onUse, onDelete, onSetDefault, deleting, selecti
|
|
|
68
91
|
const summary = parts.length > 0 ? undefined : address.locality || address.district || address.state;
|
|
69
92
|
|
|
70
93
|
return (
|
|
71
|
-
<div
|
|
94
|
+
<div
|
|
95
|
+
className={`qr-addr-card${address.is_default && !unavailable ? ' qr-addr-card--default' : ''}${unavailable ? ' qr-addr-card--unavailable' : ''}`}
|
|
96
|
+
>
|
|
72
97
|
<div className="qr-addr-card__header">
|
|
73
98
|
<div className="qr-addr-card__label-icon">
|
|
74
99
|
<LabelIcon label={address.label} />
|
|
@@ -146,20 +171,36 @@ function AddressCard({ address, onUse, onDelete, onSetDefault, deleting, selecti
|
|
|
146
171
|
</div>
|
|
147
172
|
</div>
|
|
148
173
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
className="qr-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
174
|
+
{unavailable ? (
|
|
175
|
+
// One message, no button: the card can't be used, and the note says why.
|
|
176
|
+
<p className="qr-addr-card__unavailable" role="note">
|
|
177
|
+
<AlertIcon />
|
|
178
|
+
Not deliverable{address.pincode ? ` to ${address.pincode}` : ''}
|
|
179
|
+
</p>
|
|
180
|
+
) : (
|
|
181
|
+
<button
|
|
182
|
+
type="button"
|
|
183
|
+
className="qr-btn qr-btn--primary qr-btn--full qr-btn--sm"
|
|
184
|
+
onClick={onUse}
|
|
185
|
+
disabled={locked}
|
|
186
|
+
aria-busy={selecting}
|
|
187
|
+
aria-describedby={error ? errorId : undefined}
|
|
188
|
+
>
|
|
189
|
+
{selecting ? (
|
|
190
|
+
<>
|
|
191
|
+
<span className="qr-spinner qr-spinner--sm" aria-hidden="true" />
|
|
192
|
+
Confirming…
|
|
193
|
+
</>
|
|
194
|
+
) : 'Deliver here'}
|
|
195
|
+
</button>
|
|
196
|
+
)}
|
|
197
|
+
|
|
198
|
+
{error && !unavailable && (
|
|
199
|
+
<p className="qr-addr-card__error" id={errorId} role="alert">
|
|
200
|
+
<AlertIcon />
|
|
201
|
+
{error}
|
|
202
|
+
</p>
|
|
203
|
+
)}
|
|
163
204
|
</div>
|
|
164
205
|
);
|
|
165
206
|
}
|
|
@@ -172,12 +213,17 @@ const SavedAddressStep: React.FC<SavedAddressStepProps> = ({
|
|
|
172
213
|
merchantId,
|
|
173
214
|
onSelectAddress,
|
|
174
215
|
onAddNew,
|
|
216
|
+
checkDeliverable,
|
|
175
217
|
}) => {
|
|
176
218
|
const [addresses, setAddresses] = useState<SavedAddress[]>([]);
|
|
177
219
|
const [loadingList, setLoadingList] = useState(true);
|
|
178
220
|
const [listError, setListError] = useState<string | null>(null);
|
|
179
221
|
const [deletingId, setDeletingId] = useState<string | null>(null);
|
|
180
222
|
const [selectingId, setSelectingId] = useState<string | null>(null);
|
|
223
|
+
/** Address id → why the merchant doesn't deliver there. */
|
|
224
|
+
const [unavailable, setUnavailable] = useState<Record<string, string>>({});
|
|
225
|
+
/** Address id → retryable error from the last "Deliver here". */
|
|
226
|
+
const [cardErrors, setCardErrors] = useState<Record<string, string>>({});
|
|
181
227
|
// A ref, not state, so a second click in the same frame is still ignored.
|
|
182
228
|
const selectingRef = useRef(false);
|
|
183
229
|
|
|
@@ -204,6 +250,31 @@ const SavedAddressStep: React.FC<SavedAddressStepProps> = ({
|
|
|
204
250
|
|
|
205
251
|
useEffect(() => { load(); }, [load]);
|
|
206
252
|
|
|
253
|
+
// Check every saved address once the list is in, so blocked ones are marked
|
|
254
|
+
// before the shopper picks. One request per distinct pincode, in parallel.
|
|
255
|
+
useEffect(() => {
|
|
256
|
+
if (!checkDeliverable || addresses.length === 0) return;
|
|
257
|
+
let cancelled = false;
|
|
258
|
+
const byPincode = new Map<string, SavedAddress[]>();
|
|
259
|
+
for (const addr of addresses) {
|
|
260
|
+
const key = addr.pincode || addr.id;
|
|
261
|
+
byPincode.set(key, [...(byPincode.get(key) ?? []), addr]);
|
|
262
|
+
}
|
|
263
|
+
byPincode.forEach((group) => {
|
|
264
|
+
checkDeliverable(group[0])
|
|
265
|
+
.then((reason) => {
|
|
266
|
+
if (cancelled || !reason) return;
|
|
267
|
+
setUnavailable((prev) => {
|
|
268
|
+
const next = { ...prev };
|
|
269
|
+
group.forEach((a) => { next[a.id] = reason; });
|
|
270
|
+
return next;
|
|
271
|
+
});
|
|
272
|
+
})
|
|
273
|
+
.catch(() => { /* a failed check never blocks; it runs again on click */ });
|
|
274
|
+
});
|
|
275
|
+
return () => { cancelled = true; };
|
|
276
|
+
}, [addresses, checkDeliverable]);
|
|
277
|
+
|
|
207
278
|
const handleDelete = useCallback(async (id: string) => {
|
|
208
279
|
setDeletingId(id);
|
|
209
280
|
try {
|
|
@@ -235,13 +306,22 @@ const SavedAddressStep: React.FC<SavedAddressStepProps> = ({
|
|
|
235
306
|
if (selectingRef.current) return;
|
|
236
307
|
selectingRef.current = true;
|
|
237
308
|
setSelectingId(address.id);
|
|
238
|
-
|
|
309
|
+
setCardErrors((prev) => {
|
|
310
|
+
const { [address.id]: _cleared, ...rest } = prev;
|
|
311
|
+
return rest;
|
|
312
|
+
});
|
|
239
313
|
try {
|
|
240
314
|
await onSelectAddress(address);
|
|
241
315
|
// On success the widget moves to the next step and this one unmounts;
|
|
242
316
|
// keep the spinner until then.
|
|
243
317
|
} catch (err: unknown) {
|
|
244
|
-
|
|
318
|
+
const message = err instanceof Error ? err.message : 'Could not use this address. Please try again.';
|
|
319
|
+
// Show the problem on the card it belongs to, not above the list.
|
|
320
|
+
if ((err as { code?: string } | null)?.code === 'ADDRESS_UNDELIVERABLE') {
|
|
321
|
+
setUnavailable((prev) => ({ ...prev, [address.id]: message }));
|
|
322
|
+
} else {
|
|
323
|
+
setCardErrors((prev) => ({ ...prev, [address.id]: message }));
|
|
324
|
+
}
|
|
245
325
|
selectingRef.current = false;
|
|
246
326
|
setSelectingId(null);
|
|
247
327
|
}
|
|
@@ -303,10 +383,18 @@ const SavedAddressStep: React.FC<SavedAddressStepProps> = ({
|
|
|
303
383
|
deleting={deletingId === addr.id}
|
|
304
384
|
selecting={selectingId === addr.id}
|
|
305
385
|
locked={selectingId !== null}
|
|
386
|
+
unavailableReason={unavailable[addr.id]}
|
|
387
|
+
error={cardErrors[addr.id]}
|
|
306
388
|
/>
|
|
307
389
|
))}
|
|
308
390
|
</div>
|
|
309
391
|
|
|
392
|
+
{addresses.length > 0 && addresses.every((a) => unavailable[a.id]) && (
|
|
393
|
+
<p className="qr-saved-step__hint" role="status">
|
|
394
|
+
None of your saved addresses can be delivered to right now. Add a new address to continue.
|
|
395
|
+
</p>
|
|
396
|
+
)}
|
|
397
|
+
|
|
310
398
|
<button
|
|
311
399
|
type="button"
|
|
312
400
|
className="qr-btn qr-btn--ghost qr-btn--full"
|
package/src/core/types.ts
CHANGED
|
@@ -519,8 +519,18 @@ export interface SavedAddressStepProps {
|
|
|
519
519
|
sessionToken: string;
|
|
520
520
|
functionBaseUrl: string;
|
|
521
521
|
merchantId: string;
|
|
522
|
-
/**
|
|
522
|
+
/**
|
|
523
|
+
* May return a promise; the step shows a spinner until it settles. If it
|
|
524
|
+
* rejects, the message is shown on that address's card. Reject with an error
|
|
525
|
+
* whose `code` is `'ADDRESS_UNDELIVERABLE'` to mark the card as undeliverable.
|
|
526
|
+
*/
|
|
523
527
|
onSelectAddress: (address: import('./checkout-api').SavedAddress) => void | Promise<void>;
|
|
528
|
+
/**
|
|
529
|
+
* Optional delivery check, run for each saved address when the list loads.
|
|
530
|
+
* Resolve with a message when the merchant doesn't deliver there, or null.
|
|
531
|
+
* Undeliverable cards are marked before the shopper picks one.
|
|
532
|
+
*/
|
|
533
|
+
checkDeliverable?: (address: import('./checkout-api').SavedAddress) => Promise<string | null>;
|
|
524
534
|
onAddNew: () => void;
|
|
525
535
|
}
|
|
526
536
|
|
package/src/styles/checkout.css
CHANGED
|
@@ -1835,6 +1835,60 @@
|
|
|
1835
1835
|
background: var(--qr-primary-light);
|
|
1836
1836
|
}
|
|
1837
1837
|
|
|
1838
|
+
/* Saved address the merchant doesn't deliver to: say so on the card itself,
|
|
1839
|
+
with an icon and text, not colour alone. */
|
|
1840
|
+
.qr-addr-card--unavailable {
|
|
1841
|
+
--qr-unavailable-text: #b91c1c; /* about 6.8:1 on the tinted note */
|
|
1842
|
+
--qr-unavailable-bg: #fef2f2;
|
|
1843
|
+
--qr-unavailable-border: #fecaca;
|
|
1844
|
+
border-color: var(--qr-unavailable-border);
|
|
1845
|
+
background: var(--qr-bg);
|
|
1846
|
+
}
|
|
1847
|
+
.qr-checkout--dark .qr-addr-card--unavailable {
|
|
1848
|
+
--qr-unavailable-text: #fca5a5;
|
|
1849
|
+
--qr-unavailable-bg: rgba(239, 68, 68, 0.12);
|
|
1850
|
+
--qr-unavailable-border: rgba(239, 68, 68, 0.45);
|
|
1851
|
+
}
|
|
1852
|
+
.qr-addr-card__unavailable {
|
|
1853
|
+
display: flex;
|
|
1854
|
+
gap: 8px;
|
|
1855
|
+
align-items: center;
|
|
1856
|
+
margin: 0;
|
|
1857
|
+
padding: 10px 12px;
|
|
1858
|
+
border-radius: calc(var(--qr-radius) - 4px);
|
|
1859
|
+
background: var(--qr-unavailable-bg);
|
|
1860
|
+
color: var(--qr-unavailable-text);
|
|
1861
|
+
font-size: 14px;
|
|
1862
|
+
font-weight: 600;
|
|
1863
|
+
line-height: 1.35;
|
|
1864
|
+
}
|
|
1865
|
+
.qr-addr-card__unavailable svg,
|
|
1866
|
+
.qr-addr-card__error svg {
|
|
1867
|
+
width: 18px;
|
|
1868
|
+
height: 18px;
|
|
1869
|
+
flex-shrink: 0;
|
|
1870
|
+
margin-top: 1px;
|
|
1871
|
+
}
|
|
1872
|
+
.qr-addr-card__error {
|
|
1873
|
+
display: flex;
|
|
1874
|
+
gap: 6px;
|
|
1875
|
+
align-items: flex-start;
|
|
1876
|
+
margin: 0;
|
|
1877
|
+
font-size: 13px;
|
|
1878
|
+
line-height: 1.45;
|
|
1879
|
+
color: #b91c1c;
|
|
1880
|
+
}
|
|
1881
|
+
.qr-checkout--dark .qr-addr-card__error {
|
|
1882
|
+
color: #fca5a5;
|
|
1883
|
+
}
|
|
1884
|
+
.qr-saved-step__hint {
|
|
1885
|
+
margin: 0;
|
|
1886
|
+
font-size: 13px;
|
|
1887
|
+
line-height: 1.5;
|
|
1888
|
+
color: var(--qr-text-muted);
|
|
1889
|
+
text-align: center;
|
|
1890
|
+
}
|
|
1891
|
+
|
|
1838
1892
|
.qr-addr-card__header {
|
|
1839
1893
|
display: flex;
|
|
1840
1894
|
align-items: center;
|