Files
tehran_taj/apps/user-app-v2/src/composables/useOrderItemProduct.ts
T

38 lines
1.4 KiB
TypeScript
Raw Normal View History

// The product and variant titles shown on one line of an order.
//
// These now arrive WITH the order: GET customer/me/orders includes each item's
// variant, its product and the product's first image.
//
// Before that, this joined against the shared catalogue in the browser. Two
// problems, both fixed by the include: it fetched the whole catalogue (~16k
// products in the real data) to label a few rows, and a product that had since
// left the storefront was absent from the catalogue, so the row rendered blank —
// precisely for old orders, which is where order history matters most.
import type { OrderItemDetail } from '../api/client'
export interface VariantLookup {
productTitle: string
variantTitle: string
imageUrl?: string
}
/** Reads the labels off the item itself; no request, no catalogue. */
export function orderItemLookup(item: OrderItemDetail): VariantLookup | undefined {
const variant = item.productVariant
if (!variant) return undefined
return {
productTitle: variant.product.title,
variantTitle: variant.title,
imageUrl: variant.product.images[0]?.url,
}
}
/**
* Kept as a composable so the call sites read unchanged. Synchronous now —
* `lookup` is resolved from the item, so it returns a value rather than a
* promise, and callers no longer need to await anything on mount.
*/
export function useOrderItemProduct() {
return { lookup: orderItemLookup }
}