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; let cart;
if (cartId) { if (cartId) {
cart = await getCart({ cartId, language: params?.locale?.toUpperCase() }); cart = await getCart(cartId);
} }
const awardsPage = await getPage({ handle: 'awards', language: params?.locale?.toUpperCase() }); 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; let cart;
if (cartId) { if (cartId) {
cart = await getCart({ cartId, language: params?.locale?.toUpperCase() }); cart = await getCart(cartId);
} }
return ( return (

View File

@ -25,7 +25,7 @@ export default async function Page({ params }: { params: { locale?: SupportedLoc
let cart; let cart;
if (cartId) { if (cartId) {
cart = await getCart({ cartId, language: params?.locale?.toUpperCase() }); cart = await getCart(cartId);
} }
return ( return (

View File

@ -25,7 +25,7 @@ export default async function Page({ params }: { params: { locale?: SupportedLoc
let cart; let cart;
if (cartId) { if (cartId) {
cart = await getCart({ cartId, language: params?.locale?.toUpperCase() }); cart = await getCart(cartId);
} }
return ( return (

View File

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

View File

@ -46,7 +46,7 @@ export default async function HomePage({
let cart; let cart;
if (cartId) { if (cartId) {
cart = await getCart({ cartId, language: locale?.toUpperCase() }); cart = await getCart(cartId);
} }
const promotedItem: Product | undefined = await getProduct({ const promotedItem: Product | undefined = await getProduct({

View File

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

View File

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

View File

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

View File

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

View File

@ -39,7 +39,7 @@ export default async function Page({ params }: { params: { locale?: SupportedLoc
let cart; let cart;
if (cartId) { 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() }); const page = await getPage({ handle: 'shop-list', language: params?.locale?.toUpperCase() });

View File

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

View File

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

View File

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

View File

@ -3,20 +3,16 @@
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: string | undefined): Promise<String | undefined> => {
variantId: string | undefined,
country?: string,
language?: 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, country, language }); cart = await getCart(cartId);
} }
if (!cartId || !cart) { if (!cartId || !cart) {
cart = await createCart({ country, language }); cart = await createCart();
cartId = cart.id; cartId = cart.id;
cookies().set('cartId', cartId); cookies().set('cartId', cartId);
} }
@ -26,7 +22,7 @@ export const addItem = async (
} }
try { try {
await addToCart(cartId, [{ merchandiseId: variantId, quantity: 1 }], country, language); await addToCart(cartId, [{ merchandiseId: variantId, quantity: 1 }]);
} catch (e) { } catch (e) {
return 'Error adding item to cart'; return 'Error adding item to cart';
} }
@ -34,24 +30,20 @@ export const addItem = async (
export const addItems = async ({ export const addItems = async ({
variantId, variantId,
quantity = 1, quantity = 1
country,
language
}: { }: {
variantId: string | undefined; variantId: string | undefined;
quantity: number; quantity: number;
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, country, language }); cart = await getCart(cartId);
} }
if (!cartId || !cart) { if (!cartId || !cart) {
cart = await createCart({ country, language }); cart = await createCart();
cartId = cart.id; cartId = cart.id;
cookies().set('cartId', cartId); cookies().set('cartId', cartId);
} }
@ -61,7 +53,7 @@ export const addItems = async ({
} }
try { try {
await addToCart(cartId, [{ merchandiseId: variantId, quantity }], country, language); await addToCart(cartId, [{ merchandiseId: variantId, quantity }]);
} 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

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

View File

@ -2,12 +2,12 @@ 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({ country, language }: { country?: string; language?: string }) { export default async function Cart() {
const cartId = cookies().get('cartId')?.value; const cartId = cookies().get('cartId')?.value;
let cart; let cart;
if (cartId) { if (cartId) {
cart = await getCart({ cartId, country, language }); cart = await getCart(cartId);
} }
return <CartModal cart={cart} />; return <CartModal cart={cart} />;

View File

@ -120,18 +120,12 @@ const removeEdgesAndNodes = (array: Connection<any>) => {
return array.edges.map((edge) => edge?.node); 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) { if (!cart.cost?.totalTaxAmount) {
cart.cost.totalTaxAmount = cart.cost.totalTaxAmount = {
country === 'US' || language === 'EN' amount: '0.0',
? { currencyCode: 'USD'
amount: '0.0', };
currencyCode: 'USD'
}
: {
amount: '0',
currencyCode: 'JPY'
};
} }
return { return {
@ -222,38 +216,24 @@ const reshapeProducts = (products: ShopifyProduct[]) => {
return reshapedProducts; return reshapedProducts;
}; };
export async function createCart({ export async function createCart(): Promise<Cart> {
country,
language
}: {
country?: string;
language?: string;
}): Promise<Cart> {
const res = await shopifyFetch<ShopifyCreateCartOperation>({ const res = await shopifyFetch<ShopifyCreateCartOperation>({
query: createCartMutation, query: createCartMutation,
cache: 'no-store', cache: 'no-store'
variables: {
country,
language
}
}); });
return reshapeCart(res.body.data.cartCreate.cart, country, language); return reshapeCart(res.body.data.cartCreate.cart);
} }
export async function addToCart( export async function addToCart(
cartId: string, cartId: string,
lines: { merchandiseId: string; quantity: number }[], lines: { merchandiseId: string; quantity: number }[]
country?: string,
language?: string
): Promise<Cart> { ): Promise<Cart> {
const res = await shopifyFetch<ShopifyAddToCartOperation>({ const res = await shopifyFetch<ShopifyAddToCartOperation>({
query: addToCartMutation, query: addToCartMutation,
variables: { variables: {
cartId, cartId,
lines, lines
country,
language
}, },
cache: 'no-store' cache: 'no-store'
}); });
@ -289,18 +269,10 @@ export async function updateCart(
return reshapeCart(res.body.data.cartLinesUpdate.cart); return reshapeCart(res.body.data.cartLinesUpdate.cart);
} }
export async function getCart({ export async function getCart(cartId: string): Promise<Cart | undefined> {
cartId,
country,
language
}: {
cartId: string;
country?: string;
language?: string;
}): Promise<Cart | undefined> {
const res = await shopifyFetch<ShopifyCartOperation>({ const res = await shopifyFetch<ShopifyCartOperation>({
query: getCartQuery, query: getCartQuery,
variables: { cartId, country, language }, variables: { cartId },
cache: 'no-store' cache: 'no-store'
}); });
@ -309,7 +281,7 @@ export async function getCart({
return undefined; return undefined;
} }
return reshapeCart(res.body.data.cart, country, language); return reshapeCart(res.body.data.cart);
} }
export async function getCollection({ export async function getCollection({

View File

@ -1,8 +1,7 @@
import cartFragment from '../fragments/cart'; import cartFragment from '../fragments/cart';
export const getCartQuery = /* GraphQL */ ` export const getCartQuery = /* GraphQL */ `
query getCart($cartId: ID!, $country: CountryCode, $language: LanguageCode) query getCart($cartId: ID!) {
@inContext(country: $country, language: $language) {
cart(id: $cartId) { cart(id: $cartId) {
...cart ...cart
} }

View File

@ -189,10 +189,6 @@ export type ShopifyCartOperation = {
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 = {