commerce/framework/spree/utils/getMediaGallery.ts
2021-08-20 13:34:57 +02:00

31 lines
749 B
TypeScript

// Based on https://github.com/spark-solutions/spree2vuestorefront/blob/d88d85ae1bcd2ec99b13b81cd2e3c25600a0216e/src/utils/index.ts
import type { ProductImage } from '@commerce/types/product'
import type { SpreeProductImage } from '../types'
const getMediaGallery = (
images: SpreeProductImage[],
getImageUrl: (
image: SpreeProductImage,
minWidth: number,
minHeight: number
) => string | null
) => {
return images.reduce<ProductImage[]>((productImages, _, imageIndex) => {
const imageUrl = getImageUrl(images[imageIndex], 800, 800)
if (imageUrl) {
return [
...productImages,
{
url: imageUrl,
},
]
}
return productImages
}, [])
}
export default getMediaGallery