fix: Localize cart

This commit is contained in:
Sol Irvine 2023-08-31 20:59:02 -07:00
parent b7d78faaf0
commit 5f664a03c3
4 changed files with 24 additions and 22 deletions

View File

@ -45,9 +45,7 @@ export default async function HomePage({
let cart; let cart;
if (cartId) { if (cartId) {
console.debug({ cartId });
cart = await getCart({ cartId, language: locale?.toUpperCase() }); cart = await getCart({ cartId, language: locale?.toUpperCase() });
console.debug({ cart });
} }
return ( return (

View File

@ -1,25 +1,22 @@
'use server'; 'use server';
import { SupportedLocale } from 'components/layout/navbar/language-control';
import { addToCart, createCart, getCart, removeFromCart, updateCart } from 'lib/shopify'; import { addToCart, createCart, getCart, removeFromCart, updateCart } from 'lib/shopify';
import { cookies } from 'next/headers'; import { cookies } from 'next/headers';
export const addItem = async ({ export const addItem = async (
variantId, variantId: string | undefined,
locale country?: string,
}: { language?: string
variantId: string | undefined; ): Promise<String | undefined> => {
locale?: string;
}): Promise<String | undefined> => {
let cartId = cookies().get('cartId')?.value; let cartId = cookies().get('cartId')?.value;
let cart; let cart;
if (cartId) { if (cartId) {
cart = await getCart({ cartId, language: locale?.toUpperCase() }); cart = await getCart({ cartId, country, language });
} }
if (!cartId || !cart) { if (!cartId || !cart) {
cart = await createCart(); cart = await createCart({ country, language });
cartId = cart.id; cartId = cart.id;
cookies().set('cartId', cartId); cookies().set('cartId', cartId);
} }
@ -29,7 +26,7 @@ export const addItem = async ({
} }
try { try {
await addToCart(cartId, [{ merchandiseId: variantId, quantity: 1 }]); await addToCart(cartId, [{ merchandiseId: variantId, quantity: 1 }], country, language);
} catch (e) { } catch (e) {
return 'Error adding item to cart'; return 'Error adding item to cart';
} }
@ -38,21 +35,23 @@ export const addItem = async ({
export const addItems = async ({ export const addItems = async ({
variantId, variantId,
quantity = 1, quantity = 1,
locale country,
language
}: { }: {
variantId: string | undefined; variantId: string | undefined;
quantity: number; quantity: number;
locale?: SupportedLocale; country?: string;
language?: string;
}): Promise<String | undefined> => { }): Promise<String | undefined> => {
let cartId = cookies().get('cartId')?.value; let cartId = cookies().get('cartId')?.value;
let cart; let cart;
if (cartId) { if (cartId) {
cart = await getCart({ cartId, language: locale?.toUpperCase() }); cart = await getCart({ cartId, country, language });
} }
if (!cartId || !cart) { if (!cartId || !cart) {
cart = await createCart(); cart = await createCart({ country, language });
cartId = cart.id; cartId = cart.id;
cookies().set('cartId', cartId); cookies().set('cartId', cartId);
} }
@ -62,7 +61,7 @@ export const addItems = async ({
} }
try { try {
await addToCart(cartId, [{ merchandiseId: variantId, quantity }]); await addToCart(cartId, [{ merchandiseId: variantId, quantity }], country, language);
} catch (e) { } catch (e) {
return quantity === 1 ? 'Error adding item to cart' : 'Error adding items to cart'; return quantity === 1 ? 'Error adding item to cart' : 'Error adding items to cart';
} }

View File

@ -1,14 +1,13 @@
import { SupportedLocale } from 'components/layout/navbar/language-control';
import { getCart } from 'lib/shopify'; import { getCart } from 'lib/shopify';
import { cookies } from 'next/headers'; import { cookies } from 'next/headers';
import CartModal from './modal'; 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; const cartId = cookies().get('cartId')?.value;
let cart; let cart;
if (cartId) { if (cartId) {
cart = await getCart({ cartId, language: locale?.toUpperCase() }); cart = await getCart({ cartId, country, language });
} }
return <CartModal cart={cart} />; return <CartModal cart={cart} />;

View File

@ -176,13 +176,17 @@ export type ShopifyCartOperation = {
}; };
variables: { variables: {
cartId: string; cartId: string;
language?: string;
country?: string; country?: string;
language?: string;
}; };
}; };
export type ShopifyCreateCartOperation = { export type ShopifyCreateCartOperation = {
data: { cartCreate: { cart: ShopifyCart } }; data: { cartCreate: { cart: ShopifyCart } };
variables: {
country?: string;
language?: string;
};
}; };
export type ShopifyAddToCartOperation = { export type ShopifyAddToCartOperation = {
@ -197,6 +201,8 @@ export type ShopifyAddToCartOperation = {
merchandiseId: string; merchandiseId: string;
quantity: number; quantity: number;
}[]; }[];
country?: string;
language?: string;
}; };
}; };