mirror of
https://github.com/vercel/commerce.git
synced 2025-04-23 19:37:51 +00:00
Adding immutable normalizer for Product
This commit is contained in:
parent
de0ba8cee8
commit
64b25ef70b
@ -1,13 +1,16 @@
|
|||||||
import { Cart, CartItem, Product } from '../../types'
|
import { Cart, CartItem, Product } from '../../types'
|
||||||
import { Product as BigCommerceProduct } from '@framework/schema'
|
import { Product as BigCommerceProduct } from '@framework/schema'
|
||||||
|
import update from "immutability-helper"
|
||||||
|
|
||||||
|
function normalizeProductOption(productOption:any) {
|
||||||
|
const {
|
||||||
|
node: {
|
||||||
|
entityId,
|
||||||
|
values: { edges },
|
||||||
|
...rest
|
||||||
|
},
|
||||||
|
} = productOption;
|
||||||
|
|
||||||
function normalizeProductOption({
|
|
||||||
node: {
|
|
||||||
entityId,
|
|
||||||
values: { edges },
|
|
||||||
...rest
|
|
||||||
},
|
|
||||||
}: any) {
|
|
||||||
return {
|
return {
|
||||||
id: entityId,
|
id: entityId,
|
||||||
values: edges?.map(({ node }: any) => node),
|
values: edges?.map(({ node }: any) => node),
|
||||||
@ -15,57 +18,55 @@ function normalizeProductOption({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function normalizeProduct(productNode: BigCommerceProduct): Product {
|
export function normalizeProduct(productNode: any): Product {
|
||||||
const {
|
const {
|
||||||
entityId: id,
|
entityId: id,
|
||||||
images,
|
|
||||||
variants,
|
|
||||||
productOptions,
|
productOptions,
|
||||||
prices,
|
prices,
|
||||||
path,
|
path,
|
||||||
id: _,
|
id: _,
|
||||||
options: _0,
|
options: _0,
|
||||||
brand,
|
|
||||||
...rest
|
|
||||||
} = productNode
|
} = productNode
|
||||||
|
|
||||||
return {
|
return update(productNode, {
|
||||||
id,
|
id: { $set: String(id) },
|
||||||
path,
|
images: {
|
||||||
slug: path?.replace(/^\/+|\/+$/g, ''),
|
$apply: ({edges} : any) => edges?.map(
|
||||||
images: images.edges
|
({ node: { urlOriginal, altText, ...rest } }: any) => ({
|
||||||
? images.edges.map(
|
url: urlOriginal,
|
||||||
({ node: { urlOriginal, altText, ...rest } }: any) => ({
|
alt: altText,
|
||||||
url: urlOriginal,
|
...rest,
|
||||||
alt: altText,
|
})
|
||||||
...rest,
|
)
|
||||||
})
|
},
|
||||||
)
|
variants: {
|
||||||
: [],
|
$apply: ({edges} : any) => edges?.map(
|
||||||
variants: variants.edges
|
({ node: { entityId, productOptions, ...rest } }: any) => ({
|
||||||
? variants.edges.map(
|
id: entityId,
|
||||||
({ node: { entityId, productOptions, ...rest } }: any) => ({
|
options: productOptions?.edges
|
||||||
id: entityId,
|
? productOptions.edges.map(normalizeProductOption)
|
||||||
options: productOptions?.edges
|
: [],
|
||||||
? productOptions.edges.map(normalizeProductOption)
|
...rest,
|
||||||
: [],
|
})
|
||||||
...rest,
|
)
|
||||||
})
|
},
|
||||||
)
|
options: {
|
||||||
: [],
|
$set: productOptions.edges ? productOptions?.edges.map(normalizeProductOption) : []
|
||||||
options: productOptions.edges
|
},
|
||||||
? productOptions?.edges.map(normalizeProductOption)
|
|
||||||
: [],
|
|
||||||
brand: {
|
brand: {
|
||||||
id: brand?.entityId ? brand?.entityId : null,
|
$apply:(brand : any) => brand?.entityId ? brand?.entityId : null,
|
||||||
...brand,
|
},
|
||||||
|
slug: {
|
||||||
|
$set: path?.replace(/^\/+|\/+$/g, '')
|
||||||
},
|
},
|
||||||
price: {
|
price: {
|
||||||
value: prices?.price.value,
|
$set: {
|
||||||
currencyCode: prices?.price.currencyCode,
|
value: prices?.price.value,
|
||||||
},
|
currencyCode: prices?.price.currencyCode,
|
||||||
...rest,
|
}
|
||||||
}
|
},
|
||||||
|
$unset: ['entityId']
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export function normalizeCart({ data, ...rest }: any): Cart {
|
export function normalizeCart({ data, ...rest }: any): Cart {
|
||||||
|
2
framework/types.d.ts
vendored
2
framework/types.d.ts
vendored
@ -8,7 +8,7 @@ interface Entity {
|
|||||||
interface Product extends Entity {
|
interface Product extends Entity {
|
||||||
name: string
|
name: string
|
||||||
description: string
|
description: string
|
||||||
slug: string
|
slug?: string
|
||||||
path?: string
|
path?: string
|
||||||
images: ProductImage[]
|
images: ProductImage[]
|
||||||
variants: ProductVariant[]
|
variants: ProductVariant[]
|
||||||
|
@ -53,6 +53,7 @@
|
|||||||
"cookie": "^0.4.1",
|
"cookie": "^0.4.1",
|
||||||
"dot-object": "^2.1.4",
|
"dot-object": "^2.1.4",
|
||||||
"email-validator": "^2.0.4",
|
"email-validator": "^2.0.4",
|
||||||
|
"immutability-helper": "^3.1.1",
|
||||||
"js-cookie": "^2.2.1",
|
"js-cookie": "^2.2.1",
|
||||||
"keen-slider": "^5.2.4",
|
"keen-slider": "^5.2.4",
|
||||||
"lodash.debounce": "^4.0.8",
|
"lodash.debounce": "^4.0.8",
|
||||||
|
@ -3958,6 +3958,11 @@ ignore@^5.1.4:
|
|||||||
resolved "https://registry.yarnpkg.com/ignore/-/ignore-5.1.8.tgz#f150a8b50a34289b33e22f5889abd4d8016f0e57"
|
resolved "https://registry.yarnpkg.com/ignore/-/ignore-5.1.8.tgz#f150a8b50a34289b33e22f5889abd4d8016f0e57"
|
||||||
integrity sha512-BMpfD7PpiETpBl/A6S498BaIJ6Y/ABT93ETbby2fP00v4EbvPBXWEoaR1UBPKs3iR53pJY7EtZk5KACI57i1Uw==
|
integrity sha512-BMpfD7PpiETpBl/A6S498BaIJ6Y/ABT93ETbby2fP00v4EbvPBXWEoaR1UBPKs3iR53pJY7EtZk5KACI57i1Uw==
|
||||||
|
|
||||||
|
immutability-helper@^3.1.1:
|
||||||
|
version "3.1.1"
|
||||||
|
resolved "https://registry.yarnpkg.com/immutability-helper/-/immutability-helper-3.1.1.tgz#2b86b2286ed3b1241c9e23b7b21e0444f52f77b7"
|
||||||
|
integrity sha512-Q0QaXjPjwIju/28TsugCHNEASwoCcJSyJV3uO1sOIQGI0jKgm9f41Lvz0DZj3n46cNCyAZTsEYoY4C2bVRUzyQ==
|
||||||
|
|
||||||
immutable@~3.7.6:
|
immutable@~3.7.6:
|
||||||
version "3.7.6"
|
version "3.7.6"
|
||||||
resolved "https://registry.yarnpkg.com/immutable/-/immutable-3.7.6.tgz#13b4d3cb12befa15482a26fe1b2ebae640071e4b"
|
resolved "https://registry.yarnpkg.com/immutable/-/immutable-3.7.6.tgz#13b4d3cb12befa15482a26fe1b2ebae640071e4b"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user