@quantaroute/checkout 1.8.1 → 1.8.3
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 +18 -0
- package/dist/embed/quantaroute-checkout.css +1 -1
- package/dist/embed/quantaroute-checkout.js +1 -1
- package/dist/lib/index.d.ts +12 -1
- package/dist/lib/quantaroute-checkout.es.js +77 -67
- package/dist/lib/quantaroute-checkout.umd.js +59 -49
- package/dist/style.css +1 -1
- package/package.json +1 -1
- package/src/components/CheckoutWidget.tsx +4 -2
- package/src/components/SavedAddressStep.tsx +134 -7
- package/src/core/types.ts +12 -1
- package/src/styles/checkout.css +61 -0
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import React, { useState, useEffect, useCallback } from 'react';
|
|
1
|
+
import React, { useState, useEffect, useCallback, useRef } from 'react';
|
|
2
2
|
import { listAddresses, deleteAddress, setDefaultAddress } from '../core/checkout-api';
|
|
3
3
|
import type { SavedAddress } from '../core/checkout-api';
|
|
4
4
|
import type { SavedAddressStepProps } from '../core/types';
|
|
@@ -46,9 +46,37 @@ interface AddressCardProps {
|
|
|
46
46
|
onDelete: () => void;
|
|
47
47
|
onSetDefault: () => void;
|
|
48
48
|
deleting: boolean;
|
|
49
|
+
/** This card's "Deliver here" is in progress. */
|
|
50
|
+
selecting: boolean;
|
|
51
|
+
/** Another card is being selected: block every action. */
|
|
52
|
+
locked: boolean;
|
|
53
|
+
/** Set when the merchant doesn't deliver to this address: why. */
|
|
54
|
+
unavailableReason?: string;
|
|
55
|
+
/** A retryable error from the last "Deliver here" on this card. */
|
|
56
|
+
error?: string;
|
|
49
57
|
}
|
|
50
58
|
|
|
51
|
-
|
|
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 noteId = `qr-addr-note-${address.id}`;
|
|
78
|
+
const errorId = `qr-addr-error-${address.id}`;
|
|
79
|
+
const unavailable = Boolean(unavailableReason);
|
|
52
80
|
const parts = [
|
|
53
81
|
address.flat_number,
|
|
54
82
|
address.building_name,
|
|
@@ -64,7 +92,9 @@ function AddressCard({ address, onUse, onDelete, onSetDefault, deleting }: Addre
|
|
|
64
92
|
const summary = parts.length > 0 ? undefined : address.locality || address.district || address.state;
|
|
65
93
|
|
|
66
94
|
return (
|
|
67
|
-
<div
|
|
95
|
+
<div
|
|
96
|
+
className={`qr-addr-card${address.is_default && !unavailable ? ' qr-addr-card--default' : ''}${unavailable ? ' qr-addr-card--unavailable' : ''}`}
|
|
97
|
+
>
|
|
68
98
|
<div className="qr-addr-card__header">
|
|
69
99
|
<div className="qr-addr-card__label-icon">
|
|
70
100
|
<LabelIcon label={address.label} />
|
|
@@ -82,6 +112,7 @@ function AddressCard({ address, onUse, onDelete, onSetDefault, deleting }: Addre
|
|
|
82
112
|
className="qr-icon-btn"
|
|
83
113
|
title="Set as default"
|
|
84
114
|
onClick={onSetDefault}
|
|
115
|
+
disabled={locked}
|
|
85
116
|
aria-label="Set as default address"
|
|
86
117
|
>
|
|
87
118
|
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round">
|
|
@@ -94,7 +125,7 @@ function AddressCard({ address, onUse, onDelete, onSetDefault, deleting }: Addre
|
|
|
94
125
|
className="qr-icon-btn qr-icon-btn--danger"
|
|
95
126
|
title="Delete address"
|
|
96
127
|
onClick={onDelete}
|
|
97
|
-
disabled={deleting}
|
|
128
|
+
disabled={deleting || locked}
|
|
98
129
|
aria-label="Delete address"
|
|
99
130
|
>
|
|
100
131
|
{deleting ? (
|
|
@@ -141,13 +172,40 @@ function AddressCard({ address, onUse, onDelete, onSetDefault, deleting }: Addre
|
|
|
141
172
|
</div>
|
|
142
173
|
</div>
|
|
143
174
|
|
|
175
|
+
{unavailable && (
|
|
176
|
+
<div className="qr-addr-card__unavailable" id={noteId}>
|
|
177
|
+
<AlertIcon />
|
|
178
|
+
<div>
|
|
179
|
+
<p className="qr-addr-card__unavailable-title">
|
|
180
|
+
Not deliverable{address.pincode ? ` to ${address.pincode}` : ''}
|
|
181
|
+
</p>
|
|
182
|
+
<p className="qr-addr-card__unavailable-text">{unavailableReason}</p>
|
|
183
|
+
</div>
|
|
184
|
+
</div>
|
|
185
|
+
)}
|
|
186
|
+
|
|
144
187
|
<button
|
|
145
188
|
type="button"
|
|
146
|
-
className=
|
|
189
|
+
className={`qr-btn qr-btn--full qr-btn--sm ${unavailable ? 'qr-btn--ghost' : 'qr-btn--primary'}`}
|
|
147
190
|
onClick={onUse}
|
|
191
|
+
disabled={locked || unavailable}
|
|
192
|
+
aria-busy={selecting}
|
|
193
|
+
aria-describedby={unavailable ? noteId : error ? errorId : undefined}
|
|
148
194
|
>
|
|
149
|
-
|
|
195
|
+
{selecting ? (
|
|
196
|
+
<>
|
|
197
|
+
<span className="qr-spinner qr-spinner--sm" aria-hidden="true" />
|
|
198
|
+
Confirming…
|
|
199
|
+
</>
|
|
200
|
+
) : unavailable ? "Can't deliver here" : 'Deliver here'}
|
|
150
201
|
</button>
|
|
202
|
+
|
|
203
|
+
{error && !unavailable && (
|
|
204
|
+
<p className="qr-addr-card__error" id={errorId} role="alert">
|
|
205
|
+
<AlertIcon />
|
|
206
|
+
{error}
|
|
207
|
+
</p>
|
|
208
|
+
)}
|
|
151
209
|
</div>
|
|
152
210
|
);
|
|
153
211
|
}
|
|
@@ -160,11 +218,19 @@ const SavedAddressStep: React.FC<SavedAddressStepProps> = ({
|
|
|
160
218
|
merchantId,
|
|
161
219
|
onSelectAddress,
|
|
162
220
|
onAddNew,
|
|
221
|
+
checkDeliverable,
|
|
163
222
|
}) => {
|
|
164
223
|
const [addresses, setAddresses] = useState<SavedAddress[]>([]);
|
|
165
224
|
const [loadingList, setLoadingList] = useState(true);
|
|
166
225
|
const [listError, setListError] = useState<string | null>(null);
|
|
167
226
|
const [deletingId, setDeletingId] = useState<string | null>(null);
|
|
227
|
+
const [selectingId, setSelectingId] = useState<string | null>(null);
|
|
228
|
+
/** Address id → why the merchant doesn't deliver there. */
|
|
229
|
+
const [unavailable, setUnavailable] = useState<Record<string, string>>({});
|
|
230
|
+
/** Address id → retryable error from the last "Deliver here". */
|
|
231
|
+
const [cardErrors, setCardErrors] = useState<Record<string, string>>({});
|
|
232
|
+
// A ref, not state, so a second click in the same frame is still ignored.
|
|
233
|
+
const selectingRef = useRef(false);
|
|
168
234
|
|
|
169
235
|
const config = { functionBaseUrl, merchantId };
|
|
170
236
|
|
|
@@ -189,6 +255,31 @@ const SavedAddressStep: React.FC<SavedAddressStepProps> = ({
|
|
|
189
255
|
|
|
190
256
|
useEffect(() => { load(); }, [load]);
|
|
191
257
|
|
|
258
|
+
// Check every saved address once the list is in, so blocked ones are marked
|
|
259
|
+
// before the shopper picks. One request per distinct pincode, in parallel.
|
|
260
|
+
useEffect(() => {
|
|
261
|
+
if (!checkDeliverable || addresses.length === 0) return;
|
|
262
|
+
let cancelled = false;
|
|
263
|
+
const byPincode = new Map<string, SavedAddress[]>();
|
|
264
|
+
for (const addr of addresses) {
|
|
265
|
+
const key = addr.pincode || addr.id;
|
|
266
|
+
byPincode.set(key, [...(byPincode.get(key) ?? []), addr]);
|
|
267
|
+
}
|
|
268
|
+
byPincode.forEach((group) => {
|
|
269
|
+
checkDeliverable(group[0])
|
|
270
|
+
.then((reason) => {
|
|
271
|
+
if (cancelled || !reason) return;
|
|
272
|
+
setUnavailable((prev) => {
|
|
273
|
+
const next = { ...prev };
|
|
274
|
+
group.forEach((a) => { next[a.id] = reason; });
|
|
275
|
+
return next;
|
|
276
|
+
});
|
|
277
|
+
})
|
|
278
|
+
.catch(() => { /* a failed check never blocks; it runs again on click */ });
|
|
279
|
+
});
|
|
280
|
+
return () => { cancelled = true; };
|
|
281
|
+
}, [addresses, checkDeliverable]);
|
|
282
|
+
|
|
192
283
|
const handleDelete = useCallback(async (id: string) => {
|
|
193
284
|
setDeletingId(id);
|
|
194
285
|
try {
|
|
@@ -216,6 +307,31 @@ const SavedAddressStep: React.FC<SavedAddressStepProps> = ({
|
|
|
216
307
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
217
308
|
}, [sessionToken]);
|
|
218
309
|
|
|
310
|
+
const handleUse = useCallback(async (address: SavedAddress) => {
|
|
311
|
+
if (selectingRef.current) return;
|
|
312
|
+
selectingRef.current = true;
|
|
313
|
+
setSelectingId(address.id);
|
|
314
|
+
setCardErrors((prev) => {
|
|
315
|
+
const { [address.id]: _cleared, ...rest } = prev;
|
|
316
|
+
return rest;
|
|
317
|
+
});
|
|
318
|
+
try {
|
|
319
|
+
await onSelectAddress(address);
|
|
320
|
+
// On success the widget moves to the next step and this one unmounts;
|
|
321
|
+
// keep the spinner until then.
|
|
322
|
+
} catch (err: unknown) {
|
|
323
|
+
const message = err instanceof Error ? err.message : 'Could not use this address. Please try again.';
|
|
324
|
+
// Show the problem on the card it belongs to, not above the list.
|
|
325
|
+
if ((err as { code?: string } | null)?.code === 'ADDRESS_UNDELIVERABLE') {
|
|
326
|
+
setUnavailable((prev) => ({ ...prev, [address.id]: message }));
|
|
327
|
+
} else {
|
|
328
|
+
setCardErrors((prev) => ({ ...prev, [address.id]: message }));
|
|
329
|
+
}
|
|
330
|
+
selectingRef.current = false;
|
|
331
|
+
setSelectingId(null);
|
|
332
|
+
}
|
|
333
|
+
}, [onSelectAddress]);
|
|
334
|
+
|
|
219
335
|
// ── Loading ──────────────────────────────────────────────────────────────────
|
|
220
336
|
if (loadingList) {
|
|
221
337
|
return (
|
|
@@ -266,18 +382,29 @@ const SavedAddressStep: React.FC<SavedAddressStepProps> = ({
|
|
|
266
382
|
<AddressCard
|
|
267
383
|
key={addr.id}
|
|
268
384
|
address={addr}
|
|
269
|
-
onUse={() =>
|
|
385
|
+
onUse={() => { void handleUse(addr); }}
|
|
270
386
|
onDelete={() => handleDelete(addr.id)}
|
|
271
387
|
onSetDefault={() => handleSetDefault(addr.id)}
|
|
272
388
|
deleting={deletingId === addr.id}
|
|
389
|
+
selecting={selectingId === addr.id}
|
|
390
|
+
locked={selectingId !== null}
|
|
391
|
+
unavailableReason={unavailable[addr.id]}
|
|
392
|
+
error={cardErrors[addr.id]}
|
|
273
393
|
/>
|
|
274
394
|
))}
|
|
275
395
|
</div>
|
|
276
396
|
|
|
397
|
+
{addresses.length > 0 && addresses.every((a) => unavailable[a.id]) && (
|
|
398
|
+
<p className="qr-saved-step__hint" role="status">
|
|
399
|
+
None of your saved addresses can be delivered to right now. Add a new address to continue.
|
|
400
|
+
</p>
|
|
401
|
+
)}
|
|
402
|
+
|
|
277
403
|
<button
|
|
278
404
|
type="button"
|
|
279
405
|
className="qr-btn qr-btn--ghost qr-btn--full"
|
|
280
406
|
onClick={onAddNew}
|
|
407
|
+
disabled={selectingId !== null}
|
|
281
408
|
>
|
|
282
409
|
<svg aria-hidden="true" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round">
|
|
283
410
|
<path d="M12 5v14M5 12h14" />
|
package/src/core/types.ts
CHANGED
|
@@ -519,7 +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
|
+
*/
|
|
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>;
|
|
523
534
|
onAddNew: () => void;
|
|
524
535
|
}
|
|
525
536
|
|
package/src/styles/checkout.css
CHANGED
|
@@ -1835,6 +1835,67 @@
|
|
|
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: flex-start;
|
|
1856
|
+
padding: 10px 12px;
|
|
1857
|
+
border-radius: calc(var(--qr-radius) - 4px);
|
|
1858
|
+
background: var(--qr-unavailable-bg);
|
|
1859
|
+
color: var(--qr-unavailable-text);
|
|
1860
|
+
}
|
|
1861
|
+
.qr-addr-card__unavailable svg,
|
|
1862
|
+
.qr-addr-card__error svg {
|
|
1863
|
+
width: 18px;
|
|
1864
|
+
height: 18px;
|
|
1865
|
+
flex-shrink: 0;
|
|
1866
|
+
margin-top: 1px;
|
|
1867
|
+
}
|
|
1868
|
+
.qr-addr-card__unavailable-title {
|
|
1869
|
+
margin: 0;
|
|
1870
|
+
font-size: 14px;
|
|
1871
|
+
font-weight: 600;
|
|
1872
|
+
line-height: 1.35;
|
|
1873
|
+
}
|
|
1874
|
+
.qr-addr-card__unavailable-text {
|
|
1875
|
+
margin: 2px 0 0;
|
|
1876
|
+
font-size: 13px;
|
|
1877
|
+
line-height: 1.45;
|
|
1878
|
+
}
|
|
1879
|
+
.qr-addr-card__error {
|
|
1880
|
+
display: flex;
|
|
1881
|
+
gap: 6px;
|
|
1882
|
+
align-items: flex-start;
|
|
1883
|
+
margin: 0;
|
|
1884
|
+
font-size: 13px;
|
|
1885
|
+
line-height: 1.45;
|
|
1886
|
+
color: #b91c1c;
|
|
1887
|
+
}
|
|
1888
|
+
.qr-checkout--dark .qr-addr-card__error {
|
|
1889
|
+
color: #fca5a5;
|
|
1890
|
+
}
|
|
1891
|
+
.qr-saved-step__hint {
|
|
1892
|
+
margin: 0;
|
|
1893
|
+
font-size: 13px;
|
|
1894
|
+
line-height: 1.5;
|
|
1895
|
+
color: var(--qr-text-muted);
|
|
1896
|
+
text-align: center;
|
|
1897
|
+
}
|
|
1898
|
+
|
|
1838
1899
|
.qr-addr-card__header {
|
|
1839
1900
|
display: flex;
|
|
1840
1901
|
align-items: center;
|