From 5f664a03c3b00b1de58629dde55ea305a5ce87c7 Mon Sep 17 00:00:00 2001 From: Sol Irvine Date: Thu, 31 Aug 2023 20:59:02 -0700 Subject: [PATCH] fix: Localize cart --- app/[locale]/page.tsx | 2 -- components/cart/actions.ts | 31 +++++++++++++++---------------- components/cart/index.tsx | 5 ++--- lib/shopify/types.ts | 8 +++++++- 4 files changed, 24 insertions(+), 22 deletions(-) diff --git a/app/[locale]/page.tsx b/app/[locale]/page.tsx index b3a64ccaa..c041baa2f 100644 --- a/app/[locale]/page.tsx +++ b/app/[locale]/page.tsx @@ -45,9 +45,7 @@ export default async function HomePage({ let cart; if (cartId) { - console.debug({ cartId }); cart = await getCart({ cartId, language: locale?.toUpperCase() }); - console.debug({ cart }); } return ( diff --git a/components/cart/actions.ts b/components/cart/actions.ts index f91dbbe98..285bab4a9 100644 --- a/components/cart/actions.ts +++ b/components/cart/actions.ts @@ -1,25 +1,22 @@ 'use server'; -import { SupportedLocale } from 'components/layout/navbar/language-control'; import { addToCart, createCart, getCart, removeFromCart, updateCart } from 'lib/shopify'; import { cookies } from 'next/headers'; -export const addItem = async ({ - variantId, - locale -}: { - variantId: string | undefined; - locale?: string; -}): Promise => { +export const addItem = async ( + variantId: string | undefined, + country?: string, + language?: string +): Promise => { let cartId = cookies().get('cartId')?.value; let cart; if (cartId) { - cart = await getCart({ cartId, language: locale?.toUpperCase() }); + cart = await getCart({ cartId, country, language }); } if (!cartId || !cart) { - cart = await createCart(); + cart = await createCart({ country, language }); cartId = cart.id; cookies().set('cartId', cartId); } @@ -29,7 +26,7 @@ export const addItem = async ({ } try { - await addToCart(cartId, [{ merchandiseId: variantId, quantity: 1 }]); + await addToCart(cartId, [{ merchandiseId: variantId, quantity: 1 }], country, language); } catch (e) { return 'Error adding item to cart'; } @@ -38,21 +35,23 @@ export const addItem = async ({ export const addItems = async ({ variantId, quantity = 1, - locale + country, + language }: { variantId: string | undefined; quantity: number; - locale?: SupportedLocale; + country?: string; + language?: string; }): Promise => { let cartId = cookies().get('cartId')?.value; let cart; if (cartId) { - cart = await getCart({ cartId, language: locale?.toUpperCase() }); + cart = await getCart({ cartId, country, language }); } if (!cartId || !cart) { - cart = await createCart(); + cart = await createCart({ country, language }); cartId = cart.id; cookies().set('cartId', cartId); } @@ -62,7 +61,7 @@ export const addItems = async ({ } try { - await addToCart(cartId, [{ merchandiseId: variantId, quantity }]); + await addToCart(cartId, [{ merchandiseId: variantId, quantity }], country, language); } catch (e) { return quantity === 1 ? 'Error adding item to cart' : 'Error adding items to cart'; } diff --git a/components/cart/index.tsx b/components/cart/index.tsx index 3ed3c037d..2b98e82d7 100644 --- a/components/cart/index.tsx +++ b/components/cart/index.tsx @@ -1,14 +1,13 @@ -import { SupportedLocale } from 'components/layout/navbar/language-control'; import { getCart } from 'lib/shopify'; import { cookies } from 'next/headers'; import CartModal from './modal'; -export default async function Cart({ locale }: { locale?: SupportedLocale }) { +export default async function Cart({ country, language }: { country?: string; language?: string }) { const cartId = cookies().get('cartId')?.value; let cart; if (cartId) { - cart = await getCart({ cartId, language: locale?.toUpperCase() }); + cart = await getCart({ cartId, country, language }); } return ; diff --git a/lib/shopify/types.ts b/lib/shopify/types.ts index caec6e4ae..370c71a44 100644 --- a/lib/shopify/types.ts +++ b/lib/shopify/types.ts @@ -176,13 +176,17 @@ export type ShopifyCartOperation = { }; variables: { cartId: string; - language?: string; country?: string; + language?: string; }; }; export type ShopifyCreateCartOperation = { data: { cartCreate: { cart: ShopifyCart } }; + variables: { + country?: string; + language?: string; + }; }; export type ShopifyAddToCartOperation = { @@ -197,6 +201,8 @@ export type ShopifyAddToCartOperation = { merchandiseId: string; quantity: number; }[]; + country?: string; + language?: string; }; };