diff --git a/src/advisor-view.component.vue b/src/advisor-view.component.vue index 3343af0..8324d2e 100644 --- a/src/advisor-view.component.vue +++ b/src/advisor-view.component.vue @@ -10,8 +10,6 @@ - - @@ -40,7 +38,6 @@ import PageComponent from './components/page/page.component.vue'; import ModalComponent from './components/modal/modal.teleport.component.vue'; import ComparisonComponent from './components/comparison/comparison.teleport.component.vue'; import ToastComponent from './components/toast/toast.teleport.component.vue'; -import ZoeDock from './components/zoe/zoe-dock.teleport.component.vue'; import './css/tokens.css'; import './css/variables.css'; diff --git a/src/components/product-card/components/product-card-footer/product-card-footer.component.vue b/src/components/product-card/components/product-card-footer/product-card-footer.component.vue index 240efa5..4a0c5ab 100644 --- a/src/components/product-card/components/product-card-footer/product-card-footer.component.vue +++ b/src/components/product-card/components/product-card-footer/product-card-footer.component.vue @@ -86,6 +86,7 @@ const props = defineProps<{ isInSummary: boolean; trackedUrl: string; addToCartToastText: string; + addToCartErrorToastText: string; compareLabel: string; startComparisonLabel: string; addToSummaryLabel: string; @@ -101,7 +102,7 @@ defineEmits<{ const { canStartComparison } = useComparisonState(); const { recordClickout } = useProductClickout(() => props.product); -const { recordAddToCart } = useProductAddToCart(() => props.product); +const { addToCart } = useProductAddToCart(() => props.product); const toggleCompare = () => { props.product.toggleSelection?.(); @@ -116,18 +117,16 @@ const openComparison = () => { EventBus.$emit('zv:modal:open:comparison'); }; -const handleAddToCart = (): void => { - // TODO: wire up your shop's cart API here. - // The full product is available as `props.product` (sku, productId, name, price, etc.). - // Example: - // fetch('/api/cart', { method: 'POST', body: JSON.stringify({ sku: props.product.sku }) }); - // Tracking: recordAddToCart() sends "add-to-cart"; call it - // only after your cart call succeeds. - recordAddToCart(); +const showToast = (toast: { message: string; variant?: 'error' }): void => { + EventBus.$emit('zv:toast:show', toast); +}; - EventBus.$emit('zv:toast:show', { - message: props.addToCartToastText, - }); +const handleAddToCart = async (): Promise => { + if (await addToCart()) { + showToast({ message: props.addToCartToastText }); + } else { + showToast({ message: props.addToCartErrorToastText, variant: 'error' }); + } }; diff --git a/src/components/product-card/product-card.component.vue b/src/components/product-card/product-card.component.vue index 76b72c4..44f4e99 100644 --- a/src/components/product-card/product-card.component.vue +++ b/src/components/product-card/product-card.component.vue @@ -55,6 +55,9 @@ :is-in-summary="isInSummary" :tracked-url="trackedUrl" :add-to-cart-toast-text="componentConfiguration.addToCartToastText" + :add-to-cart-error-toast-text=" + componentConfiguration.addToCartErrorToastText + " :compare-label="t(componentConfiguration.compareLabel)" :start-comparison-label=" t(componentConfiguration.startComparisonLabel) diff --git a/src/components/product-card/product-card.configuration.ts b/src/components/product-card/product-card.configuration.ts index fbb0016..c5eb31d 100644 --- a/src/components/product-card/product-card.configuration.ts +++ b/src/components/product-card/product-card.configuration.ts @@ -10,4 +10,5 @@ export class ProductCardConfiguration { addedToSummaryLabel!: string; opensInNewTabLabel!: string; addToCartToastText!: string; + addToCartErrorToastText!: string; } diff --git a/src/components/product-card/product-card.preset.ts b/src/components/product-card/product-card.preset.ts index 2abcfde..daf5904 100644 --- a/src/components/product-card/product-card.preset.ts +++ b/src/components/product-card/product-card.preset.ts @@ -44,6 +44,10 @@ export const productCardPreset = object( default: 'Added to cart', label: 'Add To Cart Toast Text', }), + addToCartErrorToastText: standardText({ + default: 'Please try again. This product is not in your cart yet.', + label: 'Add To Cart Error Toast Text', + }), }, { label: 'Product Card', format: EmbeddedComponentParameterFormat.ACCORDION }, ); diff --git a/src/components/results-page/results-page.component.vue b/src/components/results-page/results-page.component.vue index 7d1af08..2f288c3 100644 --- a/src/components/results-page/results-page.component.vue +++ b/src/components/results-page/results-page.component.vue @@ -5,9 +5,14 @@ - - {{ t(componentConfiguration.headlineText) }} - + + + {{ t(componentConfiguration.headlineText) }} + + + {{ t(componentConfiguration.subheadlineText) }} + + @@ -135,6 +140,12 @@ const loadMore = () => { width: 100%; } +.results-page__intro { + display: flex; + flex-direction: column; + gap: var(--zv-space-sm); +} + .results-page__headline { font-family: var(--zv-font-heading); font-weight: var(--zv-font-weight-bold); @@ -145,6 +156,14 @@ const loadMore = () => { text-align: center; } +.results-page__subheadline { + font-family: var(--zv-font-body); + font-size: var(--zv-font-size-md); + line-height: var(--zv-line-height-loose); + color: var(--zv-text-secondary); + text-align: center; +} + .results-page__products { display: flex; flex-direction: column; diff --git a/src/components/results-page/results-page.configuration.ts b/src/components/results-page/results-page.configuration.ts index 1e50a9a..a8d51a6 100644 --- a/src/components/results-page/results-page.configuration.ts +++ b/src/components/results-page/results-page.configuration.ts @@ -2,6 +2,7 @@ import { ProductClusterConfiguration } from '../product-cluster/product-cluster. export class ResultsPageConfiguration { headlineText!: string; + subheadlineText!: string; resultsLabel!: string; noResultsText!: string; loadMoreLabel!: string; diff --git a/src/components/results-page/results-page.preset.ts b/src/components/results-page/results-page.preset.ts index bff253c..bd77011 100644 --- a/src/components/results-page/results-page.preset.ts +++ b/src/components/results-page/results-page.preset.ts @@ -11,6 +11,10 @@ export const resultsPagePreset = object( default: 'Your Recommendations', label: 'Headline Text', }), + subheadlineText: standardText({ + default: 'Picked for your next trip.', + label: 'Subheadline Text', + }), resultsLabel: standardText({ default: 'Results', label: 'Results Label' }), noResultsText: standardText({ default: 'No products match your criteria. Try adjusting your answers.', diff --git a/src/components/toast/toast.teleport.component.vue b/src/components/toast/toast.teleport.component.vue index dd3c391..f8e1015 100644 --- a/src/components/toast/toast.teleport.component.vue +++ b/src/components/toast/toast.teleport.component.vue @@ -8,10 +8,13 @@ aria-label="Dismiss notification" @click="dismiss" > - + void; +type ToastVariant = 'success' | 'error'; + interface ToastPayload { message: string; duration?: number; + variant?: ToastVariant; } +const ICON_PATHS: Record = { + success: 'M3 7.2L5.8 10L11 4.5', + error: 'M4 4L10 10M10 4L4 10', +}; + +const isToastVariant = (value: unknown): value is ToastVariant => + value === 'success' || value === 'error'; + const isToastPayload = (payload: unknown): payload is ToastPayload => { if (typeof payload !== 'object' || payload === null) { return false; @@ -45,6 +59,9 @@ const isToastPayload = (payload: unknown): payload is ToastPayload => { if ('duration' in payload && typeof payload.duration !== 'number') { return false; } + if ('variant' in payload && !isToastVariant(payload.variant)) { + return false; + } return 'message' in payload && typeof payload.message === 'string'; }; @@ -54,6 +71,7 @@ const DEFAULT_DURATION_MS = 2400; const visible = ref(false); const message = ref(''); +const variant = ref('success'); let dismissTimer: ReturnType | null = null; const clearTimer = () => { @@ -66,6 +84,7 @@ const clearTimer = () => { const show = (payload: ToastPayload) => { clearTimer(); message.value = payload.message; + variant.value = payload.variant ?? 'success'; visible.value = true; dismissTimer = setTimeout(() => { visible.value = false; @@ -156,10 +175,13 @@ onBeforeUnmount(() => { flex-shrink: 0; } +.zv-toast__icon--error { + background: var(--zv-bg-error); +} + .zv-toast__message { - white-space: nowrap; - overflow: hidden; - text-overflow: ellipsis; + text-align: start; + text-wrap: balance; } .zv-toast-enter-active { diff --git a/src/composables/__tests__/use-product-add-to-cart.spec.ts b/src/composables/__tests__/use-product-add-to-cart.spec.ts index 4918d68..20a38b4 100644 --- a/src/composables/__tests__/use-product-add-to-cart.spec.ts +++ b/src/composables/__tests__/use-product-add-to-cart.spec.ts @@ -1,7 +1,14 @@ -import { describe, expect, it } from 'vitest'; +import { beforeEach, describe, expect, it, vi } from 'vitest'; import { useProductAddToCart } from '../use-product-add-to-cart'; +import { addToShopCart } from '../../helpers/add-to-shop-cart'; import { getProductFixture } from '../../test-utils/fixtures'; +vi.mock('../../helpers/add-to-shop-cart'); + +beforeEach(() => { + vi.mocked(addToShopCart).mockReset(); +}); + describe('useProductAddToCart', () => { it('tells the runner once that the shopper added the product to the cart', () => { const product = getProductFixture(); @@ -11,4 +18,25 @@ describe('useProductAddToCart', () => { expect(product.notifyProductAddedToCart).toHaveBeenCalledOnce(); }); + + it('adds the product to the shop cart by SKU, then tells the runner', async () => { + vi.mocked(addToShopCart).mockResolvedValue(); + const product = getProductFixture(); + + const added = await useProductAddToCart(() => product).addToCart(); + + expect(added).toBe(true); + expect(addToShopCart).toHaveBeenCalledWith({ sku: 'DW-42' }); + expect(product.notifyProductAddedToCart).toHaveBeenCalledOnce(); + }); + + it('does not tell the runner when the shop cart fails', async () => { + vi.mocked(addToShopCart).mockRejectedValue(new Error('CART_DOWN')); + const product = getProductFixture(); + + const added = await useProductAddToCart(() => product).addToCart(); + + expect(added).toBe(false); + expect(product.notifyProductAddedToCart).not.toHaveBeenCalled(); + }); }); diff --git a/src/composables/use-product-add-to-cart.ts b/src/composables/use-product-add-to-cart.ts index 9145181..15dc736 100644 --- a/src/composables/use-product-add-to-cart.ts +++ b/src/composables/use-product-add-to-cart.ts @@ -1,9 +1,20 @@ import type { ProductRecommendation } from '@zoovu/exd-api'; +import { addToShopCart } from '../helpers/add-to-shop-cart'; export const useProductAddToCart = (product: () => ProductRecommendation) => { const recordAddToCart = (): void => { product().notifyProductAddedToCart(); }; - return { recordAddToCart }; + const addToCart = async (): Promise => { + try { + await addToShopCart({ sku: product().sku }); + } catch { + return false; + } + recordAddToCart(); + return true; + }; + + return { recordAddToCart, addToCart }; }; diff --git a/src/helpers/__tests__/add-to-shop-cart.spec.ts b/src/helpers/__tests__/add-to-shop-cart.spec.ts new file mode 100644 index 0000000..9ee45fc --- /dev/null +++ b/src/helpers/__tests__/add-to-shop-cart.spec.ts @@ -0,0 +1,27 @@ +import { afterEach, describe, expect, it, vi } from 'vitest'; +import { ADD_TO_CART_EVENT, addToShopCart } from '../add-to-shop-cart'; + +const refuse = (request: Event): void => request.preventDefault(); + +afterEach(() => { + window.removeEventListener(ADD_TO_CART_EVENT, refuse); +}); + +describe('addToShopCart', () => { + it('tells the shop page which product to add', async () => { + const onRequest = vi.fn(); + window.addEventListener(ADD_TO_CART_EVENT, onRequest, { once: true }); + + await addToShopCart({ sku: 'TENT-2' }); + + expect(onRequest.mock.calls[0][0].detail).toEqual({ sku: 'TENT-2' }); + }); + + it('fails when the shop page refuses the request', async () => { + window.addEventListener(ADD_TO_CART_EVENT, refuse); + + await expect(addToShopCart({ sku: 'TENT-2' })).rejects.toThrow( + 'ADD_TO_CART_REFUSED', + ); + }); +}); diff --git a/src/helpers/add-to-shop-cart.ts b/src/helpers/add-to-shop-cart.ts new file mode 100644 index 0000000..61d7123 --- /dev/null +++ b/src/helpers/add-to-shop-cart.ts @@ -0,0 +1,12 @@ +export const ADD_TO_CART_EVENT = 'solstice:add-to-cart'; + +export function addToShopCart({ sku }: { sku: string }): Promise { + const request = new CustomEvent(ADD_TO_CART_EVENT, { + detail: { sku }, + cancelable: true, + }); + if (window.dispatchEvent(request)) { + return Promise.resolve(); + } + return Promise.reject(new Error('ADD_TO_CART_REFUSED')); +} diff --git a/src/i18n/index.ts b/src/i18n/index.ts index 9463d03..6071f6f 100644 --- a/src/i18n/index.ts +++ b/src/i18n/index.ts @@ -13,7 +13,9 @@ export function useT(): TranslateFn { const { t, te } = useI18n({ useScope: 'global' }); return ((key: string, params?: Record) => { if (typeof key !== 'string') return key as unknown as string; - if (!params) return t(key); + if (!params) { + return te(key) ? t(key) : key; + } // An unpublished default is a literal sentence, not a message id: vue-i18n // returns it verbatim, so substitute its placeholders here. return te(key) ? t(key, params) : interpolate({ text: key, params });
+ {{ t(componentConfiguration.subheadlineText) }} +