Skip to content

Commit

Permalink
Added support to access other non-image files on product objects. (#283)
Browse files Browse the repository at this point in the history
* Added support to access other non-image files on product objects.

* Changeset.
  • Loading branch information
amcaffee-ep authored Feb 4, 2025
1 parent 35a5d9b commit 597d5a2
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 32 deletions.
5 changes: 5 additions & 0 deletions .changeset/pretty-pumas-love.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@elasticpath/shopper-common": patch
---

Added support for non-image file types to product interfaces and response processing.
3 changes: 2 additions & 1 deletion packages/shopper-common/src/products/product.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import type { MatrixObjectEntry } from "../shared/types/matrix-object-entry"

export interface ProductBase {
main_image: File | null
otherImages: File[]
otherImages: File[],
otherFiles?: File[]
}

export type VariationProduct = BaseProduct | ChildProduct
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ export function processImageFiles(files: File[], mainImageId?: string) {
"image/svg+xml",
]

if (!mainImageId) {
return files
}

return files.filter(
(fileEntry) =>
fileEntry.id !== mainImageId &&
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,47 @@
import type { ShopperCatalogResource, ElasticPath } from "@elasticpath/js-sdk"
import { sortAlphabetically } from "./sort-alphabetically"
import type {
ElasticPath,
File,
ShopperCatalogResource,
} from "@elasticpath/js-sdk"
import { ProductResponse } from "@elasticpath/js-sdk"
import {
BaseProduct,
BaseProductResponse,
BundleProduct,
BundleProductResponse,
ChildProduct,
ChildProductResponse,
ShopperProduct,
SimpleProduct,
SimpleProductResponse,
BundleProduct,
} from "../"
import { getFilesByIds, getProductById } from "../services/product"
import {
getProductMainImage,
getProductOtherImageUrls,
} from "./product-image-helpers"
import { getFilesByIds, getProductById } from "../services/product"
import { sortAlphabetically } from "./sort-alphabetically"

function processOtherFiles(
files: File[],
imageFiles: File[],
mainImageId?: string,
) {
const imageFileIds = imageFiles.map((file) => file.id)
return files.filter(
(file) => !imageFileIds.includes(file.id) && file.id !== mainImageId,
)
}

function processFiles(files: File[] | undefined, mainImage?: File) {
const otherImages = getProductOtherImageUrls(files, mainImage)
const otherFiles = processOtherFiles(files ?? [], otherImages, mainImage?.id)

return {
otherImages,
otherFiles,
}
}

export async function createShopperBundleProduct(
productResource: ShopperCatalogResource<BundleProductResponse>,
Expand All @@ -35,14 +60,17 @@ export async function createShopperBundleProduct(
client,
)

const processedFiles = processFiles(
productResource.included?.files,
productResource.included?.main_images?.[0],
)

return {
kind: "bundle-product",
response: productResource.data,
main_image: getProductMainImage(productResource.included?.main_images),
otherImages: getProductOtherImageUrls(
productResource.included?.files,
productResource.included?.main_images?.[0],
),
otherImages: processedFiles.otherImages,
otherFiles: processedFiles.otherFiles,
componentProductResponses: componentProducts,
componentProductImages: mainProductComponentImages,
}
Expand All @@ -55,14 +83,16 @@ function isString(x: any): x is string {
export function createShopperSimpleProduct(
productResource: ShopperCatalogResource<SimpleProductResponse>,
): SimpleProduct {
const processedFiles = processFiles(
productResource.included?.files,
productResource.included?.main_images?.[0],
)
return {
kind: "simple-product",
response: productResource.data,
main_image: getProductMainImage(productResource.included?.main_images),
otherImages: getProductOtherImageUrls(
productResource.included?.files,
productResource.included?.main_images?.[0],
),
otherImages: processedFiles.otherImages,
otherFiles: processedFiles.otherFiles,
}
}

Expand Down Expand Up @@ -91,6 +121,11 @@ export async function createShopperChildProduct(
)
}

const processedFiles = processFiles(
productResources.included?.files,
productResources.included?.main_images?.[0],
)

return {
kind: "child-product",
response: productResources.data,
Expand All @@ -99,10 +134,8 @@ export async function createShopperChildProduct(
main_image: getProductMainImage(baseProduct.included?.main_images),
},
main_image: getProductMainImage(productResources.included?.main_images),
otherImages: getProductOtherImageUrls(
productResources.included?.files,
productResources.included?.main_images?.[0],
),
otherImages: processedFiles.otherImages,
otherFiles: processedFiles.otherFiles,
variationsMatrix: variation_matrix,
variations: variations.sort(sortAlphabetically),
}
Expand All @@ -124,17 +157,20 @@ export function createShopperBaseProduct(
)
}

return {
kind: "base-product",
response: productResource.data,
main_image: getProductMainImage(productResource.included?.main_images),
otherImages: getProductOtherImageUrls(
productResource.included?.files,
productResource.included?.main_images?.[0],
),
variationsMatrix: variation_matrix,
variations: variations.sort(sortAlphabetically),
}
const processedFiles = processFiles(
productResource.included?.files,
productResource.included?.main_images?.[0],
)

return {
kind: "base-product",
response: productResource.data,
main_image: getProductMainImage(productResource.included?.main_images),
otherImages: processedFiles.otherImages,
otherFiles: processedFiles.otherFiles,
variationsMatrix: variation_matrix,
variations: variations.sort(sortAlphabetically),
}
}

export function isBundleProduct(
Expand Down

0 comments on commit 597d5a2

Please sign in to comment.