Search code examples
reactjsgatsbymoltin

How to display additional image thumbnails below main featured product image


I can't get files from Moltin API to display under main product image.

I'm using this repository: https://github.com/moltin/gatsby-demo-store and updating @moltin/gatsby-source-moltin to 1.3.1. I have tried expanding the product node schema to include relationships as per Moltin API documentation, and then referencing this in a new component, but no additional images are rendered.

I had a look at other implementations, i.e. Gatsby Store example, which uses thumbnails and could get a tiny clickable thin purple strip rendering, but with no image. The store uses Shopify and localFile storage for image rendering, so this use case is not applicable.

import React, { useContext, useEffect, useReducer, useState } from 'react'
import { graphql, withPrefix } from 'gatsby'

import SEO from '../components/SEO'
import Photo from '../components/Photo'
import Badge from '../components/Badge'
import AddToCart from '../components/AddToCart'
import { Shopkit } from '../shopkit'
import Img from 'gatsby-image'


  const {
    meta: { display_price }
  } = product

  return (
    <React.Fragment>
      <SEO
        type="product"
        title={product.meta_title || product.name}
        description={product.meta_description || product.description}
        image={withPrefix(product.mainImage.childImageSharp.fixed.src)}
      />

      <div className="flex flex-wrap md:bg-grey-light">
        <div className="py-2 md:py-5 md:px-5 w-full lg:w-1/2">
          <div className="sticky pin-t">
            <Photo
              src={product.mainImage}
              alt={product.main_image_alt_text || product.name}
            />
           <Img 
             src={product.relationships.files.data.id}
             alt=""
           />  
          </div>
        </div>
......
.....
....
    </React.Fragment>
  )
}

export const query = graphql`
  query($id: String!) {
    product: moltinProduct(id: { eq: $id }) {
      id
      slug
      name
      description
      sku
      mainImage {
        childImageSharp {
          fixed(width: 560) {
            ...GatsbyImageSharpFixed
          }
        }
        publicURL
      }
      meta {
        display_price {
          without_tax {
            formatted
          }
        }
      }
      manage_stock
      meta_title
      meta_description
      on_sale
      bulb
      bulb_qty
      material
      finish
      max_watt
      relationships {
             main_image {
              data {
                id
              }
            }
        files {
          data {
            type
            id
          }
    }
  }
`

export default ProductPage

I would like to get small thumbnail rendered below main product image. Console error message when using Img component:

Uncaught TypeError: Cannot read property 'src' of undefined.


Solution

  • gatsby-source-moltin adds a images field to the product schema which you can query like so:

    images {
      childImageSharp {
        fluid(maxWidth: 300) {
          ...GatsbyImageSharpFluid
        }
      }
    }
    

    I put together an example on CodeSandbox.