From 08a6b2efcfde665cea35cbcde38f2bd127ca8045 Mon Sep 17 00:00:00 2001 From: Leah Wagner Date: Thu, 28 Jan 2021 09:09:28 -0800 Subject: [PATCH] Issue/79 fallback image (#179) * Filter products from the homepage grid display when they do not have and image. Provide a fallback/placeholder image for the ProductCard for when a product does not have an image. * Avoid repeating placeholder image reference and move to a variable * Remove filter to avoid additional computations as per https://github.com/vercel/commerce/pull/80#discussion_r531142893. Placeholder images will display as fallback. * Filter products from the homepage grid display when they do not have and image. Provide a fallback/placeholder image for the ProductCard for when a product does not have an image. * Avoid repeating placeholder image reference and move to a variable * Remove filter to avoid additional computations as per https://github.com/vercel/commerce/pull/80#discussion_r531142893. Placeholder images will display as fallback. * Remove comment about filter. Code removed so no longer needed. Co-authored-by: B --- components/product/ProductCard/ProductCard.tsx | 5 +++-- pages/index.tsx | 1 + public/product-img-placeholder.svg | 7 +++++++ 3 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 public/product-img-placeholder.svg diff --git a/components/product/ProductCard/ProductCard.tsx b/components/product/ProductCard/ProductCard.tsx index 85e69b3f2..a63aacc6e 100644 --- a/components/product/ProductCard/ProductCard.tsx +++ b/components/product/ProductCard/ProductCard.tsx @@ -32,6 +32,7 @@ const ProductCard: FC = ({ imgLayout = 'responsive', }) => { const src = p.images.edges?.[0]?.node?.urlOriginal! + const placeholderImg = '/product-img-placeholder.svg'; const { price } = usePrice({ amount: p.prices?.price?.value, baseAmount: p.prices?.retailPrice?.value, @@ -58,7 +59,7 @@ const ProductCard: FC = ({ layout={imgLayout} loading={imgLoading} priority={imgPriority} - src={p.images.edges?.[0]?.node.urlOriginal!} + src={p.images.edges?.[0]?.node.urlOriginal! || placeholderImg} alt={p.images.edges?.[0]?.node.altText || 'Product Image'} /> @@ -81,7 +82,7 @@ const ProductCard: FC = ({
{p.name} { // Create a copy of products that we can mutate + // Filter products that do not have images const products = [...newestProducts] // If the lists of featured and best selling products don't have enough // products, then fill them with products from the products list, this diff --git a/public/product-img-placeholder.svg b/public/product-img-placeholder.svg new file mode 100644 index 000000000..fbb43bb62 --- /dev/null +++ b/public/product-img-placeholder.svg @@ -0,0 +1,7 @@ + + + + + + +