4
0
forked from crowetic/commerce

Updated cart item, fixed deprecations

This commit is contained in:
cond0r 2021-02-22 15:00:32 +02:00
parent 528d7556a8
commit 849b0275f0
5 changed files with 52 additions and 20 deletions

View File

@ -12,7 +12,9 @@ export interface Cart extends Core.Cart {
lineItems: LineItem[] lineItems: LineItem[]
} }
export interface LineItem extends Core.LineItem {} export interface LineItem extends Core.LineItem {
options: any[]
}
/** /**
* Cart mutations * Cart mutations

View File

@ -40,8 +40,9 @@ const normalizeProductImages = ({ edges }: ImageConnection) =>
...rest, ...rest,
})) }))
const normalizeProductVariants = ({ edges }: ProductVariantConnection) => const normalizeProductVariants = ({ edges }: ProductVariantConnection) => {
edges?.map(({ node: { id, selectedOptions } }) => ({ console.log(edges)
return edges?.map(({ node: { id, selectedOptions } }) => ({
id, id,
options: selectedOptions.map(({ name, value }: SelectedOption) => options: selectedOptions.map(({ name, value }: SelectedOption) =>
normalizeProductOption({ normalizeProductOption({
@ -51,6 +52,7 @@ const normalizeProductVariants = ({ edges }: ProductVariantConnection) =>
}) })
), ),
})) }))
}
export function normalizeProduct(productNode: ShopifyProduct): any { export function normalizeProduct(productNode: ShopifyProduct): any {
const { const {
@ -66,7 +68,7 @@ export function normalizeProduct(productNode: ShopifyProduct): any {
...rest ...rest
} = productNode } = productNode
return { const product = {
id, id,
name, name,
vendor, vendor,
@ -79,6 +81,8 @@ export function normalizeProduct(productNode: ShopifyProduct): any {
options: options ? options.map((o) => normalizeProductOption(o)) : [], options: options ? options.map((o) => normalizeProductOption(o)) : [],
...rest, ...rest,
} }
return product
} }
export function normalizeCart(checkout: Checkout): Cart { export function normalizeCart(checkout: Checkout): Cart {
@ -88,13 +92,13 @@ export function normalizeCart(checkout: Checkout): Cart {
email: '', email: '',
createdAt: checkout.createdAt, createdAt: checkout.createdAt,
currency: { currency: {
code: checkout.currencyCode, code: checkout.totalPriceV2?.currencyCode,
}, },
taxesIncluded: checkout.taxesIncluded, taxesIncluded: checkout.taxesIncluded,
lineItems: checkout.lineItems?.edges.map(normalizeLineItem), lineItems: checkout.lineItems?.edges.map(normalizeLineItem),
lineItemsSubtotalPrice: checkout.subtotalPrice, lineItemsSubtotalPrice: checkout.subtotalPriceV2?.amount,
subtotalPrice: checkout.subtotalPrice, subtotalPrice: checkout.subtotalPriceV2?.amount,
totalPrice: checkout.totalPrice, totalPrice: checkout.totalPriceV2?.amount,
discounts: [], discounts: [],
} }
} }
@ -106,7 +110,7 @@ function normalizeLineItem({
id, id,
variantId: String(variant?.id), variantId: String(variant?.id),
productId: String(variant?.id), productId: String(variant?.id),
name: `${title} - ${variant?.title}`, name: `${title}`,
quantity, quantity,
variant: { variant: {
id: String(variant?.id), id: String(variant?.id),
@ -116,10 +120,15 @@ function normalizeLineItem({
url: variant?.image?.originalSrc, url: variant?.image?.originalSrc,
}, },
requiresShipping: variant?.requiresShipping ?? false, requiresShipping: variant?.requiresShipping ?? false,
price: variant?.price, price: variant?.priceV2?.amount,
listPrice: variant?.compareAtPrice, listPrice: variant?.compareAtPriceV2?.amount,
}, },
path: '', path: '',
discounts: [], discounts: [],
options: [
{
value: variant?.title,
},
],
} }
} }

View File

@ -1,10 +1,18 @@
export const checkoutDetailsFragment = ` export const checkoutDetailsFragment = `
id id
webUrl webUrl
subtotalPrice subtotalPriceV2{
totalTax amount
totalPrice
currencyCode currencyCode
}
totalTaxV2 {
amount
currencyCode
}
totalPriceV2 {
amount
currencyCode
}
completedAt completedAt
createdAt createdAt
taxesIncluded taxesIncluded
@ -27,7 +35,14 @@ export const checkoutDetailsFragment = `
width width
height height
} }
price priceV2{
amount
currencyCode
}
compareAtPriceV2{
amount
currencyCode
}
} }
quantity quantity
} }

View File

@ -36,8 +36,14 @@ const getProductQuery = /* GraphQL */ `
name name
value value
} }
price priceV2 {
compareAtPrice amount
currencyCode
}
compareAtPriceV2 {
amount
currencyCode
}
} }
} }
} }

View File

@ -22,8 +22,8 @@
"@utils/*": ["utils/*"], "@utils/*": ["utils/*"],
"@commerce/*": ["framework/commerce/*"], "@commerce/*": ["framework/commerce/*"],
"@commerce": ["framework/commerce"], "@commerce": ["framework/commerce"],
"@framework/*": ["framework/bigcommerce/*"], "@framework/*": ["framework/shopify/*"],
"@framework": ["framework/bigcommerce"] "@framework": ["framework/shopify"]
} }
}, },
"include": ["next-env.d.ts", "**/*.d.ts", "**/*.ts", "**/*.tsx", "**/*.js"], "include": ["next-env.d.ts", "**/*.d.ts", "**/*.ts", "**/*.tsx", "**/*.js"],