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 { revalidateTag } from 'next/cache';
import { cookies } from 'next/headers'; import { cookies } from 'next/headers';
export const fetchCart = async function (cartId?: string): Promise<ExtendedCart | undefined> { async function fetchCart(cartId?: string): Promise<ExtendedCart | undefined> {
try { try {
const apiClient = getApiClient(cartId); const apiClient = getApiClient(cartId);
const cart = await apiClient.invoke('readCart get /checkout/cart?name', {}); 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); console.error('==>', error);
} }
} }
}; }
export async function addItem(prevState: any, selectedVariantId: string | undefined) { export async function addItem(prevState: any, selectedVariantId: string | undefined) {
const cart = await getCart(); 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; const cartId = cookies().get('sw-context-token')?.value;
if (cartId) { if (cartId) {

View File

@ -1,17 +1,11 @@
import { fetchCart } from 'components/cart/actions'; import { getCart } from 'components/cart/actions';
import { cookies } from 'next/headers';
import CartModal from './modal'; import CartModal from './modal';
import { transformCart } from 'lib/shopware/transform'; import { transformCart } from 'lib/shopware/transform';
export default async function Cart() { export default async function Cart() {
let resCart;
const cartId = cookies().get('sw-context-token')?.value;
if (cartId) {
resCart = await fetchCart(cartId);
}
let cart; let cart;
const resCart = await getCart();
if (resCart) { if (resCart) {
cart = transformCart(resCart); cart = transformCart(resCart);
} }