@eldrajs/theme-cli 0.2.4 → 0.2.6
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 +4 -0
- package/README.md +6 -0
- package/dist/cli.js +8 -2
- package/dist/index.d.ts +6 -0
- package/dist/index.js +1 -1
- package/dist/{validate-FjQ-5_ne.js → validate-CC6Mp5i_.js} +2 -1
- package/package.json +3 -3
- package/template/.eldra/block-types.d.ts +1 -1
- package/template/.eldra/manifest.json +30 -2
- package/template/.eldra/previews.json +35 -35
- package/template/README.md +3 -1
- package/template/app/app.vue +16 -3
- package/template/app/assets/main.css +13 -15
- package/template/app/components/ui/UiImage.vue +29 -17
- package/template/app/components/ui/__tests__/UiImage.spec.ts +24 -15
- package/template/app/composables/useStorefront.ts +7 -2
- package/template/app/pages/[...slug].vue +1 -1
- package/template/app/plugins/eldra-storefront.ts +9 -0
- package/template/app/storefront/analytics.ts +15 -7
- package/template/app/storefront/cart.ts +228 -51
- package/template/app/storefront/commerce.ts +8 -2
- package/template/app/storefront/currency.ts +205 -0
- package/template/app/storefront/demo.ts +10 -2
- package/template/app/storefront/feedback.ts +2 -0
- package/template/app/storefront/gateway.ts +153 -35
- package/template/app/storefront/money.ts +6 -18
- package/template/app/storefront/structuredData.ts +16 -5
- package/template/app/storefront/toProductCard.ts +14 -14
- package/template/app/storefront/types.ts +13 -0
- package/template/app/storefront/volatile.ts +4 -0
- package/template/app/utils/seoInput.ts +4 -3
- package/template/blocks/announcement-bar/Block.vue +3 -2
- package/template/blocks/announcement-bar/__tests__/Block.spec.ts +1 -1
- package/template/blocks/announcement-bar/preview.png +0 -0
- package/template/blocks/article/Block.vue +3 -4
- package/template/blocks/article/preview.png +0 -0
- package/template/blocks/article-list/Block.vue +18 -9
- package/template/blocks/article-list/preview.png +0 -0
- package/template/blocks/breadcrumbs/Block.vue +1 -1
- package/template/blocks/breadcrumbs/__tests__/Block.spec.ts +9 -9
- package/template/blocks/breadcrumbs/preview.png +0 -0
- package/template/blocks/cart/Block.vue +42 -23
- package/template/blocks/cart/__tests__/Block.spec.ts +2 -3
- package/template/blocks/cart/__tests__/loadFailed.spec.ts +135 -0
- package/template/blocks/cart/__tests__/notSold.spec.ts +109 -0
- package/template/blocks/cart/parts/CartLines.vue +16 -3
- package/template/blocks/cart/parts/CheckoutButton.vue +45 -0
- package/template/blocks/cart/parts/LoadFailed.vue +37 -0
- package/template/blocks/cart/parts/NotSoldNotice.vue +69 -0
- package/template/blocks/cart/parts/ShippingBar.vue +1 -1
- package/template/blocks/cart/parts/Summary.vue +14 -30
- package/template/blocks/cart/parts/notSold.ts +19 -0
- package/template/blocks/cart/preview.png +0 -0
- package/template/blocks/collection-grid/Block.vue +26 -16
- package/template/blocks/collection-grid/__tests__/Block.spec.ts +26 -14
- package/template/blocks/collection-grid/preview.png +0 -0
- package/template/blocks/collection-header/Block.vue +5 -5
- package/template/blocks/collection-header/preview.png +0 -0
- package/template/blocks/contact/Block.vue +1 -1
- package/template/blocks/contact/__tests__/Block.spec.ts +2 -2
- package/template/blocks/contact/preview.png +0 -0
- package/template/blocks/cta/Block.vue +16 -18
- package/template/blocks/cta/__tests__/Block.spec.ts +8 -12
- package/template/blocks/cta/preview.png +0 -0
- package/template/blocks/faq/Block.vue +3 -13
- package/template/blocks/faq/__tests__/Block.spec.ts +2 -2
- package/template/blocks/faq/preview.png +0 -0
- package/template/blocks/feature-grid/Block.vue +1 -1
- package/template/blocks/feature-grid/__tests__/Block.spec.ts +1 -1
- package/template/blocks/feature-grid/preview.png +0 -0
- package/template/blocks/footer/Block.vue +33 -55
- package/template/blocks/footer/__tests__/Block.spec.ts +5 -5
- package/template/blocks/footer/preview.png +0 -0
- package/template/blocks/form/Block.vue +1 -1
- package/template/blocks/form/preview.png +0 -0
- package/template/blocks/gallery/__tests__/Block.spec.ts +1 -1
- package/template/blocks/gallery/preview.png +0 -0
- package/template/blocks/hero/Block.vue +8 -11
- package/template/blocks/hero/__tests__/Block.spec.ts +9 -7
- package/template/blocks/hero/preview.png +0 -0
- package/template/blocks/image/Block.vue +4 -4
- package/template/blocks/image/__tests__/Block.spec.ts +5 -6
- package/template/blocks/image/preview.png +0 -0
- package/template/blocks/logo-cloud/__tests__/Block.spec.ts +1 -1
- package/template/blocks/logo-cloud/preview.png +0 -0
- package/template/blocks/navigation/Block.vue +55 -29
- package/template/blocks/navigation/__tests__/Block.spec.ts +22 -20
- package/template/blocks/navigation/preview.png +0 -0
- package/template/blocks/newsletter/__tests__/Block.spec.ts +1 -1
- package/template/blocks/newsletter/preview.png +0 -0
- package/template/blocks/order-status/Block.vue +23 -15
- package/template/blocks/order-status/__tests__/Block.spec.ts +1 -2
- package/template/blocks/order-status/preview.png +0 -0
- package/template/blocks/pricing-table/__tests__/Block.spec.ts +1 -1
- package/template/blocks/pricing-table/preview.png +0 -0
- package/template/blocks/product-carousel/Block.vue +11 -8
- package/template/blocks/product-carousel/__tests__/Block.spec.ts +26 -8
- package/template/blocks/product-carousel/preview.png +0 -0
- package/template/blocks/product-detail/Block.vue +54 -18
- package/template/blocks/product-detail/__tests__/Block.spec.ts +79 -2
- package/template/blocks/product-detail/parts/Gallery.vue +1 -1
- package/template/blocks/product-detail/preview.png +0 -0
- package/template/blocks/quote/Block.vue +1 -1
- package/template/blocks/quote/__tests__/Block.spec.ts +7 -4
- package/template/blocks/quote/preview.png +0 -0
- package/template/blocks/rich-text/preview.png +0 -0
- package/template/blocks/search/Block.vue +10 -4
- package/template/blocks/search/PopularChips.vue +1 -1
- package/template/blocks/search/TypeSection.vue +8 -2
- package/template/blocks/search/preview.png +0 -0
- package/template/blocks/search/results.ts +5 -2
- package/template/blocks/split-content/Block.vue +2 -3
- package/template/blocks/split-content/__tests__/Block.spec.ts +2 -2
- package/template/blocks/split-content/preview.png +0 -0
- package/template/blocks/stats/__tests__/Block.spec.ts +1 -1
- package/template/blocks/stats/preview.png +0 -0
- package/template/blocks/tabs/Block.vue +4 -5
- package/template/blocks/tabs/__tests__/Block.spec.ts +2 -2
- package/template/blocks/tabs/preview.png +0 -0
- package/template/blocks/team/preview.png +0 -0
- package/template/blocks/testimonials/Block.vue +10 -9
- package/template/blocks/testimonials/__tests__/Block.spec.ts +1 -1
- package/template/blocks/testimonials/preview.png +0 -0
- package/template/blocks/timeline/__tests__/Block.spec.ts +2 -2
- package/template/blocks/timeline/preview.png +0 -0
- package/template/blocks/trust-strip/Block.vue +1 -1
- package/template/blocks/trust-strip/__tests__/Block.spec.ts +1 -1
- package/template/blocks/trust-strip/preview.png +0 -0
- package/template/blocks/video-embed/Block.vue +3 -3
- package/template/blocks/video-embed/__tests__/Block.spec.ts +1 -1
- package/template/blocks/video-embed/preview.png +0 -0
- package/template/blocks/wishlist/Block.vue +6 -2
- package/template/blocks/wishlist/preview.png +0 -0
- package/template/i18n/en-US.json +19 -3
- package/template/i18n/is-IS.json +19 -3
- package/template/nuxt.config.ts +5 -4
- package/template/package.json +1 -0
- package/template/stories/pages/article.stories.ts +2 -3
- package/template/stories/pages/cart.stories.ts +2 -3
- package/template/stories/pages/category.stories.ts +2 -3
- package/template/stories/pages/collection.stories.ts +2 -3
- package/template/stories/pages/home.stories.ts +2 -3
- package/template/stories/pages/product.stories.ts +3 -4
- package/template/stories/pages/products.stories.ts +2 -3
- package/template/stories/pages/search.stories.ts +2 -3
- package/template/stories/pages/wishlist.stories.ts +2 -3
- package/template/test/carouselKeyboard.spec.ts +2 -2
- package/template/test/carouselRing.browser.spec.ts +2 -2
- package/template/test/carouselSlideWidth.browser.spec.ts +9 -9
- package/template/test/currencyPicker.spec.ts +66 -0
- package/template/test/eldraIcon.spec.ts +1 -1
- package/template/test/mainCss.spec.ts +7 -0
- package/template/test/pages/article.spec.ts +5 -5
- package/template/test/pages/collection.spec.ts +5 -5
- package/template/test/pages/hydration.spec.ts +10 -10
- package/template/test/prerenderRefresh.browser.spec.ts +1 -1
- package/template/test/seoInput.spec.ts +22 -0
- package/template/test/storefront/analytics.spec.ts +15 -0
- package/template/test/storefront/cart.spec.ts +4 -4
- package/template/test/storefront/currency.spec.ts +549 -0
- package/template/test/storefront/currencyStore.spec.ts +845 -0
- package/template/test/storefront/gateway.spec.ts +22 -0
- package/template/test/storefront/money.spec.ts +2 -8
- package/template/test/storefront/structuredData.spec.ts +45 -0
- package/template/test/storefront/toProductCard.spec.ts +1 -1
- package/template/test/storefront/volatile.spec.ts +11 -0
- package/template/test/support/mountPage.ts +2 -3
|
@@ -77,7 +77,7 @@ describe('UiImage', () => {
|
|
|
77
77
|
},
|
|
78
78
|
});
|
|
79
79
|
const img = wrapper.get('[data-part="media"]');
|
|
80
|
-
expect(img.classes()).toContain('object-cover');
|
|
80
|
+
expect(img.classes()).toContain('eldra:object-cover');
|
|
81
81
|
expect(img.attributes('style')).toContain('object-position: 25% 75%');
|
|
82
82
|
expect(img.attributes('data-eldra-framing')).toBeUndefined();
|
|
83
83
|
});
|
|
@@ -134,13 +134,13 @@ describe('UiImage', () => {
|
|
|
134
134
|
it('applies no radius class by default', () => {
|
|
135
135
|
const wrapper = mount(UiImage, { props: { src: '/demo/hero.svg', alt: 'Hero' } });
|
|
136
136
|
const frame = wrapper.get('[data-part="frame"]');
|
|
137
|
-
expect(frame.classes()).not.toContain('rounded-lg');
|
|
138
|
-
expect(frame.classes()).not.toContain('rounded-xl');
|
|
137
|
+
expect(frame.classes()).not.toContain('eldra:rounded-lg');
|
|
138
|
+
expect(frame.classes()).not.toContain('eldra:rounded-xl');
|
|
139
139
|
});
|
|
140
140
|
|
|
141
141
|
it.each(['lg', 'xl'] as const)('forwards rounded="%s" to the frame', (rounded) => {
|
|
142
142
|
const wrapper = mount(UiImage, { props: { src: '/demo/hero.svg', alt: 'Hero', rounded } });
|
|
143
|
-
expect(wrapper.get('[data-part="frame"]').classes()).toContain(`rounded-${rounded}`);
|
|
143
|
+
expect(wrapper.get('[data-part="frame"]').classes()).toContain(`eldra:rounded-${rounded}`);
|
|
144
144
|
});
|
|
145
145
|
});
|
|
146
146
|
|
|
@@ -158,7 +158,7 @@ describe('UiImage', () => {
|
|
|
158
158
|
const frame = wrapper.get('[data-part="frame"]');
|
|
159
159
|
expect(frame.classes()).toEqual(expect.arrayContaining(['h-full', 'w-full']));
|
|
160
160
|
const media = wrapper.get('[data-part="media"]');
|
|
161
|
-
expect(media.classes()).toContain('object-cover');
|
|
161
|
+
expect(media.classes()).toContain('eldra:object-cover');
|
|
162
162
|
});
|
|
163
163
|
|
|
164
164
|
it('resolves to ratio="auto" (no forced aspect-ratio) while filling', () => {
|
|
@@ -183,11 +183,13 @@ describe('UiImage', () => {
|
|
|
183
183
|
it('defaults to cover: the frame stays w-full, the media stays h-full w-full object-cover — unchanged', () => {
|
|
184
184
|
const wrapper = mount(UiImage, { props: { src: '/demo/hero.svg', alt: 'Hero' } });
|
|
185
185
|
const frame = wrapper.get('[data-part="frame"]');
|
|
186
|
-
expect(frame.classes()).toContain('w-full');
|
|
186
|
+
expect(frame.classes()).toContain('eldra:w-full');
|
|
187
187
|
expect(frame.classes()).not.toContain('w-auto');
|
|
188
188
|
const media = wrapper.get('[data-part="media"]');
|
|
189
|
-
expect(media.classes()).toEqual(
|
|
190
|
-
|
|
189
|
+
expect(media.classes()).toEqual(
|
|
190
|
+
expect.arrayContaining(['eldra:h-full', 'eldra:w-full', 'eldra:object-cover'])
|
|
191
|
+
);
|
|
192
|
+
expect(media.classes()).not.toContain('eldra:object-contain');
|
|
191
193
|
});
|
|
192
194
|
|
|
193
195
|
/**
|
|
@@ -208,7 +210,6 @@ describe('UiImage', () => {
|
|
|
208
210
|
});
|
|
209
211
|
const frame = wrapper.get('[data-part="frame"]');
|
|
210
212
|
expect(frame.classes()).toEqual(expect.arrayContaining(['w-auto', 'max-w-full']));
|
|
211
|
-
expect(frame.classes()).not.toContain('w-full');
|
|
212
213
|
const media = wrapper.get('[data-part="media"]');
|
|
213
214
|
expect(media.classes()).toEqual(
|
|
214
215
|
expect.arrayContaining([
|
|
@@ -219,9 +220,6 @@ describe('UiImage', () => {
|
|
|
219
220
|
'max-h-[inherit]',
|
|
220
221
|
])
|
|
221
222
|
);
|
|
222
|
-
expect(media.classes()).not.toContain('object-cover');
|
|
223
|
-
expect(media.classes()).not.toContain('h-full');
|
|
224
|
-
expect(media.classes()).not.toContain('w-full');
|
|
225
223
|
});
|
|
226
224
|
|
|
227
225
|
it("composes with a caller-supplied classes.frame height cap (the lightbox's max-h-[85vh])", () => {
|
|
@@ -254,7 +252,7 @@ describe('UiImage', () => {
|
|
|
254
252
|
});
|
|
255
253
|
const frame = wrapper.get('[data-part="frame"]');
|
|
256
254
|
expect(frame.classes()).toContain('max-h-[85vh]');
|
|
257
|
-
expect(frame.classes()).toContain('rounded-lg');
|
|
255
|
+
expect(frame.classes()).toContain('eldra:rounded-lg');
|
|
258
256
|
});
|
|
259
257
|
|
|
260
258
|
it("lets the caller's own classes.frame win over fill's forced h-full w-full", () => {
|
|
@@ -282,11 +280,22 @@ describe('UiImage', () => {
|
|
|
282
280
|
expect(wrapper.get('[data-part="media"]').classes()).toContain('object-cover');
|
|
283
281
|
});
|
|
284
282
|
|
|
285
|
-
it('passes
|
|
283
|
+
it('passes class through to the root untouched when fill is not set', () => {
|
|
286
284
|
const wrapper = mount(UiImage, {
|
|
287
|
-
props: { src: '/demo/hero.svg', alt: 'Hero'
|
|
285
|
+
props: { src: '/demo/hero.svg', alt: 'Hero' },
|
|
286
|
+
attrs: { class: 'custom-root' },
|
|
288
287
|
});
|
|
289
288
|
expect(wrapper.get('[data-part="root"]').classes()).toContain('custom-root');
|
|
290
289
|
});
|
|
290
|
+
|
|
291
|
+
it("lets the caller's own class win over fill's root classes", () => {
|
|
292
|
+
const wrapper = mount(UiImage, {
|
|
293
|
+
props: { src: '/demo/hero.svg', alt: 'Hero', fill: true },
|
|
294
|
+
attrs: { class: 'relative' },
|
|
295
|
+
});
|
|
296
|
+
const classes = wrapper.get('[data-part="root"]').classes();
|
|
297
|
+
expect(classes).toContain('relative');
|
|
298
|
+
expect(classes).not.toContain('absolute');
|
|
299
|
+
});
|
|
291
300
|
});
|
|
292
301
|
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { inject } from 'vue';
|
|
1
|
+
import { inject, onScopeDispose } from 'vue';
|
|
2
2
|
import { STOREFRONT_KEY, type StorefrontSource } from '../storefront/types';
|
|
3
3
|
import { createDemoStorefront } from '../storefront/demo';
|
|
4
4
|
|
|
@@ -14,5 +14,10 @@ import { createDemoStorefront } from '../storefront/demo';
|
|
|
14
14
|
* nothing was provided.
|
|
15
15
|
*/
|
|
16
16
|
export function useStorefront(): StorefrontSource {
|
|
17
|
-
|
|
17
|
+
const provided = inject(STOREFRONT_KEY, null);
|
|
18
|
+
if (provided !== null) return provided;
|
|
19
|
+
// A storefront made for this component alone (Storybook, a demo) goes with it.
|
|
20
|
+
const own = createDemoStorefront();
|
|
21
|
+
onScopeDispose(() => own.currency.dispose());
|
|
22
|
+
return own;
|
|
18
23
|
}
|
|
@@ -137,7 +137,7 @@ const structure = computed(() =>
|
|
|
137
137
|
spacing="lg"
|
|
138
138
|
labelled-by="not-found-title"
|
|
139
139
|
data-eldra-not-found
|
|
140
|
-
|
|
140
|
+
class="text-center"
|
|
141
141
|
>
|
|
142
142
|
<Container width="narrow">
|
|
143
143
|
<p class="text-muted text-sm font-semibold tracking-widest uppercase">
|
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
type VolatileSnapshot,
|
|
9
9
|
} from '../storefront/types';
|
|
10
10
|
import { toStorefrontCommerce } from '../storefront/commerce';
|
|
11
|
+
import { fetchVisitorCountry } from '../storefront/currency';
|
|
11
12
|
import {
|
|
12
13
|
createGatewayStorefront,
|
|
13
14
|
type StorefrontPrerenderHandle,
|
|
@@ -300,6 +301,14 @@ export default defineNuxtPlugin({
|
|
|
300
301
|
runtime,
|
|
301
302
|
});
|
|
302
303
|
|
|
304
|
+
// After mount, so the hydrated page is the prerendered one; only then may the visitor's currency differ.
|
|
305
|
+
if (import.meta.client) {
|
|
306
|
+
const currency = source.currency;
|
|
307
|
+
const start = (): void => void currency.start(() => fetchVisitorCountry());
|
|
308
|
+
if (mounted) start();
|
|
309
|
+
else afterMount.push(start);
|
|
310
|
+
}
|
|
311
|
+
|
|
303
312
|
// Commerce events (`app/storefront/analytics.ts`) go through the tracker `@eldrajs/theme-nuxt`
|
|
304
313
|
// loads on a deployed site; everywhere else (dev, preview, no organisation, opted out)
|
|
305
314
|
// `enabled` is false and the source is provided untouched. The unwrapped `source` stays the one
|
|
@@ -56,18 +56,20 @@ export function cartIdFromCheckoutUrl(url: string | null): string | undefined {
|
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
export function withAnalytics(source: StorefrontSource, track: Track): StorefrontSource {
|
|
59
|
-
const currency = (): string | undefined =>
|
|
59
|
+
const currency = (): string | undefined =>
|
|
60
|
+
source.cart.currency.value ?? source.commerce?.currency;
|
|
60
61
|
|
|
61
62
|
function lineProps(
|
|
62
63
|
line: Pick<StorefrontCartLine, 'productId' | 'variantId' | 'unitPrice'>,
|
|
63
|
-
quantity: number
|
|
64
|
+
quantity: number,
|
|
65
|
+
pricedIn: string | undefined = currency()
|
|
64
66
|
): Record<string, string | number | undefined> {
|
|
65
67
|
return {
|
|
66
68
|
product_id: line.productId,
|
|
67
69
|
variant_id: line.variantId,
|
|
68
70
|
quantity,
|
|
69
71
|
value: formatValue(line.unitPrice * quantity),
|
|
70
|
-
currency:
|
|
72
|
+
currency: pricedIn,
|
|
71
73
|
};
|
|
72
74
|
}
|
|
73
75
|
|
|
@@ -98,7 +100,7 @@ export function withAnalytics(source: StorefrontSource, track: Track): Storefron
|
|
|
98
100
|
handle: product.handle,
|
|
99
101
|
variant_id: product.variantId,
|
|
100
102
|
value: formatValue(product.price.amount),
|
|
101
|
-
currency: currency
|
|
103
|
+
currency: product.price.currency ?? source.commerce?.currency,
|
|
102
104
|
});
|
|
103
105
|
},
|
|
104
106
|
{ immediate: true }
|
|
@@ -126,21 +128,26 @@ export function withAnalytics(source: StorefrontSource, track: Track): Storefron
|
|
|
126
128
|
},
|
|
127
129
|
async remove(lineId) {
|
|
128
130
|
const line = source.cart.lines.value.find((l) => l.id === lineId);
|
|
131
|
+
const pricedIn = currency();
|
|
129
132
|
const failure = await source.cart.remove(lineId);
|
|
130
133
|
if (failure === null && line !== undefined) {
|
|
131
|
-
track('remove_from_cart', lineProps(line, line.quantity));
|
|
134
|
+
track('remove_from_cart', lineProps(line, line.quantity, pricedIn));
|
|
132
135
|
}
|
|
133
136
|
return failure;
|
|
134
137
|
},
|
|
135
138
|
async setQuantity(lineId, quantity) {
|
|
136
139
|
const before = source.cart.lines.value.find((l) => l.id === lineId);
|
|
140
|
+
const pricedIn = currency();
|
|
137
141
|
const failure = await source.cart.setQuantity(lineId, quantity);
|
|
138
142
|
if (failure === null && before !== undefined) {
|
|
139
143
|
// The cart's own answer, not the request: the backend may clamp to stock.
|
|
140
144
|
const after = source.cart.lines.value.find((l) => l.id === lineId);
|
|
141
145
|
const diff = (after?.quantity ?? 0) - before.quantity;
|
|
142
146
|
if (diff !== 0) {
|
|
143
|
-
track(
|
|
147
|
+
track(
|
|
148
|
+
diff > 0 ? 'add_to_cart' : 'remove_from_cart',
|
|
149
|
+
lineProps(before, Math.abs(diff), pricedIn)
|
|
150
|
+
);
|
|
144
151
|
}
|
|
145
152
|
}
|
|
146
153
|
return failure;
|
|
@@ -148,9 +155,10 @@ export function withAnalytics(source: StorefrontSource, track: Track): Storefron
|
|
|
148
155
|
async undoRemove() {
|
|
149
156
|
// Undo puts a line back that `remove` already counted as removed, so it is an add.
|
|
150
157
|
const restored = source.cart.lastRemoved.value?.line;
|
|
158
|
+
const pricedIn = currency();
|
|
151
159
|
const failure = await source.cart.undoRemove();
|
|
152
160
|
if (failure === null && restored !== undefined) {
|
|
153
|
-
track('add_to_cart', lineProps(restored, restored.quantity));
|
|
161
|
+
track('add_to_cart', lineProps(restored, restored.quantity, pricedIn));
|
|
154
162
|
}
|
|
155
163
|
return failure;
|
|
156
164
|
},
|
|
@@ -65,6 +65,20 @@ export interface CartStore {
|
|
|
65
65
|
undoRemove(): Promise<StorefrontFailure | null>;
|
|
66
66
|
applyDiscount(code: string): Promise<StorefrontAck>;
|
|
67
67
|
removeDiscount(code: string): Promise<StorefrontFailure | null>;
|
|
68
|
+
/** The cart's currency, or `undefined` before there is a cart. */
|
|
69
|
+
currency: Ref<string | undefined>;
|
|
70
|
+
/** Lines with no price in the cart's currency. While there are any, `checkoutUrl` is `null`. */
|
|
71
|
+
notSold: ComputedRef<StorefrontCartLine[]>;
|
|
72
|
+
/** Moves the whole cart to `code`. Nothing to do without a cart, or when it is in `code` already. */
|
|
73
|
+
setCurrency(code: string): Promise<StorefrontFailure | null | undefined>;
|
|
74
|
+
/** The last read of the cart failed (a timeout or 5xx); the basket id is kept. */
|
|
75
|
+
loadFailed: Ref<boolean>;
|
|
76
|
+
/** `none`: no cart. `loaded`: the cart is shown. `unloaded`: a cart is remembered but not read yet, or its read failed. */
|
|
77
|
+
loadState: Ref<CartLoadState>;
|
|
78
|
+
/** Reads the cart again, as another tab may have changed it. */
|
|
79
|
+
reload(): Promise<StorefrontFailure | null>;
|
|
80
|
+
/** Removes every line in `notSold`: "Remove and check out". */
|
|
81
|
+
removeNotSold(): Promise<StorefrontFailure | null>;
|
|
68
82
|
checkoutUrl: ComputedRef<string | null>;
|
|
69
83
|
/**
|
|
70
84
|
* The shopper followed the Check out link. Does nothing here — the link itself does the
|
|
@@ -81,6 +95,14 @@ export interface CartStore {
|
|
|
81
95
|
* (`StorefrontProduct.productId`/`variantId`) or from a cart line (`StorefrontCartLine`); a product
|
|
82
96
|
* card carries no variant at all, by design (`app/storefront/types.ts`).
|
|
83
97
|
*/
|
|
98
|
+
export type CartLoadState = 'none' | 'loaded' | 'unloaded';
|
|
99
|
+
|
|
100
|
+
/** A write refused locally: the cart could not be read, so nothing was sent. */
|
|
101
|
+
export const CART_NOT_LOADED = 'CART_NOT_LOADED';
|
|
102
|
+
|
|
103
|
+
/** A cart read that has not answered after this long counts as failed. */
|
|
104
|
+
export const CART_READ_TIMEOUT_MS = 8000;
|
|
105
|
+
|
|
84
106
|
export interface CartAddInput {
|
|
85
107
|
productId: string;
|
|
86
108
|
variantId: string;
|
|
@@ -90,6 +112,8 @@ export interface CartAddInput {
|
|
|
90
112
|
export interface CartSnapshot {
|
|
91
113
|
lines: StorefrontCartLine[];
|
|
92
114
|
totals: StorefrontCartTotals;
|
|
115
|
+
/** The cart's own currency; absent from a backend that does not say. */
|
|
116
|
+
currency?: string;
|
|
93
117
|
}
|
|
94
118
|
|
|
95
119
|
/**
|
|
@@ -106,6 +130,8 @@ export interface CartOps {
|
|
|
106
130
|
remove(lineId: string): Promise<CartSnapshot>;
|
|
107
131
|
applyDiscount(code: string): Promise<{ ack: StorefrontAck; snapshot?: CartSnapshot }>;
|
|
108
132
|
removeDiscount(): Promise<CartSnapshot>;
|
|
133
|
+
/** Reprices the cart in `code`. Absent: the backend cannot change a cart's currency. */
|
|
134
|
+
setCurrency?(code: string): Promise<CartSnapshot>;
|
|
109
135
|
/** Reactive so a cart id assigned after the first `add()` updates the link immediately. */
|
|
110
136
|
checkoutUrl: Ref<string | null>;
|
|
111
137
|
}
|
|
@@ -144,32 +170,183 @@ export function createCartStore(ops: CartOps): CartStore {
|
|
|
144
170
|
index: number;
|
|
145
171
|
} | null>;
|
|
146
172
|
|
|
173
|
+
const currency = ref<string | undefined>(undefined);
|
|
147
174
|
const count = computed(() => lines.value.reduce((sum, line) => sum + line.quantity, 0));
|
|
148
|
-
const
|
|
175
|
+
const notSold = computed(() => lines.value.filter((line) => line.soldInCurrency === false));
|
|
176
|
+
const checkoutUrl = computed(() => (notSold.value.length > 0 ? null : ops.checkoutUrl.value));
|
|
177
|
+
|
|
178
|
+
// Every write runs through one queue, in the shopper's order, one request at a time, so the last
|
|
179
|
+
// answer applied is always the newest server state.
|
|
180
|
+
let tail: Promise<unknown> = Promise.resolve();
|
|
181
|
+
let queuedWrites = 0;
|
|
182
|
+
let reads = 0;
|
|
183
|
+
let writeEpoch = 0;
|
|
184
|
+
let reading: Promise<unknown> = Promise.resolve();
|
|
185
|
+
let latestRead = 0;
|
|
186
|
+
const loadFailed = ref(false);
|
|
187
|
+
const loadState = ref<CartLoadState>('unloaded');
|
|
188
|
+
const notLoaded: StorefrontFailure = {
|
|
189
|
+
message: 'The cart could not be read.',
|
|
190
|
+
errorId: CART_NOT_LOADED,
|
|
191
|
+
code: null,
|
|
192
|
+
status: null,
|
|
193
|
+
};
|
|
194
|
+
const settleBusy = (): void => {
|
|
195
|
+
pending.value = queuedWrites > 0 || reads > 0;
|
|
196
|
+
};
|
|
149
197
|
|
|
150
|
-
|
|
198
|
+
function apply(snapshot: CartSnapshot): void {
|
|
199
|
+
lines.value = snapshot.lines;
|
|
200
|
+
totals.value = snapshot.totals;
|
|
201
|
+
currency.value = snapshot.currency;
|
|
202
|
+
loadFailed.value = false;
|
|
203
|
+
loadState.value =
|
|
204
|
+
snapshot.currency !== undefined || snapshot.lines.length > 0 ? 'loaded' : 'none';
|
|
205
|
+
}
|
|
151
206
|
|
|
152
|
-
|
|
153
|
-
|
|
207
|
+
function serial<T>(task: () => Promise<T>, refused: () => T): Promise<T> {
|
|
208
|
+
queuedWrites += 1;
|
|
154
209
|
pending.value = true;
|
|
210
|
+
const next = tail.then(async () => {
|
|
211
|
+
// A write waits for a read in flight, and a failed read is tried once more first; a cart that
|
|
212
|
+
// still cannot be read takes no write, so a write never lands in a cart the page has not shown.
|
|
213
|
+
// The newest read, not only the one current when this write began waiting.
|
|
214
|
+
let awaited: Promise<unknown>;
|
|
215
|
+
do {
|
|
216
|
+
awaited = reading;
|
|
217
|
+
await awaited;
|
|
218
|
+
} while (awaited !== reading);
|
|
219
|
+
if (loadFailed.value) await read();
|
|
220
|
+
if (loadFailed.value) {
|
|
221
|
+
queuedWrites -= 1;
|
|
222
|
+
settleBusy();
|
|
223
|
+
lastFailure.value = notLoaded;
|
|
224
|
+
error.value = notLoaded.message;
|
|
225
|
+
return refused();
|
|
226
|
+
}
|
|
227
|
+
writeEpoch += 1;
|
|
228
|
+
try {
|
|
229
|
+
return await task();
|
|
230
|
+
} finally {
|
|
231
|
+
writeEpoch += 1;
|
|
232
|
+
queuedWrites -= 1;
|
|
233
|
+
settleBusy();
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
tail = next.catch(() => undefined);
|
|
237
|
+
return next;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
async function attempt(task: () => Promise<CartSnapshot>): Promise<StorefrontFailure | null> {
|
|
155
241
|
clearFailure();
|
|
156
242
|
try {
|
|
157
|
-
|
|
158
|
-
lines.value = snapshot.lines;
|
|
159
|
-
totals.value = snapshot.totals;
|
|
243
|
+
apply(await task());
|
|
160
244
|
return null;
|
|
161
245
|
} catch (caught) {
|
|
162
246
|
return fail(caught);
|
|
163
|
-
} finally {
|
|
164
|
-
inFlight -= 1;
|
|
165
|
-
if (inFlight === 0) pending.value = false;
|
|
166
247
|
}
|
|
167
248
|
}
|
|
168
249
|
|
|
250
|
+
const run = (task: () => Promise<CartSnapshot>): Promise<StorefrontFailure | null> =>
|
|
251
|
+
serial(
|
|
252
|
+
() => attempt(task),
|
|
253
|
+
() => notLoaded
|
|
254
|
+
);
|
|
255
|
+
|
|
256
|
+
/** Only the newest read applies, and only when no write started or settled since it was sent. */
|
|
257
|
+
function read(): Promise<StorefrontFailure | null> {
|
|
258
|
+
const issuedAt = writeEpoch;
|
|
259
|
+
latestRead += 1;
|
|
260
|
+
const mine = latestRead;
|
|
261
|
+
reads += 1;
|
|
262
|
+
pending.value = true;
|
|
263
|
+
clearFailure();
|
|
264
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
265
|
+
const timedOut = new Promise<never>((_resolve, reject) => {
|
|
266
|
+
timer = setTimeout(() => reject(new Error('The cart read timed out.')), CART_READ_TIMEOUT_MS);
|
|
267
|
+
});
|
|
268
|
+
const answer = Promise.race([ops.init(), timedOut]).then(
|
|
269
|
+
(snapshot) => {
|
|
270
|
+
if (mine === latestRead && writeEpoch === issuedAt) apply(snapshot);
|
|
271
|
+
return null;
|
|
272
|
+
},
|
|
273
|
+
(caught: unknown) => {
|
|
274
|
+
// A read overtaken by a newer read or by a write says nothing about the cart shown now.
|
|
275
|
+
if (mine !== latestRead || writeEpoch !== issuedAt) return null;
|
|
276
|
+
loadFailed.value = true;
|
|
277
|
+
if (loadState.value !== 'none') loadState.value = 'unloaded';
|
|
278
|
+
return fail(caught);
|
|
279
|
+
}
|
|
280
|
+
);
|
|
281
|
+
const settled = answer.finally(() => {
|
|
282
|
+
clearTimeout(timer);
|
|
283
|
+
reads -= 1;
|
|
284
|
+
settleBusy();
|
|
285
|
+
});
|
|
286
|
+
reading = settled;
|
|
287
|
+
return settled;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// Consecutive currency choices waiting in the queue collapse into the last one; only that one is
|
|
291
|
+
// sent, and the choices it replaced answer `undefined`.
|
|
292
|
+
type MoveAnswer = StorefrontFailure | null | undefined;
|
|
293
|
+
let waitingMove: {
|
|
294
|
+
code: string;
|
|
295
|
+
callers: Array<{ code: string; resolve: (answer: MoveAnswer) => void }>;
|
|
296
|
+
} | null = null;
|
|
297
|
+
|
|
298
|
+
function moveCurrency(code: string): Promise<MoveAnswer> {
|
|
299
|
+
const change = ops.setCurrency;
|
|
300
|
+
// A backend that cannot move a cart still answers in turn, after the read the choice waited for.
|
|
301
|
+
if (change === undefined) {
|
|
302
|
+
return serial(
|
|
303
|
+
async () => null,
|
|
304
|
+
() => notLoaded
|
|
305
|
+
);
|
|
306
|
+
}
|
|
307
|
+
const joining = waitingMove;
|
|
308
|
+
if (joining !== null) {
|
|
309
|
+
joining.code = code;
|
|
310
|
+
return new Promise((resolve) => joining.callers.push({ code, resolve }));
|
|
311
|
+
}
|
|
312
|
+
const move = {
|
|
313
|
+
code,
|
|
314
|
+
callers: [] as Array<{ code: string; resolve: (answer: MoveAnswer) => void }>,
|
|
315
|
+
};
|
|
316
|
+
waitingMove = move;
|
|
317
|
+
const own = new Promise<MoveAnswer>((resolve) => move.callers.push({ code, resolve }));
|
|
318
|
+
void serial(
|
|
319
|
+
async () => {
|
|
320
|
+
waitingMove = null;
|
|
321
|
+
const target = move.code;
|
|
322
|
+
const before = currency.value;
|
|
323
|
+
const failure =
|
|
324
|
+
before === undefined || before === target ? null : await attempt(() => change(target));
|
|
325
|
+
if (failure === null && before !== target && currency.value === target) {
|
|
326
|
+
lastRemoved.value = null;
|
|
327
|
+
}
|
|
328
|
+
for (const caller of move.callers) {
|
|
329
|
+
if (caller.code !== target) caller.resolve(undefined);
|
|
330
|
+
}
|
|
331
|
+
[...move.callers]
|
|
332
|
+
.reverse()
|
|
333
|
+
.find((caller) => caller.code === target)
|
|
334
|
+
?.resolve(failure);
|
|
335
|
+
for (const caller of move.callers) caller.resolve(undefined);
|
|
336
|
+
},
|
|
337
|
+
() => {
|
|
338
|
+
waitingMove = null;
|
|
339
|
+
[...move.callers].reverse()[0]?.resolve(notLoaded);
|
|
340
|
+
for (const caller of move.callers) caller.resolve(undefined);
|
|
341
|
+
}
|
|
342
|
+
);
|
|
343
|
+
return own;
|
|
344
|
+
}
|
|
345
|
+
|
|
169
346
|
// Loads whatever cart already exists (a remembered cart id for the gateway, the Northwind
|
|
170
347
|
// fixture for the demo) as soon as the store is created — no separate "refresh" the caller has
|
|
171
348
|
// to remember to call first.
|
|
172
|
-
void
|
|
349
|
+
void read();
|
|
173
350
|
|
|
174
351
|
return {
|
|
175
352
|
lines,
|
|
@@ -198,55 +375,55 @@ export function createCartStore(ops: CartOps): CartStore {
|
|
|
198
375
|
async undoRemove() {
|
|
199
376
|
const removed = lastRemoved.value;
|
|
200
377
|
if (!removed) return null;
|
|
201
|
-
// Reinsert at the exact index it was removed from — trusting a re-`add()`'s own returned
|
|
202
|
-
// line order would not guarantee that, so this half is local and synchronous.
|
|
203
|
-
lines.value = [
|
|
204
|
-
...lines.value.slice(0, removed.index),
|
|
205
|
-
removed.line,
|
|
206
|
-
...lines.value.slice(removed.index),
|
|
207
|
-
];
|
|
208
378
|
lastRemoved.value = null;
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
clearFailure();
|
|
212
|
-
try {
|
|
379
|
+
// The restored line comes from the server's answer, in the cart's currency, put back where it was.
|
|
380
|
+
return run(async () => {
|
|
213
381
|
const snapshot = await ops.add({
|
|
214
382
|
productId: removed.line.productId,
|
|
215
383
|
variantId: removed.line.variantId,
|
|
216
384
|
quantity: removed.line.quantity,
|
|
217
385
|
});
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
} finally {
|
|
225
|
-
inFlight -= 1;
|
|
226
|
-
if (inFlight === 0) pending.value = false;
|
|
227
|
-
}
|
|
228
|
-
},
|
|
229
|
-
async applyDiscount(code) {
|
|
230
|
-
inFlight += 1;
|
|
231
|
-
pending.value = true;
|
|
232
|
-
clearFailure();
|
|
233
|
-
try {
|
|
234
|
-
const { ack, snapshot } = await ops.applyDiscount(code);
|
|
235
|
-
if (snapshot) {
|
|
236
|
-
lines.value = snapshot.lines;
|
|
237
|
-
totals.value = snapshot.totals;
|
|
238
|
-
}
|
|
239
|
-
return ack;
|
|
240
|
-
} catch (caught) {
|
|
241
|
-
fail(caught);
|
|
242
|
-
return { ok: false, reason: 'failed' };
|
|
243
|
-
} finally {
|
|
244
|
-
inFlight -= 1;
|
|
245
|
-
if (inFlight === 0) pending.value = false;
|
|
246
|
-
}
|
|
386
|
+
const at = snapshot.lines.findIndex((line) => line.variantId === removed.line.variantId);
|
|
387
|
+
if (at === -1) return snapshot;
|
|
388
|
+
const others = snapshot.lines.filter((_line, index) => index !== at);
|
|
389
|
+
others.splice(Math.min(removed.index, others.length), 0, snapshot.lines[at]!);
|
|
390
|
+
return { ...snapshot, lines: others };
|
|
391
|
+
});
|
|
247
392
|
},
|
|
393
|
+
applyDiscount: (code) =>
|
|
394
|
+
serial(
|
|
395
|
+
async () => {
|
|
396
|
+
clearFailure();
|
|
397
|
+
try {
|
|
398
|
+
const { ack, snapshot } = await ops.applyDiscount(code);
|
|
399
|
+
if (snapshot) apply(snapshot);
|
|
400
|
+
return ack;
|
|
401
|
+
} catch (caught) {
|
|
402
|
+
fail(caught);
|
|
403
|
+
return { ok: false, reason: 'failed' } as StorefrontAck;
|
|
404
|
+
}
|
|
405
|
+
},
|
|
406
|
+
() => ({ ok: false, reason: 'failed' }) as StorefrontAck
|
|
407
|
+
),
|
|
248
408
|
// `code` identifies which applied-discount chip the caller removed; a cart only ever carries
|
|
249
409
|
// one discount today, so `ops.removeDiscount()` itself needs no argument.
|
|
250
410
|
removeDiscount: (_code) => run(() => ops.removeDiscount()),
|
|
411
|
+
currency,
|
|
412
|
+
notSold,
|
|
413
|
+
setCurrency: moveCurrency,
|
|
414
|
+
loadFailed,
|
|
415
|
+
loadState,
|
|
416
|
+
reload: read,
|
|
417
|
+
removeNotSold: () =>
|
|
418
|
+
serial(
|
|
419
|
+
async () => {
|
|
420
|
+
for (const line of [...notSold.value]) {
|
|
421
|
+
const failure = await attempt(() => ops.remove(line.id));
|
|
422
|
+
if (failure !== null) return failure;
|
|
423
|
+
}
|
|
424
|
+
return null;
|
|
425
|
+
},
|
|
426
|
+
() => notLoaded
|
|
427
|
+
),
|
|
251
428
|
};
|
|
252
429
|
}
|
|
@@ -26,11 +26,17 @@ export function toStorefrontCommerce(value: unknown): StorefrontCommerce | null
|
|
|
26
26
|
// report.
|
|
27
27
|
if (value === null || value === undefined || value === '') return null;
|
|
28
28
|
if (typeof value !== 'object') return discard(value, 'commerce is not an object');
|
|
29
|
-
const { currency, taxInclusivePricing, defaultTaxRate } = value as Record<
|
|
29
|
+
const { currency, taxInclusivePricing, defaultTaxRate, currencies } = value as Record<
|
|
30
|
+
string,
|
|
31
|
+
unknown
|
|
32
|
+
>;
|
|
30
33
|
if (typeof currency !== 'string' || currency === '') return discard(value, 'currency');
|
|
31
34
|
if (typeof taxInclusivePricing !== 'boolean') return discard(value, 'taxInclusivePricing');
|
|
32
35
|
if (typeof defaultTaxRate !== 'number') return discard(value, 'defaultTaxRate');
|
|
33
|
-
|
|
36
|
+
// The list was cleaned at build (`@eldrajs/theme-nuxt`); here it is only checked to be one.
|
|
37
|
+
return Array.isArray(currencies) && currencies.every((code) => typeof code === 'string')
|
|
38
|
+
? { currency, taxInclusivePricing, defaultTaxRate, currencies: currencies as string[] }
|
|
39
|
+
: { currency, taxInclusivePricing, defaultTaxRate };
|
|
34
40
|
}
|
|
35
41
|
|
|
36
42
|
/**
|