@quantaroute/checkout 1.8.2 → 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 +11 -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 +100 -7
- package/src/core/types.ts +11 -1
- package/src/styles/checkout.css +61 -0
|
@@ -50,9 +50,33 @@ 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: why. */
|
|
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 noteId = `qr-addr-note-${address.id}`;
|
|
78
|
+
const errorId = `qr-addr-error-${address.id}`;
|
|
79
|
+
const unavailable = Boolean(unavailableReason);
|
|
56
80
|
const parts = [
|
|
57
81
|
address.flat_number,
|
|
58
82
|
address.building_name,
|
|
@@ -68,7 +92,9 @@ function AddressCard({ address, onUse, onDelete, onSetDefault, deleting, selecti
|
|
|
68
92
|
const summary = parts.length > 0 ? undefined : address.locality || address.district || address.state;
|
|
69
93
|
|
|
70
94
|
return (
|
|
71
|
-
<div
|
|
95
|
+
<div
|
|
96
|
+
className={`qr-addr-card${address.is_default && !unavailable ? ' qr-addr-card--default' : ''}${unavailable ? ' qr-addr-card--unavailable' : ''}`}
|
|
97
|
+
>
|
|
72
98
|
<div className="qr-addr-card__header">
|
|
73
99
|
<div className="qr-addr-card__label-icon">
|
|
74
100
|
<LabelIcon label={address.label} />
|
|
@@ -146,20 +172,40 @@ function AddressCard({ address, onUse, onDelete, onSetDefault, deleting, selecti
|
|
|
146
172
|
</div>
|
|
147
173
|
</div>
|
|
148
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
|
+
|
|
149
187
|
<button
|
|
150
188
|
type="button"
|
|
151
|
-
className=
|
|
189
|
+
className={`qr-btn qr-btn--full qr-btn--sm ${unavailable ? 'qr-btn--ghost' : 'qr-btn--primary'}`}
|
|
152
190
|
onClick={onUse}
|
|
153
|
-
disabled={locked}
|
|
191
|
+
disabled={locked || unavailable}
|
|
154
192
|
aria-busy={selecting}
|
|
193
|
+
aria-describedby={unavailable ? noteId : error ? errorId : undefined}
|
|
155
194
|
>
|
|
156
195
|
{selecting ? (
|
|
157
196
|
<>
|
|
158
197
|
<span className="qr-spinner qr-spinner--sm" aria-hidden="true" />
|
|
159
198
|
Confirming…
|
|
160
199
|
</>
|
|
161
|
-
) : 'Deliver here'}
|
|
200
|
+
) : unavailable ? "Can't deliver here" : 'Deliver here'}
|
|
162
201
|
</button>
|
|
202
|
+
|
|
203
|
+
{error && !unavailable && (
|
|
204
|
+
<p className="qr-addr-card__error" id={errorId} role="alert">
|
|
205
|
+
<AlertIcon />
|
|
206
|
+
{error}
|
|
207
|
+
</p>
|
|
208
|
+
)}
|
|
163
209
|
</div>
|
|
164
210
|
);
|
|
165
211
|
}
|
|
@@ -172,12 +218,17 @@ const SavedAddressStep: React.FC<SavedAddressStepProps> = ({
|
|
|
172
218
|
merchantId,
|
|
173
219
|
onSelectAddress,
|
|
174
220
|
onAddNew,
|
|
221
|
+
checkDeliverable,
|
|
175
222
|
}) => {
|
|
176
223
|
const [addresses, setAddresses] = useState<SavedAddress[]>([]);
|
|
177
224
|
const [loadingList, setLoadingList] = useState(true);
|
|
178
225
|
const [listError, setListError] = useState<string | null>(null);
|
|
179
226
|
const [deletingId, setDeletingId] = useState<string | null>(null);
|
|
180
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>>({});
|
|
181
232
|
// A ref, not state, so a second click in the same frame is still ignored.
|
|
182
233
|
const selectingRef = useRef(false);
|
|
183
234
|
|
|
@@ -204,6 +255,31 @@ const SavedAddressStep: React.FC<SavedAddressStepProps> = ({
|
|
|
204
255
|
|
|
205
256
|
useEffect(() => { load(); }, [load]);
|
|
206
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
|
+
|
|
207
283
|
const handleDelete = useCallback(async (id: string) => {
|
|
208
284
|
setDeletingId(id);
|
|
209
285
|
try {
|
|
@@ -235,13 +311,22 @@ const SavedAddressStep: React.FC<SavedAddressStepProps> = ({
|
|
|
235
311
|
if (selectingRef.current) return;
|
|
236
312
|
selectingRef.current = true;
|
|
237
313
|
setSelectingId(address.id);
|
|
238
|
-
|
|
314
|
+
setCardErrors((prev) => {
|
|
315
|
+
const { [address.id]: _cleared, ...rest } = prev;
|
|
316
|
+
return rest;
|
|
317
|
+
});
|
|
239
318
|
try {
|
|
240
319
|
await onSelectAddress(address);
|
|
241
320
|
// On success the widget moves to the next step and this one unmounts;
|
|
242
321
|
// keep the spinner until then.
|
|
243
322
|
} catch (err: unknown) {
|
|
244
|
-
|
|
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
|
+
}
|
|
245
330
|
selectingRef.current = false;
|
|
246
331
|
setSelectingId(null);
|
|
247
332
|
}
|
|
@@ -303,10 +388,18 @@ const SavedAddressStep: React.FC<SavedAddressStepProps> = ({
|
|
|
303
388
|
deleting={deletingId === addr.id}
|
|
304
389
|
selecting={selectingId === addr.id}
|
|
305
390
|
locked={selectingId !== null}
|
|
391
|
+
unavailableReason={unavailable[addr.id]}
|
|
392
|
+
error={cardErrors[addr.id]}
|
|
306
393
|
/>
|
|
307
394
|
))}
|
|
308
395
|
</div>
|
|
309
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
|
+
|
|
310
403
|
<button
|
|
311
404
|
type="button"
|
|
312
405
|
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,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;
|