fix(images): resolve product images universally via resolveProductImage and fallback hierarchy across all catalog, store, cart, wishlist, and compare components
This commit is contained in:
parent
86b881441b
commit
15681f87bd
11 changed files with 65 additions and 38 deletions
|
|
@ -9,7 +9,7 @@ import { useEffect, useState } from 'react';
|
||||||
import Link from 'next/link';
|
import Link from 'next/link';
|
||||||
import { useRouter } from 'next/navigation';
|
import { useRouter } from 'next/navigation';
|
||||||
import { useCartStore } from '@/store/cartStore';
|
import { useCartStore } from '@/store/cartStore';
|
||||||
import { formatCurrency, getImageUrl } from '@/lib/utils';
|
import { formatCurrency, getImageUrl, resolveProductImage } from '@/lib/utils';
|
||||||
import { requireLoginForCheckout } from '@/lib/requireLoginForCheckout';
|
import { requireLoginForCheckout } from '@/lib/requireLoginForCheckout';
|
||||||
import { Header } from '@/layout/Header';
|
import { Header } from '@/layout/Header';
|
||||||
import { Footer } from '@/layout/Footer';
|
import { Footer } from '@/layout/Footer';
|
||||||
|
|
@ -27,12 +27,12 @@ import {
|
||||||
import { deliveryService, DeliverySettings } from '@/services/api/deliveryService';
|
import { deliveryService, DeliverySettings } from '@/services/api/deliveryService';
|
||||||
|
|
||||||
function resolveItemImage(item: {
|
function resolveItemImage(item: {
|
||||||
product: { images?: { image_url?: string }[]; name?: string };
|
product: any;
|
||||||
selectedVariant: { images?: { image_url?: string }[] };
|
selectedVariant: any;
|
||||||
}): string {
|
}): string {
|
||||||
const fromVariant = item.selectedVariant?.images?.[0]?.image_url;
|
const fromVariant = item.selectedVariant?.thumbnail_url || item.selectedVariant?.images?.[0]?.image_url;
|
||||||
const fromProduct = item.product?.images?.[0]?.image_url;
|
if (fromVariant) return getImageUrl(fromVariant);
|
||||||
return getImageUrl(fromVariant || fromProduct || '');
|
return resolveProductImage(item.product);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function CartPage() {
|
export default function CartPage() {
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ import Image from 'next/image';
|
||||||
import { useCartStore } from '@/store/cartStore';
|
import { useCartStore } from '@/store/cartStore';
|
||||||
import { catalogService, ProductResponse } from '@/services/api/catalogService';
|
import { catalogService, ProductResponse } from '@/services/api/catalogService';
|
||||||
import { mapProductCardToProductResponse } from '@/utils/productMapper';
|
import { mapProductCardToProductResponse } from '@/utils/productMapper';
|
||||||
import { formatCatalogPriceLabel, getImageUrl } from '@/lib/utils';
|
import { formatCatalogPriceLabel, getImageUrl, resolveProductImage } from '@/lib/utils';
|
||||||
import { ArrowLeftRight, Trash2 } from 'lucide-react';
|
import { ArrowLeftRight, Trash2 } from 'lucide-react';
|
||||||
import { AnnouncementBar } from '@/layout/AnnouncementBar';
|
import { AnnouncementBar } from '@/layout/AnnouncementBar';
|
||||||
import { Header } from '@/layout/Header';
|
import { Header } from '@/layout/Header';
|
||||||
|
|
@ -155,7 +155,7 @@ export default function ComparePage() {
|
||||||
<tr className="bg-gray-50 border-b border-gray-200 text-gray-900">
|
<tr className="bg-gray-50 border-b border-gray-200 text-gray-900">
|
||||||
<th className="p-4 w-44 font-extrabold">Feature</th>
|
<th className="p-4 w-44 font-extrabold">Feature</th>
|
||||||
{products.map((product) => {
|
{products.map((product) => {
|
||||||
const img = getImageUrl(product.images?.[0]?.image_url);
|
const img = resolveProductImage(product);
|
||||||
return (
|
return (
|
||||||
<th key={product.product_id} className="p-4 min-w-[200px]">
|
<th key={product.product_id} className="p-4 min-w-[200px]">
|
||||||
<div className="flex flex-col items-center text-center">
|
<div className="flex flex-col items-center text-center">
|
||||||
|
|
|
||||||
|
|
@ -15,12 +15,10 @@ import { StickyHeaderSpacer } from '@/components/StickyHeaderSpacer';
|
||||||
import { FloatingButtons } from '@/components/FloatingButtons';
|
import { FloatingButtons } from '@/components/FloatingButtons';
|
||||||
import { useCartStore } from '@/store/cartStore';
|
import { useCartStore } from '@/store/cartStore';
|
||||||
import { ProductResponse } from '@/services/api/catalogService';
|
import { ProductResponse } from '@/services/api/catalogService';
|
||||||
import { formatCatalogPriceLabel, getImageUrl } from '@/lib/utils';
|
import { formatCatalogPriceLabel, getImageUrl, resolveProductImage } from '@/lib/utils';
|
||||||
|
|
||||||
function getProductImage(product: ProductResponse): string {
|
function getProductImage(product: ProductResponse): string {
|
||||||
const fromProduct = product.images?.[0]?.image_url;
|
return resolveProductImage(product);
|
||||||
const fromVariant = product.variants?.[0]?.images?.[0]?.image_url;
|
|
||||||
return getImageUrl(fromProduct || fromVariant || '');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getPriceLabel(product: ProductResponse): string {
|
function getPriceLabel(product: ProductResponse): string {
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ import { BrandSelector } from './BrandSelector';
|
||||||
import { SeriesSelector } from './SeriesSelector';
|
import { SeriesSelector } from './SeriesSelector';
|
||||||
import { ModelSelector } from './ModelSelector';
|
import { ModelSelector } from './ModelSelector';
|
||||||
import { StandardCatalog } from './StandardCatalog';
|
import { StandardCatalog } from './StandardCatalog';
|
||||||
import { formatCurrency, getCatalogPriceInfo, getImageUrl } from '@/lib/utils';
|
import { formatCurrency, getCatalogPriceInfo, getImageUrl, resolveProductImage } from '@/lib/utils';
|
||||||
import { ShoppingCart, Heart, ArrowLeftRight, Search, RotateCcw } from 'lucide-react';
|
import { ShoppingCart, Heart, ArrowLeftRight, Search, RotateCcw } from 'lucide-react';
|
||||||
import { useCartStore } from '@/store/cartStore';
|
import { useCartStore } from '@/store/cartStore';
|
||||||
import { mapProductCardToProductResponse } from '@/utils/productMapper';
|
import { mapProductCardToProductResponse } from '@/utils/productMapper';
|
||||||
|
|
@ -361,7 +361,7 @@ export function ParentFeatureCatalog({ category }: ParentFeatureCatalogProps) {
|
||||||
{products.map((p) => {
|
{products.map((p) => {
|
||||||
const firstVariant = p.variants?.[0];
|
const firstVariant = p.variants?.[0];
|
||||||
const { price, oldPrice: comparePrice } = getCatalogPriceInfo(p);
|
const { price, oldPrice: comparePrice } = getCatalogPriceInfo(p);
|
||||||
const primaryImage = getImageUrl(p.images?.[0]?.image_url);
|
const primaryImage = resolveProductImage(p);
|
||||||
|
|
||||||
const isWishlisted = wishlist.some((item) => item.product_id === p.product_id);
|
const isWishlisted = wishlist.some((item) => item.product_id === p.product_id);
|
||||||
const isCompared = compareList.some((item) => item.product_id === p.product_id);
|
const isCompared = compareList.some((item) => item.product_id === p.product_id);
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ import {
|
||||||
CategoryResponse,
|
CategoryResponse,
|
||||||
BrandResponse,
|
BrandResponse,
|
||||||
} from '@/services/api/catalogService';
|
} from '@/services/api/catalogService';
|
||||||
import { formatCurrency, getCatalogPriceInfo, getImageUrl } from '@/lib/utils';
|
import { formatCurrency, getCatalogPriceInfo, getImageUrl, resolveProductImage } from '@/lib/utils';
|
||||||
import {
|
import {
|
||||||
applyCatalogFilters,
|
applyCatalogFilters,
|
||||||
CATALOG_API_LIMIT,
|
CATALOG_API_LIMIT,
|
||||||
|
|
@ -90,7 +90,7 @@ function CatalogProductCard({
|
||||||
const discount =
|
const discount =
|
||||||
mappedDiscount ||
|
mappedDiscount ||
|
||||||
(compare > price ? Math.round(((compare - price) / compare) * 100) : 0);
|
(compare > price ? Math.round(((compare - price) / compare) * 100) : 0);
|
||||||
const image = getImageUrl((product as any).thumbnail_url || product.images?.[0]?.image_url);
|
const image = resolveProductImage(product);
|
||||||
const rating = getDisplayStarCount(product);
|
const rating = getDisplayStarCount(product);
|
||||||
const isWishlisted = wishlist.some((i) => i.product_id === product.product_id);
|
const isWishlisted = wishlist.some((i) => i.product_id === product.product_id);
|
||||||
const isCompared = compareList.some((i) => i.product_id === product.product_id);
|
const isCompared = compareList.some((i) => i.product_id === product.product_id);
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ import Image from 'next/image';
|
||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
import BlurHashImage from '@/components/ui/BlurHashImage';
|
import BlurHashImage from '@/components/ui/BlurHashImage';
|
||||||
import { ProductResponse } from '@/services/api/catalogService';
|
import { ProductResponse } from '@/services/api/catalogService';
|
||||||
import { formatCurrency, getCatalogPriceInfo, getImageUrl } from '@/lib/utils';
|
import { formatCurrency, getCatalogPriceInfo, getImageUrl, resolveProductImage } from '@/lib/utils';
|
||||||
|
|
||||||
interface YouMayAlsoLikeProps {
|
interface YouMayAlsoLikeProps {
|
||||||
products: ProductResponse[];
|
products: ProductResponse[];
|
||||||
|
|
@ -101,12 +101,7 @@ export function YouMayAlsoLike({ products }: YouMayAlsoLikeProps) {
|
||||||
style={{ gridTemplateColumns: `repeat(${Math.min(products.length, 5)}, minmax(168px, 1fr))` }}
|
style={{ gridTemplateColumns: `repeat(${Math.min(products.length, 5)}, minmax(168px, 1fr))` }}
|
||||||
>
|
>
|
||||||
{products.slice(0, 5).map((prod, index) => {
|
{products.slice(0, 5).map((prod, index) => {
|
||||||
const img = getImageUrl(
|
const img = resolveProductImage(prod);
|
||||||
prod.thumbnail_url ||
|
|
||||||
prod.images?.[0]?.image_url ||
|
|
||||||
prod.variants?.[0]?.thumbnail_url ||
|
|
||||||
prod.variants?.[0]?.images?.[0]?.image_url
|
|
||||||
);
|
|
||||||
const off = discountPct(prod);
|
const off = discountPct(prod);
|
||||||
const pricing = priceLabel(prod);
|
const pricing = priceLabel(prod);
|
||||||
const onSale = off > 0;
|
const onSale = off > 0;
|
||||||
|
|
|
||||||
|
|
@ -8,17 +8,17 @@ import { useEffect, useState } from 'react';
|
||||||
import Link from 'next/link';
|
import Link from 'next/link';
|
||||||
import { X, Trash2, Plus, Minus, ShoppingBag, ArrowRight, Truck } from 'lucide-react';
|
import { X, Trash2, Plus, Minus, ShoppingBag, ArrowRight, Truck } from 'lucide-react';
|
||||||
import { useCartStore } from '@/store/cartStore';
|
import { useCartStore } from '@/store/cartStore';
|
||||||
import { formatCurrency, getImageUrl } from '@/lib/utils';
|
import { formatCurrency, getImageUrl, resolveProductImage } from '@/lib/utils';
|
||||||
import { requireLoginForCheckout } from '@/lib/requireLoginForCheckout';
|
import { requireLoginForCheckout } from '@/lib/requireLoginForCheckout';
|
||||||
import { deliveryService, DeliverySettings } from '@/services/api/deliveryService';
|
import { deliveryService, DeliverySettings } from '@/services/api/deliveryService';
|
||||||
|
|
||||||
function resolveItemImage(item: {
|
function resolveItemImage(item: {
|
||||||
product: { images?: { image_url?: string }[]; name?: string };
|
product: any;
|
||||||
selectedVariant: { images?: { image_url?: string }[] };
|
selectedVariant: any;
|
||||||
}): string {
|
}): string {
|
||||||
const fromVariant = item.selectedVariant?.images?.[0]?.image_url;
|
const fromVariant = item.selectedVariant?.thumbnail_url || item.selectedVariant?.images?.[0]?.image_url;
|
||||||
const fromProduct = item.product?.images?.[0]?.image_url;
|
if (fromVariant) return getImageUrl(fromVariant);
|
||||||
return getImageUrl(fromVariant || fromProduct || '');
|
return resolveProductImage(item.product);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function CartDrawer() {
|
export function CartDrawer() {
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ import { useRouter } from 'next/navigation';
|
||||||
import { Star, Eye, Heart, ArrowLeftRight, ShoppingBag, Check } from 'lucide-react';
|
import { Star, Eye, Heart, ArrowLeftRight, ShoppingBag, Check } from 'lucide-react';
|
||||||
import { ProductResponse } from '@/services/api/catalogService';
|
import { ProductResponse } from '@/services/api/catalogService';
|
||||||
import { useCartStore } from '@/store/cartStore';
|
import { useCartStore } from '@/store/cartStore';
|
||||||
import { formatCurrency, getImageUrl } from '@/lib/utils';
|
import { formatCurrency, getImageUrl, resolveProductImage } from '@/lib/utils';
|
||||||
import BlurHashImage from '@/components/ui/BlurHashImage';
|
import BlurHashImage from '@/components/ui/BlurHashImage';
|
||||||
import { addToCompareAndNavigate } from '@/lib/compare';
|
import { addToCompareAndNavigate } from '@/lib/compare';
|
||||||
|
|
||||||
|
|
@ -31,9 +31,9 @@ export function ProductCard({ product }: ProductCardProps) {
|
||||||
const setQuickViewProduct = useCartStore((state) => state.setQuickViewProduct);
|
const setQuickViewProduct = useCartStore((state) => state.setQuickViewProduct);
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
|
||||||
const primaryVariant = product.variants[0];
|
const primaryVariant = product.variants?.[0];
|
||||||
const primaryImage = getImageUrl(product.images[0]?.image_url);
|
const primaryImage = resolveProductImage(product);
|
||||||
const secondaryImage = getImageUrl(product.images[1]?.image_url) || primaryImage;
|
const secondaryImage = getImageUrl(product.images?.[1]?.image_url) || primaryImage;
|
||||||
const productUrl = (product as any).variant_url || `/products/${product.slug}`;
|
const productUrl = (product as any).variant_url || `/products/${product.slug}`;
|
||||||
|
|
||||||
const price = Number(primaryVariant?.price || 0);
|
const price = Number(primaryVariant?.price || 0);
|
||||||
|
|
|
||||||
33
lib/utils.ts
33
lib/utils.ts
|
|
@ -93,8 +93,18 @@ export function getImageUrl(path?: string | null | unknown): string {
|
||||||
if (typeof path === 'string') {
|
if (typeof path === 'string') {
|
||||||
resolvedPath = path;
|
resolvedPath = path;
|
||||||
} else if (path && typeof path === 'object') {
|
} else if (path && typeof path === 'object') {
|
||||||
const obj = path as Record<string, unknown>;
|
const obj = path as Record<string, any>;
|
||||||
resolvedPath = String(obj.image_url ?? obj.url ?? obj.src ?? '');
|
resolvedPath = String(
|
||||||
|
obj.thumbnail_url ??
|
||||||
|
obj.image_url ??
|
||||||
|
obj.image ??
|
||||||
|
(Array.isArray(obj.images) && obj.images.length > 0
|
||||||
|
? (typeof obj.images[0] === 'string' ? obj.images[0] : (obj.images[0]?.image_url ?? ''))
|
||||||
|
: '') ??
|
||||||
|
obj.url ??
|
||||||
|
obj.src ??
|
||||||
|
''
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
return '/images/placeholder-product.webp';
|
return '/images/placeholder-product.webp';
|
||||||
}
|
}
|
||||||
|
|
@ -153,3 +163,22 @@ export function getImageUrl(path?: string | null | unknown): string {
|
||||||
return cleanPath;
|
return cleanPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Universal bulletproof image resolver for any product or variant representation.
|
||||||
|
* Prioritizes thumbnail_url, product.image, variant images, product images, and imageHover.
|
||||||
|
*/
|
||||||
|
export function resolveProductImage(product?: any): string {
|
||||||
|
if (!product) return '/images/placeholder-product.webp';
|
||||||
|
const raw =
|
||||||
|
product.thumbnail_url ||
|
||||||
|
product.image ||
|
||||||
|
product.images?.[0]?.image_url ||
|
||||||
|
(typeof product.images?.[0] === 'string' ? product.images[0] : null) ||
|
||||||
|
product.variants?.[0]?.thumbnail_url ||
|
||||||
|
product.variants?.[0]?.images?.[0]?.image_url ||
|
||||||
|
(typeof product.variants?.[0]?.images?.[0] === 'string' ? product.variants[0].images[0] : null) ||
|
||||||
|
product.imageHover;
|
||||||
|
return getImageUrl(raw);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -263,7 +263,7 @@ export const catalogService = {
|
||||||
category_id: p.category_id,
|
category_id: p.category_id,
|
||||||
slug: p.slug,
|
slug: p.slug,
|
||||||
name: p.name,
|
name: p.name,
|
||||||
thumbnail_url: p.images[0]?.image_url || null,
|
thumbnail_url: (p as any).thumbnail_url || p.images?.[0]?.image_url || null,
|
||||||
price,
|
price,
|
||||||
compare_price,
|
compare_price,
|
||||||
discount_percent,
|
discount_percent,
|
||||||
|
|
|
||||||
|
|
@ -105,11 +105,16 @@ export function mapProductCardToProductResponse(
|
||||||
export function mapCatalogProductToProduct(
|
export function mapCatalogProductToProduct(
|
||||||
p: ProductResponse | ProductCardResponse | Record<string, any>
|
p: ProductResponse | ProductCardResponse | Record<string, any>
|
||||||
): Product {
|
): Product {
|
||||||
|
const cardMapped = mapProductCardToProductResponse(p);
|
||||||
const normalized: ProductResponse =
|
const normalized: ProductResponse =
|
||||||
Array.isArray((p as any).variants) && (p as any).variants.length > 0
|
Array.isArray((p as any).variants) && (p as any).variants.length > 0
|
||||||
? ({
|
? ({
|
||||||
...mapProductCardToProductResponse(p),
|
...cardMapped,
|
||||||
...p,
|
...p,
|
||||||
|
images:
|
||||||
|
Array.isArray((p as any).images) && (p as any).images.length > 0
|
||||||
|
? (p as any).images
|
||||||
|
: cardMapped.images,
|
||||||
variants: (p as any).variants.map((v: any) => ({
|
variants: (p as any).variants.map((v: any) => ({
|
||||||
...v,
|
...v,
|
||||||
price: parseMoney(v.price),
|
price: parseMoney(v.price),
|
||||||
|
|
@ -119,7 +124,7 @@ export function mapCatalogProductToProduct(
|
||||||
: null,
|
: null,
|
||||||
})),
|
})),
|
||||||
} as ProductResponse)
|
} as ProductResponse)
|
||||||
: mapProductCardToProductResponse(p);
|
: cardMapped;
|
||||||
|
|
||||||
const { price, priceMax, oldPrice, discount } = getCatalogPriceInfo(normalized);
|
const { price, priceMax, oldPrice, discount } = getCatalogPriceInfo(normalized);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue