@tokenoftrust/storefront-runner 7.12.0 → 7.13.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/apps/storefront/src/components/catalog/storefront/ProductCard.astro +23 -1
- package/apps/storefront/src/components/compliance/storefront/compliance/StatePicker.astro +83 -0
- package/apps/storefront/src/components/content/storefront/chrome/SiteHeader.astro +1 -0
- package/apps/storefront/src/layouts/Layout.astro +25 -1
- package/apps/storefront/src/lib/compliance/shopper/statePickerClient.ts +372 -0
- package/package.json +1 -1
- package/packages/public-runtime/package.json +1 -0
- package/packages/public-runtime/src/compliance/state-directory.ts +296 -0
- package/packages/public-runtime/src/tenant.ts +2 -0
|
@@ -7,6 +7,8 @@
|
|
|
7
7
|
* color swatches; price with struck compare-at. Theme-agnostic.
|
|
8
8
|
*/
|
|
9
9
|
import type { CatalogProduct } from "@tot/public-runtime";
|
|
10
|
+
import { deriveProductProfile } from "@tot/public-runtime";
|
|
11
|
+
import { extractDirectoryStatesFromTags } from "@tot/public-runtime/compliance/state-directory";
|
|
10
12
|
import { hidesPublicCatalogPrices, resolveShopperAccessPolicy } from "@tot/public-runtime/declared-tenant-config";
|
|
11
13
|
import { discountPercent, truncate } from "@/lib/content/shared/format";
|
|
12
14
|
import { isOnSale, isNew, isAvailable, fromPrice } from "@/lib/catalog/tot/query";
|
|
@@ -136,9 +138,29 @@ const quickview = {
|
|
|
136
138
|
maxCompareAt,
|
|
137
139
|
sku: product.variants[0]?.sku ?? null,
|
|
138
140
|
};
|
|
141
|
+
const complianceProfile = deriveProductProfile(product);
|
|
142
|
+
const directoryStates = extractDirectoryStatesFromTags(product.tags);
|
|
143
|
+
const eligibleStatesAttr =
|
|
144
|
+
complianceProfile.allowedRegions && complianceProfile.allowedRegions.length > 0
|
|
145
|
+
? complianceProfile.allowedRegions.join(",")
|
|
146
|
+
: undefined;
|
|
147
|
+
const restrictedStatesAttr =
|
|
148
|
+
complianceProfile.restrictedRegions.length > 0
|
|
149
|
+
? complianceProfile.restrictedRegions.join(",")
|
|
150
|
+
: undefined;
|
|
151
|
+
const directoryStatesAttr =
|
|
152
|
+
directoryStates.length > 0 ? directoryStates.join(",") : undefined;
|
|
139
153
|
---
|
|
140
154
|
|
|
141
|
-
<div
|
|
155
|
+
<div
|
|
156
|
+
class="group relative"
|
|
157
|
+
data-reveal
|
|
158
|
+
data-product-card
|
|
159
|
+
data-product-handle={product.handle}
|
|
160
|
+
data-eligible-states={eligibleStatesAttr}
|
|
161
|
+
data-restricted-states={restrictedStatesAttr}
|
|
162
|
+
data-directory-states={directoryStatesAttr}
|
|
163
|
+
>
|
|
142
164
|
<div class="relative aspect-square overflow-hidden rounded-md bg-border ring-1 ring-[var(--color-border)] transition-shadow duration-300 motion-safe:group-hover:shadow-[var(--shadow-md)]">
|
|
143
165
|
{
|
|
144
166
|
primary ? (
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* <StatePicker> — lightweight, accessible "Ship to" US state picker for
|
|
4
|
+
* regulated storefronts with state-level restrictions or per-state directory
|
|
5
|
+
* allow-lists. Default-on whenever `stateEligibility` / `directoryStates`
|
|
6
|
+
* config is present; renders nothing when absent.
|
|
7
|
+
*/
|
|
8
|
+
import type { StateEligibilityConfig } from "@tot/public-runtime";
|
|
9
|
+
import {
|
|
10
|
+
US_STATE_OPTIONS,
|
|
11
|
+
type StateDirectoryRule,
|
|
12
|
+
} from "@tot/public-runtime/compliance/state-directory";
|
|
13
|
+
|
|
14
|
+
interface Props {
|
|
15
|
+
/** From `tenant.compliance.stateEligibility` (or explicit directory rule). */
|
|
16
|
+
config?: (StateEligibilityConfig & StateDirectoryRule) | null;
|
|
17
|
+
/** Accessible label prefix (defaults to "Ship to"). */
|
|
18
|
+
label?: string;
|
|
19
|
+
/** Optional unique id for the `<select>` element. */
|
|
20
|
+
id?: string;
|
|
21
|
+
class?: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const {
|
|
25
|
+
config,
|
|
26
|
+
label = "Ship to",
|
|
27
|
+
id = "tot-ship-to-state",
|
|
28
|
+
class: className,
|
|
29
|
+
} = Astro.props;
|
|
30
|
+
|
|
31
|
+
const hasRules = Boolean(
|
|
32
|
+
config &&
|
|
33
|
+
((config.directoryStates && config.directoryStates.length > 0) ||
|
|
34
|
+
(config.restrictedStates && config.restrictedStates.length > 0) ||
|
|
35
|
+
(config.eligibleStates && config.eligibleStates.length > 0)),
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
const serializedRule: StateDirectoryRule | null = hasRules && config
|
|
39
|
+
? {
|
|
40
|
+
...(config.directoryStates?.length ? { directoryStates: [...config.directoryStates] } : {}),
|
|
41
|
+
...(config.restrictedStates?.length ? { restrictedStates: [...config.restrictedStates] } : {}),
|
|
42
|
+
...(config.eligibleStates?.length ? { eligibleStates: [...config.eligibleStates] } : {}),
|
|
43
|
+
}
|
|
44
|
+
: null;
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
{
|
|
48
|
+
serializedRule && (
|
|
49
|
+
<>
|
|
50
|
+
<div
|
|
51
|
+
class:list={["inline-flex items-center gap-1.5 text-xs", className]}
|
|
52
|
+
data-compliance="state-picker"
|
|
53
|
+
data-state-picker
|
|
54
|
+
data-state-picker-config={JSON.stringify(serializedRule)}
|
|
55
|
+
>
|
|
56
|
+
<label
|
|
57
|
+
for={id}
|
|
58
|
+
class="hidden shrink-0 font-medium opacity-80 sm:inline"
|
|
59
|
+
>
|
|
60
|
+
{label}
|
|
61
|
+
</label>
|
|
62
|
+
<select
|
|
63
|
+
id={id}
|
|
64
|
+
name="ship_state"
|
|
65
|
+
aria-label={`${label} state`}
|
|
66
|
+
data-state-picker-select
|
|
67
|
+
class="min-h-9 rounded-md border border-border bg-surface px-2 py-1 text-xs font-medium text-text shadow-xs transition-colors focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-accent"
|
|
68
|
+
>
|
|
69
|
+
<option value="">All states</option>
|
|
70
|
+
{US_STATE_OPTIONS.map((state) => (
|
|
71
|
+
<option value={state.code}>
|
|
72
|
+
{state.code} — {state.name}
|
|
73
|
+
</option>
|
|
74
|
+
))}
|
|
75
|
+
</select>
|
|
76
|
+
</div>
|
|
77
|
+
<script>
|
|
78
|
+
import { bootStatePickersFromDom } from "@/lib/compliance/shopper/statePickerClient";
|
|
79
|
+
bootStatePickersFromDom();
|
|
80
|
+
</script>
|
|
81
|
+
</>
|
|
82
|
+
)
|
|
83
|
+
}
|
|
@@ -239,6 +239,7 @@ const currentPath = Astro.url.pathname;
|
|
|
239
239
|
|
|
240
240
|
<!-- Actions: saved items + marketing CTAs + auth + account + cart (link only) + mobile menu -->
|
|
241
241
|
<div class:list={["flex items-center gap-1", compact ? "col-start-3 justify-self-end" : "ml-auto"]}>
|
|
242
|
+
<slot name="actions-prefix" />
|
|
242
243
|
{
|
|
243
244
|
compact && searchFormAction && (
|
|
244
245
|
<a
|
|
@@ -19,6 +19,7 @@ import CartDrawerStyles from "@/components/checkout/storefront/commerce/CartDraw
|
|
|
19
19
|
import NicotineWarning from "@/components/compliance/storefront/compliance/NicotineWarning.astro";
|
|
20
20
|
import PactActNotice from "@/components/compliance/storefront/compliance/PactActNotice.astro";
|
|
21
21
|
import ShippingRestrictionNotice from "@/components/compliance/storefront/compliance/ShippingRestrictionNotice.astro";
|
|
22
|
+
import StatePicker from "@/components/compliance/storefront/compliance/StatePicker.astro";
|
|
22
23
|
import ExciseTaxNotice from "@/components/compliance/storefront/compliance/ExciseTaxNotice.astro";
|
|
23
24
|
import { EXCISE_TAX_DISCLOSURE_TEXT } from "@/lib/compliance/rawComplianceNotices";
|
|
24
25
|
import { complianceSections } from "@/lib/compliance/complianceSections";
|
|
@@ -435,11 +436,34 @@ const footerWidgets = await renderWidgetSlot(
|
|
|
435
436
|
authLink={chrome.header.accountHref ? undefined : authLink}
|
|
436
437
|
cartHref={cartHref}
|
|
437
438
|
sticky={false}
|
|
438
|
-
|
|
439
|
+
>
|
|
440
|
+
{compliance?.stateEligibility && (
|
|
441
|
+
<StatePicker slot="actions-prefix" config={compliance.stateEligibility} />
|
|
442
|
+
)}
|
|
443
|
+
</SiteHeader>
|
|
439
444
|
</div>
|
|
440
445
|
</>
|
|
441
446
|
)}
|
|
442
447
|
|
|
448
|
+
{compliance?.stateEligibility && (
|
|
449
|
+
<div class="container-prose space-y-2 pt-2">
|
|
450
|
+
<p
|
|
451
|
+
hidden
|
|
452
|
+
role="status"
|
|
453
|
+
aria-live="polite"
|
|
454
|
+
data-state-filter-status
|
|
455
|
+
class="rounded-md border border-border bg-surface px-3 py-2 text-xs text-text"
|
|
456
|
+
/>
|
|
457
|
+
<p
|
|
458
|
+
hidden
|
|
459
|
+
role="alert"
|
|
460
|
+
aria-live="assertive"
|
|
461
|
+
data-state-cart-notice
|
|
462
|
+
class="rounded-md border border-amber-500/40 bg-amber-500/10 px-3 py-2 text-xs font-medium text-text"
|
|
463
|
+
/>
|
|
464
|
+
</div>
|
|
465
|
+
)}
|
|
466
|
+
|
|
443
467
|
<main id="main">
|
|
444
468
|
{homeSlotHtml !== null ? <Fragment set:html={homeSlotHtml} /> : <slot />}
|
|
445
469
|
</main>
|
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shopper "Ship to" state picker client helper.
|
|
3
|
+
*
|
|
4
|
+
* Responsibilities:
|
|
5
|
+
* 1. Persist selected ship-to US state code in `localStorage` under `tot:ship-state`
|
|
6
|
+
* and dispatch the `tot:ship-state-change` custom event on `window`.
|
|
7
|
+
* 2. Filter rendered catalog cards (`[data-product-card]`) in place using
|
|
8
|
+
* `data-eligible-states`, `data-restricted-states`, and `data-directory-states`
|
|
9
|
+
* attributes, updating any `[data-state-filter-status]` status banner.
|
|
10
|
+
* 3. Revalidate cart lines via `revalidateCartForState`, removing ineligible items
|
|
11
|
+
* and rendering a shopper-facing notice into `[data-state-cart-notice]`.
|
|
12
|
+
*/
|
|
13
|
+
import {
|
|
14
|
+
evaluateProductStateEligibility,
|
|
15
|
+
normalizeUsStateCode,
|
|
16
|
+
revalidateCartForState,
|
|
17
|
+
type CartRevalidationLine,
|
|
18
|
+
type CartStateRevalidationResult,
|
|
19
|
+
type ProductStateEligibilityInput,
|
|
20
|
+
type StateDirectoryRule,
|
|
21
|
+
} from "@tot/public-runtime/compliance/state-directory";
|
|
22
|
+
|
|
23
|
+
export const SHIP_STATE_STORAGE_KEY = "tot:ship-state";
|
|
24
|
+
export const SHIP_STATE_CHANGE_EVENT = "tot:ship-state-change";
|
|
25
|
+
|
|
26
|
+
export interface ShipStateChangeDetail {
|
|
27
|
+
stateCode: string | null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface StorageLike {
|
|
31
|
+
getItem(key: string): string | null;
|
|
32
|
+
setItem(key: string, value: string): void;
|
|
33
|
+
removeItem(key: string): void;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface EventTargetLike {
|
|
37
|
+
dispatchEvent(event: Event): boolean;
|
|
38
|
+
addEventListener?(type: string, listener: EventListenerOrEventListenerObject): void;
|
|
39
|
+
removeEventListener?(type: string, listener: EventListenerOrEventListenerObject): void;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface StateCardElement {
|
|
43
|
+
hidden: boolean;
|
|
44
|
+
getAttribute(name: string): string | null;
|
|
45
|
+
setAttribute(name: string, value: string): void;
|
|
46
|
+
removeAttribute(name: string): void;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface NoticeBannerElement {
|
|
50
|
+
hidden: boolean;
|
|
51
|
+
textContent: string | null;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface StateFilterRoot {
|
|
55
|
+
querySelectorAll(selector: string): Iterable<unknown>;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface SelectElementLike {
|
|
59
|
+
value: string;
|
|
60
|
+
addEventListener(type: string, listener: EventListenerOrEventListenerObject): void;
|
|
61
|
+
removeEventListener(type: string, listener: EventListenerOrEventListenerObject): void;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface CatalogFilterSummary {
|
|
65
|
+
stateCode: string | null;
|
|
66
|
+
total: number;
|
|
67
|
+
visible: number;
|
|
68
|
+
hidden: number;
|
|
69
|
+
statusMessage: string | null;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function defaultStorage(): StorageLike | null {
|
|
73
|
+
try {
|
|
74
|
+
if (typeof globalThis !== "undefined" && "localStorage" in globalThis && globalThis.localStorage) {
|
|
75
|
+
return globalThis.localStorage;
|
|
76
|
+
}
|
|
77
|
+
} catch {
|
|
78
|
+
// Storage access may be blocked in restricted browser contexts.
|
|
79
|
+
}
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function defaultEventTarget(): EventTargetLike | null {
|
|
84
|
+
if (typeof globalThis !== "undefined" && "window" in globalThis && globalThis.window) {
|
|
85
|
+
return globalThis.window;
|
|
86
|
+
}
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function defaultRoot(): StateFilterRoot | null {
|
|
91
|
+
if (typeof globalThis !== "undefined" && "document" in globalThis && globalThis.document) {
|
|
92
|
+
return globalThis.document;
|
|
93
|
+
}
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function parseStateListAttribute(raw: string | null): string[] {
|
|
98
|
+
if (!raw) return [];
|
|
99
|
+
return raw
|
|
100
|
+
.split(/[\s,;]+/)
|
|
101
|
+
.map((token) => token.trim())
|
|
102
|
+
.filter(Boolean);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Read the persisted ship-to state code from `localStorage` (`tot:ship-state`). */
|
|
106
|
+
export function readStoredShipState(storage: StorageLike | null = defaultStorage()): string | null {
|
|
107
|
+
if (!storage) return null;
|
|
108
|
+
let raw: string | null = null;
|
|
109
|
+
try {
|
|
110
|
+
raw = storage.getItem(SHIP_STATE_STORAGE_KEY);
|
|
111
|
+
} catch (error) {
|
|
112
|
+
if (
|
|
113
|
+
(typeof DOMException !== "undefined" && error instanceof DOMException) ||
|
|
114
|
+
(error instanceof Error && error.name === "SecurityError")
|
|
115
|
+
) {
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
throw error;
|
|
119
|
+
}
|
|
120
|
+
return normalizeUsStateCode(raw);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Persist the selected ship-to state code in `localStorage` (`tot:ship-state`) and
|
|
125
|
+
* dispatch `tot:ship-state-change` with `{ stateCode }`. Passing `null` or an empty/invalid
|
|
126
|
+
* string clears the stored selection.
|
|
127
|
+
*/
|
|
128
|
+
export function writeStoredShipState(
|
|
129
|
+
rawState: string | null | undefined,
|
|
130
|
+
options: {
|
|
131
|
+
storage?: StorageLike | null;
|
|
132
|
+
eventTarget?: EventTargetLike | null;
|
|
133
|
+
} = {},
|
|
134
|
+
): string | null {
|
|
135
|
+
const storage = options.storage === undefined ? defaultStorage() : options.storage;
|
|
136
|
+
const eventTarget = options.eventTarget === undefined ? defaultEventTarget() : options.eventTarget;
|
|
137
|
+
const normalized = normalizeUsStateCode(rawState);
|
|
138
|
+
|
|
139
|
+
if (storage) {
|
|
140
|
+
try {
|
|
141
|
+
if (normalized) {
|
|
142
|
+
storage.setItem(SHIP_STATE_STORAGE_KEY, normalized);
|
|
143
|
+
} else {
|
|
144
|
+
storage.removeItem(SHIP_STATE_STORAGE_KEY);
|
|
145
|
+
}
|
|
146
|
+
} catch {
|
|
147
|
+
// Ignore storage quota / privacy mode failures; still dispatch event.
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (eventTarget && typeof CustomEvent === "function") {
|
|
152
|
+
eventTarget.dispatchEvent(
|
|
153
|
+
new CustomEvent<ShipStateChangeDetail>(SHIP_STATE_CHANGE_EVENT, {
|
|
154
|
+
detail: { stateCode: normalized },
|
|
155
|
+
}),
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return normalized;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** Extract product state-eligibility inputs from a `[data-product-card]` element. */
|
|
163
|
+
export function readProductCardEligibility(card: StateCardElement): ProductStateEligibilityInput {
|
|
164
|
+
const eligible = parseStateListAttribute(card.getAttribute("data-eligible-states"));
|
|
165
|
+
const restricted = parseStateListAttribute(card.getAttribute("data-restricted-states"));
|
|
166
|
+
const directory = parseStateListAttribute(card.getAttribute("data-directory-states"));
|
|
167
|
+
|
|
168
|
+
return {
|
|
169
|
+
allowedStates: eligible.length > 0 ? eligible : null,
|
|
170
|
+
restrictedStates: restricted.length > 0 ? restricted : null,
|
|
171
|
+
tags: directory.map((code) => `directory:${code}`),
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function formatFilterStatus(
|
|
176
|
+
stateCode: string | null,
|
|
177
|
+
total: number,
|
|
178
|
+
visible: number,
|
|
179
|
+
hidden: number,
|
|
180
|
+
): string | null {
|
|
181
|
+
if (!stateCode) return null;
|
|
182
|
+
if (total === 0) return `Shipping to ${stateCode}.`;
|
|
183
|
+
if (hidden === 0) {
|
|
184
|
+
return `Showing all ${total} ${total === 1 ? "product" : "products"} eligible to ship to ${stateCode}.`;
|
|
185
|
+
}
|
|
186
|
+
if (visible === 0) {
|
|
187
|
+
return `No products in this catalog can ship to ${stateCode}.`;
|
|
188
|
+
}
|
|
189
|
+
return `Showing ${visible} of ${total} products eligible to ship to ${stateCode} (${hidden} unavailable in ${stateCode}).`;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Filter `[data-product-card]` elements in `root` for `stateCode` and update
|
|
194
|
+
* `[data-state-filter-status]` banner(s).
|
|
195
|
+
*/
|
|
196
|
+
export function filterCatalogCardsForState(
|
|
197
|
+
stateCode: string | null | undefined,
|
|
198
|
+
config?: StateDirectoryRule | null,
|
|
199
|
+
root: StateFilterRoot | null = defaultRoot(),
|
|
200
|
+
): CatalogFilterSummary {
|
|
201
|
+
const normalized = normalizeUsStateCode(stateCode);
|
|
202
|
+
if (!root) {
|
|
203
|
+
return { stateCode: normalized, total: 0, visible: 0, hidden: 0, statusMessage: null };
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const cards = Array.from(root.querySelectorAll("[data-product-card]")) as StateCardElement[];
|
|
207
|
+
let visible = 0;
|
|
208
|
+
let hidden = 0;
|
|
209
|
+
|
|
210
|
+
for (const card of cards) {
|
|
211
|
+
const product = readProductCardEligibility(card);
|
|
212
|
+
const decision = evaluateProductStateEligibility(product, normalized, config);
|
|
213
|
+
card.hidden = !decision.eligible;
|
|
214
|
+
if (decision.eligible) {
|
|
215
|
+
card.removeAttribute("data-state-ineligible");
|
|
216
|
+
visible += 1;
|
|
217
|
+
} else {
|
|
218
|
+
card.setAttribute("data-state-ineligible", decision.reason);
|
|
219
|
+
hidden += 1;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const total = cards.length;
|
|
224
|
+
const statusMessage = formatFilterStatus(normalized, total, visible, hidden);
|
|
225
|
+
const banners = Array.from(
|
|
226
|
+
root.querySelectorAll("[data-state-filter-status]"),
|
|
227
|
+
) as NoticeBannerElement[];
|
|
228
|
+
|
|
229
|
+
for (const banner of banners) {
|
|
230
|
+
banner.textContent = statusMessage ?? "";
|
|
231
|
+
banner.hidden = statusMessage === null;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
return { stateCode: normalized, total, visible, hidden, statusMessage };
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
export interface RevalidateShopperCartOptions<TLine extends CartRevalidationLine> {
|
|
238
|
+
stateCode: string | null | undefined;
|
|
239
|
+
config?: StateDirectoryRule | null;
|
|
240
|
+
readLines: () => readonly TLine[];
|
|
241
|
+
writeLines: (kept: TLine[]) => void;
|
|
242
|
+
resolveProduct: (line: TLine) => ProductStateEligibilityInput | null | undefined;
|
|
243
|
+
root?: StateFilterRoot | null;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Revalidate the shopper's cart for `stateCode`, persist the kept lines when
|
|
248
|
+
* any line is removed, and render the removal notice into `[data-state-cart-notice]`.
|
|
249
|
+
*/
|
|
250
|
+
export function revalidateShopperCartForState<TLine extends CartRevalidationLine>(
|
|
251
|
+
options: RevalidateShopperCartOptions<TLine>,
|
|
252
|
+
): CartStateRevalidationResult<TLine> {
|
|
253
|
+
const root = options.root === undefined ? defaultRoot() : options.root;
|
|
254
|
+
const lines = options.readLines();
|
|
255
|
+
const result = revalidateCartForState(
|
|
256
|
+
lines,
|
|
257
|
+
options.stateCode,
|
|
258
|
+
options.resolveProduct,
|
|
259
|
+
options.config,
|
|
260
|
+
);
|
|
261
|
+
|
|
262
|
+
if (result.removed.length > 0) {
|
|
263
|
+
options.writeLines(result.kept);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
if (root) {
|
|
267
|
+
const notices = Array.from(
|
|
268
|
+
root.querySelectorAll("[data-state-cart-notice]"),
|
|
269
|
+
) as NoticeBannerElement[];
|
|
270
|
+
for (const noticeEl of notices) {
|
|
271
|
+
noticeEl.textContent = result.notice ?? "";
|
|
272
|
+
noticeEl.hidden = result.notice === null;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
return result;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
export interface MountStatePickerOptions<TLine extends CartRevalidationLine = CartRevalidationLine> {
|
|
280
|
+
select: SelectElementLike;
|
|
281
|
+
config?: StateDirectoryRule | null;
|
|
282
|
+
storage?: StorageLike | null;
|
|
283
|
+
eventTarget?: EventTargetLike | null;
|
|
284
|
+
root?: StateFilterRoot | null;
|
|
285
|
+
cart?: {
|
|
286
|
+
readLines: () => readonly TLine[];
|
|
287
|
+
writeLines: (kept: TLine[]) => void;
|
|
288
|
+
resolveProduct: (line: TLine) => ProductStateEligibilityInput | null | undefined;
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* Wire a `<select>` state picker to `localStorage`, catalog filtering, and optional
|
|
294
|
+
* cart revalidation. Returns a `dispose()` cleanup callback.
|
|
295
|
+
*/
|
|
296
|
+
export function mountStatePicker<TLine extends CartRevalidationLine = CartRevalidationLine>(
|
|
297
|
+
options: MountStatePickerOptions<TLine>,
|
|
298
|
+
): () => void {
|
|
299
|
+
const storage = options.storage === undefined ? defaultStorage() : options.storage;
|
|
300
|
+
const eventTarget = options.eventTarget === undefined ? defaultEventTarget() : options.eventTarget;
|
|
301
|
+
const root = options.root === undefined ? defaultRoot() : options.root;
|
|
302
|
+
|
|
303
|
+
const applyState = (stateCode: string | null) => {
|
|
304
|
+
options.select.value = stateCode ?? "";
|
|
305
|
+
filterCatalogCardsForState(stateCode, options.config, root);
|
|
306
|
+
if (options.cart) {
|
|
307
|
+
revalidateShopperCartForState({
|
|
308
|
+
stateCode,
|
|
309
|
+
config: options.config,
|
|
310
|
+
readLines: options.cart.readLines,
|
|
311
|
+
writeLines: options.cart.writeLines,
|
|
312
|
+
resolveProduct: options.cart.resolveProduct,
|
|
313
|
+
root,
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
const initial = readStoredShipState(storage);
|
|
319
|
+
applyState(initial);
|
|
320
|
+
|
|
321
|
+
const onSelectChange: EventListener = () => {
|
|
322
|
+
const next = writeStoredShipState(options.select.value, { storage, eventTarget });
|
|
323
|
+
applyState(next);
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
const onExternalStateChange: EventListener = (event) => {
|
|
327
|
+
const detail = (event as CustomEvent<ShipStateChangeDetail>).detail;
|
|
328
|
+
const next = normalizeUsStateCode(detail?.stateCode);
|
|
329
|
+
if (options.select.value !== (next ?? "")) {
|
|
330
|
+
applyState(next);
|
|
331
|
+
}
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
options.select.addEventListener("change", onSelectChange);
|
|
335
|
+
eventTarget?.addEventListener?.(SHIP_STATE_CHANGE_EVENT, onExternalStateChange);
|
|
336
|
+
|
|
337
|
+
return () => {
|
|
338
|
+
options.select.removeEventListener("change", onSelectChange);
|
|
339
|
+
eventTarget?.removeEventListener?.(SHIP_STATE_CHANGE_EVENT, onExternalStateChange);
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* Boot every unmounted `[data-state-picker]` widget in the document.
|
|
345
|
+
* Reads its `StateDirectoryRule` JSON from `data-state-picker-config`.
|
|
346
|
+
*/
|
|
347
|
+
export function bootStatePickersFromDom(): Array<() => void> {
|
|
348
|
+
if (typeof document === "undefined") return [];
|
|
349
|
+
const containers = Array.from(document.querySelectorAll<HTMLElement>("[data-state-picker]"));
|
|
350
|
+
const disposers: Array<() => void> = [];
|
|
351
|
+
|
|
352
|
+
for (const container of containers) {
|
|
353
|
+
if (container.dataset.statePickerMounted === "true") continue;
|
|
354
|
+
const select = container.querySelector<HTMLSelectElement>("[data-state-picker-select]");
|
|
355
|
+
if (!select) continue;
|
|
356
|
+
|
|
357
|
+
let config: StateDirectoryRule | null = null;
|
|
358
|
+
const rawConfig = container.getAttribute("data-state-picker-config");
|
|
359
|
+
if (rawConfig) {
|
|
360
|
+
try {
|
|
361
|
+
config = JSON.parse(rawConfig) as StateDirectoryRule;
|
|
362
|
+
} catch {
|
|
363
|
+
config = null;
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
container.dataset.statePickerMounted = "true";
|
|
368
|
+
disposers.push(mountStatePicker({ select, config }));
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
return disposers;
|
|
372
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tokenoftrust/storefront-runner",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.13.0",
|
|
4
4
|
"license": "SEE LICENSE IN LICENSE",
|
|
5
5
|
"description": "World-shareable storefront runner: multi-tenant renderer on Astro/Cloudflare. No control plane.",
|
|
6
6
|
"packageManager": "pnpm@11.9.0",
|
|
@@ -49,6 +49,7 @@
|
|
|
49
49
|
"./declared-tenant-config": "./src/declared-tenant-config.mjs",
|
|
50
50
|
"./theme-stylesheets": "./src/theme-stylesheets.mjs",
|
|
51
51
|
"./compliance-coherence": "./src/compliance-coherence.mjs",
|
|
52
|
+
"./compliance/state-directory": "./src/compliance/state-directory.ts",
|
|
52
53
|
"./integration/lifecycle-host-asset": "./src/integration/lifecycle-host-asset.mjs",
|
|
53
54
|
"./integration/native-ga4-sandbox-asset": "./src/integration/native-ga4-sandbox-asset.mjs",
|
|
54
55
|
"./integration/visitor-region": "./src/integration/visitor-region.mjs"
|
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-state directory allow-list eligibility & cart revalidation.
|
|
3
|
+
*
|
|
4
|
+
* Pure, edge-safe decision core for:
|
|
5
|
+
* 1. Validating/normalizing 2-letter US state/DC codes (`normalizeUsStateCode`).
|
|
6
|
+
* 2. Evaluating whether a product is eligible to ship to a selected state under
|
|
7
|
+
* store-wide restrictions, per-state directory allow-lists (`directoryStates`),
|
|
8
|
+
* product-level `allowedStates` / `restrictedStates`, and directory tags
|
|
9
|
+
* (`directory:<ST>`, `state-directory:<ST>`, `state:<ST>`).
|
|
10
|
+
* 3. Revalidating a shopper's cart when their ship-to state changes, partitioning
|
|
11
|
+
* kept vs. removed lines and composing a clear shopper-facing notice.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** US states and DC (50 states + District of Columbia) in alphabetical order by name. */
|
|
15
|
+
export const US_STATE_OPTIONS: ReadonlyArray<{ readonly code: string; readonly name: string }> = [
|
|
16
|
+
{ code: "AL", name: "Alabama" },
|
|
17
|
+
{ code: "AK", name: "Alaska" },
|
|
18
|
+
{ code: "AZ", name: "Arizona" },
|
|
19
|
+
{ code: "AR", name: "Arkansas" },
|
|
20
|
+
{ code: "CA", name: "California" },
|
|
21
|
+
{ code: "CO", name: "Colorado" },
|
|
22
|
+
{ code: "CT", name: "Connecticut" },
|
|
23
|
+
{ code: "DE", name: "Delaware" },
|
|
24
|
+
{ code: "DC", name: "District of Columbia" },
|
|
25
|
+
{ code: "FL", name: "Florida" },
|
|
26
|
+
{ code: "GA", name: "Georgia" },
|
|
27
|
+
{ code: "HI", name: "Hawaii" },
|
|
28
|
+
{ code: "ID", name: "Idaho" },
|
|
29
|
+
{ code: "IL", name: "Illinois" },
|
|
30
|
+
{ code: "IN", name: "Indiana" },
|
|
31
|
+
{ code: "IA", name: "Iowa" },
|
|
32
|
+
{ code: "KS", name: "Kansas" },
|
|
33
|
+
{ code: "KY", name: "Kentucky" },
|
|
34
|
+
{ code: "LA", name: "Louisiana" },
|
|
35
|
+
{ code: "ME", name: "Maine" },
|
|
36
|
+
{ code: "MD", name: "Maryland" },
|
|
37
|
+
{ code: "MA", name: "Massachusetts" },
|
|
38
|
+
{ code: "MI", name: "Michigan" },
|
|
39
|
+
{ code: "MN", name: "Minnesota" },
|
|
40
|
+
{ code: "MS", name: "Mississippi" },
|
|
41
|
+
{ code: "MO", name: "Missouri" },
|
|
42
|
+
{ code: "MT", name: "Montana" },
|
|
43
|
+
{ code: "NE", name: "Nebraska" },
|
|
44
|
+
{ code: "NV", name: "Nevada" },
|
|
45
|
+
{ code: "NH", name: "New Hampshire" },
|
|
46
|
+
{ code: "NJ", name: "New Jersey" },
|
|
47
|
+
{ code: "NM", name: "New Mexico" },
|
|
48
|
+
{ code: "NY", name: "New York" },
|
|
49
|
+
{ code: "NC", name: "North Carolina" },
|
|
50
|
+
{ code: "ND", name: "North Dakota" },
|
|
51
|
+
{ code: "OH", name: "Ohio" },
|
|
52
|
+
{ code: "OK", name: "Oklahoma" },
|
|
53
|
+
{ code: "OR", name: "Oregon" },
|
|
54
|
+
{ code: "PA", name: "Pennsylvania" },
|
|
55
|
+
{ code: "RI", name: "Rhode Island" },
|
|
56
|
+
{ code: "SC", name: "South Carolina" },
|
|
57
|
+
{ code: "SD", name: "South Dakota" },
|
|
58
|
+
{ code: "TN", name: "Tennessee" },
|
|
59
|
+
{ code: "TX", name: "Texas" },
|
|
60
|
+
{ code: "UT", name: "Utah" },
|
|
61
|
+
{ code: "VT", name: "Vermont" },
|
|
62
|
+
{ code: "VA", name: "Virginia" },
|
|
63
|
+
{ code: "WA", name: "Washington" },
|
|
64
|
+
{ code: "WV", name: "West Virginia" },
|
|
65
|
+
{ code: "WI", name: "Wisconsin" },
|
|
66
|
+
{ code: "WY", name: "Wyoming" },
|
|
67
|
+
];
|
|
68
|
+
|
|
69
|
+
const US_STATE_CODE_SET: ReadonlySet<string> = new Set(US_STATE_OPTIONS.map((s) => s.code));
|
|
70
|
+
|
|
71
|
+
const DIRECTORY_TAG_PATTERN = /^(?:directory|state-directory|state):\s*(.+)$/i;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Normalize a US state or DC code to its canonical 2-letter uppercase form.
|
|
75
|
+
* Accepts optional leading/trailing whitespace and an optional `US-` prefix.
|
|
76
|
+
* Returns `null` for nullish, empty, or unrecognized values.
|
|
77
|
+
*/
|
|
78
|
+
export function normalizeUsStateCode(raw: string | null | undefined): string | null {
|
|
79
|
+
if (typeof raw !== "string") return null;
|
|
80
|
+
const trimmed = raw.trim().toUpperCase();
|
|
81
|
+
if (!trimmed) return null;
|
|
82
|
+
const code = trimmed.startsWith("US-") ? trimmed.slice(3) : trimmed;
|
|
83
|
+
return US_STATE_CODE_SET.has(code) ? code : null;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Normalize a list of raw state codes into a deduplicated set of valid US state/DC codes. */
|
|
87
|
+
function normalizeStateSet(codes: readonly string[] | null | undefined): Set<string> {
|
|
88
|
+
const out = new Set<string>();
|
|
89
|
+
if (!codes) return out;
|
|
90
|
+
for (const raw of codes) {
|
|
91
|
+
const code = normalizeUsStateCode(raw);
|
|
92
|
+
if (code) out.add(code);
|
|
93
|
+
}
|
|
94
|
+
return out;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Extract valid 2-letter US state codes declared via product directory tags
|
|
99
|
+
* (`directory:<ST>`, `state-directory:<ST>`, or `state:<ST>`).
|
|
100
|
+
*/
|
|
101
|
+
export function extractDirectoryStatesFromTags(
|
|
102
|
+
tags: readonly string[] | null | undefined,
|
|
103
|
+
): string[] {
|
|
104
|
+
if (!tags || tags.length === 0) return [];
|
|
105
|
+
const seen = new Set<string>();
|
|
106
|
+
const out: string[] = [];
|
|
107
|
+
for (const tag of tags) {
|
|
108
|
+
if (typeof tag !== "string") continue;
|
|
109
|
+
const match = DIRECTORY_TAG_PATTERN.exec(tag.trim());
|
|
110
|
+
if (!match) continue;
|
|
111
|
+
const code = normalizeUsStateCode(match[1]);
|
|
112
|
+
if (code && !seen.has(code)) {
|
|
113
|
+
seen.add(code);
|
|
114
|
+
out.push(code);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return out;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Store-level state eligibility & per-state directory allow-list configuration. */
|
|
121
|
+
export interface StateDirectoryRule {
|
|
122
|
+
/**
|
|
123
|
+
* States that maintain a regulated product directory / allow-list.
|
|
124
|
+
* Shipping to a state in this list fails closed unless the product explicitly
|
|
125
|
+
* allows that state (via `allowedStates` or a matching directory tag).
|
|
126
|
+
*/
|
|
127
|
+
directoryStates?: readonly string[];
|
|
128
|
+
/** States where sale/shipment is prohibited store-wide. */
|
|
129
|
+
restrictedStates?: readonly string[];
|
|
130
|
+
/** Store-wide allow-list — when non-empty, states outside this list are restricted. */
|
|
131
|
+
eligibleStates?: readonly string[];
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** Product-level state eligibility inputs. */
|
|
135
|
+
export interface ProductStateEligibilityInput {
|
|
136
|
+
/** Explicit per-product ship-to allow-list (when non-empty, only listed states are eligible). */
|
|
137
|
+
allowedStates?: readonly string[] | null;
|
|
138
|
+
/** Explicit per-product no-ship list. */
|
|
139
|
+
restrictedStates?: readonly string[] | null;
|
|
140
|
+
/** Product tags; `directory:<ST>`, `state-directory:<ST>`, and `state:<ST>` grant directory eligibility. */
|
|
141
|
+
tags?: readonly string[] | null;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export type ProductStateEligibilityReason =
|
|
145
|
+
| "no-state-selected"
|
|
146
|
+
| "eligible"
|
|
147
|
+
| "state-restricted"
|
|
148
|
+
| "not-on-state-directory";
|
|
149
|
+
|
|
150
|
+
export interface ProductStateEligibilityDecision {
|
|
151
|
+
eligible: boolean;
|
|
152
|
+
reason: ProductStateEligibilityReason;
|
|
153
|
+
stateCode: string | null;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Evaluate whether a product may be sold/shipped to `stateCode` under `config`.
|
|
158
|
+
*
|
|
159
|
+
* Precedence:
|
|
160
|
+
* 1. Unset/invalid `stateCode` → `{ eligible: true, reason: "no-state-selected" }`.
|
|
161
|
+
* 2. `config.restrictedStates`, `product.restrictedStates`, or outside non-empty
|
|
162
|
+
* `config.eligibleStates` → `{ eligible: false, reason: "state-restricted" }`.
|
|
163
|
+
* 3. `config.directoryStates` includes `stateCode` → product MUST explicitly allow
|
|
164
|
+
* `stateCode` (via `product.allowedStates` or a directory tag for `stateCode`),
|
|
165
|
+
* otherwise fail closed with `{ eligible: false, reason: "not-on-state-directory" }`.
|
|
166
|
+
* 4. Non-empty `product.allowedStates` omitting `stateCode` →
|
|
167
|
+
* `{ eligible: false, reason: "not-on-state-directory" }`.
|
|
168
|
+
* 5. Otherwise → `{ eligible: true, reason: "eligible" }`.
|
|
169
|
+
*/
|
|
170
|
+
export function evaluateProductStateEligibility(
|
|
171
|
+
product: ProductStateEligibilityInput,
|
|
172
|
+
stateCode: string | null | undefined,
|
|
173
|
+
config?: StateDirectoryRule | null,
|
|
174
|
+
): ProductStateEligibilityDecision {
|
|
175
|
+
const normalized = normalizeUsStateCode(stateCode);
|
|
176
|
+
if (!normalized) {
|
|
177
|
+
return { eligible: true, reason: "no-state-selected", stateCode: null };
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const configRestricted = normalizeStateSet(config?.restrictedStates);
|
|
181
|
+
const productRestricted = normalizeStateSet(product.restrictedStates);
|
|
182
|
+
if (configRestricted.has(normalized) || productRestricted.has(normalized)) {
|
|
183
|
+
return { eligible: false, reason: "state-restricted", stateCode: normalized };
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const configEligible = normalizeStateSet(config?.eligibleStates);
|
|
187
|
+
if (configEligible.size > 0 && !configEligible.has(normalized)) {
|
|
188
|
+
return { eligible: false, reason: "state-restricted", stateCode: normalized };
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const productAllowed = normalizeStateSet(product.allowedStates);
|
|
192
|
+
const directoryStates = normalizeStateSet(config?.directoryStates);
|
|
193
|
+
if (directoryStates.has(normalized)) {
|
|
194
|
+
const tagDirectoryStates = new Set(extractDirectoryStatesFromTags(product.tags));
|
|
195
|
+
if (!productAllowed.has(normalized) && !tagDirectoryStates.has(normalized)) {
|
|
196
|
+
return { eligible: false, reason: "not-on-state-directory", stateCode: normalized };
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
if (productAllowed.size > 0 && !productAllowed.has(normalized)) {
|
|
201
|
+
return { eligible: false, reason: "not-on-state-directory", stateCode: normalized };
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
return { eligible: true, reason: "eligible", stateCode: normalized };
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export interface CartRevalidationLine {
|
|
208
|
+
sku?: string;
|
|
209
|
+
code?: string;
|
|
210
|
+
handle?: string;
|
|
211
|
+
title?: string;
|
|
212
|
+
name?: string;
|
|
213
|
+
quantity?: number;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
export interface RemovedCartLine<TLine extends CartRevalidationLine = CartRevalidationLine> {
|
|
217
|
+
line: TLine;
|
|
218
|
+
reason: Exclude<ProductStateEligibilityReason, "eligible" | "no-state-selected">;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export interface CartStateRevalidationResult<TLine extends CartRevalidationLine = CartRevalidationLine> {
|
|
222
|
+
stateCode: string | null;
|
|
223
|
+
kept: TLine[];
|
|
224
|
+
removed: RemovedCartLine<TLine>[];
|
|
225
|
+
/** Clear shopper-facing summary when one or more items were removed; `null` otherwise. */
|
|
226
|
+
notice: string | null;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function lineDisplayName(line: CartRevalidationLine): string | null {
|
|
230
|
+
const candidate = (line.title ?? line.name ?? line.sku ?? line.code ?? line.handle ?? "").trim();
|
|
231
|
+
return candidate.length > 0 ? candidate : null;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function formatRemovalNotice<TLine extends CartRevalidationLine>(
|
|
235
|
+
removed: readonly RemovedCartLine<TLine>[],
|
|
236
|
+
stateCode: string,
|
|
237
|
+
): string | null {
|
|
238
|
+
if (removed.length === 0) return null;
|
|
239
|
+
const names = removed
|
|
240
|
+
.map((entry) => lineDisplayName(entry.line))
|
|
241
|
+
.filter((n): n is string => n !== null);
|
|
242
|
+
const MAX_NAMED = 3;
|
|
243
|
+
const summaryNames =
|
|
244
|
+
names.length === 0
|
|
245
|
+
? ""
|
|
246
|
+
: names.length <= MAX_NAMED
|
|
247
|
+
? ` (${names.join(", ")})`
|
|
248
|
+
: ` (${names.slice(0, MAX_NAMED).join(", ")}, and ${names.length - MAX_NAMED} more)`;
|
|
249
|
+
|
|
250
|
+
if (removed.length === 1) {
|
|
251
|
+
return `1 item${summaryNames} was removed from your cart because it cannot be shipped to ${stateCode}.`;
|
|
252
|
+
}
|
|
253
|
+
return `${removed.length} items${summaryNames} were removed from your cart because they cannot be shipped to ${stateCode}.`;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Revalidate every cart line against `stateCode`, returning the kept lines,
|
|
258
|
+
* removed lines (with per-line reasons), and a shopper-facing notice string
|
|
259
|
+
* when any line is removed.
|
|
260
|
+
*/
|
|
261
|
+
export function revalidateCartForState<TLine extends CartRevalidationLine>(
|
|
262
|
+
lines: readonly TLine[],
|
|
263
|
+
stateCode: string | null | undefined,
|
|
264
|
+
resolveProduct: (line: TLine) => ProductStateEligibilityInput | null | undefined,
|
|
265
|
+
config?: StateDirectoryRule | null,
|
|
266
|
+
): CartStateRevalidationResult<TLine> {
|
|
267
|
+
const normalized = normalizeUsStateCode(stateCode);
|
|
268
|
+
if (!normalized) {
|
|
269
|
+
return {
|
|
270
|
+
stateCode: null,
|
|
271
|
+
kept: [...lines],
|
|
272
|
+
removed: [],
|
|
273
|
+
notice: null,
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
const kept: TLine[] = [];
|
|
278
|
+
const removed: RemovedCartLine<TLine>[] = [];
|
|
279
|
+
|
|
280
|
+
for (const line of lines) {
|
|
281
|
+
const product = resolveProduct(line) ?? {};
|
|
282
|
+
const decision = evaluateProductStateEligibility(product, normalized, config);
|
|
283
|
+
if (decision.eligible) {
|
|
284
|
+
kept.push(line);
|
|
285
|
+
} else if (decision.reason === "state-restricted" || decision.reason === "not-on-state-directory") {
|
|
286
|
+
removed.push({ line, reason: decision.reason });
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
return {
|
|
291
|
+
stateCode: normalized,
|
|
292
|
+
kept,
|
|
293
|
+
removed,
|
|
294
|
+
notice: formatRemovalNotice(removed, normalized),
|
|
295
|
+
};
|
|
296
|
+
}
|
|
@@ -252,6 +252,8 @@ export interface StateEligibilityConfig {
|
|
|
252
252
|
restrictedStates?: string[];
|
|
253
253
|
/** Allowlist — sale/shipment available ONLY in these. */
|
|
254
254
|
eligibleStates?: string[];
|
|
255
|
+
/** States requiring products to appear on that state's directory/allow-list (fail-closed). */
|
|
256
|
+
directoryStates?: string[];
|
|
255
257
|
/** Custom display message; when omitted a sentence is composed from the lists. */
|
|
256
258
|
note?: string;
|
|
257
259
|
}
|