From d5dee1c2618f8f457b82ca3ef9eb0df52e5d35bf Mon Sep 17 00:00:00 2001 From: paolosantarsiero Date: Tue, 20 May 2025 16:57:38 +0200 Subject: [PATCH] fix: store api --- app/api/cart/route.ts | 5 ++++- app/checkout/review/page.tsx | 27 +++++++++++++++++---------- app/page.tsx | 3 ++- components/cart/add-to-cart.tsx | 23 +++++++++++------------ lib/woocomerce/storeApi.ts | 7 +++++-- 5 files changed, 39 insertions(+), 26 deletions(-) diff --git a/app/api/cart/route.ts b/app/api/cart/route.ts index f61845fa1..c298008af 100644 --- a/app/api/cart/route.ts +++ b/app/api/cart/route.ts @@ -38,7 +38,10 @@ export async function POST(req: NextRequest) { return NextResponse.json(cart, { status: 200 }); } catch (error) { return NextResponse.json( - { error: 'Failed to add item to cart', message: JSON.stringify(error) }, + { + error: 'Failed to add item to cart', + message: JSON.stringify(error) + }, { status: 500 } ); } diff --git a/app/checkout/review/page.tsx b/app/checkout/review/page.tsx index cab033343..1ba39bc10 100644 --- a/app/checkout/review/page.tsx +++ b/app/checkout/review/page.tsx @@ -10,16 +10,23 @@ export default function CheckoutReview() { const { checkout } = useCheckout(); const handleCreateOrder = async () => { - const order = await fetch('/api/customer/order', { - method: 'POST', - body: JSON.stringify({ - billing_address: checkout?.billing, - shipping_address: checkout?.shipping, - payment_method: checkout?.payment_method - }) - }).catch((err) => { - console.error('Error creating order', err); - }); + try { + const order = await fetch('/api/customer/order', { + method: 'POST', + body: JSON.stringify({ + billing_address: checkout?.billing, + shipping_address: checkout?.shipping, + payment_method: checkout?.payment_method + }) + }); + if (!order.ok) { + const errorData = await order.json(); + console.error('Error creating order:', errorData); + throw new Error(errorData.error || 'Failed to create order'); + } + } catch (error) { + console.error('Error creating order', error); + } }; return (
diff --git a/app/page.tsx b/app/page.tsx index df8e6f1bc..98426efc1 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -20,7 +20,8 @@ import Bg2 from '../assets/images/slide-bg-02.webp'; async function Products({ category }: { category: Category }) { const products: Product[] = await woocommerce.get('products', { - category: category.id.toString() + category: category.id.toString(), + author: 1, // Use admin user to get all products }); return ; diff --git a/components/cart/add-to-cart.tsx b/components/cart/add-to-cart.tsx index 6b66d06d4..7e10844f3 100644 --- a/components/cart/add-to-cart.tsx +++ b/components/cart/add-to-cart.tsx @@ -5,7 +5,6 @@ import clsx from 'clsx'; import { useProduct } from 'components/product/product-context'; import { Product, ProductVariations } from 'lib/woocomerce/models/product'; import { useTranslations } from 'next-intl'; -import { toast } from 'sonner'; import { useCart } from './cart-context'; function SubmitButton({ disabled = false }: { disabled: boolean }) { @@ -45,18 +44,18 @@ export function AddToCart({ return (
{ - try { - const cart = await ( - await fetch('/api/cart', { - method: 'POST', - body: JSON.stringify({ id: product.id, quantity: 1, variation }) - }) - ).json(); - setNewCart(cart); - toast('Item added to cart'); - } catch (error) { - console.error(error); + const response = await fetch('/api/cart', { + method: 'POST', + body: JSON.stringify({ id: product.id, quantity: 1, variation }) + }); + + if (!response.ok) { + console.error('Error adding to cart'); + return; } + + const cart = await response.json(); + setNewCart(cart); }} > diff --git a/lib/woocomerce/storeApi.ts b/lib/woocomerce/storeApi.ts index 5a4c60f25..55efb5ed1 100644 --- a/lib/woocomerce/storeApi.ts +++ b/lib/woocomerce/storeApi.ts @@ -45,8 +45,9 @@ function createStoreApiClient({ method, url: baseURL + url, data, - headers + headers, }); + return response; } catch (error: any) { if (error.response) { @@ -55,8 +56,9 @@ function createStoreApiClient({ console.debug('Token expired, regenerating...'); const newAuthToken = await regenerateAuthToken(); headers.Authorization = `Bearer ${newAuthToken}`; - return _request(method, url, data); + return _request(method, url, data); // Retry the request with the new token } + throw new Error( `Request failed with status ${error.response.status}: ${error.response.data.message}` ); @@ -76,6 +78,7 @@ function createStoreApiClient({ params?: Record ): Promise<{ cart: Cart; cartToken?: string }> { const res = await _request('get', '/cart', { params }); + return { cart: res.data, cartToken: res.headers['cart-token'] }; },