From 66dd5eae90d7c883b9b98eaebc58fd9f4735e667 Mon Sep 17 00:00:00 2001 From: Sol Irvine Date: Thu, 31 Aug 2023 20:59:25 -0700 Subject: [PATCH] wip: Saving work --- lib/shopify/index.ts | 54 ++++++++++++++++++++++++++--------- lib/shopify/mutations/cart.ts | 19 +++++++++--- 2 files changed, 56 insertions(+), 17 deletions(-) diff --git a/lib/shopify/index.ts b/lib/shopify/index.ts index 0117928e3..c28f47259 100644 --- a/lib/shopify/index.ts +++ b/lib/shopify/index.ts @@ -120,12 +120,18 @@ const removeEdgesAndNodes = (array: Connection) => { return array.edges.map((edge) => edge?.node); }; -const reshapeCart = (cart: ShopifyCart): Cart => { +const reshapeCart = (cart: ShopifyCart, country?: string, language?: string): Cart => { if (!cart.cost?.totalTaxAmount) { - cart.cost.totalTaxAmount = { - amount: '0.0', - currencyCode: 'USD' - }; + cart.cost.totalTaxAmount = + country === 'US' || language === 'EN' + ? { + amount: '0.0', + currencyCode: 'USD' + } + : { + amount: '0', + currencyCode: 'JPY' + }; } return { @@ -216,24 +222,38 @@ const reshapeProducts = (products: ShopifyProduct[]) => { return reshapedProducts; }; -export async function createCart(): Promise { +export async function createCart({ + country, + language +}: { + country?: string; + language?: string; +}): Promise { const res = await shopifyFetch({ query: createCartMutation, - cache: 'no-store' + cache: 'no-store', + variables: { + country, + language + } }); - return reshapeCart(res.body.data.cartCreate.cart); + return reshapeCart(res.body.data.cartCreate.cart, country, language); } export async function addToCart( cartId: string, - lines: { merchandiseId: string; quantity: number }[] + lines: { merchandiseId: string; quantity: number }[], + country?: string, + language?: string ): Promise { const res = await shopifyFetch({ query: addToCartMutation, variables: { cartId, - lines + lines, + country, + language }, cache: 'no-store' }); @@ -269,10 +289,18 @@ export async function updateCart( return reshapeCart(res.body.data.cartLinesUpdate.cart); } -export async function getCart(cartId: string): Promise { +export async function getCart({ + cartId, + country, + language +}: { + cartId: string; + country?: string; + language?: string; +}): Promise { const res = await shopifyFetch({ query: getCartQuery, - variables: { cartId }, + variables: { cartId, country, language }, cache: 'no-store' }); @@ -281,7 +309,7 @@ export async function getCart(cartId: string): Promise { return undefined; } - return reshapeCart(res.body.data.cart); + return reshapeCart(res.body.data.cart, country, language); } export async function getCollection({ diff --git a/lib/shopify/mutations/cart.ts b/lib/shopify/mutations/cart.ts index 4cc1b5ac6..07e3d02d1 100644 --- a/lib/shopify/mutations/cart.ts +++ b/lib/shopify/mutations/cart.ts @@ -1,7 +1,12 @@ import cartFragment from '../fragments/cart'; export const addToCartMutation = /* GraphQL */ ` - mutation addToCart($cartId: ID!, $lines: [CartLineInput!]!) { + mutation addToCart( + $cartId: ID! + $lines: [CartLineInput!]! + $country: CountryCode + $language: LanguageCode + ) @inContext($country: String, $language: String) { cartLinesAdd(cartId: $cartId, lines: $lines) { cart { ...cart @@ -12,7 +17,11 @@ export const addToCartMutation = /* GraphQL */ ` `; export const createCartMutation = /* GraphQL */ ` - mutation createCart($lineItems: [CartLineInput!]) { + mutation createCart( + $lineItems: [CartLineInput!], + $country: CountryCode, + $language: LanguageCode + ) @inContext($country: String, $language: String) { cartCreate(input: { lines: $lineItems }) { cart { ...cart @@ -23,7 +32,8 @@ export const createCartMutation = /* GraphQL */ ` `; export const editCartItemsMutation = /* GraphQL */ ` - mutation editCartItems($cartId: ID!, $lines: [CartLineUpdateInput!]!) { + mutation editCartItems($cartId: ID!, $lines: [CartLineUpdateInput!]!, $country: CountryCode, $language: LanguageCode) + @inContext($country: String, $language: String) { cartLinesUpdate(cartId: $cartId, lines: $lines) { cart { ...cart @@ -34,7 +44,8 @@ export const editCartItemsMutation = /* GraphQL */ ` `; export const removeFromCartMutation = /* GraphQL */ ` - mutation removeFromCart($cartId: ID!, $lineIds: [ID!]!) { + mutation removeFromCart($cartId: ID!, $lineIds: [ID!]!, $country: CountryCode, $language: LanguageCode) + @inContext($country: String, $language: String) { cartLinesRemove(cartId: $cartId, lineIds: $lineIds) { cart { ...cart