4
0
forked from crowetic/commerce

Adding immutable normalizer for Product

This commit is contained in:
bc 2021-01-18 13:23:37 -03:00
parent de0ba8cee8
commit 64b25ef70b
4 changed files with 54 additions and 47 deletions

View File

@ -1,13 +1,16 @@
import { Cart, CartItem, Product } from '../../types'
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 {
id: entityId,
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 {
entityId: id,
images,
variants,
productOptions,
prices,
path,
id: _,
options: _0,
brand,
...rest
} = productNode
return {
id,
path,
slug: path?.replace(/^\/+|\/+$/g, ''),
images: images.edges
? images.edges.map(
({ node: { urlOriginal, altText, ...rest } }: any) => ({
url: urlOriginal,
alt: altText,
...rest,
})
)
: [],
variants: variants.edges
? variants.edges.map(
({ node: { entityId, productOptions, ...rest } }: any) => ({
id: entityId,
options: productOptions?.edges
? productOptions.edges.map(normalizeProductOption)
: [],
...rest,
})
)
: [],
options: productOptions.edges
? productOptions?.edges.map(normalizeProductOption)
: [],
return update(productNode, {
id: { $set: String(id) },
images: {
$apply: ({edges} : any) => edges?.map(
({ node: { urlOriginal, altText, ...rest } }: any) => ({
url: urlOriginal,
alt: altText,
...rest,
})
)
},
variants: {
$apply: ({edges} : any) => edges?.map(
({ node: { entityId, productOptions, ...rest } }: any) => ({
id: entityId,
options: productOptions?.edges
? productOptions.edges.map(normalizeProductOption)
: [],
...rest,
})
)
},
options: {
$set: productOptions.edges ? productOptions?.edges.map(normalizeProductOption) : []
},
brand: {
id: brand?.entityId ? brand?.entityId : null,
...brand,
$apply:(brand : any) => brand?.entityId ? brand?.entityId : null,
},
slug: {
$set: path?.replace(/^\/+|\/+$/g, '')
},
price: {
value: prices?.price.value,
currencyCode: prices?.price.currencyCode,
},
...rest,
}
$set: {
value: prices?.price.value,
currencyCode: prices?.price.currencyCode,
}
},
$unset: ['entityId']
})
}
export function normalizeCart({ data, ...rest }: any): Cart {

View File

@ -8,7 +8,7 @@ interface Entity {
interface Product extends Entity {
name: string
description: string
slug: string
slug?: string
path?: string
images: ProductImage[]
variants: ProductVariant[]

View File

@ -53,6 +53,7 @@
"cookie": "^0.4.1",
"dot-object": "^2.1.4",
"email-validator": "^2.0.4",
"immutability-helper": "^3.1.1",
"js-cookie": "^2.2.1",
"keen-slider": "^5.2.4",
"lodash.debounce": "^4.0.8",

View File

@ -3958,6 +3958,11 @@ ignore@^5.1.4:
resolved "https://registry.yarnpkg.com/ignore/-/ignore-5.1.8.tgz#f150a8b50a34289b33e22f5889abd4d8016f0e57"
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:
version "3.7.6"
resolved "https://registry.yarnpkg.com/immutable/-/immutable-3.7.6.tgz#13b4d3cb12befa15482a26fe1b2ebae640071e4b"