/
productHelper.ts
108 lines (87 loc) · 3.61 KB
/
productHelper.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import {CtAsset, CtPrice} from 'lib/types'
export const getName = (product: any, locale: string | undefined) =>
product?.masterData?.current?.name[locale ?? 'en-US']
export const getSku = (product: any) =>
product?.masterData?.current?.masterVariant?.sku
export const getProductType = (product: any) => product?.productType?.obj?.name
export const getProductAttribute = (product: any, attributeName: string) =>
product?.masterData?.current?.masterVariant?.attributes?.find(
(x: any) => x.name === attributeName,
)?.value
export const getVariantAttribute = (variant: any, attributeName: string) =>
variant?.attributes?.find((x: any) => x.name === attributeName)?.value
export const getPrice = (product: any): CtPrice | undefined => {
if ((product?.masterData?.current?.masterVariant?.prices?.length ?? 0) > 0) {
const firstPrice = product.masterData.current.masterVariant.prices[0]
return firstPrice.value
}
return undefined
}
export const getAllAssets = (product: any, sort?: boolean): CtAsset[] => {
const assets = product?.masterData?.current?.masterVariant?.assets ?? []
return sort ? sortAssets(assets) : assets
}
export const getUniqueAssets = (assets: CtAsset[], sort?: boolean): CtAsset[] => {
// Only return unique assets, avoiding to return multiple assets from the same spinset
const uniqueAssets: CtAsset[] = []
const spinsets: string[] = []
assets.forEach(asset => {
const spinsetTags = asset.tags?.filter(tag => tag.includes('spinset'))
const isSpinset = spinsetTags.length > 0
if (!isSpinset) {
// Take all assets that are NOT spinsets
uniqueAssets.push(asset)
} else if (!spinsets.includes(spinsetTags[0])){
// Then add all unique spinsets (avoiding to add multiple thumbnails for the same spinset)
uniqueAssets.push(asset)
spinsets.push(spinsetTags[0])
}
})
return sort ? sortAssets(uniqueAssets) : uniqueAssets
}
export const sortAssets = (assets: CtAsset[]): CtAsset[] => {
const sortPropertyName = process.env.NEXT_PUBLIC_COMMERCETOOLS_PROPERTY_SORT || 'sortNumber'
return assets.sort((a, b) => {
const sortOrderA = (a.custom?.fields || {} as any)[sortPropertyName];
const sortOrderB = (b.custom?.fields || {} as any)[sortPropertyName];
if (sortOrderA && sortOrderB) {
return sortOrderA > sortOrderB ? 1 : -1
} else if (sortOrderA) {
return -1
} else {
return 1
}
})
}
export const getImageAssets = (product: any): CtAsset[] =>
getAllAssets(product).filter(
x => !x.sources[0]?.contentType?.startsWith('video'),
) ?? []
export const getAssetName = (asset: any, locale: string | undefined) =>
asset?.name ? asset.name[locale ?? 'en-US'] : ''
export const getAssetDescription = (asset: any, locale: string | undefined) =>
asset?.description ? asset.description[locale ?? 'en-US'] : ''
export const getAssetUri = (asset: CtAsset): string | undefined =>
asset?.sources[0]?.uri
export const getAllVariants = (product: any) => [
product?.masterData?.current?.masterVariant,
...product?.masterData?.current?.variants,
]
export const convertToMediaAsset = (asset: CtAsset) => {
const spinSetTags = asset.tags?.filter(x => x.includes('spinset')) ?? []
let mediaType: 'image' | 'video' | 'spin' | '3d' = 'image'
if (spinSetTags.length > 0) {
mediaType = 'spin'
} else if (asset.sources[0].contentType?.startsWith('video')) {
mediaType = 'video'
} else if (asset.sources[0].contentType?.startsWith('image/glb')) {
mediaType = '3d'
}
return (mediaType === 'spin') ? {
tag: spinSetTags[0],
mediaType: 'spin'
} : {
publicId: asset.sources[0].uri,
mediaType,
}
}