From 7bf920ccc0873a3d7f227e6c9266a8228aa67c27 Mon Sep 17 00:00:00 2001 From: Sol Irvine Date: Fri, 1 Sep 2023 02:32:21 -0700 Subject: [PATCH] fix: Revert issues with cart localization --- app/[locale]/about/page.tsx | 2 +- app/[locale]/bar/page.tsx | 2 +- app/[locale]/company/page.tsx | 2 +- app/[locale]/concept/page.tsx | 2 +- app/[locale]/disclosures/page.tsx | 2 +- app/[locale]/page.tsx | 2 +- app/[locale]/privacy/page.tsx | 2 +- app/[locale]/product/[handle]/layout.tsx | 2 +- app/[locale]/product/[handle]/page.tsx | 2 - app/[locale]/products/page.tsx | 2 +- app/[locale]/shop-list/page.tsx | 2 +- app/[locale]/stories/[handle]/layout.tsx | 2 +- app/[locale]/stories/page.tsx | 2 +- app/[locale]/terms/page.tsx | 2 +- components/cart/actions.ts | 24 ++++------- components/cart/add-many-to-cart.tsx | 3 +- components/cart/index.tsx | 4 +- lib/shopify/index.ts | 54 ++++++------------------ lib/shopify/queries/cart.ts | 3 +- lib/shopify/types.ts | 4 -- 20 files changed, 38 insertions(+), 82 deletions(-) diff --git a/app/[locale]/about/page.tsx b/app/[locale]/about/page.tsx index 390b5f766..a1354a4a0 100644 --- a/app/[locale]/about/page.tsx +++ b/app/[locale]/about/page.tsx @@ -23,7 +23,7 @@ export default async function Page({ params }: { params: { locale?: SupportedLoc let cart; if (cartId) { - cart = await getCart({ cartId, language: params?.locale?.toUpperCase() }); + cart = await getCart(cartId); } const awardsPage = await getPage({ handle: 'awards', language: params?.locale?.toUpperCase() }); diff --git a/app/[locale]/bar/page.tsx b/app/[locale]/bar/page.tsx index 179501e0b..98fe0ea39 100644 --- a/app/[locale]/bar/page.tsx +++ b/app/[locale]/bar/page.tsx @@ -25,7 +25,7 @@ export default async function Page({ params }: { params: { locale?: SupportedLoc let cart; if (cartId) { - cart = await getCart({ cartId, language: params?.locale?.toUpperCase() }); + cart = await getCart(cartId); } return ( diff --git a/app/[locale]/company/page.tsx b/app/[locale]/company/page.tsx index f9ffadcdf..ae9820f10 100644 --- a/app/[locale]/company/page.tsx +++ b/app/[locale]/company/page.tsx @@ -25,7 +25,7 @@ export default async function Page({ params }: { params: { locale?: SupportedLoc let cart; if (cartId) { - cart = await getCart({ cartId, language: params?.locale?.toUpperCase() }); + cart = await getCart(cartId); } return ( diff --git a/app/[locale]/concept/page.tsx b/app/[locale]/concept/page.tsx index 4b9e062af..e23066ec4 100644 --- a/app/[locale]/concept/page.tsx +++ b/app/[locale]/concept/page.tsx @@ -25,7 +25,7 @@ export default async function Page({ params }: { params: { locale?: SupportedLoc let cart; if (cartId) { - cart = await getCart({ cartId, language: params?.locale?.toUpperCase() }); + cart = await getCart(cartId); } return ( diff --git a/app/[locale]/disclosures/page.tsx b/app/[locale]/disclosures/page.tsx index dd370341a..5e4d03468 100644 --- a/app/[locale]/disclosures/page.tsx +++ b/app/[locale]/disclosures/page.tsx @@ -29,7 +29,7 @@ export default async function DisclosuresPage({ let cart; if (cartId) { - cart = await getCart({ cartId, language: locale?.toUpperCase() }); + cart = await getCart(cartId); } return ( diff --git a/app/[locale]/page.tsx b/app/[locale]/page.tsx index db95f6c5f..000cfefeb 100644 --- a/app/[locale]/page.tsx +++ b/app/[locale]/page.tsx @@ -46,7 +46,7 @@ export default async function HomePage({ let cart; if (cartId) { - cart = await getCart({ cartId, language: locale?.toUpperCase() }); + cart = await getCart(cartId); } const promotedItem: Product | undefined = await getProduct({ diff --git a/app/[locale]/privacy/page.tsx b/app/[locale]/privacy/page.tsx index 398e23dca..a45c66492 100644 --- a/app/[locale]/privacy/page.tsx +++ b/app/[locale]/privacy/page.tsx @@ -29,7 +29,7 @@ export default async function PrivacyPage({ let cart; if (cartId) { - cart = await getCart({ cartId, language: locale?.toUpperCase() }); + cart = await getCart(cartId); } return ( diff --git a/app/[locale]/product/[handle]/layout.tsx b/app/[locale]/product/[handle]/layout.tsx index 489b7cc69..a9aebb272 100644 --- a/app/[locale]/product/[handle]/layout.tsx +++ b/app/[locale]/product/[handle]/layout.tsx @@ -28,7 +28,7 @@ export default async function ProductLayout({ let cart; if (cartId) { - cart = await getCart({ cartId, language: locale?.toUpperCase() }); + cart = await getCart(cartId); } return ( diff --git a/app/[locale]/product/[handle]/page.tsx b/app/[locale]/product/[handle]/page.tsx index f381db116..b34f4ccea 100644 --- a/app/[locale]/product/[handle]/page.tsx +++ b/app/[locale]/product/[handle]/page.tsx @@ -72,8 +72,6 @@ export default async function ProductPage({ language: params?.locale?.toUpperCase() }); - console.debug({ product }); - let otherImages: MediaImage[] = []; if (!!product) { otherImages = product.images diff --git a/app/[locale]/products/page.tsx b/app/[locale]/products/page.tsx index 361cf3d3f..cd18759b1 100644 --- a/app/[locale]/products/page.tsx +++ b/app/[locale]/products/page.tsx @@ -29,7 +29,7 @@ export default async function ProductPage({ let cart; if (cartId) { - cart = await getCart({ cartId, language: locale?.toUpperCase() }); + cart = await getCart(cartId); } return ( diff --git a/app/[locale]/shop-list/page.tsx b/app/[locale]/shop-list/page.tsx index 5862f64f2..e44ad1929 100644 --- a/app/[locale]/shop-list/page.tsx +++ b/app/[locale]/shop-list/page.tsx @@ -39,7 +39,7 @@ export default async function Page({ params }: { params: { locale?: SupportedLoc let cart; if (cartId) { - cart = await getCart({ cartId, language: params?.locale?.toUpperCase() }); + cart = await getCart(cartId); } const page = await getPage({ handle: 'shop-list', language: params?.locale?.toUpperCase() }); diff --git a/app/[locale]/stories/[handle]/layout.tsx b/app/[locale]/stories/[handle]/layout.tsx index cd103bd50..928cb9df9 100644 --- a/app/[locale]/stories/[handle]/layout.tsx +++ b/app/[locale]/stories/[handle]/layout.tsx @@ -28,7 +28,7 @@ export default async function BlogLayout({ let cart; if (cartId) { - cart = await getCart({ cartId, language: locale?.toUpperCase() }); + cart = await getCart(cartId); } return ( diff --git a/app/[locale]/stories/page.tsx b/app/[locale]/stories/page.tsx index 9657b0679..ac707eb65 100644 --- a/app/[locale]/stories/page.tsx +++ b/app/[locale]/stories/page.tsx @@ -28,7 +28,7 @@ export default async function StoriesPage({ let cart; if (cartId) { - cart = await getCart({ cartId, language: locale?.toUpperCase() }); + cart = await getCart(cartId); } return ( diff --git a/app/[locale]/terms/page.tsx b/app/[locale]/terms/page.tsx index cedcc1a5c..123c2e28a 100644 --- a/app/[locale]/terms/page.tsx +++ b/app/[locale]/terms/page.tsx @@ -27,7 +27,7 @@ export default async function TermsPage({ let cart; if (cartId) { - cart = await getCart({ cartId, language: locale?.toUpperCase() }); + cart = await getCart(cartId); } return ( diff --git a/components/cart/actions.ts b/components/cart/actions.ts index 285bab4a9..40a74789d 100644 --- a/components/cart/actions.ts +++ b/components/cart/actions.ts @@ -3,20 +3,16 @@ import { addToCart, createCart, getCart, removeFromCart, updateCart } from 'lib/shopify'; import { cookies } from 'next/headers'; -export const addItem = async ( - variantId: string | undefined, - country?: string, - language?: string -): Promise => { +export const addItem = async (variantId: string | undefined): Promise => { let cartId = cookies().get('cartId')?.value; let cart; if (cartId) { - cart = await getCart({ cartId, country, language }); + cart = await getCart(cartId); } if (!cartId || !cart) { - cart = await createCart({ country, language }); + cart = await createCart(); cartId = cart.id; cookies().set('cartId', cartId); } @@ -26,7 +22,7 @@ export const addItem = async ( } try { - await addToCart(cartId, [{ merchandiseId: variantId, quantity: 1 }], country, language); + await addToCart(cartId, [{ merchandiseId: variantId, quantity: 1 }]); } catch (e) { return 'Error adding item to cart'; } @@ -34,24 +30,20 @@ export const addItem = async ( export const addItems = async ({ variantId, - quantity = 1, - country, - language + quantity = 1 }: { variantId: string | undefined; quantity: number; - country?: string; - language?: string; }): Promise => { let cartId = cookies().get('cartId')?.value; let cart; if (cartId) { - cart = await getCart({ cartId, country, language }); + cart = await getCart(cartId); } if (!cartId || !cart) { - cart = await createCart({ country, language }); + cart = await createCart(); cartId = cart.id; cookies().set('cartId', cartId); } @@ -61,7 +53,7 @@ export const addItems = async ({ } try { - await addToCart(cartId, [{ merchandiseId: variantId, quantity }], country, language); + await addToCart(cartId, [{ merchandiseId: variantId, quantity }]); } catch (e) { return quantity === 1 ? 'Error adding item to cart' : 'Error adding items to cart'; } diff --git a/components/cart/add-many-to-cart.tsx b/components/cart/add-many-to-cart.tsx index cf77732bb..abc2b18b5 100644 --- a/components/cart/add-many-to-cart.tsx +++ b/components/cart/add-many-to-cart.tsx @@ -84,8 +84,7 @@ export function AddManyToCart({ startTransition(async () => { const error = await addItems({ variantId: selectedVariantId, - quantity: currentQuantity, - country: locale.toUpperCase() + quantity: currentQuantity }); if (error) { diff --git a/components/cart/index.tsx b/components/cart/index.tsx index 2b98e82d7..3e250ba93 100644 --- a/components/cart/index.tsx +++ b/components/cart/index.tsx @@ -2,12 +2,12 @@ import { getCart } from 'lib/shopify'; import { cookies } from 'next/headers'; import CartModal from './modal'; -export default async function Cart({ country, language }: { country?: string; language?: string }) { +export default async function Cart() { const cartId = cookies().get('cartId')?.value; let cart; if (cartId) { - cart = await getCart({ cartId, country, language }); + cart = await getCart(cartId); } return ; diff --git a/lib/shopify/index.ts b/lib/shopify/index.ts index c28f47259..0117928e3 100644 --- a/lib/shopify/index.ts +++ b/lib/shopify/index.ts @@ -120,18 +120,12 @@ const removeEdgesAndNodes = (array: Connection) => { return array.edges.map((edge) => edge?.node); }; -const reshapeCart = (cart: ShopifyCart, country?: string, language?: string): Cart => { +const reshapeCart = (cart: ShopifyCart): Cart => { if (!cart.cost?.totalTaxAmount) { - cart.cost.totalTaxAmount = - country === 'US' || language === 'EN' - ? { - amount: '0.0', - currencyCode: 'USD' - } - : { - amount: '0', - currencyCode: 'JPY' - }; + cart.cost.totalTaxAmount = { + amount: '0.0', + currencyCode: 'USD' + }; } return { @@ -222,38 +216,24 @@ const reshapeProducts = (products: ShopifyProduct[]) => { return reshapedProducts; }; -export async function createCart({ - country, - language -}: { - country?: string; - language?: string; -}): Promise { +export async function createCart(): Promise { const res = await shopifyFetch({ query: createCartMutation, - cache: 'no-store', - variables: { - country, - language - } + cache: 'no-store' }); - return reshapeCart(res.body.data.cartCreate.cart, country, language); + return reshapeCart(res.body.data.cartCreate.cart); } export async function addToCart( cartId: string, - lines: { merchandiseId: string; quantity: number }[], - country?: string, - language?: string + lines: { merchandiseId: string; quantity: number }[] ): Promise { const res = await shopifyFetch({ query: addToCartMutation, variables: { cartId, - lines, - country, - language + lines }, cache: 'no-store' }); @@ -289,18 +269,10 @@ export async function updateCart( return reshapeCart(res.body.data.cartLinesUpdate.cart); } -export async function getCart({ - cartId, - country, - language -}: { - cartId: string; - country?: string; - language?: string; -}): Promise { +export async function getCart(cartId: string): Promise { const res = await shopifyFetch({ query: getCartQuery, - variables: { cartId, country, language }, + variables: { cartId }, cache: 'no-store' }); @@ -309,7 +281,7 @@ export async function getCart({ return undefined; } - return reshapeCart(res.body.data.cart, country, language); + return reshapeCart(res.body.data.cart); } export async function getCollection({ diff --git a/lib/shopify/queries/cart.ts b/lib/shopify/queries/cart.ts index f26745d50..044e47f66 100644 --- a/lib/shopify/queries/cart.ts +++ b/lib/shopify/queries/cart.ts @@ -1,8 +1,7 @@ import cartFragment from '../fragments/cart'; export const getCartQuery = /* GraphQL */ ` - query getCart($cartId: ID!, $country: CountryCode, $language: LanguageCode) - @inContext(country: $country, language: $language) { + query getCart($cartId: ID!) { cart(id: $cartId) { ...cart } diff --git a/lib/shopify/types.ts b/lib/shopify/types.ts index 855a2ed6d..9d3f71a44 100644 --- a/lib/shopify/types.ts +++ b/lib/shopify/types.ts @@ -189,10 +189,6 @@ export type ShopifyCartOperation = { export type ShopifyCreateCartOperation = { data: { cartCreate: { cart: ShopifyCart } }; - variables: { - country?: string; - language?: string; - }; }; export type ShopifyAddToCartOperation = {