chore: simplify cart function

This commit is contained in:
Björn Meyer 2023-10-27 15:56:38 +02:00
parent d67e8f57c1
commit 63dbf7fc9e
2 changed files with 6 additions and 12 deletions

View File

@ -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<ExtendedCart | undefined> {
async function fetchCart(cartId?: string): Promise<ExtendedCart | undefined> {
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<ExtendedCart
console.error('==>', 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) {

View File

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