fix: Revert issues with cart localization

This commit is contained in:
Sol Irvine 2023-09-01 02:32:21 -07:00
parent f1bd230b81
commit 7bf920ccc0
20 changed files with 38 additions and 82 deletions

View File

@ -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() });

View File

@ -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 (

View File

@ -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 (

View File

@ -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 (

View File

@ -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 (

View File

@ -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({

View File

@ -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 (

View File

@ -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 (

View File

@ -72,8 +72,6 @@ export default async function ProductPage({
language: params?.locale?.toUpperCase()
});
console.debug({ product });
let otherImages: MediaImage[] = [];
if (!!product) {
otherImages = product.images

View File

@ -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 (

View File

@ -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() });

View File

@ -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 (

View File

@ -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 (

View File

@ -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 (

View File

@ -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<String | undefined> => {
export const addItem = async (variantId: string | undefined): Promise<String | undefined> => {
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<String | undefined> => {
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';
}

View File

@ -84,8 +84,7 @@ export function AddManyToCart({
startTransition(async () => {
const error = await addItems({
variantId: selectedVariantId,
quantity: currentQuantity,
country: locale.toUpperCase()
quantity: currentQuantity
});
if (error) {

View File

@ -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 <CartModal cart={cart} />;

View File

@ -120,18 +120,12 @@ const removeEdgesAndNodes = (array: Connection<any>) => {
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<Cart> {
export async function createCart(): Promise<Cart> {
const res = await shopifyFetch<ShopifyCreateCartOperation>({
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<Cart> {
const res = await shopifyFetch<ShopifyAddToCartOperation>({
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<Cart | undefined> {
export async function getCart(cartId: string): Promise<Cart | undefined> {
const res = await shopifyFetch<ShopifyCartOperation>({
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({

View File

@ -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
}

View File

@ -189,10 +189,6 @@ export type ShopifyCartOperation = {
export type ShopifyCreateCartOperation = {
data: { cartCreate: { cart: ShopifyCart } };
variables: {
country?: string;
language?: string;
};
};
export type ShopifyAddToCartOperation = {