From 63dbf7fc9e8f6ff0e4b79a3fe5c7dc08604a0f76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Meyer?= Date: Fri, 27 Oct 2023 15:56:38 +0200 Subject: [PATCH] chore: simplify cart function --- components/cart/actions.ts | 6 +++--- components/cart/index.tsx | 12 +++--------- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/components/cart/actions.ts b/components/cart/actions.ts index 1b6cbde48..770d6a628 100644 --- a/components/cart/actions.ts +++ b/components/cart/actions.ts @@ -7,7 +7,7 @@ import { ExtendedCart, ExtendedLineItem, messageKeys } from 'lib/shopware/api-ex import { revalidateTag } from 'next/cache'; import { cookies } from 'next/headers'; -export const fetchCart = async function (cartId?: string): Promise { +async function fetchCart(cartId?: string): Promise { try { const apiClient = getApiClient(cartId); const cart = await apiClient.invoke('readCart get /checkout/cart?name', {}); @@ -21,7 +21,7 @@ export const fetchCart = async function (cartId?: string): Promise', error); } } -}; +} export async function addItem(prevState: any, selectedVariantId: string | undefined) { const cart = await getCart(); @@ -73,7 +73,7 @@ export async function addItem(prevState: any, selectedVariantId: string | undefi } } -async function getCart() { +export async function getCart() { const cartId = cookies().get('sw-context-token')?.value; if (cartId) { diff --git a/components/cart/index.tsx b/components/cart/index.tsx index 0c732ca99..3b55b54d6 100644 --- a/components/cart/index.tsx +++ b/components/cart/index.tsx @@ -1,17 +1,11 @@ -import { fetchCart } from 'components/cart/actions'; -import { cookies } from 'next/headers'; +import { getCart } from 'components/cart/actions'; import CartModal from './modal'; import { transformCart } from 'lib/shopware/transform'; export default async function Cart() { - let resCart; - const cartId = cookies().get('sw-context-token')?.value; - - if (cartId) { - resCart = await fetchCart(cartId); - } - let cart; + const resCart = await getCart(); + if (resCart) { cart = transformCart(resCart); }